forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
170 lines (142 loc) · 4.9 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
from __future__ import print_function
import logging
import mock
import responses
import pytest
from faker import Factory
from website import settings as website_settings
from framework.celery_tasks import app as celery_app
from elasticsearch_dsl.connections import connections
from django.core.management import call_command
logger = logging.getLogger(__name__)
# Silence some 3rd-party logging and some "loud" internal loggers
SILENT_LOGGERS = [
'api.caching.tasks',
'factory.generate',
'factory.containers',
'framework.analytics',
'framework.auth.core',
'website.app',
'website.archiver.tasks',
'website.mails',
'website.notifications.listeners',
'website.search.elastic_search',
'website.search_migration.migrate',
'website.util.paths',
'requests_oauthlib.oauth2_session',
'raven.base.Client',
'raven.contrib.django.client.DjangoClient',
'transitions.core',
'MARKDOWN',
'elasticsearch',
]
for logger_name in SILENT_LOGGERS:
logging.getLogger(logger_name).setLevel(logging.CRITICAL)
@pytest.fixture(autouse=True)
def override_settings():
"""Override settings for the test environment.
"""
# Make tasks run synchronously, and make sure exceptions get propagated
celery_app.conf.update({
'task_always_eager': True,
'task_eager_propagates': True,
})
website_settings.ENABLE_EMAIL_SUBSCRIPTIONS = False
# TODO: Remove if this is unused?
website_settings.BCRYPT_LOG_ROUNDS = 1
# Make sure we don't accidentally send any emails
website_settings.SENDGRID_API_KEY = None
# Set this here instead of in SILENT_LOGGERS, in case developers
# call setLevel in local.py
logging.getLogger('website.mails.mails').setLevel(logging.CRITICAL)
@pytest.fixture()
def fake():
return Factory.create()
_MOCKS = {
'osf.models.user.new_bookmark_collection': {
'mark': 'enable_bookmark_creation',
'replacement': lambda *args, **kwargs: None,
},
'osf.models.user._create_quickfiles_project': {
'mark': 'enable_quickfiles_creation',
'replacement': lambda *args, **kwargs: None,
},
'framework.celery_tasks.handlers._enqueue_task': {
'mark': 'enable_enqueue_task',
'replacement': lambda *args, **kwargs: None,
},
'osf.models.base.BaseModel.full_clean': {
'mark': 'enable_implicit_clean',
'replacement': lambda *args, **kwargs: None,
},
'osf.models.base._check_blacklist': {
'mark': 'enable_blacklist_check',
'replacement': lambda *args, **kwargs: False,
},
'website.search.search.search_engine': {
'mark': 'enable_search',
'replacement': mock.MagicMock()
},
'website.search.elastic_search': {
'mark': 'enable_search',
'replacement': mock.MagicMock()
}
}
@pytest.fixture(autouse=True, scope='session')
def _test_speedups():
mocks = {}
for target, config in _MOCKS.items():
mocks[target] = mock.patch(target, config['replacement'])
mocks[target].start()
yield mocks
for patcher in mocks.values():
patcher.stop()
@pytest.fixture(autouse=True)
def _test_speedups_disable(request, settings, _test_speedups):
patchers = []
for target, config in _MOCKS.items():
if not request.node.get_marker(config['mark']):
continue
patchers.append(_test_speedups[target])
patchers[-1].stop()
yield
for patcher in patchers:
patcher.start()
@pytest.fixture(scope='function')
def es6_client():
return connections.get_connection()
@pytest.fixture(scope='function', autouse=True)
def _es_marker(request, es6_client):
"""Clear out all indices and index templates before and after
tests marked with ``es``.
"""
marker = request.node.get_closest_marker('es')
if marker:
def teardown_es():
es6_client.indices.delete(index='*')
es6_client.indices.delete_template('*')
teardown_es()
call_command('sync_metrics')
yield
teardown_es()
else:
yield
@pytest.fixture
def mock_share():
with mock.patch('api.share.utils.settings.SHARE_ENABLED', True):
with mock.patch('api.share.utils.settings.SHARE_API_TOKEN', 'mock-api-token'):
with responses.RequestsMock(assert_all_requests_are_fired=True) as rsps:
rsps.add(responses.POST, 'https://share.osf.io/api/v2/normalizeddata/', status=200)
yield rsps
@pytest.fixture
def mock_akismet():
"""
This should be used to mock our anti-spam service akismet.
Relevent endpoints:
'https://{api_key}.rest.akismet.com/1.1/submit-spam'
'https://{api_key}.rest.akismet.com/1.1/submit-ham'
'https://{api_key}.rest.akismet.com/1.1/comment-check'
"""
with mock.patch.object(website_settings, 'SPAM_CHECK_ENABLED', True):
with responses.RequestsMock(assert_all_requests_are_fired=True) as rsps:
yield rsps