1
|
|
|
""" |
2
|
|
|
:Copyright: 2006-2020 Jochen Kupperschmidt |
3
|
|
|
:License: Modified BSD, see LICENSE for details. |
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
from pathlib import Path |
7
|
|
|
from tempfile import TemporaryDirectory |
8
|
|
|
from typing import Optional |
9
|
|
|
|
10
|
|
|
import pytest |
11
|
|
|
|
12
|
|
|
from byceps.services.board import board_service |
13
|
|
|
from byceps.services.brand import service as brand_service |
14
|
|
|
from byceps.services.email import service as email_service |
15
|
|
|
from byceps.services.party import service as party_service |
16
|
|
|
from byceps.services.site import service as site_service |
17
|
|
|
from byceps.services.user import ( |
18
|
|
|
command_service as user_command_service, |
19
|
|
|
service as user_service, |
20
|
|
|
) |
21
|
|
|
|
22
|
|
|
from tests.base import create_admin_app, create_site_app |
23
|
|
|
from tests.database import set_up_database, tear_down_database |
24
|
|
|
from tests.helpers import ( |
25
|
|
|
create_brand, |
26
|
|
|
create_party, |
27
|
|
|
create_site, |
28
|
|
|
create_user, |
29
|
|
|
create_user_with_detail, |
30
|
|
|
DEFAULT_EMAIL_CONFIG_ID, |
31
|
|
|
) |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
CONFIG_PATH_DATA_KEY = 'PATH_DATA' |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
@pytest.fixture(scope='session') |
38
|
|
|
def make_admin_app(data_path): |
39
|
|
|
"""Provide the admin web application.""" |
40
|
|
|
|
41
|
|
|
def _wrapper(**config_overrides): |
42
|
|
|
if CONFIG_PATH_DATA_KEY not in config_overrides: |
43
|
|
|
config_overrides[CONFIG_PATH_DATA_KEY] = data_path |
44
|
|
|
return create_admin_app(config_overrides) |
45
|
|
|
|
46
|
|
|
return _wrapper |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
@pytest.fixture(scope='session') |
50
|
|
|
def admin_app(make_admin_app): |
51
|
|
|
"""Provide the admin web application.""" |
52
|
|
|
app = make_admin_app() |
53
|
|
|
with app.app_context(): |
54
|
|
|
set_up_database() |
55
|
|
|
yield app |
56
|
|
|
tear_down_database() |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
@pytest.fixture(scope='session') |
60
|
|
|
def admin_client(admin_app): |
61
|
|
|
"""Provide a test HTTP client against the admin web application.""" |
62
|
|
|
return admin_app.test_client() |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
@pytest.fixture(scope='session') |
66
|
|
|
def make_site_app(admin_app, data_path): |
67
|
|
|
"""Provide a site web application.""" |
68
|
|
|
|
69
|
|
|
def _wrapper(**config_overrides): |
70
|
|
|
if CONFIG_PATH_DATA_KEY not in config_overrides: |
71
|
|
|
config_overrides[CONFIG_PATH_DATA_KEY] = data_path |
72
|
|
|
return create_site_app(config_overrides) |
73
|
|
|
|
74
|
|
|
return _wrapper |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
@pytest.fixture(scope='session') |
78
|
|
|
def site_app(make_site_app): |
79
|
|
|
"""Provide a site web application.""" |
80
|
|
|
app = make_site_app() |
81
|
|
|
with app.app_context(): |
82
|
|
|
yield app |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
@pytest.fixture(scope='session') |
86
|
|
|
def data_path(): |
87
|
|
|
with TemporaryDirectory() as d: |
88
|
|
|
yield Path(d) |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
@pytest.fixture(scope='session') |
92
|
|
|
def make_user(admin_app): |
93
|
|
|
user_ids = set() |
94
|
|
|
|
95
|
|
|
def _wrapper(*args, **kwargs): |
96
|
|
|
user = create_user(*args, **kwargs) |
97
|
|
|
user_ids.add(user.id) |
98
|
|
|
user_dto = user_service._db_entity_to_user(user) |
99
|
|
|
return user_dto |
100
|
|
|
|
101
|
|
|
yield _wrapper |
102
|
|
|
|
103
|
|
|
for user_id in user_ids: |
104
|
|
|
user_command_service.delete_account(user_id, user_id, 'clean up') |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
@pytest.fixture(scope='module') |
108
|
|
|
def make_user_with_detail(admin_app): |
109
|
|
|
user_ids = set() |
110
|
|
|
|
111
|
|
|
def _wrapper(*args, **kwargs): |
112
|
|
|
user = create_user_with_detail(*args, **kwargs) |
113
|
|
|
user_ids.add(user.id) |
114
|
|
|
user_dto = user_service._db_entity_to_user_with_detail(user) |
115
|
|
|
return user_dto |
116
|
|
|
|
117
|
|
|
yield _wrapper |
118
|
|
|
|
119
|
|
|
for user_id in user_ids: |
120
|
|
|
user_command_service.delete_account(user_id, user_id, 'clean up') |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
@pytest.fixture(scope='session') |
124
|
|
|
def admin_user(make_user): |
125
|
|
|
return make_user('Admin') |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
@pytest.fixture(scope='session') |
129
|
|
|
def user(make_user): |
130
|
|
|
return make_user('User') |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
@pytest.fixture(scope='session') |
134
|
|
|
def uninitialized_user(make_user): |
135
|
|
|
return make_user('UninitializedUser', initialized=False) |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
@pytest.fixture(scope='session') |
139
|
|
|
def suspended_user(make_user): |
140
|
|
|
return make_user('SuspendedUser', suspended=True) |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
@pytest.fixture(scope='session') |
144
|
|
|
def deleted_user(make_user): |
145
|
|
|
return make_user('DeletedUser', deleted=True) |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
@pytest.fixture(scope='session') |
149
|
|
|
def make_email_config(admin_app): |
150
|
|
|
def _wrapper( |
151
|
|
|
config_id: str, |
152
|
|
|
sender_address: str, |
153
|
|
|
*, |
154
|
|
|
sender_name: Optional[str] = None, |
155
|
|
|
contact_address: Optional[str] = None, |
156
|
|
|
): |
157
|
|
|
email_service.set_config( |
158
|
|
|
config_id, |
159
|
|
|
sender_address, |
160
|
|
|
sender_name=sender_name, |
161
|
|
|
contact_address=contact_address, |
162
|
|
|
) |
163
|
|
|
|
164
|
|
|
return email_service.get_config(config_id) |
165
|
|
|
|
166
|
|
|
return _wrapper |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
@pytest.fixture(scope='session') |
170
|
|
|
def email_config(make_email_config): |
171
|
|
|
return make_email_config( |
172
|
|
|
DEFAULT_EMAIL_CONFIG_ID, sender_address='[email protected]' |
173
|
|
|
) |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
@pytest.fixture(scope='session') |
177
|
|
|
def site(email_config, party, board): |
178
|
|
|
site = create_site( |
179
|
|
|
'acmecon-2014-website', |
180
|
|
|
party.brand_id, |
181
|
|
|
title='ACMECon 2014 website', |
182
|
|
|
server_name='www.acmecon.test', |
183
|
|
|
party_id=party.id, |
184
|
|
|
board_id=board.id, |
185
|
|
|
) |
186
|
|
|
yield site |
187
|
|
|
site_service.delete_site(site.id) |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
@pytest.fixture(scope='session') |
191
|
|
|
def brand(admin_app): |
192
|
|
|
brand = create_brand() |
193
|
|
|
yield brand |
194
|
|
|
brand_service.delete_brand(brand.id) |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
@pytest.fixture(scope='session') |
198
|
|
|
def party(brand): |
199
|
|
|
party = create_party(brand.id) |
200
|
|
|
yield party |
201
|
|
|
party_service.delete_party(party.id) |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
@pytest.fixture(scope='session') |
205
|
|
|
def board(brand): |
206
|
|
|
board_id = brand.id |
207
|
|
|
board = board_service.create_board(brand.id, board_id) |
208
|
|
|
yield board |
209
|
|
|
board_service.delete_board(board.id) |
210
|
|
|
|