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