-
Notifications
You must be signed in to change notification settings - Fork 964
/
conftest.py
339 lines (255 loc) · 10.1 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
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import os.path
import xmlrpc.client
from collections import defaultdict
from contextlib import contextmanager
from unittest import mock
import alembic.command
import click.testing
import pretend
import pyramid.testing
import pytest
import webtest as _webtest
from pyramid.i18n import TranslationString
from pyramid.static import ManifestCacheBuster
from pytest_postgresql.factories import DatabaseJanitor, get_config
from sqlalchemy import event
from warehouse import admin, config, static
from warehouse.accounts import services as account_services
from warehouse.macaroons import services as macaroon_services
from warehouse.metrics import IMetricsService
from .common.db import Session
def pytest_collection_modifyitems(items):
for item in items:
if not hasattr(item, "module"): # e.g.: DoctestTextfile
continue
module_path = os.path.relpath(
item.module.__file__, os.path.commonprefix([__file__, item.module.__file__])
)
module_root_dir = module_path.split(os.pathsep)[0]
if module_root_dir.startswith("functional"):
item.add_marker(pytest.mark.functional)
elif module_root_dir.startswith("unit"):
item.add_marker(pytest.mark.unit)
else:
raise RuntimeError("Unknown test type (filename = {0})".format(module_path))
@contextmanager
def metrics_timing(*args, **kwargs):
yield None
@pytest.fixture
def metrics():
return pretend.stub(
event=pretend.call_recorder(lambda *args, **kwargs: None),
increment=pretend.call_recorder(lambda *args, **kwargs: None),
histogram=pretend.call_recorder(lambda *args, **kwargs: None),
timing=pretend.call_recorder(lambda *args, **kwargs: None),
timed=pretend.call_recorder(
lambda *args, **kwargs: metrics_timing(*args, **kwargs)
),
)
class _Services:
def __init__(self):
self._services = defaultdict(lambda: defaultdict(dict))
def register_service(self, service_obj, iface=None, context=None, name=""):
self._services[iface][context][name] = service_obj
def find_service(self, iface=None, context=None, name=""):
return self._services[iface][context][name]
@pytest.fixture
def pyramid_services(metrics):
services = _Services()
# Register our global services.
services.register_service(metrics, IMetricsService, None, name="")
return services
@pytest.fixture
def pyramid_request(pyramid_services):
dummy_request = pyramid.testing.DummyRequest()
dummy_request.find_service = pyramid_services.find_service
dummy_request.remote_addr = "1.2.3.4"
def localize(message, **kwargs):
ts = TranslationString(message, **kwargs)
return ts.interpolate()
dummy_request._ = localize
return dummy_request
@pytest.yield_fixture
def pyramid_config(pyramid_request):
with pyramid.testing.testConfig(request=pyramid_request) as config:
yield config
@pytest.yield_fixture
def cli():
runner = click.testing.CliRunner()
with runner.isolated_filesystem():
yield runner
@pytest.fixture(scope="session")
def database(request):
config = get_config(request)
pg_host = config.get("host")
pg_port = config.get("port") or os.environ.get("PGPORT", 5432)
pg_user = config.get("user")
pg_db = config.get("db", "tests")
pg_version = config.get("version", 10.1)
janitor = DatabaseJanitor(pg_user, pg_host, pg_port, pg_db, pg_version)
# In case the database already exists, possibly due to an aborted test run,
# attempt to drop it before creating
janitor.drop()
# Create our Database.
janitor.init()
# Ensure our database gets deleted.
@request.addfinalizer
def drop_database():
janitor.drop()
return "postgresql://{}@{}:{}/{}".format(pg_user, pg_host, pg_port, pg_db)
class MockManifestCacheBuster(ManifestCacheBuster):
def __init__(self, *args, strict=True, **kwargs):
super().__init__(*args, **kwargs)
def get_manifest(self):
return {}
@pytest.fixture
def mock_manifest_cache_buster():
return MockManifestCacheBuster
@pytest.fixture(scope="session")
def app_config(database):
settings = {
"warehouse.prevent_esi": True,
"warehouse.token": "insecure token",
"camo.url": "http://localhost:9000/",
"camo.key": "insecure key",
"celery.broker_url": "amqp://",
"celery.result_url": "redis://localhost:0/",
"celery.scheduler_url": "redis://localhost:0/",
"database.url": database,
"docs.url": "http://docs.example.com/",
"ratelimit.url": "memory://",
"elasticsearch.url": "https://localhost/warehouse",
"files.backend": "warehouse.packaging.services.LocalFileStorage",
"docs.backend": "warehouse.packaging.services.LocalFileStorage",
"mail.backend": "warehouse.email.services.SMTPEmailSender",
"malware_check.backend": (
"warehouse.malware.services.PrinterMalwareCheckService"
),
"files.url": "http://localhost:7000/",
"sessions.secret": "123456",
"sessions.url": "redis://localhost:0/",
"statuspage.url": "https://2p66nmmycsj3.statuspage.io",
"warehouse.xmlrpc.cache.url": "redis://localhost:0/",
}
with mock.patch.object(config, "ManifestCacheBuster", MockManifestCacheBuster):
with mock.patch("warehouse.admin.ManifestCacheBuster", MockManifestCacheBuster):
with mock.patch.object(static, "whitenoise_add_manifest"):
cfg = config.configure(settings=settings)
# Ensure our migrations have been ran.
alembic.command.upgrade(cfg.alembic_config(), "head")
return cfg
@pytest.yield_fixture
def db_session(app_config):
engine = app_config.registry["sqlalchemy.engine"]
conn = engine.connect()
trans = conn.begin()
session = Session(bind=conn)
# Start the session in a SAVEPOINT
session.begin_nested()
# Then each time that SAVEPOINT ends, reopen it
@event.listens_for(session, "after_transaction_end")
def restart_savepoint(session, transaction):
if transaction.nested and not transaction._parent.nested:
session.begin_nested()
try:
yield session
finally:
session.close()
Session.remove()
trans.rollback()
conn.close()
engine.dispose()
@pytest.yield_fixture
def user_service(db_session, metrics):
return account_services.DatabaseUserService(db_session, metrics=metrics)
@pytest.yield_fixture
def macaroon_service(db_session):
return macaroon_services.DatabaseMacaroonService(db_session)
@pytest.yield_fixture
def token_service(app_config):
return account_services.TokenService(secret="secret", salt="salt", max_age=21600)
class QueryRecorder:
def __init__(self):
self.queries = []
self.recording = False
def __enter__(self):
self.start()
def __exit__(self, type, value, traceback):
self.stop()
def record(self, conn, cursor, statement, *args):
if self.recording:
self.queries.append(statement)
def start(self):
self.recording = True
def stop(self):
self.recording = False
def clear(self):
self.queries = []
@pytest.yield_fixture
def query_recorder(app_config):
recorder = QueryRecorder()
engine = app_config.registry["sqlalchemy.engine"]
event.listen(engine, "before_cursor_execute", recorder.record)
try:
yield recorder
finally:
event.remove(engine, "before_cursor_execute", recorder.record)
@pytest.fixture
def db_request(pyramid_request, db_session):
pyramid_request.db = db_session
pyramid_request.flags = admin.flags.Flags(pyramid_request)
return pyramid_request
class _TestApp(_webtest.TestApp):
def xmlrpc(self, path, method, *args):
body = xmlrpc.client.dumps(args, methodname=method)
resp = self.post(path, body, headers={"Content-Type": "text/xml"})
return xmlrpc.client.loads(resp.body)
@pytest.yield_fixture
def webtest(app_config):
# TODO: Ensure that we have per test isolation of the database level
# changes. This probably involves flushing the database or something
# between test cases to wipe any committed changes.
# We want to disable anything that relies on TLS here.
app_config.add_settings(enforce_https=False)
try:
yield _TestApp(app_config.make_wsgi_app())
finally:
app_config.registry["sqlalchemy.engine"].dispose()
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
# execute all other hooks to obtain the report object
outcome = yield
rep = outcome.get_result()
# we only look at actual failing test calls, not setup/teardown
if rep.when == "call" and rep.failed:
if "browser" in item.fixturenames:
browser = item.funcargs["browser"]
for log_type in set(browser.log_types) - {"har"}:
data = "\n\n".join(
filter(
None, (log.get("message") for log in browser.get_log(log_type))
)
)
if data:
rep.sections.append(("Captured {} log".format(log_type), data))
@pytest.fixture(scope="session")
def monkeypatch_session():
# NOTE: This is a minor hack to avoid duplicate monkeypatching
# on every function scope for dummy_localize.
# https://github.com/pytest-dev/pytest/issues/1872#issuecomment-375108891
from _pytest.monkeypatch import MonkeyPatch
m = MonkeyPatch()
yield m
m.undo()