-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.py
161 lines (146 loc) · 5.11 KB
/
tests.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
from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client
class WebmasterVerificationTest(TestCase):
def setUp(self):
self.client = Client()
def test_google_file_access_and_content(self):
if 'google' in settings.WEBMASTER_VERIFICATION:
codes = settings.WEBMASTER_VERIFICATION['google']
if type(codes) == tuple:
for code in codes:
self._test_google_file_access_and_content(code)
else:
self._test_google_file_access_and_content(codes)
def _test_google_file_access_and_content(self, code):
"""
Test if the google verification file for a specific code exists and
if it's content is correct.
"""
url = self._get_google_url(code)
r = self.client.get(url)
self.assertEqual(
r.status_code,
200,
"Couldn't access %s, got %d" % (url, r.status_code)
)
self.assertRegexpMatches(
r.content,
'.*google%s\.html.*' % code,
'Verification code not found in response body',
)
def test_google_file_404s(self):
bad_codes = (
'',
'012345678',
'0123456789abcdef',
)
for code in bad_codes:
url = self._get_google_url(code)
r = self.client.get(url)
self.assertEqual(
r.status_code,
404,
"Could access %s for inexistent code, got %d" % (url, r.status_code)
)
def _get_google_url(self, code):
return '/google%s.html' % code
def test_bing_file_access_and_content(self):
if 'bing' in settings.WEBMASTER_VERIFICATION:
code = settings.WEBMASTER_VERIFICATION['bing']
url = '/BingSiteAuth.xml'
r = self.client.get(url)
self.assertEqual(
r.status_code,
200,
"Couldn't access %s, got %d" % (url, r.status_code)
)
self.assertEqual(
r['Content-Type'],
'text/xml',
"Got %s content type for xml file" % r['Content-Type']
)
self.assertRegexpMatches(
r.content,
'.*%s.*' % code,
'Verification code not found in response body',
)
def test_mj_file_access(self):
if 'majestic' in settings.WEBMASTER_VERIFICATION:
codes = settings.WEBMASTER_VERIFICATION['majestic']
if type(codes) == tuple:
for code in codes:
self._test_mj_file_access(code)
else:
self._test_mj_file_access(codes)
def _test_mj_file_access(self, code):
url = self._get_mj_url(code)
r = self.client.get(url)
self.assertEqual(
r.status_code,
200,
"Couldn't access %s, got %d" % (url, r.status_code)
)
self.assertEqual(
r['Content-Type'],
'text/plain',
"Got %s content type for text file" % r['Content-Type']
)
def test_mj_file_404s(self):
bad_codes = (
'',
'012345678',
'0123456789ABCDEF0123456789ABCDEF',
)
for code in bad_codes:
url = self._get_mj_url(code)
r = self.client.get(url)
self.assertEqual(
r.status_code,
404,
"Could access %s for inexistent code, got %d" % (url, r.status_code)
)
def _get_mj_url(self, code):
return '/MJ12_%s.txt' % code
# TODO look into refactoring this
def test_yandex_file_acces(self):
if 'yandex' in settings.WEBMASTER_VERIFICATION:
codes = settings.WEBMASTER_VERIFICATION['yandex']
if type(codes) == tuple:
for code in codes:
self._test_yandex_file_access(code)
else:
self._test_yandex_file_access(codes)
# TODO look into refactoring this
def _test_yandex_file_access(self, code):
url = self._get_yandex_url(code)
r = self.client.get(url)
self.assertEqual(
r.status_code,
200,
"Couldn't access %s, got %d" % (url, r.status_code)
)
self.assertEqual(
r['Content-Type'],
'text/plain',
"Got %s content type for text file" % r['Content-Type']
)
# TODO look into refactoring this
def test_yandex_file_404s(self):
bad_codes = (
'',
'012345678',
'0123456789ABCDEF0123456789ABCDEF',
)
for code in bad_codes:
url = self._get_yandex_url(code)
r = self.client.get(url)
self.assertEqual(
r.status_code,
404,
"Could access %s for inexistent code, got %d" % (url, r.status_code)
)
# TODO look into refactoring this
def _get_yandex_url(self, code):
return '/yandex_%s.txt' % code