refactoring some code by removing unnecessary checks and moving tests block to root /
This commit is contained in:
13
main.py
13
main.py
@@ -1,5 +1,6 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from src.web.protected_routes.routes import router as protected_router
|
from src.web.protected_routes.routes import router as protected_router
|
||||||
|
from pathlib import Path
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
app=FastAPI(root_path="/")
|
app=FastAPI(root_path="/")
|
||||||
@@ -9,7 +10,19 @@ app.include_router(router=protected_router)
|
|||||||
def root()->dict:
|
def root()->dict:
|
||||||
return {"root":"hello, this is root"}
|
return {"root":"hello, this is root"}
|
||||||
|
|
||||||
|
|
||||||
|
def create_dirs():
|
||||||
|
|
||||||
|
dirs_to_create=("./DB",
|
||||||
|
"./upload",
|
||||||
|
"./upload_bad",
|
||||||
|
"./upload_finished")
|
||||||
|
|
||||||
|
for x in dirs_to_create:
|
||||||
|
Path(x).mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
create_dirs()
|
||||||
uvicorn.run("main:app", reload=True)
|
uvicorn.run("main:app", reload=True)
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
|
|||||||
0
pytest.ini
Normal file
0
pytest.ini
Normal file
@@ -22,6 +22,7 @@ class CurrentUser:
|
|||||||
def _check(self, form_data_email:str, form_data_password:str,):
|
def _check(self, form_data_email:str, form_data_password:str,):
|
||||||
'''check user by email'''
|
'''check user by email'''
|
||||||
user=self.crud_db_actions.get_user_by_email(form_data_email)
|
user=self.crud_db_actions.get_user_by_email(form_data_email)
|
||||||
|
|
||||||
if user is None:
|
if user is None:
|
||||||
raise self.error.credentials_error(detail="Wrong credentials")
|
raise self.error.credentials_error(detail="Wrong credentials")
|
||||||
|
|
||||||
@@ -36,20 +37,21 @@ class CurrentUser:
|
|||||||
def get_current_user(self, token:str)->UserOut:
|
def get_current_user(self, token:str)->UserOut:
|
||||||
|
|
||||||
payload=self.jwt_service.jwt_decode(token)
|
payload=self.jwt_service.jwt_decode(token)
|
||||||
|
sub=payload.get("sub")
|
||||||
|
|
||||||
if (sub:=payload.get("sub")) is None:
|
|
||||||
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sub=UUID(sub)
|
sub=UUID(sub)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError) as e:
|
||||||
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
raise self.error.credentials_error(detail="Jwt token is incorrect") from e
|
||||||
|
|
||||||
|
|
||||||
user=self.crud_db_actions.get_user_by_id(sub)
|
user=self.crud_db_actions.get_user_by_id(sub)
|
||||||
if user is None:
|
if user is None:
|
||||||
raise self.error.not_found_error(detail="User with this email address not found")
|
raise self.error.not_found_error(detail="User with this email address not found")
|
||||||
|
|
||||||
|
if user.status is False:
|
||||||
|
raise self.error.credentials_error(detail="This user is deactivated")
|
||||||
|
|
||||||
return UserOut.model_validate(user)
|
return UserOut.model_validate(user)
|
||||||
|
|
||||||
|
|
||||||
@@ -61,12 +63,13 @@ class CurrentUser:
|
|||||||
|
|
||||||
|
|
||||||
def create_refresh_token(self,user_id:UUID, request:Request)->str:
|
def create_refresh_token(self,user_id:UUID, request:Request)->str:
|
||||||
|
|
||||||
token, jti=self.jwt_service.create_refresh_token({"sub":str(user_id)})
|
token, jti=self.jwt_service.create_refresh_token({"sub":str(user_id)})
|
||||||
|
|
||||||
try:
|
try:
|
||||||
jti=UUID(jti)
|
jti=UUID(jti)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError) as e:
|
||||||
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
raise self.error.credentials_error(detail="Jwt token is incorrect") from e
|
||||||
|
|
||||||
'''create new refresh token if all the checks are successful'''
|
'''create new refresh token if all the checks are successful'''
|
||||||
token_record=RefreshTokensCreate(
|
token_record=RefreshTokensCreate(
|
||||||
@@ -83,16 +86,19 @@ class CurrentUser:
|
|||||||
|
|
||||||
|
|
||||||
def refresh_token(self, refresh_token:str, request:Request)->tuple[str, str]:
|
def refresh_token(self, refresh_token:str, request:Request)->tuple[str, str]:
|
||||||
|
|
||||||
'''decode old refresh token'''
|
'''decode old refresh token'''
|
||||||
old_refresh_token=self.jwt_service.jwt_decode(refresh_token)
|
old_refresh_token=self.jwt_service.jwt_decode(refresh_token)
|
||||||
if (sub:=old_refresh_token.get("sub")) is None or (old_jti:=old_refresh_token.get("jti")) is None:
|
sub=old_refresh_token.get("sub")
|
||||||
|
|
||||||
|
if (old_jti:=old_refresh_token.get("jti")) is None:
|
||||||
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
old_jti=UUID(old_jti)
|
old_jti=UUID(old_jti)
|
||||||
sub=UUID(sub)
|
sub=UUID(sub)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError) as e:
|
||||||
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
raise self.error.credentials_error(detail="Jwt token is incorrect") from e
|
||||||
|
|
||||||
|
|
||||||
'''old refresh token check'''
|
'''old refresh token check'''
|
||||||
@@ -122,6 +128,7 @@ class CurrentUser:
|
|||||||
'''create new refresh token if all the checks are successful'''
|
'''create new refresh token if all the checks are successful'''
|
||||||
new_refresh_token, new_jti=self.jwt_service.create_refresh_token({"sub":str(sub)})
|
new_refresh_token, new_jti=self.jwt_service.create_refresh_token({"sub":str(sub)})
|
||||||
new_access_token=self.create_access_token(user_id=sub)
|
new_access_token=self.create_access_token(user_id=sub)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
new_jti=UUID(new_jti)
|
new_jti=UUID(new_jti)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
@@ -152,11 +159,12 @@ class CurrentUser:
|
|||||||
payload=self.jwt_service.jwt_decode(refresh_token)
|
payload=self.jwt_service.jwt_decode(refresh_token)
|
||||||
|
|
||||||
if (jti:=payload.get("jti")) is None:
|
if (jti:=payload.get("jti")) is None:
|
||||||
raise self.error.credentials_error(detail="Invalid Refresh Token")
|
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
jti=UUID(jti)
|
jti=UUID(jti)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError) as e:
|
||||||
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
raise self.error.credentials_error(detail="Jwt token is incorrect") from e
|
||||||
|
|
||||||
current_token = self.jwt_db_actions.get_token_by_id(jti)
|
current_token = self.jwt_db_actions.get_token_by_id(jti)
|
||||||
if current_token is None:
|
if current_token is None:
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ class Jwt:
|
|||||||
user_info=data.copy()
|
user_info=data.copy()
|
||||||
user_info.update({"exp": datetime.now(timezone.utc)+timedelta(minutes=env_settings.ACCESS_TOKEN_EXPIRE_MINUTES),
|
user_info.update({"exp": datetime.now(timezone.utc)+timedelta(minutes=env_settings.ACCESS_TOKEN_EXPIRE_MINUTES),
|
||||||
"token_type":"access"})
|
"token_type":"access"})
|
||||||
print(f"DEBUG: expires at {datetime.now(timezone.utc)+timedelta(minutes=env_settings.ACCESS_TOKEN_EXPIRE_MINUTES)}, minutes={env_settings.ACCESS_TOKEN_EXPIRE_MINUTES}")
|
|
||||||
return jwt.encode(user_info, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
return jwt.encode(user_info, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
||||||
|
|
||||||
|
|
||||||
@@ -57,7 +56,7 @@ class Jwt:
|
|||||||
payload=jwt.decode(token, env_settings.SECRET_KEY, algorithms=[env_settings.ALGORITHM])
|
payload=jwt.decode(token, env_settings.SECRET_KEY, algorithms=[env_settings.ALGORITHM])
|
||||||
|
|
||||||
if (payload.get("sub")) is None:
|
if (payload.get("sub")) is None:
|
||||||
raise self.error.credentials_error(detail="Sub block is missing")
|
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
||||||
|
|
||||||
except JWTError as e:
|
except JWTError as e:
|
||||||
raise self.error.credentials_error(detail="JWTerror") from e
|
raise self.error.credentials_error(detail="JWTerror") from e
|
||||||
|
|||||||
0
tests/conftest.py
Normal file
0
tests/conftest.py
Normal file
0
tests/e2e/conftest.py
Normal file
0
tests/e2e/conftest.py
Normal file
0
tests/integrated/conftest.py
Normal file
0
tests/integrated/conftest.py
Normal file
0
tests/unit/conftest.py
Normal file
0
tests/unit/conftest.py
Normal file
Reference in New Issue
Block a user