-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_debug.py
157 lines (119 loc) · 5.78 KB
/
test_debug.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
# coding=utf-8
import base64
import unittest
from tornado.escape import to_unicode
from frontik import http_codes
from .instances import create_basic_auth_header, frontik_no_debug_app
class DebugTestCase(unittest.TestCase):
DEBUG_BASIC_AUTH = create_basic_auth_header('user:god')
def test_complex_debug_page(self):
response = frontik_no_debug_app.get_page(
'debug?debug', headers={'Authorization': self.DEBUG_BASIC_AUTH}
)
response_content = to_unicode(response.content)
self.assertEqual(response.status_code, 200)
# Basic debug messages
basic_messages = (
'debug: starting debug page',
'warning: testing simple inherited debug',
'error: testing failing urls',
'info: testing responses',
'debug mode is ON',
'<span class="entry__head__expandtext">XSLT profiling results</span>',
)
for msg in basic_messages:
self.assertIn(msg, response_content)
# Extra output and different types of content
extra_output = (
u'<child2>тест</child2>',
u'юникод\ndebug',
u'"тест": "value"',
u'SomeProtobufObject()',
u'<response>some xml</response>',
u'document.body.write("Привет")',
u'привет charset',
)
for msg in extra_output:
self.assertIn(msg, response_content)
# Iframes
self.assertEqual(response_content.count("doiframe('"), 3)
# Check that all http requests are present
self.assertEqual(response_content.count('<div class="timebar">'), 15)
# Inherited debug
assert_occurs_twice = (
'ValueError: Testing an exception',
'<span class="entry__head__expandtext">Exception traceback</span>',
'<span class="entry__head__expandtext">testing xml output</span>',
'<span class="entry__head__expandtext">testing utf-8 text output</span>',
'<span class="entry__head__expandtext">testing unicode text output</span>',
)
for msg in assert_occurs_twice:
self.assertEqual(response_content.count(msg), 2)
# Check that everything went right
assert_not_found = (
'cannot parse request body',
'cannot parse response body',
'cannot append time info',
'cannot log response info',
'cannot decode parameter name or value',
'cannot add traceback lines',
'error creating log entry with attrs',
'XSLT debug file error',
)
for msg in assert_not_found:
self.assertNotIn(msg, response_content)
def assertDebugResponseCode(self, page, expected_code, headers=None):
response = frontik_no_debug_app.get_page(page, headers=headers)
self.assertEqual(response.status_code, expected_code)
return response
def test_debug_by_basic_auth(self):
for param in ('debug', 'noxsl', 'notpl'):
response = self.assertDebugResponseCode(page='simple?{}'.format(param),
expected_code=http_codes.UNAUTHORIZED)
self.assertIn('Www-Authenticate', response.headers)
self.assertRegexpMatches(response.headers['Www-Authenticate'], 'Basic realm="[^"]+"')
self.assertDebugResponseCode(page='simple?{}'.format(param),
headers={'Authorization': self.DEBUG_BASIC_AUTH},
expected_code=http_codes.OK)
def test_debug_by_basic_auth_with_invalid_header(self):
invalid_headers = (
'Token user:god',
'Bearer abcdfe0123456789',
'Basic',
'Basic ',
'Basic ScrewYou',
create_basic_auth_header(':'),
create_basic_auth_header(''),
create_basic_auth_header('not:pass'),
'BASIC {}'.format(to_unicode(base64.b64encode(b'user:god')))
)
for h in invalid_headers:
self.assertDebugResponseCode('simple?debug', http_codes.UNAUTHORIZED, headers={'Authorization': h})
def test_debug_by_header(self):
for param in ('debug', 'noxsl', 'notpl'):
response = self.assertDebugResponseCode('simple?{}'.format(param), http_codes.UNAUTHORIZED)
self.assertIn('Www-Authenticate', response.headers)
self.assertEqual('Basic realm="Secure Area"', response.headers['Www-Authenticate'])
self.assertDebugResponseCode(
'simple?{}'.format(param), http_codes.OK, headers={'Frontik-Debug-Auth': 'user:god'}
)
self.assertDebugResponseCode(
'simple?{}'.format(param), http_codes.OK,
headers={'Frontik-Debug-Auth': 'user:god', 'Authorization': 'Basic bad'}
)
def test_debug_by_header_with_wrong_header(self):
for value in ('', 'not:pass', 'user: god', self.DEBUG_BASIC_AUTH):
response = self.assertDebugResponseCode(
'simple?debug', http_codes.UNAUTHORIZED, headers={'Frontik-Debug-Auth': value}
)
self.assertIn('Www-Authenticate', response.headers)
self.assertEqual('Frontik-Debug-Auth-Header realm="Secure Area"', response.headers['Www-Authenticate'])
def test_debug_by_cookie(self):
for param in ('debug', 'noxsl', 'notpl'):
self.assertDebugResponseCode(
'simple', http_codes.UNAUTHORIZED, headers={'Cookie': '{}=true'.format(param)}
)
self.assertDebugResponseCode(
'simple', http_codes.OK,
headers={'Cookie': '{}=true;'.format(param), 'Authorization': self.DEBUG_BASIC_AUTH}
)