-
Notifications
You must be signed in to change notification settings - Fork 17
/
conftest.py
134 lines (119 loc) · 4.39 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
"""
Pytest configuration file for the entire micromasters app
"""
# pylint: disable=redefined-outer-name
import warnings
from unittest.mock import patch
from types import SimpleNamespace
from django.utils.deprecation import RemovedInDjango30Warning
import pytest
from search import tasks
@pytest.fixture(autouse=True)
def warnings_as_errors():
"""
Convert warnings to errors. This should only affect unit tests, letting pylint and other plugins
raise DeprecationWarnings without erroring.
"""
try:
warnings.resetwarnings()
warnings.simplefilter('error')
# For celery
warnings.simplefilter('ignore', category=ImportWarning)
warnings.filterwarnings(
"ignore",
message="'async' and 'await' will become reserved keywords in Python 3.7",
category=DeprecationWarning,
)
warnings.filterwarnings(
"ignore",
message=(
"Using or importing the ABCs from 'collections' instead of "
"from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working"
),
category=DeprecationWarning
)
warnings.filterwarnings(
"ignore",
message=(
"Using or importing the ABCs from 'collections' instead of "
"from 'collections.abc' is deprecated, and in 3.8 it will stop working"
),
category=DeprecationWarning
)
# For compatibility modules in various libraries
warnings.filterwarnings(
"ignore",
module=".*(compat|permission_tags).*",
)
# For pysftp
warnings.filterwarnings(
"ignore",
category=UserWarning,
message='Failed to load HostKeys',
)
# For Django 3.0 compatibility, which we don't care about yet
warnings.filterwarnings("ignore", category=RemovedInDjango30Warning)
yield
finally:
warnings.resetwarnings()
@pytest.fixture(autouse=True)
def settings_defaults(settings):
"""
Sets default settings to safe defaults
"""
settings.FEATURES['OPEN_DISCUSSIONS_USER_SYNC'] = False
@pytest.fixture(scope='function')
def mocked_opensearch_module_patcher(settings):
"""
Fixture that patches all indexing API functions that communicate directly with OpenSearch
"""
settings.DEBUG=True
patchers = []
patcher_mocks = []
for name, val in tasks.__dict__.items():
# This looks for functions starting with _ because those are the functions which are imported
# from indexing_api. The _ lets it prevent name collisions.
if callable(val) and name.startswith("_"):
patchers.append(patch('search.tasks.{0}'.format(name), autospec=True))
for patcher in patchers:
mock = patcher.start()
mock.name = patcher.attribute
patcher_mocks.append(mock)
yield SimpleNamespace(
patchers=patchers,
patcher_mocks=patcher_mocks
)
for patcher in patchers:
patcher.stop()
@pytest.fixture()
def mocked_opensearch(mocked_opensearch_module_patcher):
"""
Fixture that resets all of the patched OpenSearch API functions
"""
for mock in mocked_opensearch_module_patcher.patcher_mocks:
mock.reset_mock()
return mocked_opensearch_module_patcher
@pytest.fixture()
def discussion_settings(settings):
"""Set discussion-specific settings"""
settings.OPEN_DISCUSSIONS_JWT_SECRET = 'secret'
settings.OPEN_DISCUSSIONS_COOKIE_NAME = 'jwt_cookie'
settings.OPEN_DISCUSSIONS_COOKIE_DOMAIN = 'localhost'
settings.OPEN_DISCUSSIONS_REDIRECT_URL = 'http://localhost/'
settings.OPEN_DISCUSSIONS_BASE_URL = 'http://localhost/'
settings.OPEN_DISCUSSIONS_API_USERNAME = 'mitodl'
settings.FEATURES['OPEN_DISCUSSIONS_USER_SYNC'] = True
@pytest.fixture()
def mocked_on_commit(mocker):
"""
Patch on_commit to execute immediately instead of waiting for transaction to commit.
Since tests run inside transactions this will delay execution until after the test.
Since uses of on_commit are usually meant to delay executing tasks, and since
tasks are executed immediately in unit tests, executing immediately shouldn't
cause problems here.
"""
return mocker.patch(
'django.db.transaction.on_commit',
autospec=True,
side_effect=lambda callback: callback(),
)