forked from deshaw/wsgi-kerberos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_wsgi_kerberos.py
executable file
·232 lines (199 loc) · 10 KB
/
test_wsgi_kerberos.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from wsgi_kerberos import KerberosAuthMiddleware, ensure_bytestring
from webtest import TestApp
import kerberos
import mock
import unittest
def index(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
response_body = 'Hello %s' % environ.get('REMOTE_USER', 'ANONYMOUS')
return [ensure_bytestring(response_body)]
class BasicAppTestCase(unittest.TestCase):
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_authentication_not_required(self, clean, name, response, step, init):
'''
Ensure that when a users auth_required_callback returns False,
authentication is not performed.
'''
false = lambda x: False
app = TestApp(KerberosAuthMiddleware(index,
hostname='example.org',
auth_required_callback=false))
r = app.get('/', expect_errors=False)
self.assertEqual(r.status, '200 OK')
self.assertEqual(r.status_int, 200)
self.assertEqual(r.body, b'Hello ANONYMOUS')
self.assertEqual(r.headers.get('WWW-Authenticate'), None)
self.assertEqual(r.headers['content-type'], 'text/plain')
self.assertEqual(init.mock_calls, [])
self.assertEqual(step.mock_calls, [])
self.assertEqual(name.mock_calls, [])
self.assertEqual(response.mock_calls, [])
self.assertEqual(clean.mock_calls, [])
def test_unauthorized(self):
'''
Ensure that when the client does not send an authorization token, they
receive a 401 Unauthorized response which includes a www-authenticate
header field which indicates the server supports Negotiate
authentication.
'''
app = TestApp(KerberosAuthMiddleware(index, hostname='example.org'))
r = app.get('/', expect_errors=True)
self.assertEqual(r.status, '401 Unauthorized')
self.assertEqual(r.status_int, 401)
self.assertEqual(r.body, b'Unauthorized')
self.assertEqual(r.headers['www-authenticate'], 'Negotiate')
self.assertEqual(r.headers['content-type'], 'text/plain')
def test_unauthorized_custom(self):
'''
Ensure that when the client does not send an authorization token, they
receive a 401 Unauthorized response which includes a www-authenticate
header field which indicates the server supports Negotiate
authentication. If configured, they should also receive customized
content.
'''
app = TestApp(KerberosAuthMiddleware(index,
hostname='example.org',
unauthorized='CUSTOM'))
r = app.get('/', expect_errors=True)
self.assertEqual(r.status, '401 Unauthorized')
self.assertEqual(r.status_int, 401)
self.assertEqual(r.body, b'CUSTOM')
self.assertEqual(r.headers['www-authenticate'], 'Negotiate')
self.assertEqual(r.headers['content-type'], 'text/plain')
def test_unauthorized_custom_content_type(self):
'''
Ensure that when the client does not send an authorization token, they
receive a 401 Unauthorized response which includes a www-authenticate
header field which indicates the server supports Negotiate
authentication. If configured, they should also receive customized
content and content type.
'''
app = TestApp(KerberosAuthMiddleware(index,
hostname='example.org',
unauthorized=('CUSTOM',
'text/html')))
r = app.get('/', expect_errors=True)
self.assertEqual(r.status, '401 Unauthorized')
self.assertEqual(r.status_int, 401)
self.assertEqual(r.body, b'CUSTOM')
self.assertEqual(r.headers['www-authenticate'], 'Negotiate')
self.assertEqual(r.headers['content-type'], 'text/html')
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_authorized(self, clean, name, response, step, init):
'''
Ensure that when the client sends an correct authorization token,
they receive a 200 OK response and the user principal is extracted and
passed on to the routed method.
'''
state = object()
init.return_value = (kerberos.AUTH_GSS_COMPLETE, state)
step.return_value = kerberos.AUTH_GSS_COMPLETE
name.return_value = "[email protected]"
response.return_value = "STOKEN"
app = TestApp(KerberosAuthMiddleware(index, hostname='example.org'))
r = app.get('/', headers={'Authorization': 'Negotiate CTOKEN'})
self.assertEqual(r.status, '200 OK')
self.assertEqual(r.status_int, 200)
self.assertEqual(r.body, b'Hello [email protected]')
self.assertEqual(r.headers['WWW-Authenticate'], 'negotiate STOKEN')
self.assertEqual(r.headers['content-type'], 'text/plain')
self.assertEqual(init.mock_calls, [mock.call('[email protected]')])
self.assertEqual(step.mock_calls, [mock.call(state, 'CTOKEN')])
self.assertEqual(name.mock_calls, [mock.call(state)])
self.assertEqual(response.mock_calls, [mock.call(state)])
self.assertEqual(clean.mock_calls, [mock.call(state)])
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_forbidden(self, clean, name, response, step, init):
'''
Ensure that when the client sends an incorrect authorization token,
they receive a 403 Forbidden response.
'''
state = object()
init.return_value = (kerberos.AUTH_GSS_COMPLETE, state)
step.side_effect = kerberos.GSSError("FAILURE")
app = TestApp(KerberosAuthMiddleware(index, hostname='example.org'))
r = app.get('/',
headers={'Authorization': 'Negotiate CTOKEN'},
expect_errors=True)
self.assertEqual(r.status, '403 Forbidden')
self.assertEqual(r.status_int, 403)
self.assertEqual(r.body, b'Forbidden')
self.assertEqual(r.headers['content-type'], 'text/plain')
self.assertEqual(init.mock_calls, [mock.call('[email protected]')])
self.assertEqual(step.mock_calls, [mock.call(state, 'CTOKEN')])
self.assertEqual(name.mock_calls, [])
self.assertEqual(response.mock_calls, [])
self.assertEqual(clean.mock_calls, [mock.call(state)])
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_forbidden_custom(self, clean, name, response, step, init):
'''
Ensure that when the client sends an incorrect authorization token,
they receive a 403 Forbidden response. If configured, they should
receive customized content.
'''
state = object()
init.return_value = (kerberos.AUTH_GSS_COMPLETE, state)
step.side_effect = kerberos.GSSError("FAILURE")
app = TestApp(KerberosAuthMiddleware(index,
hostname='example.org',
forbidden='CUSTOM'))
r = app.get('/',
headers={'Authorization': 'Negotiate CTOKEN'},
expect_errors=True)
self.assertEqual(r.status, '403 Forbidden')
self.assertEqual(r.status_int, 403)
self.assertEqual(r.body, b'CUSTOM')
self.assertEqual(r.headers['content-type'], 'text/plain')
self.assertEqual(init.mock_calls, [mock.call('[email protected]')])
self.assertEqual(step.mock_calls, [mock.call(state, 'CTOKEN')])
self.assertEqual(name.mock_calls, [])
self.assertEqual(response.mock_calls, [])
self.assertEqual(clean.mock_calls, [mock.call(state)])
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_forbidden_custom_content_type(self, clean, name, response, step, init):
'''
Ensure that when the client sends an incorrect authorization token,
they receive a 403 Forbidden response. If configured, they should
receive customized content and content-type.
'''
state = object()
init.return_value = (kerberos.AUTH_GSS_COMPLETE, state)
step.side_effect = kerberos.GSSError("FAILURE")
app = TestApp(KerberosAuthMiddleware(index,
hostname='example.org',
forbidden=('CUSTOM',
'text/html')))
r = app.get('/',
headers={'Authorization': 'Negotiate CTOKEN'},
expect_errors=True)
self.assertEqual(r.status, '403 Forbidden')
self.assertEqual(r.status_int, 403)
self.assertEqual(r.body, b'CUSTOM')
self.assertEqual(r.headers['content-type'], 'text/html')
self.assertEqual(init.mock_calls, [mock.call('[email protected]')])
self.assertEqual(step.mock_calls, [mock.call(state, 'CTOKEN')])
self.assertEqual(name.mock_calls, [])
self.assertEqual(response.mock_calls, [])
self.assertEqual(clean.mock_calls, [mock.call(state)])
if __name__ == '__main__':
unittest.main()