commit | author | age
|
496fcf
|
1 |
import unittest |
TS |
2 |
|
|
3 |
class TestBasicAuthPlugin(unittest.TestCase): |
|
4 |
|
|
5 |
def _getTargetClass(self): |
|
6 |
from repoze.who.plugins.basicauth import BasicAuthPlugin |
|
7 |
return BasicAuthPlugin |
|
8 |
|
|
9 |
def _makeOne(self, *arg, **kw): |
|
10 |
plugin = self._getTargetClass()(*arg, **kw) |
|
11 |
return plugin |
|
12 |
|
9c1e6f
|
13 |
def failUnless(self, predicate, message=''): |
TS |
14 |
self.assertTrue(predicate, message) # Nannies go home! |
|
15 |
|
496fcf
|
16 |
def _makeEnviron(self, kw=None): |
02a504
|
17 |
from wsgiref.util import setup_testing_defaults |
496fcf
|
18 |
environ = {} |
02a504
|
19 |
setup_testing_defaults(environ) |
496fcf
|
20 |
if kw is not None: |
TS |
21 |
environ.update(kw) |
|
22 |
return environ |
|
23 |
|
|
24 |
def test_implements(self): |
|
25 |
from zope.interface.verify import verifyClass |
|
26 |
from repoze.who.interfaces import IChallenger |
|
27 |
from repoze.who.interfaces import IIdentifier |
|
28 |
klass = self._getTargetClass() |
|
29 |
verifyClass(IChallenger, klass) |
|
30 |
verifyClass(IIdentifier, klass) |
|
31 |
|
|
32 |
def test_challenge(self): |
|
33 |
plugin = self._makeOne('realm') |
|
34 |
environ = self._makeEnviron() |
|
35 |
result = plugin.challenge(environ, '401 Unauthorized', [], []) |
|
36 |
self.assertNotEqual(result, None) |
|
37 |
app_iter = result(environ, lambda *arg: None) |
|
38 |
items = [] |
|
39 |
for item in app_iter: |
|
40 |
items.append(item) |
a90306
|
41 |
response = b''.join(items).decode('utf-8') |
496fcf
|
42 |
self.failUnless(response.startswith('401 Unauthorized')) |
79b475
|
43 |
|
496fcf
|
44 |
def test_identify_noauthinfo(self): |
TS |
45 |
plugin = self._makeOne('realm') |
|
46 |
environ = self._makeEnviron() |
|
47 |
creds = plugin.identify(environ) |
|
48 |
self.assertEqual(creds, None) |
|
49 |
|
|
50 |
def test_identify_nonbasic(self): |
|
51 |
plugin = self._makeOne('realm') |
|
52 |
environ = self._makeEnviron({'HTTP_AUTHORIZATION':'Digest abc'}) |
|
53 |
creds = plugin.identify(environ) |
|
54 |
self.assertEqual(creds, None) |
|
55 |
|
|
56 |
def test_identify_basic_badencoding(self): |
|
57 |
plugin = self._makeOne('realm') |
|
58 |
environ = self._makeEnviron({'HTTP_AUTHORIZATION':'Basic abc'}) |
|
59 |
creds = plugin.identify(environ) |
|
60 |
self.assertEqual(creds, None) |
|
61 |
|
|
62 |
def test_identify_basic_badrepr(self): |
a90306
|
63 |
from repoze.who._compat import encodebytes |
496fcf
|
64 |
plugin = self._makeOne('realm') |
a90306
|
65 |
value = encodebytes(b'foo').decode('ascii') |
496fcf
|
66 |
environ = self._makeEnviron({'HTTP_AUTHORIZATION':'Basic %s' % value}) |
TS |
67 |
creds = plugin.identify(environ) |
|
68 |
self.assertEqual(creds, None) |
|
69 |
|
|
70 |
def test_identify_basic_ok(self): |
a90306
|
71 |
from repoze.who._compat import encodebytes |
496fcf
|
72 |
plugin = self._makeOne('realm') |
a90306
|
73 |
value = encodebytes(b'foo:bar').decode('ascii') |
496fcf
|
74 |
environ = self._makeEnviron({'HTTP_AUTHORIZATION':'Basic %s' % value}) |
TS |
75 |
creds = plugin.identify(environ) |
|
76 |
self.assertEqual(creds, {'login':'foo', 'password':'bar'}) |
|
77 |
|
330d95
|
78 |
def test_identify_basic_ok_utf8_values(self): |
TS |
79 |
from repoze.who._compat import encodebytes |
|
80 |
LOGIN = b'b\xc3\xa2tard' |
|
81 |
PASSWD = b'l\xc3\xa0 demain' |
|
82 |
plugin = self._makeOne('realm') |
|
83 |
value = encodebytes(b':'.join((LOGIN, PASSWD))).decode('ascii') |
|
84 |
environ = self._makeEnviron({'HTTP_AUTHORIZATION':'Basic %s' % value}) |
|
85 |
creds = plugin.identify(environ) |
|
86 |
self.assertEqual(creds, {'login': LOGIN.decode('utf-8'), |
|
87 |
'password': PASSWD.decode('utf-8')}) |
|
88 |
|
|
89 |
def test_identify_basic_ok_latin1_values(self): |
|
90 |
from repoze.who._compat import encodebytes |
|
91 |
LOGIN = b'b\xe2tard' |
|
92 |
PASSWD = b'l\xe0 demain' |
|
93 |
plugin = self._makeOne('realm') |
|
94 |
value = encodebytes(b':'.join((LOGIN, PASSWD))).decode('ascii') |
|
95 |
environ = self._makeEnviron({'HTTP_AUTHORIZATION':'Basic %s' % value}) |
|
96 |
creds = plugin.identify(environ) |
|
97 |
self.assertEqual(creds, {'login': LOGIN.decode('latin1'), |
|
98 |
'password': PASSWD.decode('latin1')}) |
|
99 |
|
496fcf
|
100 |
def test_remember(self): |
TS |
101 |
plugin = self._makeOne('realm') |
|
102 |
creds = {} |
|
103 |
environ = self._makeEnviron() |
|
104 |
result = plugin.remember(environ, creds) |
|
105 |
self.assertEqual(result, None) |
|
106 |
|
|
107 |
def test_forget(self): |
|
108 |
plugin = self._makeOne('realm') |
|
109 |
creds = {'login':'foo', 'password':'password'} |
|
110 |
environ = self._makeEnviron() |
|
111 |
result = plugin.forget(environ, creds) |
|
112 |
self.assertEqual(result, [('WWW-Authenticate', 'Basic realm="realm"')] ) |
|
113 |
|
|
114 |
def test_challenge_forgetheaders_includes(self): |
|
115 |
plugin = self._makeOne('realm') |
|
116 |
creds = {'login':'foo', 'password':'password'} |
|
117 |
environ = self._makeEnviron() |
|
118 |
forget = plugin._get_wwwauth() |
|
119 |
result = plugin.challenge(environ, '401 Unauthorized', [], forget) |
02a504
|
120 |
self.assertTrue(forget[0] in result.headers.items()) |
79b475
|
121 |
|
496fcf
|
122 |
def test_challenge_forgetheaders_omits(self): |
TS |
123 |
plugin = self._makeOne('realm') |
|
124 |
creds = {'login':'foo', 'password':'password'} |
|
125 |
environ = self._makeEnviron() |
|
126 |
forget = plugin._get_wwwauth() |
|
127 |
result = plugin.challenge(environ, '401 Unauthorized', [], []) |
02a504
|
128 |
self.assertTrue(forget[0] in result.headers.items()) |
496fcf
|
129 |
|
TS |
130 |
def test_factory(self): |
|
131 |
from repoze.who.plugins.basicauth import make_plugin |
|
132 |
plugin = make_plugin('realm') |
|
133 |
self.assertEqual(plugin.realm, 'realm') |
|
134 |
|