-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathconftest.py
More file actions
297 lines (236 loc) · 9.9 KB
/
conftest.py
File metadata and controls
297 lines (236 loc) · 9.9 KB
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
import os
import subprocess
from collections import namedtuple
from contextlib import contextmanager
from urllib.parse import urlparse
import freezegun
import pytest
import sqlalchemy
from sqlalchemy import delete, text
from app import create_app, db, reset_memos
from app.authentication.auth import requires_admin_auth, requires_no_auth
from app.dao.provider_details_dao import get_provider_details_by_identifier
from app.notify_api_flask_app import NotifyApiFlaskApp
from tests.routes import test_admin_auth_blueprint, test_no_auth_blueprint
# Freezegun has a negative interaction with prompt_toolkit that ends up suppressing text written on the prompt of ipdb
# so let's ignore that module
# https://stackoverflow.com/questions/71584885/ipdb-stops-showing-prompt-text-after-carriage-return
# https://github.com/spulec/freezegun/pull/481
freezegun.configure(extend_ignore_list=["prompt_toolkit"])
@pytest.fixture(scope="session")
def notify_api():
app = NotifyApiFlaskApp("test")
create_app(app)
# Attach some routes that can be used as helpers for specific tests.
test_no_auth_blueprint.before_request(requires_no_auth)
app.register_blueprint(test_no_auth_blueprint, url_prefix="/test")
test_admin_auth_blueprint.before_request(requires_admin_auth)
app.register_blueprint(test_admin_auth_blueprint, url_prefix="/admin-test")
# deattach server-error error handlers - error_handler_spec looks like:
# {'blueprint_name': {
# status_code: [error_handlers],
# None: { ExceptionClass: error_handler }
# }}
for error_handlers in app.error_handler_spec.values():
error_handlers.pop(500, None)
if None in error_handlers:
error_handlers[None] = {
exc_class: error_handler
for exc_class, error_handler in error_handlers[None].items()
if exc_class is not Exception
}
if error_handlers[None] == []:
error_handlers.pop(None)
ctx = app.app_context()
ctx.push()
yield app
ctx.pop()
reset_memos()
@pytest.fixture(scope="function")
def client(notify_api):
with notify_api.test_request_context(), notify_api.test_client() as client:
yield client
def create_test_db(database_uri):
# get the
db_uri_parts = database_uri.split("/")
postgres_db_uri = "/".join(db_uri_parts[:-1] + ["postgres"])
postgres_db = sqlalchemy.create_engine(postgres_db_uri, echo=False, client_encoding="utf8")
try:
with postgres_db.connect() as connection:
connection.execution_options(isolation_level="AUTOCOMMIT")
connection.execute(text(f"CREATE DATABASE {db_uri_parts[-1]}"))
except sqlalchemy.exc.ProgrammingError:
# database "test_notification_api_master" already exists
pass
finally:
postgres_db.dispose()
@pytest.fixture(scope="session", autouse=True)
def _notify_db(notify_api, worker_id):
"""
Manages the connection to the database. Generally this shouldn't be used, instead you should use the
`notify_db_session` fixture which also cleans up any data you've got left over after your test run.
"""
from flask import current_app
base_uri = current_app.config["SQLALCHEMY_DATABASE_URI"]
# the path as used with urlparse has a leading slash
db_name = f"/test_notification_api_{worker_id}"
db_uri = urlparse(str(base_uri))._replace(path=db_name).geturl()
# create a database for this worker thread -
current_app.config["SQLALCHEMY_DATABASE_URI"] = db_uri
current_app.config["SQLALCHEMY_BINDS"]["bulk"]["url"] = db_uri
# get rid of the old SQLAlchemy instance because we can’t have multiple on the same app
notify_api.extensions.pop("sqlalchemy")
# reinitalise the db so it picks up on the new test database name
db.init_app(notify_api)
create_test_db(current_app.config["SQLALCHEMY_DATABASE_URI"])
# Run this in a subprocess - alembic loads a lot of logging config that will otherwise splatter over our desired
# app logging config and breaks pytest.caplog.
result = subprocess.run(
["flask", "db", "upgrade"],
env={
**os.environ,
"SQLALCHEMY_DATABASE_URI": current_app.config["SQLALCHEMY_DATABASE_URI"],
"FLASK_APP": "application:application",
},
capture_output=True,
)
assert result.returncode == 0, result.stderr.decode()
# now db is initialised, run cleanup on it to remove any artifacts from migrations (such as the notify service and
# templates). Otherwise the very first test executed by a worker will be running on a different db setup to
# other tests that run later.
_clean_database(db)
with notify_api.app_context():
yield db
db.session.remove()
db.session_bulk.remove()
for engine in db.engines.values():
engine.dispose()
@pytest.fixture(scope="function")
def sms_providers(_notify_db):
"""
In production we randomly choose which provider to use based on their priority. To guarantee tests run the same each
time, make sure we always choose mmg. You'll need to override them in your tests if you wish to do something
different.
"""
get_provider_details_by_identifier("mmg").priority = 100
get_provider_details_by_identifier("firetext").priority = 0
@pytest.fixture(scope="function")
def notify_db_session_bulk(_notify_db, sms_providers):
"""
This fixture clears down all non static data after your test run. It yields the SQLAlchemy bulk session variable,
which is used for bulk/replica DB operations. Use this session to manually route queries to the replica database.
"""
yield _notify_db.session_bulk
_clean_database(_notify_db)
@pytest.fixture(scope="function")
def notify_db_session(_notify_db, sms_providers):
"""
This fixture clears down all non static data after your test run. It yields the sqlalchemy session variable
so you can manually add, commit, etc if needed.
`notify_db_session.commit()`
"""
yield _notify_db.session
_clean_database(_notify_db)
def _clean_database(_db):
_db.session.remove()
_db.session_bulk.remove()
for tbl in reversed(_db.metadata.sorted_tables):
if tbl.name not in [
"provider_details",
"key_types",
"branding_type",
"job_status",
"provider_details_history",
"template_process_type",
"notifications_all_time_view",
"notification_status_types",
"organisation_types",
"service_permission_types",
"auth_type",
"invite_status_type",
"service_callback_type",
"default_annual_allowance",
]:
stmt = delete(tbl)
_db.session.execute(stmt)
_db.session.commit()
# based on https://github.com/sqlalchemy/sqlalchemy/issues/5709#issuecomment-729689097
@pytest.fixture(scope="function")
def notify_db_session_log(notify_db_session):
queries = []
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
queries.append(
(
statement,
parameters,
context,
executemany,
)
)
sqlalchemy.event.listen(sqlalchemy.engine.Engine, "before_cursor_execute", before_cursor_execute)
try:
yield queries
finally:
sqlalchemy.event.remove(sqlalchemy.engine.Engine, "before_cursor_execute", before_cursor_execute)
@pytest.fixture
def os_environ():
"""
clear os.environ, and restore it after the test runs
"""
# for use whenever you expect code to edit environment variables
old_env = os.environ.copy()
os.environ.clear()
yield
# clear afterwards in case anything extra was added to the environment during the test
os.environ.clear()
for k, v in old_env.items():
os.environ[k] = v
@pytest.fixture(scope="session")
def hostnames(notify_api):
api_url = notify_api.config["API_HOST_NAME"]
admin_url = notify_api.config["ADMIN_BASE_URL"]
template_preview_url = notify_api.config["TEMPLATE_PREVIEW_API_HOST"]
return namedtuple("NotifyHostnames", ["api", "admin", "template_preview"])(
api=api_url, admin=admin_url, template_preview=template_preview_url
)
def pytest_generate_tests(metafunc):
# Copied from https://gist.github.com/pfctdayelise/5719730
idparametrize = metafunc.definition.get_closest_marker("idparametrize")
if idparametrize:
argnames, testdata = idparametrize.args
ids, argvalues = zip(*sorted(testdata.items()), strict=True)
metafunc.parametrize(argnames, argvalues, ids=ids)
@contextmanager
def set_config(app, name, value):
old_val = app.config.get(name)
app.config[name] = value
try:
yield
finally:
app.config[name] = old_val
@contextmanager
def set_config_values(app, dict):
old_values = {}
for key in dict:
old_values[key] = app.config.get(key)
app.config[key] = dict[key]
try:
yield
finally:
for key in dict:
app.config[key] = old_values[key]
def pytest_collection_modifyitems(session, config, items):
# This change forces the test_notification_dao_delete_notifications.py file to run at the very end of all the tests.
# The tests in this file delete data, which was causing other tests to fail.
# Running them last, preventing it from impacting other tests.
last_items = [item for item in items if "test_notification_dao_delete_notifications.py" in item.nodeid]
other_items = [item for item in items if "test_notification_dao_delete_notifications.py" not in item.nodeid]
items[:] = other_items + last_items
class Matcher:
def __init__(self, description, key):
self.description = description
self.key = key
def __eq__(self, other):
return self.key(other)
def __repr__(self):
return f"<Matcher: {self.description}>"