forked from ciudadanointeligente/write-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_test_case.py
201 lines (142 loc) · 6.04 KB
/
global_test_case.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
import re
from django.test import TestCase
from django.utils.unittest import skipUnless
from django.core.management import call_command
from tastypie.test import ResourceTestCase
from django.conf import settings
from django.contrib.sites.models import Site
from popit.tests import instance_helpers
import os
import subprocess
import threading
from django.db import DEFAULT_DB_ALIAS
from django.test import RequestFactory
from django.test.client import Client
import logging
from haystack.signals import BaseSignalProcessor
_LOCALS = threading.local()
def set_test_db(db_name):
"Sets the database name to route to."
setattr(_LOCALS, 'test_db_name', db_name)
def get_test_db():
"Get the current database name or the default."
return getattr(_LOCALS, 'test_db_name', DEFAULT_DB_ALIAS)
def del_test_db():
"Clear the database name (restore default)"
try:
delattr(_LOCALS, 'test_db_name')
except AttributeError:
pass
def whitespace_ignoring_in(a, b):
"""Strip all the whitespace from a and b, and then check a in b."""
def squash(x):
return re.sub('\s', '', x)
return squash(a) in squash(b)
class TestUsingDbRouter(object):
"Simple router to allow DB selection by name."
def db_for_read(self, model, **kwargs):
return get_test_db()
def db_for_write(self, model, **kwargs):
return get_test_db()
class UsingDbMixin(object):
"A mixin to allow a TestCase to select the DB to use."
multi_db = True
using_db = None
def setUp(self, *args, **kwargs):
super(UsingDbMixin, self).setUp(*args, **kwargs)
set_test_db(self.using_db)
def tearDown(self, *args, **kwargs):
del_test_db()
super(UsingDbMixin, self).tearDown(*args, **kwargs)
from urlparse import urlparse
def get_path_and_subdomain(path, **extra):
parsed_uri = urlparse(path)
# parsed_uri.hostname is None when we say for a request to follow=True
# and because django.test.Client in the method _handle_redirects(self, response, **extra)
# does a self.get(url.path, QueryDict(url.query), follow=False, **extra)
# which cuts the full url but adds the server_name and port to the extra dictionary.
# We can still override the _handle_redirects in the class WriteItClient
if parsed_uri.hostname is None:
full_path = extra['wsgi.url_scheme'] + "://" + extra['SERVER_NAME'] + ":" + extra['SERVER_PORT'] + path
parsed_uri = urlparse(full_path)
subdomain = parsed_uri.hostname.split('.')[0]
domain = parsed_uri.netloc
if subdomain:
subdomain = subdomain
path = path.replace(subdomain + ".", '')
else:
subdomain = None
return path, subdomain, domain
class WriteItRequestFactory(RequestFactory):
'''
This is pretty much the same django RequestFactory
but in order to work with subdomains it returns the same
request as the SubdomainMiddleware would. In other words
it determines from the url what the subdomain is.
'''
def get(self, path, data={}, secure=False, **extra):
path, subdomain, domain = get_path_and_subdomain(path, **extra)
extra.update({
'SERVER_NAME': str(domain),
})
request = super(WriteItRequestFactory, self).get(path, data=data, secure=secure, **extra)
if subdomain:
request.subdomain = subdomain
return request
def post(self, path, data={}, secure=False, **extra):
path, subdomain, domain = get_path_and_subdomain(path)
extra.update({
'SERVER_NAME': str(domain),
})
request = super(WriteItRequestFactory, self).post(path, data=data, secure=secure, **extra)
if subdomain:
request.subdomain = subdomain
return request
class WriteItClient(WriteItRequestFactory, Client):
pass
class WriteItTestCaseMixin(object):
client_class = WriteItClient
fixtures = ['example_data.yaml']
def setUp(self):
self.site = Site.objects.get_current()
self.factory = WriteItRequestFactory()
def assertRedirects(self, response, expected_url, status_code=302, target_status_code=200, host=None, msg_prefix=''):
self.assertEquals(response.status_code, status_code)
self.assertEquals(response.url, expected_url)
def assertModerationMailSent(self, message, moderation_mail):
self.assertEquals(moderation_mail.to[0], message.writeitinstance.owner.email)
self.assertTrue(whitespace_ignoring_in(message.content, moderation_mail.body))
self.assertTrue(message.subject in moderation_mail.body)
self.assertTrue(message.author_name in moderation_mail.body)
self.assertTrue(message.author_email in moderation_mail.body)
for person in message.people:
self.assertTrue(person.name in moderation_mail.body)
class GlobalTestCase(WriteItTestCaseMixin, TestCase):
pass
class ResourceGlobalTestCase(WriteItTestCaseMixin, ResourceTestCase):
pass
@skipUnless(settings.LOCAL_ELASTICSEARCH, "No local elasticsearch")
class SearchIndexTestCase(GlobalTestCase):
def setUp(self):
super(SearchIndexTestCase, self).setUp()
call_command('rebuild_index', verbosity=0, interactive=False)
from djcelery.contrib.test_runner import CeleryTestSuiteRunner
from django_nose import NoseTestSuiteRunner
class WriteItTestRunner(CeleryTestSuiteRunner, NoseTestSuiteRunner):
def run_tests(self, test_labels, extra_tests=None, **kwargs):
# don't show logging messages while testing
logging.disable(logging.CRITICAL)
return super(WriteItTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
class CeleryTestingSignalProcessor(BaseSignalProcessor):
pass
from vcr import VCR
CASSETTES_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)),
"nuntium/tests/fixtures/vcr_cassettes/")
writeit_vcr = VCR(
cassette_library_dir=CASSETTES_PATH,
serializer='json',
record_mode='once'
)
def popit_load_data(fixture_name='default'):
func = writeit_vcr.use_cassette(fixture_name + '.yaml')
return func