From acb343f559be2dcf6028fad408d557d809aa58a6 Mon Sep 17 00:00:00 2001 From: Campbell Alden Date: Thu, 3 Sep 2026 23:08:19 +0900 Subject: [PATCH] Add more tests for the users repo and fix bugs they uncovered --- src/infra/users.py | 1 + src/infra/users_test.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/infra/users.py b/src/infra/users.py index cf4170b..aa17d43 100644 --- a/src/infra/users.py +++ b/src/infra/users.py @@ -73,5 +73,6 @@ class UserRepoImpl(CRUDRepo[UserModel, User, UserDTO], UserRepo): try: self._hasher.verify(full_user.password_hash.expose_secret(), user.raw_password.expose_secret()) + return full_user except VerifyMismatchError: return None diff --git a/src/infra/users_test.py b/src/infra/users_test.py index 119c135..a7f8da7 100644 --- a/src/infra/users_test.py +++ b/src/infra/users_test.py @@ -5,12 +5,10 @@ from src.infra.users import UserRepoImpl from src.infra.db_test import mock_db import pytest -DB = mock_db() - @pytest.fixture def db(): - return DB + return mock_db() @pytest.fixture @@ -29,3 +27,15 @@ def users(repo: UserRepo): def test_getting_a_user_by_email(users: list[User], repo: UserRepo): assert repo.get_user_by_email('example@example.com') == users[0] assert repo.get_user_by_email('fred@example.com') is None + + +def test_authenticating_as_a_user_can_succeed(users: list[User], repo: UserRepo): + assert repo.auth_as_user(UserDTO('example@example.com', SecretBox('hunter1'))) == users[0] + + +def test_authenticating_fails_with_bad_password(users: list[User], repo: UserRepo): + assert repo.auth_as_user(UserDTO('example@example.com', SecretBox('something-wrong'))) is None + + +def test_authenticating_fails_for_nonexistent_user(users: list[User], repo: UserRepo): + assert repo.auth_as_user(UserDTO('rando@example.com', SecretBox('test123'))) is None