Files
mtusi2026lab6/service-tests/unit/test_user_service.py
T
ricoromaandClaude Opus 5.5 9cc6c0c7a6 chore: port lab 5 solution into the lab 6 skeleton
Source: mtusi2026lab5-safronov, solution/bvt2301-safronov, 046ec9e.
Contracts, public tests and evidence of lab 5 are not carried over.
The compose project, image and CI prefix are renamed to lab6; the first
broker start defect is recorded in KNOWN_ISSUES.md; PLAN.md outlines the
OpenSpec changes of lab 6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 04:46:56 +03:00

61 lines
1.7 KiB
Python

"""`UserService`: KDF вне блока, занятый username."""
import pytest
from fakes import FakeDatabase, FakeUnitOfWork
from src.core import exceptions, services
from src.core.services import user as user_module
pytestmark = pytest.mark.anyio
@pytest.fixture(autouse=True)
def cheap_kdf(monkeypatch: pytest.MonkeyPatch, database: FakeDatabase) -> None:
async def hash_password(password: str) -> str:
database.events.append("kdf")
return f"hash:{password}"
monkeypatch.setattr(user_module, "hash_password", hash_password)
async def test_register_stores_user_outside_kdf(
uow: FakeUnitOfWork,
database: FakeDatabase,
) -> None:
created = await services.UserService(uow).register(
username="alice",
display_name="Alice",
password="password-1234",
)
assert database.users[created.id] == created
assert database.credentials["alice"].password_hash == "hash:password-1234"
# KDF до блока, соединение не занято
assert database.events[0] == "kdf"
assert database.events[1:] == [
"enter",
"users.insert_user",
"commit",
"exit",
]
async def test_taken_username_rejected_without_commit(
uow: FakeUnitOfWork,
database: FakeDatabase,
) -> None:
service = services.UserService(uow)
await service.register(
username="alice",
display_name="Alice",
password="password-1234",
)
database.events.clear()
with pytest.raises(exceptions.UsernameTaken):
await service.register(
username="alice",
display_name="Other",
password="password-5678",
)
assert "commit" not in database.events
assert len(database.users) == 1