1 import unittest
2
4
8
12
19
25
34
36 from StringIO import StringIO
37 io = StringIO()
38 plugin = self._makeOne(io, None)
39 environ = self._makeEnviron()
40 creds = {'login':'chrism', 'password':'pass'}
41 result = plugin.authenticate(environ, creds)
42 self.assertEqual(result, None)
43
45 from StringIO import StringIO
46 io = StringIO('nobody:foo')
47 plugin = self._makeOne(io, None)
48 environ = self._makeEnviron()
49 creds = {'login':'chrism', 'password':'pass'}
50 result = plugin.authenticate(environ, creds)
51 self.assertEqual(result, None)
52
54 from StringIO import StringIO
55 io = StringIO('chrism:pass')
56 def check(password, hashed):
57 return True
58 plugin = self._makeOne(io, check)
59 environ = self._makeEnviron()
60 creds = {'login':'chrism', 'password':'pass'}
61 result = plugin.authenticate(environ, creds)
62 self.assertEqual(result, 'chrism')
63
65 from StringIO import StringIO
66 io = StringIO('badline\nchrism:pass')
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
76 import os
77 here = os.path.abspath(os.path.dirname(__file__))
78 htpasswd = os.path.join(here, 'fixtures', 'test.htpasswd')
79 def check(password, hashed):
80 return True
81 plugin = self._makeOne(htpasswd, check)
82 environ = self._makeEnviron()
83 creds = {'login':'chrism', 'password':'pass'}
84 result = plugin.authenticate(environ, creds)
85 self.assertEqual(result, 'chrism')
86
88 import os
89 here = os.path.abspath(os.path.dirname(__file__))
90 htpasswd = os.path.join(here, 'fixtures', 'test.htpasswd.nonesuch')
91 def check(password, hashed):
92 return True
93 plugin = self._makeOne(htpasswd, check)
94 environ = self._makeEnviron()
95 class DummyLogger:
96 warnings = []
97 def warn(self, msg):
98 self.warnings.append(msg)
99 logger = environ['repoze.who.logger'] = DummyLogger()
100 creds = {'login':'chrism', 'password':'pass'}
101 result = plugin.authenticate(environ, creds)
102 self.assertEqual(result, None)
103 self.assertEqual(len(logger.warnings), 1)
104 self.failUnless('could not open htpasswd' in logger.warnings[0])
105
107 import sys
108
109
110 if "win32" == sys.platform:
111 return
112
113 from crypt import crypt
114 salt = '123'
115 hashed = crypt('password', salt)
116 from repoze.who.plugins.htpasswd import crypt_check
117 self.assertEqual(crypt_check('password', hashed), True)
118 self.assertEqual(crypt_check('notpassword', hashed), False)
119
124
128
132
140