source: TracAccountManager/0.10/acct_mgr/tests/db.py @ 8

Last change on this file since 8 was 2, checked in by guillaume, 17 years ago

Ajout TracAccountManager en français

File size: 3.4 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2007 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
12import os.path
13import shutil
14import tempfile
15import unittest
16
17from trac.test import EnvironmentStub, Mock
18
19from acct_mgr.db import SessionStore
20
21class _BaseTestCase(unittest.TestCase):
22    def setUp(self):
23        #self.basedir = os.path.realpath(tempfile.mkdtemp())
24        self.env = EnvironmentStub()
25        self.env.config.set('account-manager', 'password_store',
26                            'SessionStore')
27        self.store = SessionStore(self.env)
28        #self.env.path = os.path.join(self.basedir, 'trac-tempenv')
29        #os.mkdir(self.env.path)
30
31    def test_get_users(self):
32        db = self.env.get_db_cnx()
33        cursor = db.cursor()
34        cursor.executemany("INSERT INTO session_attribute "
35                       "(sid,authenticated,name,value) "
36                       "VALUES (%s,1,'password',%s)",
37                       [('a', 'a'),
38                        ('b', 'b'),
39                        ('c', 'c')])
40        self.assertEqual(['a', 'b', 'c'], list(self.store.get_users()))
41
42    def test_has_user(self):
43        db = self.env.get_db_cnx()
44        cursor = db.cursor()
45        cursor.execute("INSERT INTO session_attribute "
46                       "(sid,authenticated,name,value) "
47                       "VALUES (%s,1,'password',%s)",
48                       ('bar', 'bar'))
49
50        self.assertFalse(self.store.has_user('foo'))
51        self.assertTrue(self.store.has_user('bar'))
52
53    def test_create_user(self):
54        self.assertFalse(self.store.has_user('foo'))
55        self.store.set_password('foo', 'password')
56        self.assertTrue(self.store.has_user('foo'))
57 
58    def test_update_password(self):
59        self.store.set_password('foo', 'pass1')
60        self.assertFalse(self.store.check_password('foo', 'pass2'))
61        self.store.set_password('foo', 'pass2')
62        self.assertTrue(self.store.check_password('foo', 'pass2'))
63
64    def test_delete_user(self):
65        self.store.set_password('foo', 'password')
66        self.assertTrue(self.store.has_user('foo'))
67        self.assertTrue(self.store.delete_user('foo'))
68        self.assertFalse(self.store.has_user('foo'))
69
70    def test_delete_nonexistant_user(self):
71        self.assertFalse(self.store.has_user('foo'))
72        self.assertFalse(self.store.delete_user('foo'))
73
74
75class HtDigestTestCase(_BaseTestCase):
76    def setUp(self):
77        _BaseTestCase.setUp(self)
78        self.env.config.set('account-manager', 'hash_method',
79                            'HtDigestHashMethod')
80        self.env.config.set('account-manager', 'htdigest_realm',
81                            'TestRealm')
82
83
84class HtPasswdTestCase(_BaseTestCase):
85    def setUp(self):
86        _BaseTestCase.setUp(self)
87        self.env.config.set('account-manager', 'hash_method',
88                            'HtPasswdHashMethod')
89
90
91def suite():
92    suite = unittest.TestSuite()
93    suite.addTest(unittest.makeSuite(HtDigestTestCase, 'test'))
94    suite.addTest(unittest.makeSuite(HtPasswdTestCase, 'test'))
95    return suite
96
97if __name__ == '__main__':
98    unittest.main(defaultTest='suite')
99
100
Note: See TracBrowser for help on using the repository browser.