forked from alphagov/transfer-coronavirus-data-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
380 lines (302 loc) · 9.31 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"""
This isn't used yet but there has to be a conftest file for
the test module includes to resolve successfully
"""
from datetime import datetime
import pytest
from config import load_environment
from main import app
from user import User
def get_standard_download_group():
return {
"preference": 10,
"value": "standard-download",
"display": "Standard download user",
}
def get_standard_upload_group():
return {
"preference": 20,
"value": "standard-upload",
"display": "Standard download and upload user",
}
def get_admin_full_group():
return {
"preference": 90,
"value": "admin-full",
"display": "User administrator full access",
}
def get_default_session():
user_paths = (
"web-app-prod-data/local_authority/haringey;"
"web-app-prod-data/local_authority/barnet"
)
session = {
"details": {
"user": "test-user@test-domain.com",
"email": "test-user@test-domain.com",
},
"name": "Test User",
"user": "test-user@test-domain.com",
"email": "test-user@test-domain.com",
"attributes": [
{"Name": "custom:is_la", "Value": "1"},
{"Name": "custom:paths", "Value": user_paths},
],
"group": get_standard_download_group(),
}
return session
@pytest.fixture()
def test_session():
return get_default_session()
@pytest.fixture()
def test_upload_session():
session = get_default_session()
session["group"] = get_standard_upload_group()
return session
@pytest.fixture()
def test_admin_session():
session = get_default_session()
session["group"] = get_admin_full_group()
return session
@pytest.fixture()
def test_jwt():
return get_test_jwt()
def get_test_jwt():
return {"cognito:username": "test-user", "email": "test-user@test-domain.com"}
@pytest.fixture()
def fake_jwt_decoder():
def decoder(
token: str, key: str, algorithms: list = [], verify: bool = False
) -> dict:
return get_test_jwt()
return decoder
@pytest.fixture()
def test_client():
# Flask provides a way to test your application by exposing the Werkzeug test Client
# and handling the context locals for you.
testing_client = app.test_client()
load_environment(app)
# Establish an application context before running the tests.
ctx = app.app_context()
ctx.push()
yield testing_client # this is where the testing happens!
ctx.pop()
@pytest.fixture()
def standard_download():
return get_standard_download_group()
@pytest.fixture()
def standard_upload():
return get_standard_upload_group()
@pytest.fixture()
def upload_form_fields():
return {
"file_location": "web-app-upload/local_authority/haringey",
"file_name": "test_upload",
"file_ext": "csv",
}
@pytest.fixture()
def valid_extensions():
return {"csv": {"ext": "csv", "display": "CSV"}}
@pytest.fixture()
def admin_user():
return {
"name": "Justin Casey",
"email": "justin.casey@communities.gov.uk",
"phone_number": "+447123456789",
"group": get_standard_download_group(),
"custom:is_la": "1",
"custom:paths": ";".join(
[
"web-app-prod-data/local_authority/barking",
"web-app-prod-data/local_authority/haringey",
]
),
}
@pytest.fixture()
def user_confirm_form():
form = {
"full-name": "Justin Casey",
"email": "justin.casey@communities.gov.uk",
"telephone-number": "07123456789",
"account": "standard-upload",
"is-la-radio": "yes",
"custom_paths": [
"web-app-prod-data/local_authority/barnet",
"web-app-prod-data/local_authority/hackney",
],
}
return form
@pytest.fixture
def mock_env_staging(monkeypatch):
monkeypatch.setenv("APP_ENVIRONMENT", "staging")
@pytest.fixture
def mock_env_production(monkeypatch):
monkeypatch.setenv("APP_ENVIRONMENT", "production")
@pytest.fixture
def valid_user():
return User("justin.casey@communities.gov.uk")
@pytest.fixture
def user_with_invalid_email():
return User("not_an_email")
@pytest.fixture
def user_with_invalid_domain():
return User("justin.casey@hotmail.com")
@pytest.fixture()
def admin_get_user():
now = datetime.utcnow()
return {
"Username": "justin.casey@communities.gov.uk",
"UserStatus": "CONFIRMED",
"UserCreateDate": now,
"UserLastModifiedDate": now,
"Enabled": True,
"UserAttributes": [
{"Name": "sub", "Value": "a_uuid"},
{"Name": "email_verified", "Value": "true"},
{
"Name": "custom:paths",
"Value": ";".join(
[
"web-app-prod-data/local_authority/barking",
"web-app-prod-data/local_authority/haringey",
]
),
},
{"Name": "name", "Value": "Justin Casey"},
{"Name": "phone_number_verified", "Value": "false"},
{"Name": "custom:is_la", "Value": "1"},
{"Name": "phone_number", "Value": "+447123456789"},
{"Name": "email", "Value": "justin.casey@communities.gov.uk"},
],
}
@pytest.fixture()
def user_details_response(group_result):
now = datetime.utcnow()
return {
"username": "justin.casey@communities.gov.uk",
"status": "a_status",
"createdate": now,
"lastmodifieddate": now,
"enabled": True,
"sub": "a_uuid",
"email_verified": "true",
"custom:paths": "some_custom_paths",
"name": "JustinCasey",
"phone_number_verified": "false",
"custom:is_la": "0",
"phone_number": "a_phone",
"email": "justin.casey@communities.gov.uk",
"group": group_result,
}
@pytest.fixture()
def standard_download_group_response():
return {"Groups": [{"GroupName": "standard-download"}]}
@pytest.fixture()
def group_result():
return {
"preference": 10,
"value": "standard-download",
"display": "Standard download user",
}
@pytest.fixture()
def create_user_arguments():
return {
"UserAttributes": [
{"Name": "name", "Value": "Justin Casey"},
{"Name": "email", "Value": "justin.casey@communities.gov.uk"},
{"Name": "email_verified", "Value": "true"},
{"Name": "phone_number", "Value": "+447123456789"},
{"Name": "phone_number_verified", "Value": "false"},
{"Name": "custom:is_la", "Value": "1"},
{
"Name": "custom:paths",
"Value": ";".join(
[
"web-app-prod-data/local_authority/barking",
"web-app-prod-data/local_authority/haringey",
]
),
},
],
"UserPoolId": "eu-west-2_poolid",
"Username": "justin.casey@communities.gov.uk",
"ForceAliasCreation": False,
"DesiredDeliveryMediums": ["EMAIL"],
}
@pytest.fixture()
def list_users_arguments():
return {
"AttributesToGet": [
"name",
"email",
"email_verified",
"phone_number",
"phone_number_verified",
"cognito:user_status",
"custom:paths",
"custom:is_la",
],
"Limit": 20,
}
@pytest.fixture()
def list_users_response(admin_get_user):
return {
"Users": [admin_get_user],
}
@pytest.fixture()
def list_users_result(user_details_response):
return {
"users": [user_details_response],
"token": "",
}
@pytest.fixture()
def test_no_mfa_user():
return {
"Username": "test-secrets",
"UserAttributes": [
{"Name": "custom:paths", "Value": "local_authority/barnet"},
{"Name": "custom:is_la", "Value": "1"},
],
}
@pytest.fixture()
def test_mfa_user():
return {
"Username": "test-secrets",
"UserAttributes": [
{"Name": "custom:paths", "Value": "local_authority/barnet"},
{"Name": "custom:is_la", "Value": "1"},
],
"MFAOptions": [{"DeliveryMedium": "SMS", "AttributeName": "phone_number"}],
"PreferredMfaSetting": "SMS_MFA",
"UserMFASettingList": ["SMS_MFA"],
}
@pytest.fixture()
def test_get_object():
return {"Body": "test,the,csv", "ResponseMetadata": {"HTTPStatusCode": 200}}
@pytest.fixture()
def test_list_object_file():
now = datetime.utcnow()
prefix = "web-app-prod-data/local_authority/barnet"
mock_list_object = {
"Key": f"{prefix}/people1.csv",
"Size": 100,
"LastModified": now,
}
return mock_list_object
@pytest.fixture()
def test_ssm_parameters():
return {
"/cognito/pool/name": "cognito_pool_name",
"/cognito/pool/id": "eu-west-2_poolid",
"/s3/bucket_name": "my_bucket",
"/s3/bucket_main_prefix": "web-app-testing-data",
"/flask/secret_key": "my_secret_key",
}
@pytest.fixture()
def test_load_cognito_settings():
return {
"client_id": "abc123",
"client_secret": "def456", # pragma: allowlist secret
"host_name": "example",
"domain": "auth.eu-west-2.amazoncognito.com",
}