Tres Seaver
2011-03-15 13b95f161d36c6a19e3d90f31a5cd0ac7e46155b
commit | author | age
496fcf 1 import unittest
TS 2
3 class TestHTPasswdPlugin(unittest.TestCase):
4
5     def _getTargetClass(self):
6         from repoze.who.plugins.htpasswd import HTPasswdPlugin
7         return HTPasswdPlugin
8
9     def _makeOne(self, *arg, **kw):
10         plugin = self._getTargetClass()(*arg, **kw)
11         return plugin
12
13     def _makeEnviron(self, kw=None):
14         environ = {}
15         environ['wsgi.version'] = (1,0)
16         if kw is not None:
17             environ.update(kw)
18         return environ
19
20     def test_implements(self):
21         from zope.interface.verify import verifyClass
22         from repoze.who.interfaces import IAuthenticator
23         klass = self._getTargetClass()
24         verifyClass(IAuthenticator, klass)
25
26     def test_authenticate_nocreds(self):
27         from StringIO import StringIO
28         io = StringIO()
29         plugin = self._makeOne(io, None)
30         environ = self._makeEnviron()
31         creds = {}
32         result = plugin.authenticate(environ, creds)
33         self.assertEqual(result, None)
34         
35     def test_authenticate_nolines(self):
36         from StringIO import StringIO
37         io = StringIO()
13b95f 38         def check(password, hashed):
TS 39             return True
40         plugin = self._makeOne(io, check)
496fcf 41         environ = self._makeEnviron()
TS 42         creds = {'login':'chrism', 'password':'pass'}
43         result = plugin.authenticate(environ, creds)
44         self.assertEqual(result, None)
45         
46     def test_authenticate_nousermatch(self):
47         from StringIO import StringIO
48         io = StringIO('nobody:foo')
13b95f 49         def check(password, hashed):
TS 50             return True
51         plugin = self._makeOne(io, check)
496fcf 52         environ = self._makeEnviron()
TS 53         creds = {'login':'chrism', 'password':'pass'}
54         result = plugin.authenticate(environ, creds)
55         self.assertEqual(result, None)
56
57     def test_authenticate_match(self):
58         from StringIO import StringIO
59         io = StringIO('chrism:pass')
60         def check(password, hashed):
61             return True
62         plugin = self._makeOne(io, check)
63         environ = self._makeEnviron()
64         creds = {'login':'chrism', 'password':'pass'}
65         result = plugin.authenticate(environ, creds)
66         self.assertEqual(result, 'chrism')
67
68     def test_authenticate_badline(self):
69         from StringIO import StringIO
70         io = StringIO('badline\nchrism:pass')
71         def check(password, hashed):
72             return True
73         plugin = self._makeOne(io, check)
74         environ = self._makeEnviron()
75         creds = {'login':'chrism', 'password':'pass'}
76         result = plugin.authenticate(environ, creds)
77         self.assertEqual(result, 'chrism')
78
79     def test_authenticate_filename(self):
80         import os
81         here = os.path.abspath(os.path.dirname(__file__))
82         htpasswd = os.path.join(here, 'fixtures', 'test.htpasswd')
83         def check(password, hashed):
84             return True
85         plugin = self._makeOne(htpasswd, check)
86         environ = self._makeEnviron()
87         creds = {'login':'chrism', 'password':'pass'}
88         result = plugin.authenticate(environ, creds)
89         self.assertEqual(result, 'chrism')
90
91     def test_authenticate_bad_filename_logs_to_repoze_who_logger(self):
92         import os
93         here = os.path.abspath(os.path.dirname(__file__))
94         htpasswd = os.path.join(here, 'fixtures', 'test.htpasswd.nonesuch')
95         def check(password, hashed):
96             return True
97         plugin = self._makeOne(htpasswd, check)
98         environ = self._makeEnviron()
99         class DummyLogger:
100             warnings = []
101             def warn(self, msg):
102                 self.warnings.append(msg)
103         logger = environ['repoze.who.logger'] = DummyLogger()
104         creds = {'login':'chrism', 'password':'pass'}
105         result = plugin.authenticate(environ, creds)
106         self.assertEqual(result, None)
107         self.assertEqual(len(logger.warnings), 1)
108         self.failUnless('could not open htpasswd' in logger.warnings[0])
109
110     def test_crypt_check(self):
111         import sys
112         # win32 does not have a crypt library, don't
113         # fail here
114         if "win32" == sys.platform:
115             return
116
117         from crypt import crypt
118         salt = '123'
119         hashed = crypt('password', salt)
120         from repoze.who.plugins.htpasswd import crypt_check
121         self.assertEqual(crypt_check('password', hashed), True)
122         self.assertEqual(crypt_check('notpassword', hashed), False)
123
2091d4 124     def test_plain_check(self):
TS 125         from repoze.who.plugins.htpasswd import plain_check
126         self.failUnless(plain_check('password', 'password'))
127         self.failIf(plain_check('notpassword', 'password'))
128
129     def test_factory_no_filename_raises(self):
130         from repoze.who.plugins.htpasswd import make_plugin
131         self.assertRaises(ValueError, make_plugin)
132
133     def test_factory_no_check_fn_raises(self):
134         from repoze.who.plugins.htpasswd import make_plugin
135         self.assertRaises(ValueError, make_plugin, 'foo')
136
496fcf 137     def test_factory(self):
TS 138         from repoze.who.plugins.htpasswd import make_plugin
139         from repoze.who.plugins.htpasswd import crypt_check
140         plugin = make_plugin('foo',
141                              'repoze.who.plugins.htpasswd:crypt_check')
142         self.assertEqual(plugin.filename, 'foo')
143         self.assertEqual(plugin.check, crypt_check)