Package repoze :: Package who :: Package plugins :: Package tests :: Module test_htpasswd
[hide private]

Source Code for Module repoze.who.plugins.tests.test_htpasswd

  1  import unittest 
  2   
3 -class TestHTPasswdPlugin(unittest.TestCase):
4
5 - def _getTargetClass(self):
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
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
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
53 - def test_authenticate_match(self):
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
64 - def test_authenticate_badline(self):
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
75 - def test_authenticate_filename(self):
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
87 - def test_authenticate_bad_filename_logs_to_repoze_who_logger(self):
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
106 - def test_crypt_check(self):
107 import sys 108 # win32 does not have a crypt library, don't 109 # fail here 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
120 - def test_plain_check(self):
121 from repoze.who.plugins.htpasswd import plain_check 122 self.failUnless(plain_check('password', 'password')) 123 self.failIf(plain_check('notpassword', 'password'))
124
125 - def test_factory_no_filename_raises(self):
126 from repoze.who.plugins.htpasswd import make_plugin 127 self.assertRaises(ValueError, make_plugin)
128
129 - def test_factory_no_check_fn_raises(self):
130 from repoze.who.plugins.htpasswd import make_plugin 131 self.assertRaises(ValueError, make_plugin, 'foo')
132
133 - def test_factory(self):
134 from repoze.who.plugins.htpasswd import make_plugin 135 from repoze.who.plugins.htpasswd import crypt_check 136 plugin = make_plugin('foo', 137 'repoze.who.plugins.htpasswd:crypt_check') 138 self.assertEqual(plugin.filename, 'foo') 139 self.assertEqual(plugin.check, crypt_check)
140