from collections.abc import Callable from typing import Any import pytest from django.core.cache import cache from apps.users.infrastructure.orm import User PASSWORD = "Vahva-Salasana-2026" @pytest.fixture(autouse=True) def _clear_cache(request: pytest.FixtureRequest) -> None: # The cache (login throttle) lives in the database; only DB tests can touch it. if request.node.get_closest_marker("django_db"): request.getfixturevalue("db") cache.clear() @pytest.fixture def make_user(db: Any) -> Callable[..., User]: counter = iter(range(1, 10_000)) def factory(**fields: Any) -> User: n = next(counter) fields.setdefault("email", f"user{n}@example.test") fields.setdefault("name", f"User {n}") fields.setdefault("role", "employee") password = fields.pop("password", PASSWORD) return User.objects.create_user(password=password, **fields) return factory @pytest.fixture def admin(make_user: Callable[..., User]) -> User: return make_user(email="admin@example.test", name="Admin", role="admin") @pytest.fixture def admin_client(client: Any, admin: User) -> Any: client.force_login(admin) return client