-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
170 lines (130 loc) · 3.68 KB
/
conftest.py
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
166
167
168
169
170
import pytest
from django_webtest import (
WebTest as BaseWebTest,
DjangoTestApp as BaseDjangoTestApp,
)
from rest_framework.test import APIClient
@pytest.fixture() # NOQA
def factories(transactional_db):
import factory
from tests.factories.events import ( # NOQA
EventFactory,
FutureEventFactory,
PastEventFactory,
)
from tests.factories.shifts import ( # NOQA
ShiftFactory,
ShiftSlotFactory,
CancelledShiftSlotFactory,
)
from tests.factories.departments import ( # NOQA
DepartmentFactory,
RoleFactory,
)
from tests.factories.accounts import ( # NOQA
UserFactory,
)
def is_factory(obj):
if not isinstance(obj, type):
return False
return issubclass(obj, factory.DjangoModelFactory)
dict_ = {k: v for k, v in locals().items() if is_factory(v)}
return type(
'fixtures',
(object,),
dict_,
)
@pytest.fixture() # NOQA
def models_no_db():
from django.apps import apps
dict_ = {M._meta.object_name: M for M in apps.get_models()}
return type(
'models',
(object,),
dict_,
)
@pytest.fixture() # NOQA
def models(models_no_db, transactional_db):
return models_no_db
class DjangoTestApp(BaseDjangoTestApp):
@property
def user(self):
from django.contrib.auth import get_user_model
User = get_user_model()
user_id = self.session.get('_auth_user_id')
if user_id:
return User.objects.get(pk=user_id)
else:
return None
class WebTest(BaseWebTest):
app_class = DjangoTestApp
def authenticate(self, user):
self.app.get('/', user=user)
def unauthenticate(self):
self.app.get('/', user=None)
@pytest.fixture() # NOQA
def webtest_client(transactional_db):
web_test = WebTest(methodName='__call__')
web_test()
return web_test.app
@pytest.fixture()
def user_webtest_client(webtest_client, user):
web_test = WebTest(methodName='__call__')
web_test()
web_test.authenticate(user)
return web_test.app
@pytest.fixture()
def admin_webtest_client(webtest_client, admin_user):
web_test = WebTest(methodName='__call__')
web_test()
web_test.authenticate(admin_user)
return web_test.app
@pytest.fixture() # NOQA
def User(django_user_model):
"""
A slightly more intuitively named
`pytest_django.fixtures.django_user_model`
"""
return django_user_model
@pytest.fixture()
def admin_user(factories, User):
try:
return User.objects.get(email='admin@example.com')
except User.DoesNotExist:
return factories.UserFactory(
email='admin@example.com',
is_superuser=True,
password='password',
)
@pytest.fixture()
def user(factories, User):
try:
return User.objects.get(email='test@example.com')
except User.DoesNotExist:
return factories.UserFactory(
email='test@example.com',
password='password',
)
@pytest.fixture()
def user_client(user, client):
assert client.login(username=user.email, password='password')
client.user = user
return client
@pytest.fixture()
def admin_client(admin_user, client):
assert client.login(username=admin_user.email, password='password')
client.user = admin_user
return client
@pytest.fixture()
def api_client(user, db):
"""
A rest_framework api test client not auth'd.
"""
client = APIClient()
client.force_authenticate(user=user)
return client
@pytest.fixture()
def request_factory(db):
from django.test import RequestFactory
rf = RequestFactory()
return rf