infrasynth-backend-kit/tests/conftest.py
2026-08-28 14:38:47 -05:00

99 lines
2.1 KiB
Python

import pytest
from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import make_password
from rest_framework.test import APIClient
UserModel = get_user_model()
@pytest.fixture
def api_client(db):
return APIClient()
@pytest.fixture
def user(db):
return UserModel.objects.create_user(
username="testuser",
email="test@example.com",
password="testpass123",
)
@pytest.fixture
def admin_user(db):
return UserModel.objects.create_superuser(
username="admin",
email="admin@example.com",
password="adminpass123",
)
@pytest.fixture
def authenticated_client(user, db):
client = APIClient()
client.force_authenticate(user=user)
return client
@pytest.fixture
def admin_client(admin_user, db):
client = APIClient()
client.force_authenticate(user=admin_user)
return client
@pytest.fixture
def user_factory():
def create_user(**kwargs):
defaults = {
"username": "factory_user",
"email": "factory@example.com",
"password": make_password("factorypass123"),
}
defaults.update(kwargs)
return UserModel.objects.create(**defaults)
return create_user
@pytest.fixture
def role_factory():
from infrasynth.security.models import Role
def create_role(**kwargs):
defaults = {
"name": "Test Role",
"slug": "test-role",
"permissions": [],
}
defaults.update(kwargs)
return Role.objects.create(**defaults)
return create_role
@pytest.fixture
def media_root(tmp_path, settings):
settings.MEDIA_ROOT = str(tmp_path)
return tmp_path
@pytest.fixture(autouse=True)
def clear_feature_cache():
from django.core.cache import cache
cache.clear()
yield
cache.clear()
@pytest.fixture
def clean_feature_registry():
"""Clears the global FeatureRegistry for the test, then restores it."""
from infrasynth.features.registry import FeatureRegistry
snapshot = dict(FeatureRegistry._features)
FeatureRegistry._features.clear()
yield
FeatureRegistry._features = snapshot