Skip to content

Commit

Permalink
Allow user to read their own user data
Browse files Browse the repository at this point in the history
  • Loading branch information
augusto-herrmann committed Feb 13, 2025
1 parent ddc25f1 commit 4c8cd5c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
22 changes: 16 additions & 6 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,22 +282,32 @@ async def create_or_update_user(
async def get_user(
user_logged: Annotated[ # pylint: disable=unused-argument
schemas.UsersSchema,
Depends(crud_auth.get_current_admin_user),
Depends(crud_auth.get_current_active_user),
],
email: str,
db: DbContextManager = Depends(DbContextManager),
) -> schemas.UsersGetSchema:
"""Retorna os dados cadastrais do usuário da API especificado pelo
e-mail informado."""
e-mail informado.
O usuário com perfil comum só pode consultar os dados do seu próprio
usuário. O usuário com perfil admin pode consultar qualquer usuário.
"""
# verifica permissões
if not user_logged.is_admin and email != user_logged.email:
raise HTTPException(
status.HTTP_403_FORBIDDEN,
detail="Usuário não tem permissões de administrador.",
)

# verifica se o usuário existe
user = await crud_auth.get_user(db, email)

if user:
return user.model_dump(exclude=["password"])
else:
raise HTTPException(
status.HTTP_404_NOT_FOUND, detail=f"Usuário `{email}` não existe"
)
raise HTTPException(
status.HTTP_404_NOT_FOUND, detail=f"Usuário `{email}` não existe"
)


@app.post(
Expand Down
17 changes: 15 additions & 2 deletions tests/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,29 @@ def test_get_user_not_logged_in(self, header_not_logged_in: dict):
assert response.status_code == status.HTTP_401_UNAUTHORIZED

def test_get_user_logged_in_not_admin(
self, user1_credentials: dict, header_usr_2: dict # user is_admin=False
):
"""Testa a obtenção de dados de um outro usuário por um usuário logado,
mas não admin.
Args:
user1_credentials (dict): Credenciais do usuário 1.
header_usr_2 (dict): Cabeçalhos HTTP para o usuário 2.
"""
response = self.get_user(user1_credentials["email"], header_usr_2)
assert response.status_code == status.HTTP_403_FORBIDDEN

def test_get_user_self_logged_in_not_admin(
self, user2_credentials: dict, header_usr_2: dict # user is_admin=False
):
"""Testa a obtenção de um usuário por um usuário logado, mas não admin.
"""Testa a obtenção de dados do próprio usuário logado, mas não admin.
Args:
user2_credentials (dict): Credenciais do usuário 2.
header_usr_2 (dict): Cabeçalhos HTTP para o usuário 2.
"""
response = self.get_user(user2_credentials["email"], header_usr_2)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.status_code == status.HTTP_200_OK

def test_get_user_as_admin(
self, user1_credentials: dict, header_usr_1: dict # user is_admin=True
Expand Down

0 comments on commit 4c8cd5c

Please sign in to comment.