|
| 1 | +import pytest |
| 2 | +from django.contrib.auth import get_user_model |
| 3 | +from rest_framework.test import APIClient |
| 4 | +from users.models.user import UserRole |
| 5 | +from model_bakery import baker |
| 6 | + |
| 7 | +User = get_user_model() |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def researcher_user(db): |
| 11 | + """Fixture to create and return a user with RESEARCHER role.""" |
| 12 | + return User.objects.create_user( |
| 13 | + email="researcher@example.com", |
| 14 | + password="password123", |
| 15 | + role=UserRole.RESEARCHER, |
| 16 | + first_name="Research", |
| 17 | + last_name="er" |
| 18 | + ) |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def participant_user(db): |
| 22 | + """Fixture to create and return an inactive user with PARTICIPANT role and profile.""" |
| 23 | + user = User.objects.create_user( |
| 24 | + email="participant@example.com", |
| 25 | + username="participant", |
| 26 | + password="password123", |
| 27 | + role=UserRole.PARTICIPANT, |
| 28 | + is_active=False, |
| 29 | + first_name="Parti", |
| 30 | + last_name="cipant" |
| 31 | + ) |
| 32 | + from participant.models import ParticipantProfile |
| 33 | + ParticipantProfile.objects.get_or_create( |
| 34 | + user=user, |
| 35 | + defaults={ |
| 36 | + "date_of_birth": "2010-01-01", |
| 37 | + "gender": "M" |
| 38 | + } |
| 39 | + ) |
| 40 | + return user |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def regular_user(db): |
| 44 | + """Fixture to create and return a regular user (defaults to PARTICIPANT but active).""" |
| 45 | + user = User.objects.create_user( |
| 46 | + email="user@example.com", |
| 47 | + username="regular_user", |
| 48 | + password="password123", |
| 49 | + role=UserRole.PARTICIPANT, |
| 50 | + is_active=True |
| 51 | + ) |
| 52 | + from participant.models import ParticipantProfile |
| 53 | + ParticipantProfile.objects.get_or_create( |
| 54 | + user=user, |
| 55 | + defaults={ |
| 56 | + "date_of_birth": "2010-01-01", |
| 57 | + "gender": "M" |
| 58 | + } |
| 59 | + ) |
| 60 | + return user |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def invitation(participant_user, researcher_user): |
| 64 | + """Fixture for a valid, active invitation.""" |
| 65 | + from participant.models import Invitation |
| 66 | + from django.utils import timezone |
| 67 | + from datetime import timedelta |
| 68 | + return Invitation.objects.create( |
| 69 | + user=participant_user, |
| 70 | + invited_by=researcher_user, |
| 71 | + parent_email="guardian@example.com", |
| 72 | + expiry_date=timezone.now() + timedelta(days=2), |
| 73 | + is_active=True, |
| 74 | + has_accepted=False |
| 75 | + ) |
| 76 | + |
| 77 | +@pytest.fixture |
| 78 | +def expired_invitation(db, researcher_user): |
| 79 | + """Fixture for an expired invitation.""" |
| 80 | + from participant.models import Invitation |
| 81 | + from django.utils import timezone |
| 82 | + from datetime import timedelta |
| 83 | + |
| 84 | + user = User.objects.create_user( |
| 85 | + email="expired@example.com", |
| 86 | + username="expired", |
| 87 | + role=UserRole.PARTICIPANT, |
| 88 | + is_active=False |
| 89 | + ) |
| 90 | + from participant.models import ParticipantProfile |
| 91 | + ParticipantProfile.objects.create(user=user, date_of_birth="2010-01-01", gender="M") |
| 92 | + |
| 93 | + return Invitation.objects.create( |
| 94 | + user=user, |
| 95 | + invited_by=researcher_user, |
| 96 | + parent_email="guardian@example.com", |
| 97 | + expiry_date=timezone.now() - timedelta(days=1), |
| 98 | + is_active=True, |
| 99 | + has_accepted=False |
| 100 | + ) |
| 101 | + |
| 102 | +@pytest.fixture |
| 103 | +def accepted_invitation(db, researcher_user): |
| 104 | + """Fixture for an already accepted invitation.""" |
| 105 | + from participant.models import Invitation |
| 106 | + from django.utils import timezone |
| 107 | + from datetime import timedelta |
| 108 | + |
| 109 | + user = User.objects.create_user( |
| 110 | + email="accepted@example.com", |
| 111 | + username="accepted", |
| 112 | + role=UserRole.PARTICIPANT, |
| 113 | + is_active=False |
| 114 | + ) |
| 115 | + from participant.models import ParticipantProfile |
| 116 | + ParticipantProfile.objects.create(user=user, date_of_birth="2010-01-01", gender="M") |
| 117 | + |
| 118 | + return Invitation.objects.create( |
| 119 | + user=user, |
| 120 | + invited_by=researcher_user, |
| 121 | + parent_email="guardian@example.com", |
| 122 | + expiry_date=timezone.now() + timedelta(days=2), |
| 123 | + is_active=True, |
| 124 | + has_accepted=True |
| 125 | + ) |
| 126 | + |
| 127 | +@pytest.fixture |
| 128 | +def mock_email_manager(mocker): |
| 129 | + """Fixture to mock EmailManager.""" |
| 130 | + mock_email = mocker.patch("participant.serializers.EmailManager") |
| 131 | + return mock_email.return_value |
| 132 | + |
| 133 | +@pytest.fixture |
| 134 | +def api_client(): |
| 135 | + """Fixture to provide a DRF APIClient.""" |
| 136 | + return APIClient() |
| 137 | + |
| 138 | +@pytest.fixture |
| 139 | +def mock_keycloak(mocker): |
| 140 | + """Fixture to mock KeycloakSync.""" |
| 141 | + mock_sync = mocker.patch("participant.serializers.KeycloakSync") |
| 142 | + return mock_sync.return_value |
0 commit comments