forked from reviewboard/reviewboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
142 lines (106 loc) · 4.32 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
"""Configures pytest and Django environment setup for Review Board.
.. important::
Do not define plugins in this file! Plugins must be in a different
package (such as in tests/). pytest overrides importers for plugins and
all modules descending from that module level, which will cause extension
importers to fail, breaking unit tests.
Version Added:
5.0
"""
import os
import sys
import tempfile
import django
import djblets
import pytest
from djblets.cache.serials import generate_media_serial
import reviewboard
sys.path.insert(0, os.path.join(os.path.dirname(__file__)))
tests_tempdir = None
@pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(django_db_reset_sequences):
"""Enable database access for all unit tests.
This is applied to all test functions, ensuring database access isn't
blocked.
"""
pass
@pytest.fixture(autouse=True, scope='session')
def setup_siteconfig():
"""Set up the siteconfig for tests.
This is run at the start of the project-wide test session, putting together
a suitable test environment for Review Board's test suite.
"""
from django.conf import settings
# Default to testing in a non-subdir install.
settings.SITE_ROOT = '/'
# Set some defaults for cache serials, in case the tests need them.
settings.AJAX_SERIAL = 123
settings.TEMPLATE_SERIAL = 123
# Set a faster password hasher, for performance.
settings.PASSWORD_HASHERS = (
'django.contrib.auth.hashers.SHA1PasswordHasher',
)
# Make sure we're using standard static files storage, and not
# something like Pipeline or S3 (since we don't want to trigger any
# special behavior). Subclasses are free to override this setting.
settings.STATICFILES_STORAGE = \
'django.contrib.staticfiles.storage.StaticFilesStorage'
# By default, don't look up DMARC records when generating From
# addresses for e-mails. Just assume we can, since we're not
# sending anything out. Some unit tests will override
# this.
settings.EMAIL_ENABLE_SMART_SPOOFING = False
# Create a temp directory that tests can rely upon.
tests_tempdir = tempfile.mkdtemp(prefix='rb-tests-')
# Configure file paths for static media. This will handle the main
# static and uploaded media directories, along with extension
# directories (for projects that need to use them).
settings.STATIC_URL = settings.SITE_ROOT + 'static/'
settings.MEDIA_URL = settings.SITE_ROOT + 'media/'
settings.STATIC_ROOT = os.path.join(tests_tempdir, 'static')
settings.MEDIA_ROOT = os.path.join(tests_tempdir, 'media')
settings.SITE_DATA_DIR = os.path.join(tests_tempdir, 'data')
settings.HAYSTACK_CONNECTIONS['default']['PATH'] = \
os.path.join(settings.SITE_DATA_DIR, 'search-index')
required_dirs = [
settings.SITE_DATA_DIR,
settings.STATIC_ROOT,
settings.MEDIA_ROOT,
os.path.join(settings.MEDIA_ROOT, 'uploaded', 'images'),
os.path.join(settings.MEDIA_ROOT, 'ext'),
os.path.join(settings.STATIC_ROOT, 'ext'),
]
for dirname in required_dirs:
if not os.path.exists(dirname):
os.makedirs(dirname)
from django.core import management
management.call_command('collectstatic',
verbosity=0,
interactive=False)
generate_media_serial()
@pytest.fixture(scope='session')
def django_db_setup(django_db_setup, django_db_blocker):
"""Set up the Django database.
This is run at the start of the project-wide test session, setting up
an initial siteconfig in the database.
"""
with django_db_blocker.unblock():
from reviewboard.admin.management.sites import init_siteconfig
siteconfig = init_siteconfig()
siteconfig.set('mail_from_spoofing', 'never')
siteconfig.save(update_fields=('settings',))
def pytest_report_header(config):
"""Return information for the report header.
This will log the version of Django.
Args:
config (object):
The pytest configuration object.
Returns:
list of unicode:
The report header entries to log.
"""
return [
'Review Board: %s' % reviewboard.get_version_string(),
'Djblets: %s' % djblets.get_version_string(),
'Django: %s' % django.get_version(),
]