uchill/backend/conftest.py

105 lines
3.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Конфигурация pytest для всего проекта.
Содержит фикстуры, доступные во всех тестах.
"""
import pytest
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient
from rest_framework_simplejwt.tokens import RefreshToken
User = get_user_model()
@pytest.fixture
def api_client():
"""REST API клиент для тестов."""
return APIClient()
@pytest.fixture
def mentor_user(db):
"""Создает пользователя-ментора для тестов."""
user = User.objects.create_user(
email='mentor@test.com',
password='TestPass123!',
first_name='Иван',
last_name='Иванов',
phone='+79991234567',
role='mentor',
is_email_verified=True,
is_active=True
)
return user
@pytest.fixture
def client_user(db):
"""Создает пользователя-клиента для тестов."""
user = User.objects.create_user(
email='client@test.com',
password='TestPass123!',
first_name='Петр',
last_name='Петров',
phone='+79991234568',
role='client',
is_email_verified=True,
is_active=True
)
return user
@pytest.fixture
def parent_user(db):
"""Создает пользователя-родителя для тестов."""
user = User.objects.create_user(
email='parent@test.com',
password='TestPass123!',
first_name='Мария',
last_name='Сидорова',
phone='+79991234569',
role='parent',
is_email_verified=True,
is_active=True
)
return user
@pytest.fixture
def authenticated_client(api_client, mentor_user):
"""API клиент с аутентификацией (ментор)."""
refresh = RefreshToken.for_user(mentor_user)
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {refresh.access_token}')
api_client.user = mentor_user
return api_client
@pytest.fixture
def authenticated_client_user(api_client, client_user):
"""API клиент с аутентификацией (клиент)."""
refresh = RefreshToken.for_user(client_user)
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {refresh.access_token}')
api_client.user = client_user
return api_client
@pytest.fixture
def authenticated_parent(api_client, parent_user):
"""API клиент с аутентификацией (родитель)."""
refresh = RefreshToken.for_user(parent_user)
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {refresh.access_token}')
api_client.user = parent_user
return api_client
@pytest.fixture
def tokens_for_user():
"""Функция для генерации JWT токенов для пользователя."""
def _get_tokens(user):
refresh = RefreshToken.for_user(user)
return {
'refresh': str(refresh),
'access': str(refresh.access_token),
}
return _get_tokens