~homeworkprod/byceps

ref: 533662eefb314d54b168e5a46bc07c88b5f88be4 byceps/tests/conftest.py -rw-r--r-- 7.1 KiB
533662ee — Jochen Kupperschmidt Use Flask v2.0's `delete`/`get`/`patch`/`post` route shortcuts 5 months ago
                                                                                
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from __future__ import annotations
from pathlib import Path
from secrets import token_hex
from tempfile import TemporaryDirectory
from typing import Optional

import pytest

from byceps.services.authorization import service as authz_service
from byceps.services.board import board_service
from byceps.services.brand import service as brand_service
from byceps.services.email import service as email_service
from byceps.services.party import service as party_service
from byceps.services.site import service as site_service
from byceps.services.user import (
    command_service as user_command_service,
    deletion_service as user_deletion_service,
    service as user_service,
)
from byceps.typing import BrandID

from tests.database import set_up_database, tear_down_database
from tests.helpers import (
    create_admin_app,
    create_party,
    create_permissions,
    create_role_with_permissions_assigned,
    create_site,
    create_site_app,
    create_user,
    create_user_with_detail,
    generate_token,
    http_client,
)


CONFIG_PATH_DATA_KEY = 'PATH_DATA'


@pytest.fixture(scope='session')
def make_admin_app(data_path):
    """Provide the admin web application."""

    def _wrapper(**config_overrides):
        if CONFIG_PATH_DATA_KEY not in config_overrides:
            config_overrides[CONFIG_PATH_DATA_KEY] = data_path
        return create_admin_app(config_overrides)

    return _wrapper


@pytest.fixture(scope='session')
def admin_app(make_admin_app):
    """Provide the admin web application."""
    app = make_admin_app()
    with app.app_context():
        set_up_database()
        yield app
        tear_down_database()


@pytest.fixture(scope='session')
def make_site_app(admin_app, data_path):
    """Provide a site web application."""

    def _wrapper(**config_overrides):
        if CONFIG_PATH_DATA_KEY not in config_overrides:
            config_overrides[CONFIG_PATH_DATA_KEY] = data_path
        return create_site_app(config_overrides)

    return _wrapper


@pytest.fixture(scope='session')
def site_app(make_site_app):
    """Provide a site web application."""
    app = make_site_app()
    with app.app_context():
        yield app


@pytest.fixture(scope='session')
def data_path():
    with TemporaryDirectory() as d:
        yield Path(d)


@pytest.fixture(scope='package')
def make_client():
    """Provide a test HTTP client against the application."""

    def _wrapper(app, *, user_id=None):
        with http_client(app, user_id=user_id) as client:
            return client

    return _wrapper


@pytest.fixture(scope='session')
def make_user(admin_app):
    user_ids = set()

    def _wrapper(*args, **kwargs):
        user = create_user(*args, **kwargs)
        user_ids.add(user.id)
        user_dto = user_service._db_entity_to_user(user)
        return user_dto

    yield _wrapper

    for user_id in user_ids:
        user_deletion_service.delete_account(user_id, user_id, 'clean up')


@pytest.fixture(scope='module')
def make_user_with_detail(admin_app):
    user_ids = set()

    def _wrapper(*args, **kwargs):
        user = create_user_with_detail(*args, **kwargs)
        user_ids.add(user.id)
        user_dto = user_service._db_entity_to_user_with_detail(user)
        return user_dto

    yield _wrapper

    for user_id in user_ids:
        user_deletion_service.delete_account(user_id, user_id, 'clean up')


@pytest.fixture(scope='session')
def make_admin(make_user):
    user_ids = set()
    created_permission_ids = set()
    created_role_ids = set()

    def _wrapper(screen_name: str, permission_ids: set[str]):
        admin = make_user(screen_name)
        user_ids.add(admin.id)

        # Create permissions and role.
        role_id = f'admin_{token_hex(3)}'
        create_permissions(permission_ids)
        created_permission_ids.update(permission_ids)
        create_role_with_permissions_assigned(role_id, permission_ids)
        authz_service.assign_role_to_user(role_id, admin.id)
        created_role_ids.add(role_id)

        return admin

    yield _wrapper

    # Remove permissions and role again.

    for user_id in user_ids:
        authz_service.deassign_all_roles_from_user(user_id)

    for role_id in created_role_ids:
        authz_service.delete_role(role_id)

    for permission_id in created_permission_ids:
        authz_service.delete_permission(permission_id)


@pytest.fixture(scope='session')
def admin_user(make_user):
    return make_user('Admin')


@pytest.fixture(scope='session')
def user(make_user):
    return make_user('User')


@pytest.fixture(scope='session')
def uninitialized_user(make_user):
    return make_user('UninitializedUser', initialized=False)


@pytest.fixture(scope='session')
def suspended_user(make_user):
    return make_user('SuspendedUser', suspended=True)


@pytest.fixture(scope='session')
def deleted_user(make_user):
    return make_user('DeletedUser', deleted=True)


# Dependency on `brand` avoids error on clean up.
@pytest.fixture(scope='session')
def make_email_config(admin_app, brand):
    configs = []

    def _wrapper(
        brand_id: BrandID,
        *,
        sender_address: Optional[str] = None,
        sender_name: Optional[str] = None,
        contact_address: Optional[str] = None,
    ):
        if sender_address is None:
            sender_address = f'{generate_token()}@domain.example'

        email_service.set_config(
            brand_id,
            sender_address,
            sender_name=sender_name,
            contact_address=contact_address,
        )

        config = email_service.get_config(brand_id)
        configs.append(config)

        return config

    yield _wrapper

    for config in configs:
        email_service.delete_config(config.brand_id)


@pytest.fixture(scope='session')
def email_config(make_email_config, brand):
    return make_email_config(brand.id, sender_address='noreply@acmecon.test')


@pytest.fixture(scope='session')
def site(email_config, party, board):
    site = create_site(
        'acmecon-2014-website',
        party.brand_id,
        title='ACMECon 2014 website',
        server_name='www.acmecon.test',
        party_id=party.id,
        board_id=board.id,
    )
    yield site
    site_service.delete_site(site.id)


@pytest.fixture(scope='session')
def make_brand(admin_app):
    brands = []

    def _wrapper(brand_id=None, title=None):
        if brand_id is None:
            brand_id = generate_token()

        if title is None:
            title = brand_id

        brand = brand_service.create_brand(brand_id, title)
        brands.append(brand)
        return brand

    yield _wrapper

    for brand in brands:
        brand_service.delete_brand(brand.id)


@pytest.fixture(scope='session')
def brand(make_brand):
    return make_brand('acmecon', 'ACME Entertainment Convention')


@pytest.fixture(scope='session')
def party(brand):
    party = create_party(brand.id)
    yield party
    party_service.delete_party(party.id)


@pytest.fixture(scope='session')
def board(brand):
    board_id = brand.id
    board = board_service.create_board(brand.id, board_id)
    yield board
    board_service.delete_board(board.id)