|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from types import SimpleNamespace |
| 4 | +from unittest.mock import AsyncMock, Mock |
| 5 | +from uuid import uuid4 |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from app.services import auth as auth_service |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def fake_session() -> object: |
| 14 | + return object() |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def active_user() -> SimpleNamespace: |
| 19 | + return SimpleNamespace( |
| 20 | + id=uuid4(), |
| 21 | + email="user@example.com", |
| 22 | + hashed_password="hashed-password", |
| 23 | + is_active=True, |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.asyncio |
| 28 | +async def test_register_user_raises_when_email_already_exists( |
| 29 | + monkeypatch: pytest.MonkeyPatch, |
| 30 | + fake_session: object, |
| 31 | + active_user: SimpleNamespace, |
| 32 | +) -> None: |
| 33 | + get_user_by_email = AsyncMock(return_value=active_user) |
| 34 | + create_user = AsyncMock() |
| 35 | + |
| 36 | + monkeypatch.setattr(auth_service.user_repo, "get_user_by_email", get_user_by_email) |
| 37 | + monkeypatch.setattr(auth_service.user_repo, "create_user", create_user) |
| 38 | + |
| 39 | + with pytest.raises(auth_service.UserAlreadyExists): |
| 40 | + await auth_service.register_user(fake_session, "user@example.com", "password") |
| 41 | + |
| 42 | + create_user.assert_not_awaited() |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.asyncio |
| 46 | +async def test_register_user_hashes_password_and_creates_user( |
| 47 | + monkeypatch: pytest.MonkeyPatch, |
| 48 | + fake_session: object, |
| 49 | + active_user: SimpleNamespace, |
| 50 | +) -> None: |
| 51 | + get_user_by_email = AsyncMock(return_value=None) |
| 52 | + hash_password = AsyncMock(return_value="hashed-by-test") |
| 53 | + create_user = AsyncMock(return_value=active_user) |
| 54 | + |
| 55 | + monkeypatch.setattr(auth_service.user_repo, "get_user_by_email", get_user_by_email) |
| 56 | + monkeypatch.setattr(auth_service, "hash_password", hash_password) |
| 57 | + monkeypatch.setattr(auth_service.user_repo, "create_user", create_user) |
| 58 | + |
| 59 | + created = await auth_service.register_user( |
| 60 | + fake_session, |
| 61 | + "user@example.com", |
| 62 | + "password", |
| 63 | + ) |
| 64 | + |
| 65 | + assert created is active_user |
| 66 | + hash_password.assert_awaited_once_with(password="password") |
| 67 | + create_user.assert_awaited_once_with( |
| 68 | + fake_session, |
| 69 | + email="user@example.com", |
| 70 | + hashed_password="hashed-by-test", |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_authenticate_user_uses_dummy_hash_when_user_not_found( |
| 76 | + monkeypatch: pytest.MonkeyPatch, |
| 77 | + fake_session: object, |
| 78 | +) -> None: |
| 79 | + get_user_by_email = AsyncMock(return_value=None) |
| 80 | + get_dummy_hash = Mock(return_value="dummy-hash") |
| 81 | + verify_password = AsyncMock(return_value=False) |
| 82 | + |
| 83 | + monkeypatch.setattr(auth_service.user_repo, "get_user_by_email", get_user_by_email) |
| 84 | + monkeypatch.setattr(auth_service, "get_dummy_hash", get_dummy_hash) |
| 85 | + monkeypatch.setattr(auth_service, "verify_password", verify_password) |
| 86 | + |
| 87 | + authenticated = await auth_service.authenticate_user( |
| 88 | + fake_session, |
| 89 | + "missing@example.com", |
| 90 | + "password", |
| 91 | + ) |
| 92 | + |
| 93 | + assert authenticated is None |
| 94 | + verify_password.assert_awaited_once_with( |
| 95 | + password="password", |
| 96 | + hashed_password="dummy-hash", |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.asyncio |
| 101 | +async def test_authenticate_active_user_raises_for_inactive_user( |
| 102 | + monkeypatch: pytest.MonkeyPatch, |
| 103 | +) -> None: |
| 104 | + inactive_user = SimpleNamespace(is_active=False) |
| 105 | + authenticate_user = AsyncMock(return_value=inactive_user) |
| 106 | + |
| 107 | + monkeypatch.setattr(auth_service, "authenticate_user", authenticate_user) |
| 108 | + |
| 109 | + with pytest.raises(auth_service.UserInactive): |
| 110 | + await auth_service.authenticate_active_user( |
| 111 | + object(), |
| 112 | + "user@example.com", |
| 113 | + "password", |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.asyncio |
| 118 | +async def test_validate_refresh_subject_raises_when_user_not_found( |
| 119 | + monkeypatch: pytest.MonkeyPatch, |
| 120 | + fake_session: object, |
| 121 | +) -> None: |
| 122 | + get_user_by_id = AsyncMock(return_value=None) |
| 123 | + monkeypatch.setattr(auth_service.user_repo, "get_user_by_id", get_user_by_id) |
| 124 | + |
| 125 | + with pytest.raises(auth_service.UserNotFound): |
| 126 | + await auth_service.validate_refresh_subject(fake_session, uuid4()) |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.asyncio |
| 130 | +async def test_validate_refresh_subject_raises_when_user_inactive( |
| 131 | + monkeypatch: pytest.MonkeyPatch, |
| 132 | + fake_session: object, |
| 133 | +) -> None: |
| 134 | + inactive_user = SimpleNamespace(is_active=False) |
| 135 | + get_user_by_id = AsyncMock(return_value=inactive_user) |
| 136 | + monkeypatch.setattr(auth_service.user_repo, "get_user_by_id", get_user_by_id) |
| 137 | + |
| 138 | + with pytest.raises(auth_service.UserInactive): |
| 139 | + await auth_service.validate_refresh_subject(fake_session, uuid4()) |
0 commit comments