Skip to content

Commit

Permalink
fix: fetch user from DB instead of jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Jun 4, 2024
1 parent 00bc942 commit cd1c59a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
19 changes: 12 additions & 7 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ def get_db():
DbDependency = Annotated[Session, Depends(get_db)]


def ensure_user(x_user: Annotated[str, Header()]) -> models.User:
def ensure_user(db: DbDependency, x_user: Annotated[str, Header()]) -> models.User:
jwt_claims = auth.parse_jwt(x_user)
if jwt_claims is None:
raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED,
detail="Necesita loguearse para continuar",
)
user = models.User(id=jwt_claims["id"], email=jwt_claims["email"])
user = crud.get_user_by_id(db, jwt_claims["id"])
if user is None:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Usuario no encontrado",
)
return user


Expand Down Expand Up @@ -72,7 +77,7 @@ def login(user: schemas.UserLogin, db: DbDependency) -> schemas.UserCredentials:

if not auth.valid_password(user.password, db_user.hashed_password):
raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED, detail="Contraseña incorrecta"
status_code=HTTPStatus.FORBIDDEN, detail="Contraseña incorrecta"
)

credentials = auth.login_user(db_user)
Expand Down Expand Up @@ -116,7 +121,7 @@ def check_group_exists_and_user_is_owner(user_id: int, group: models.Group):
# If user is in group, but is not the owner
if group.owner_id != user_id:
raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED,
status_code=HTTPStatus.FORBIDDEN,
detail="No tiene permisos para modificar este grupo",
)

Expand Down Expand Up @@ -163,17 +168,17 @@ def add_user_to_group(
user_to_add = crud.get_user_by_email(db, req.user_identifier)
else:
user_to_add = crud.get_user_by_id(db, req.user_identifier)

if user_to_add is None:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Usuario no existe"
)

group = crud.get_group_by_id(db, group_id)

check_group_exists_and_user_is_owner(user.id, group)
check_group_is_unarchived(group)
if user_id_in_group(user.id, group):
if user_id_in_group(user_to_add.id, group):
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"El usuario ya es miembro del grupo {group.name}",
Expand Down
19 changes: 8 additions & 11 deletions src/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def some_group_members(
response = client.post(
url=f"/group/{some_group.id}/member",
headers={"x-user": some_credentials.jwt},
json={"user_id": credentials.id},
json={"user_identifier": credentials.id},
)
assert response.status_code == HTTPStatus.CREATED

Expand Down Expand Up @@ -168,7 +168,7 @@ def test_login_with_wrong_password(client: TestClient):
json={"email": "example@example.com", "password": "a_wrong_password"},
)

assert second_response.status_code == HTTPStatus.UNAUTHORIZED
assert second_response.status_code == HTTPStatus.FORBIDDEN
assert "jwt" not in second_response.json()


Expand Down Expand Up @@ -332,21 +332,19 @@ def test_add_user_to_group(
some_group: schemas.Group,
):
# Create new user
body = {"email": "some_email@example.com", "password": "some_password"}
response = client.post(url="/user/register", json=body)
assert response.status_code == HTTPStatus.CREATED
user = response.json()
new_user = make_user_credentials(client, "some_random_email@email.com")

# Add new user to group
response = client.post(
url=f"/group/{some_group.id}/member",
headers={"x-user": some_credentials.jwt},
json={"user_id": user["id"]},
json={"user_identifier": new_user.id},
)
expected_members = sorted([some_credentials.id, new_user.id])
body = response.json()
assert response.status_code == HTTPStatus.CREATED
assert response.status_code == HTTPStatus.CREATED, str(body)
assert len(body) == 2
assert sorted([u["id"] for u in body]) == sorted([some_credentials.id, user["id"]])
assert sorted([u["id"] for u in body]) == expected_members

# GET group members
response = client.get(
Expand All @@ -358,7 +356,7 @@ def test_add_user_to_group(

assert response.status_code == HTTPStatus.OK
assert len(body) == 2
assert sorted([u["id"] for u in body]) == sorted([some_credentials.id, user["id"]])
assert sorted([u["id"] for u in body]) == expected_members


################################################
Expand Down Expand Up @@ -690,7 +688,6 @@ def some_invite(
some_other_credentials: schemas.UserCredentials,
some_group: schemas.Group,
):

# Create Invite
response = client.post(
url="/invite",
Expand Down

0 comments on commit cd1c59a

Please sign in to comment.