1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005 Matthew Good <trac@matt-good.net> |
---|
4 | # |
---|
5 | # "THE BEER-WARE LICENSE" (Revision 42): |
---|
6 | # <trac@matt-good.net> wrote this file. As long as you retain this notice you |
---|
7 | # can do whatever you want with this stuff. If we meet some day, and you think |
---|
8 | # this stuff is worth it, you can buy me a beer in return. Matthew Good |
---|
9 | # |
---|
10 | # Author: Matthew Good <trac@matt-good.net> |
---|
11 | |
---|
12 | import os.path |
---|
13 | import shutil |
---|
14 | import tempfile |
---|
15 | import unittest |
---|
16 | |
---|
17 | from trac.test import EnvironmentStub, Mock |
---|
18 | |
---|
19 | from acct_mgr.htfile import HtDigestStore, HtPasswdStore |
---|
20 | |
---|
21 | class _BaseTestCase(unittest.TestCase): |
---|
22 | def setUp(self): |
---|
23 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
24 | self.env = EnvironmentStub() |
---|
25 | self.env.path = os.path.join(self.basedir, 'trac-tempenv') |
---|
26 | os.mkdir(self.env.path) |
---|
27 | |
---|
28 | def tearDown(self): |
---|
29 | shutil.rmtree(self.basedir) |
---|
30 | |
---|
31 | def _create_file(self, *path, **kw): |
---|
32 | filename = os.path.join(self.basedir, *path) |
---|
33 | dirname = os.path.dirname(filename) |
---|
34 | if not os.path.exists(dirname): |
---|
35 | os.makedirs(dirname) |
---|
36 | fd = file(filename, 'w') |
---|
37 | content = kw.get('content') |
---|
38 | if content is not None: |
---|
39 | fd.write(content) |
---|
40 | fd.close() |
---|
41 | return filename |
---|
42 | |
---|
43 | def _do_password_test(self, filename, content): |
---|
44 | filename = self._create_file(filename, content=content) |
---|
45 | self.env.config.set('account-manager', 'password_file', filename) |
---|
46 | self.assertTrue(self.store.check_password('user', 'password')) |
---|
47 | |
---|
48 | |
---|
49 | class HtDigestTestCase(_BaseTestCase): |
---|
50 | def setUp(self): |
---|
51 | _BaseTestCase.setUp(self) |
---|
52 | self.env.config.set('account-manager', 'password_store', |
---|
53 | 'HtDigestStore') |
---|
54 | self.env.config.set('account-manager', 'htdigest_realm', |
---|
55 | 'TestRealm') |
---|
56 | self.store = HtDigestStore(self.env) |
---|
57 | |
---|
58 | def test_userline(self): |
---|
59 | self.assertEqual(self.store.userline('user', 'password'), |
---|
60 | 'user:TestRealm:752b304cc7cf011d69ee9b79e2cd0866') |
---|
61 | |
---|
62 | def test_file(self): |
---|
63 | self._do_password_test('test_file', |
---|
64 | 'user:TestRealm:752b304cc7cf011d69ee9b79e2cd0866') |
---|
65 | |
---|
66 | class HtPasswdTestCase(_BaseTestCase): |
---|
67 | def setUp(self): |
---|
68 | _BaseTestCase.setUp(self) |
---|
69 | self.env.config.set('account-manager', 'password_store', |
---|
70 | 'HtPasswdStore') |
---|
71 | self.store = HtPasswdStore(self.env) |
---|
72 | |
---|
73 | def test_md5(self): |
---|
74 | self._do_password_test('test_md5', |
---|
75 | 'user:$apr1$xW/09...$fb150dT95SoL1HwXtHS/I0\n') |
---|
76 | |
---|
77 | def test_crypt(self): |
---|
78 | self._do_password_test('test_crypt', 'user:QdQ/xnl2v877c\n') |
---|
79 | |
---|
80 | def test_sha(self): |
---|
81 | self._do_password_test('test_sha', |
---|
82 | 'user:{SHA}W6ph5Mm5Pz8GgiULbPgzG37mj9g=\n') |
---|
83 | |
---|
84 | def test_no_trailing_newline(self): |
---|
85 | self._do_password_test('test_no_trailing_newline', |
---|
86 | 'user:$apr1$xW/09...$fb150dT95SoL1HwXtHS/I0') |
---|
87 | |
---|
88 | |
---|
89 | def suite(): |
---|
90 | suite = unittest.TestSuite() |
---|
91 | suite.addTest(unittest.makeSuite(HtDigestTestCase, 'test')) |
---|
92 | suite.addTest(unittest.makeSuite(HtPasswdTestCase, 'test')) |
---|
93 | return suite |
---|
94 | |
---|
95 | if __name__ == '__main__': |
---|
96 | unittest.main(defaultTest='suite') |
---|
97 | |
---|