-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
165 lines (126 loc) · 4.25 KB
/
Copy pathconftest.py
File metadata and controls
165 lines (126 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import asyncio
from datetime import timedelta
import aio_pika
import factory
import pytest
from django.utils import timezone
from factory.django import DjangoModelFactory
from rest_framework.test import APIClient
from rest_framework_simplejwt.tokens import RefreshToken
from apps.comments.models import Comment
from apps.projects.models import Project
from apps.tasks.models import Task
from apps.users.models import User
class UserFactory(DjangoModelFactory):
class Meta:
model = User
email = factory.Sequence(lambda n: f"user{n}@example.com")
first_name = factory.Faker("first_name")
last_name = factory.Faker("last_name")
role = User.Role.MEMBER
@factory.post_generation
def password(obj, create, extracted, **kwargs):
raw = extracted or "StrongPassword123"
obj.set_password(raw)
if create:
obj.save(update_fields=["password"])
class ProjectFactory(DjangoModelFactory):
class Meta:
model = Project
name = factory.Sequence(lambda n: f"Project {n}")
description = factory.Faker("sentence")
owner = factory.SubFactory(UserFactory, role=User.Role.MANAGER)
@factory.post_generation
def members(obj, create, extracted, **kwargs):
if not create or not extracted:
return
for member in extracted:
obj.members.add(member)
class TaskFactory(DjangoModelFactory):
class Meta:
model = Task
title = factory.Sequence(lambda n: f"Task {n}")
description = factory.Faker("sentence")
status = Task.Status.TODO
priority = Task.Priority.MEDIUM
assignee = factory.SubFactory(UserFactory, role=User.Role.MEMBER)
project = factory.SubFactory(ProjectFactory)
due_date = factory.LazyFunction(lambda: timezone.now() + timedelta(days=7))
class CommentFactory(DjangoModelFactory):
class Meta:
model = Comment
text = factory.Faker("sentence")
author = factory.SubFactory(UserFactory, role=User.Role.MEMBER)
task = factory.SubFactory(TaskFactory)
def _auth_client(user: User) -> APIClient:
client = APIClient()
refresh = RefreshToken.for_user(user)
client.credentials(HTTP_AUTHORIZATION=f"Bearer {refresh.access_token}")
return client
@pytest.fixture
def api_client():
return APIClient()
@pytest.fixture
def admin_user(db):
return UserFactory(role=User.Role.ADMIN, is_superuser=True, is_staff=True)
@pytest.fixture
def manager_user(db):
return UserFactory(role=User.Role.MANAGER)
@pytest.fixture
def member_user(db):
return UserFactory(role=User.Role.MEMBER)
@pytest.fixture
def admin_client(admin_user):
return _auth_client(admin_user)
@pytest.fixture
def manager_client(manager_user):
return _auth_client(manager_user)
@pytest.fixture
def member_client(member_user):
return _auth_client(member_user)
@pytest.fixture
def db_setup(manager_user, member_user):
project = ProjectFactory(owner=manager_user, members=[member_user])
task_1 = TaskFactory(
title="Fix auth bug",
project=project,
assignee=member_user,
status=Task.Status.TODO,
priority=Task.Priority.HIGH,
)
task_2 = TaskFactory(
title="Refactor API docs",
project=project,
assignee=member_user,
status=Task.Status.IN_PROGRESS,
priority=Task.Priority.MEDIUM,
)
task_3 = TaskFactory(
title="Deploy to staging",
project=project,
assignee=manager_user,
status=Task.Status.DONE,
priority=Task.Priority.CRITICAL,
)
return {
"project": project,
"tasks": [task_1, task_2, task_3],
"manager": manager_user,
"member": member_user,
}
@pytest.fixture
def rabbitmq_available():
"""
Реальный RabbitMQ (тот же URL, что и у Django publisher).
Если брокер недоступен — тесты с маркером integration будут пропущены.
"""
from django.conf import settings
url = settings.RABBITMQ_URL
async def _ping() -> None:
conn = await asyncio.wait_for(aio_pika.connect_robust(url), timeout=5)
await conn.close()
try:
asyncio.run(_ping())
except Exception as exc: # noqa: BLE001
pytest.skip(f"RabbitMQ not available: {exc}")
return url