Skip to content

Commit

Permalink
Fix #145: handle error when attempting to log in with a non-existing …
Browse files Browse the repository at this point in the history
…user
  • Loading branch information
augusto-herrmann committed Oct 25, 2024
1 parent 20f59a8 commit 5c0c0df
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/crud_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ async def authenticate_user(
"""
user = await get_user(db_session=db, email=username)

if not user:
return False

if not verify_password(password, user.password):
if not user or not verify_password(password, user.password):
raise InvalidCredentialsError("Username ou password incorretos")

if user.disabled:
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ def fixture_register_user_2(
return response


@pytest.fixture()
def non_existing_user_credentials() -> dict:
"""Retorna pseudo credenciais de um usuário não existente."""
return {"email": "[email protected]", "password": "inexistente"}


@pytest.fixture()
def disabled_user_1(
header_admin: dict, # pylint: disable=unused-argument
Expand Down
18 changes: 18 additions & 0 deletions tests/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ def test_authenticate(self, header_usr_1: dict):
token = header_usr_1.get("Authorization")
assert isinstance(token, str)

def test_attempt_log_in_non_existent_user(
self,
truncate_users, # pylint: disable=unused-argument
non_existing_user_credentials: dict,
):
"""Tenta fazer login com um usuário que não existe.
Args:
truncate_users (fixture): Trunca a tabela de usuários.
non_existing_user_credentials (dict): Credenciais do usuário 1.
"""
# Faz login com um usuário que não existe.
with pytest.raises(HTTPStatusError):
self.get_bearer_token(
username=non_existing_user_credentials["email"],
password=non_existing_user_credentials["password"],
)

def test_attempt_log_in_disabled_user(
self,
truncate_users, # pylint: disable=unused-argument
Expand Down

0 comments on commit 5c0c0df

Please sign in to comment.