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