Passed
Push — main ( 4e2ecc...893310 )
by Jochen
04:46
created

tests.conftest.make_ticket_category()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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