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 |
|
|
13 |
def _makeEnviron(self, kw=None): |
02a504
|
14 |
from wsgiref.util import setup_testing_defaults |
496fcf
|
15 |
environ = {} |
02a504
|
16 |
setup_testing_defaults(environ) |
496fcf
|
17 |
if kw is not None: |
TS |
18 |
environ.update(kw) |
|
19 |
return environ |
|
20 |
|
|
21 |
def test_implements(self): |
|
22 |
from zope.interface.verify import verifyClass |
|
23 |
from repoze.who.interfaces import IChallenger |
|
24 |
from repoze.who.interfaces import IIdentifier |
|
25 |
klass = self._getTargetClass() |
|
26 |
verifyClass(IChallenger, klass) |
|
27 |
verifyClass(IIdentifier, klass) |
|
28 |
|
|
29 |
def test_challenge(self): |
|
30 |
plugin = self._makeOne('realm') |
|
31 |
environ = self._makeEnviron() |
|
32 |
result = plugin.challenge(environ, '401 Unauthorized', [], []) |
|
33 |
self.assertNotEqual(result, None) |
|
34 |
app_iter = result(environ, lambda *arg: None) |
|
35 |
items = [] |
|
36 |
for item in app_iter: |
|
37 |
items.append(item) |
a90306
|
38 |
response = b''.join(items).decode('utf-8') |
496fcf
|
39 |
self.failUnless(response.startswith('401 Unauthorized')) |
79b475
|
40 |
|
496fcf
|
41 |
def test_identify_noauthinfo(self): |
TS |
42 |
plugin = self._makeOne('realm') |
|
43 |
environ = self._makeEnviron() |
|
44 |
creds = plugin.identify(environ) |
|
45 |
self.assertEqual(creds, None) |
|
46 |
|
|
47 |
def test_identify_nonbasic(self): |
|
48 |
plugin = self._makeOne('realm') |
|
49 |
environ = self._makeEnviron({'HTTP_AUTHORIZATION':'Digest abc'}) |
|
50 |
creds = plugin.identify(environ) |
|
51 |
self.assertEqual(creds, None) |
|
52 |
|
|
53 |
def test_identify_basic_badencoding(self): |
|
54 |
plugin = self._makeOne('realm') |
|
55 |
environ = self._makeEnviron({'HTTP_AUTHORIZATION':'Basic abc'}) |
|
56 |
creds = plugin.identify(environ) |
|
57 |
self.assertEqual(creds, None) |
|
58 |
|
|
59 |
def test_identify_basic_badrepr(self): |
a90306
|
60 |
from repoze.who._compat import encodebytes |
496fcf
|
61 |
plugin = self._makeOne('realm') |
a90306
|
62 |
value = encodebytes(b'foo').decode('ascii') |
496fcf
|
63 |
environ = self._makeEnviron({'HTTP_AUTHORIZATION':'Basic %s' % value}) |
TS |
64 |
creds = plugin.identify(environ) |
|
65 |
self.assertEqual(creds, None) |
|
66 |
|
|
67 |
def test_identify_basic_ok(self): |
a90306
|
68 |
from repoze.who._compat import encodebytes |
496fcf
|
69 |
plugin = self._makeOne('realm') |
a90306
|
70 |
value = encodebytes(b'foo:bar').decode('ascii') |
496fcf
|
71 |
environ = self._makeEnviron({'HTTP_AUTHORIZATION':'Basic %s' % value}) |
TS |
72 |
creds = plugin.identify(environ) |
|
73 |
self.assertEqual(creds, {'login':'foo', 'password':'bar'}) |
|
74 |
|
|
75 |
def test_remember(self): |
|
76 |
plugin = self._makeOne('realm') |
|
77 |
creds = {} |
|
78 |
environ = self._makeEnviron() |
|
79 |
result = plugin.remember(environ, creds) |
|
80 |
self.assertEqual(result, None) |
|
81 |
|
|
82 |
def test_forget(self): |
|
83 |
plugin = self._makeOne('realm') |
|
84 |
creds = {'login':'foo', 'password':'password'} |
|
85 |
environ = self._makeEnviron() |
|
86 |
result = plugin.forget(environ, creds) |
|
87 |
self.assertEqual(result, [('WWW-Authenticate', 'Basic realm="realm"')] ) |
|
88 |
|
|
89 |
def test_challenge_forgetheaders_includes(self): |
|
90 |
plugin = self._makeOne('realm') |
|
91 |
creds = {'login':'foo', 'password':'password'} |
|
92 |
environ = self._makeEnviron() |
|
93 |
forget = plugin._get_wwwauth() |
|
94 |
result = plugin.challenge(environ, '401 Unauthorized', [], forget) |
02a504
|
95 |
self.assertTrue(forget[0] in result.headers.items()) |
79b475
|
96 |
|
496fcf
|
97 |
def test_challenge_forgetheaders_omits(self): |
TS |
98 |
plugin = self._makeOne('realm') |
|
99 |
creds = {'login':'foo', 'password':'password'} |
|
100 |
environ = self._makeEnviron() |
|
101 |
forget = plugin._get_wwwauth() |
|
102 |
result = plugin.challenge(environ, '401 Unauthorized', [], []) |
02a504
|
103 |
self.assertTrue(forget[0] in result.headers.items()) |
496fcf
|
104 |
|
TS |
105 |
def test_factory(self): |
|
106 |
from repoze.who.plugins.basicauth import make_plugin |
|
107 |
plugin = make_plugin('realm') |
|
108 |
self.assertEqual(plugin.realm, 'realm') |
|
109 |
|