1
|
|
|
""" |
2
|
|
|
:Copyright: 2006-2021 Jochen Kupperschmidt |
3
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
from __future__ import annotations |
7
|
|
|
from pathlib import Path |
8
|
|
|
from secrets import token_hex |
9
|
|
|
from tempfile import TemporaryDirectory |
10
|
|
|
from typing import Optional |
11
|
|
|
|
12
|
|
|
import pytest |
13
|
|
|
|
14
|
|
|
from byceps.services.authorization import service as authz_service |
15
|
|
|
from byceps.services.board import board_service |
16
|
|
|
from byceps.services.brand import service as brand_service |
17
|
|
|
from byceps.services.email import service as email_service |
18
|
|
|
from byceps.services.party import service as party_service |
19
|
|
|
from byceps.services.site import service as site_service |
20
|
|
|
from byceps.services.ticketing import ( |
21
|
|
|
category_service as ticketing_category_service, |
22
|
|
|
) |
23
|
|
|
from byceps.services.user import ( |
24
|
|
|
command_service as user_command_service, |
25
|
|
|
deletion_service as user_deletion_service, |
26
|
|
|
) |
27
|
|
|
from byceps.services.user.transfer.models import User |
28
|
|
|
from byceps.typing import BrandID |
29
|
|
|
|
30
|
|
|
from tests.database import set_up_database, tear_down_database |
31
|
|
|
from tests.helpers import ( |
32
|
|
|
create_admin_app, |
33
|
|
|
create_party, |
34
|
|
|
create_permissions, |
35
|
|
|
create_role_with_permissions_assigned, |
36
|
|
|
create_site, |
37
|
|
|
create_site_app, |
38
|
|
|
create_user, |
39
|
|
|
generate_token, |
40
|
|
|
http_client, |
41
|
|
|
) |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
CONFIG_PATH_DATA_KEY = 'PATH_DATA' |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
@pytest.fixture(scope='session') |
48
|
|
|
def make_admin_app(data_path): |
49
|
|
|
"""Provide the admin web application.""" |
50
|
|
|
|
51
|
|
|
def _wrapper(**config_overrides): |
52
|
|
|
if CONFIG_PATH_DATA_KEY not in config_overrides: |
53
|
|
|
config_overrides[CONFIG_PATH_DATA_KEY] = data_path |
54
|
|
|
return create_admin_app(config_overrides) |
55
|
|
|
|
56
|
|
|
return _wrapper |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
@pytest.fixture(scope='session') |
60
|
|
|
def admin_app(make_admin_app): |
61
|
|
|
"""Provide the admin web application.""" |
62
|
|
|
app = make_admin_app() |
63
|
|
|
with app.app_context(): |
64
|
|
|
set_up_database() |
65
|
|
|
yield app |
66
|
|
|
tear_down_database() |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
@pytest.fixture(scope='session') |
70
|
|
|
def make_site_app(admin_app, data_path): |
71
|
|
|
"""Provide a site web application.""" |
72
|
|
|
|
73
|
|
|
def _wrapper(**config_overrides): |
74
|
|
|
if CONFIG_PATH_DATA_KEY not in config_overrides: |
75
|
|
|
config_overrides[CONFIG_PATH_DATA_KEY] = data_path |
76
|
|
|
return create_site_app(config_overrides) |
77
|
|
|
|
78
|
|
|
return _wrapper |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
@pytest.fixture(scope='session') |
82
|
|
|
def site_app(make_site_app): |
83
|
|
|
"""Provide a site web application.""" |
84
|
|
|
app = make_site_app() |
85
|
|
|
with app.app_context(): |
86
|
|
|
yield app |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
@pytest.fixture(scope='session') |
90
|
|
|
def data_path(): |
91
|
|
|
with TemporaryDirectory() as d: |
92
|
|
|
yield Path(d) |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
@pytest.fixture(scope='package') |
96
|
|
|
def make_client(): |
97
|
|
|
"""Provide a test HTTP client against the application.""" |
98
|
|
|
|
99
|
|
|
def _wrapper(app, *, user_id=None): |
100
|
|
|
with http_client(app, user_id=user_id) as client: |
101
|
|
|
return client |
102
|
|
|
|
103
|
|
|
return _wrapper |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
@pytest.fixture(scope='session') |
107
|
|
|
def permission_tickle_mortals(admin_app): |
108
|
|
|
permission = authz_service.create_permission( |
109
|
|
|
'tickle_mortals', 'Tickle mortals' |
110
|
|
|
) |
111
|
|
|
yield permission |
112
|
|
|
authz_service.delete_permission(permission.id) |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
@pytest.fixture(scope='session') |
116
|
|
|
def make_user(admin_app): |
117
|
|
|
user_ids = set() |
118
|
|
|
|
119
|
|
|
def _wrapper(*args, **kwargs) -> User: |
120
|
|
|
user = create_user(*args, **kwargs) |
121
|
|
|
user_ids.add(user.id) |
122
|
|
|
return user |
123
|
|
|
|
124
|
|
|
yield _wrapper |
125
|
|
|
|
126
|
|
|
for user_id in user_ids: |
127
|
|
|
user_deletion_service.delete_account(user_id, user_id, 'clean up') |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
@pytest.fixture(scope='session') |
131
|
|
|
def make_admin(make_user): |
132
|
|
|
user_ids = set() |
133
|
|
|
created_permission_ids = set() |
134
|
|
|
created_role_ids = set() |
135
|
|
|
|
136
|
|
|
def _wrapper(screen_name: str, permission_ids: set[str], *args, **kwargs): |
137
|
|
|
admin = make_user(screen_name, *args, **kwargs) |
138
|
|
|
user_ids.add(admin.id) |
139
|
|
|
|
140
|
|
|
# Create (not yet created) permissions. |
141
|
|
|
new_permission_ids = permission_ids.difference(created_permission_ids) |
142
|
|
|
create_permissions(new_permission_ids) |
143
|
|
|
created_permission_ids.update(new_permission_ids) |
144
|
|
|
|
145
|
|
|
# Create role. |
146
|
|
|
role_id = f'admin_{token_hex(3)}' |
147
|
|
|
create_role_with_permissions_assigned(role_id, permission_ids) |
148
|
|
|
created_role_ids.add(role_id) |
149
|
|
|
|
150
|
|
|
# Assign role to user. |
151
|
|
|
authz_service.assign_role_to_user(role_id, admin.id) |
152
|
|
|
|
153
|
|
|
return admin |
154
|
|
|
|
155
|
|
|
yield _wrapper |
156
|
|
|
|
157
|
|
|
# Remove permissions and role again. |
158
|
|
|
|
159
|
|
|
for user_id in user_ids: |
160
|
|
|
authz_service.deassign_all_roles_from_user(user_id) |
161
|
|
|
|
162
|
|
|
for role_id in created_role_ids: |
163
|
|
|
authz_service.delete_role(role_id) |
164
|
|
|
|
165
|
|
|
for permission_id in created_permission_ids: |
166
|
|
|
authz_service.delete_permission(permission_id) |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
@pytest.fixture(scope='session') |
170
|
|
|
def admin_user(make_user): |
171
|
|
|
return make_user('Admin') |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
@pytest.fixture(scope='session') |
175
|
|
|
def user(make_user): |
176
|
|
|
return make_user('User') |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
@pytest.fixture(scope='session') |
180
|
|
|
def uninitialized_user(make_user): |
181
|
|
|
return make_user('UninitializedUser', initialized=False) |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
@pytest.fixture(scope='session') |
185
|
|
|
def suspended_user(make_user): |
186
|
|
|
return make_user('SuspendedUser', suspended=True) |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
@pytest.fixture(scope='session') |
190
|
|
|
def deleted_user(make_user): |
191
|
|
|
return make_user('DeletedUser', deleted=True) |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
# Dependency on `brand` avoids error on clean up. |
195
|
|
|
@pytest.fixture(scope='session') |
196
|
|
|
def make_email_config(admin_app, brand): |
197
|
|
|
config_brand_ids = set() |
198
|
|
|
|
199
|
|
|
def _wrapper( |
200
|
|
|
brand_id: BrandID, |
201
|
|
|
*, |
202
|
|
|
sender_address: Optional[str] = None, |
203
|
|
|
sender_name: Optional[str] = None, |
204
|
|
|
contact_address: Optional[str] = None, |
205
|
|
|
): |
206
|
|
|
if sender_address is None: |
207
|
|
|
sender_address = f'{generate_token()}@domain.example' |
208
|
|
|
|
209
|
|
|
email_service.set_config( |
210
|
|
|
brand_id, |
211
|
|
|
sender_address, |
212
|
|
|
sender_name=sender_name, |
213
|
|
|
contact_address=contact_address, |
214
|
|
|
) |
215
|
|
|
|
216
|
|
|
config = email_service.get_config(brand_id) |
217
|
|
|
config_brand_ids.add(config.brand_id) |
218
|
|
|
|
219
|
|
|
return config |
220
|
|
|
|
221
|
|
|
yield _wrapper |
222
|
|
|
|
223
|
|
|
for brand_id in config_brand_ids: |
224
|
|
|
email_service.delete_config(brand_id) |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
@pytest.fixture(scope='session') |
228
|
|
|
def email_config(make_email_config, brand): |
229
|
|
|
return make_email_config(brand.id, sender_address='[email protected]') |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
@pytest.fixture(scope='session') |
233
|
|
|
def site(email_config, party, board): |
234
|
|
|
site = create_site( |
235
|
|
|
'acmecon-2014-website', |
236
|
|
|
party.brand_id, |
237
|
|
|
title='ACMECon 2014 website', |
238
|
|
|
server_name='www.acmecon.test', |
239
|
|
|
party_id=party.id, |
240
|
|
|
board_id=board.id, |
241
|
|
|
) |
242
|
|
|
yield site |
243
|
|
|
site_service.delete_site(site.id) |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
@pytest.fixture(scope='session') |
247
|
|
|
def make_brand(admin_app): |
248
|
|
|
brand_ids = set() |
249
|
|
|
|
250
|
|
|
def _wrapper(brand_id=None, title=None): |
251
|
|
|
if brand_id is None: |
252
|
|
|
brand_id = generate_token() |
253
|
|
|
|
254
|
|
|
if title is None: |
255
|
|
|
title = brand_id |
256
|
|
|
|
257
|
|
|
brand = brand_service.create_brand(brand_id, title) |
258
|
|
|
brand_ids.add(brand.id) |
259
|
|
|
return brand |
260
|
|
|
|
261
|
|
|
yield _wrapper |
262
|
|
|
|
263
|
|
|
for brand_id in brand_ids: |
264
|
|
|
brand_service.delete_brand(brand_id) |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
@pytest.fixture(scope='session') |
268
|
|
|
def brand(make_brand): |
269
|
|
|
return make_brand('acmecon', 'ACME Entertainment Convention') |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
@pytest.fixture(scope='session') |
273
|
|
|
def make_party(admin_app, make_brand): |
274
|
|
|
party_ids = set() |
275
|
|
|
|
276
|
|
|
def _wrapper(*args, **kwargs): |
277
|
|
|
party = create_party(*args, **kwargs) |
278
|
|
|
party_ids.add(party.id) |
279
|
|
|
return party |
280
|
|
|
|
281
|
|
|
yield _wrapper |
282
|
|
|
|
283
|
|
|
for party_id in party_ids: |
284
|
|
|
party_service.delete_party(party_id) |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
@pytest.fixture(scope='session') |
288
|
|
|
def party(make_party, brand): |
289
|
|
|
return make_party(brand.id) |
290
|
|
|
|
291
|
|
|
|
292
|
|
|
@pytest.fixture(scope='session') |
293
|
|
|
def make_ticket_category(admin_app, party): |
294
|
|
|
category_ids = set() |
295
|
|
|
|
296
|
|
|
def _wrapper(party_id, title): |
297
|
|
|
category = ticketing_category_service.create_category(party_id, title) |
298
|
|
|
category_ids.add(category.id) |
299
|
|
|
return category |
300
|
|
|
|
301
|
|
|
yield _wrapper |
302
|
|
|
|
303
|
|
|
for category_id in category_ids: |
304
|
|
|
ticketing_category_service.delete_category(category_id) |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
@pytest.fixture(scope='session') |
308
|
|
|
def board(brand): |
309
|
|
|
board_id = brand.id |
310
|
|
|
board = board_service.create_board(brand.id, board_id) |
311
|
|
|
yield board |
312
|
|
|
board_service.delete_board(board.id) |
313
|
|
|
|