Add more tests for the users repo and fix bugs they uncovered

This commit is contained in:
Campbell Alden 2026-09-03 23:08:19 +09:00
parent 6c46a1df3a
commit acb343f559
2 changed files with 14 additions and 3 deletions

View file

@ -73,5 +73,6 @@ class UserRepoImpl(CRUDRepo[UserModel, User, UserDTO], UserRepo):
try: try:
self._hasher.verify(full_user.password_hash.expose_secret(), user.raw_password.expose_secret()) self._hasher.verify(full_user.password_hash.expose_secret(), user.raw_password.expose_secret())
return full_user
except VerifyMismatchError: except VerifyMismatchError:
return None return None

View file

@ -5,12 +5,10 @@ from src.infra.users import UserRepoImpl
from src.infra.db_test import mock_db from src.infra.db_test import mock_db
import pytest import pytest
DB = mock_db()
@pytest.fixture @pytest.fixture
def db(): def db():
return DB return mock_db()
@pytest.fixture @pytest.fixture
@ -29,3 +27,15 @@ def users(repo: UserRepo):
def test_getting_a_user_by_email(users: list[User], 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('example@example.com') == users[0]
assert repo.get_user_by_email('fred@example.com') is None 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