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