1 # $Id: test_userauditor.py,v 1.4 2007-09-12 21:11:14 jpend Exp $
3 import os, unittest, shutil
4 from db_test_base import setupTracker
6 class UserAuditorTest(unittest.TestCase):
7 def setUp(self):
8 self.dirname = '_test_user_auditor'
9 self.instance = setupTracker(self.dirname)
10 self.db = self.instance.open('admin')
12 try:
13 import pytz
14 self.pytz = True
15 except ImportError:
16 self.pytz = False
18 self.db.user.create(username='kyle', address='kyle@example.com',
19 realname='Kyle Broflovski', roles='User')
21 def tearDown(self):
22 self.db.close()
23 try:
24 shutil.rmtree(self.dirname)
25 except OSError, error:
26 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
28 def testBadTimezones(self):
29 self.assertRaises(ValueError, self.db.user.create, username='eric', timezone='24')
31 userid = self.db.user.lookup('kyle')
33 self.assertRaises(ValueError, self.db.user.set, userid, timezone='3000')
34 self.assertRaises(ValueError, self.db.user.set, userid, timezone='24')
35 self.assertRaises(ValueError, self.db.user.set, userid, timezone='-24')
36 self.assertRaises(ValueError, self.db.user.set, userid, timezone='-3000')
38 if self.pytz:
39 try:
40 from pytz import UnknownTimeZoneError
41 except:
42 UnknownTimeZoneError = ValueError
43 self.assertRaises(UnknownTimeZoneError, self.db.user.set, userid, timezone='MiddleOf/Nowhere')
45 def testGoodTimezones(self):
46 self.db.user.create(username='test_user01', timezone='12')
48 if self.pytz:
49 self.db.user.create(username='test_user02', timezone='MST')
51 userid = self.db.user.lookup('kyle')
53 # TODO: roundup should accept non-integer offsets since those are valid
54 # this is the offset for Tehran, Iran
55 #self.db.user.set(userid, timezone='3.5')
57 self.db.user.set(userid, timezone='-23')
58 self.db.user.set(userid, timezone='23')
59 self.db.user.set(userid, timezone='0')
61 if self.pytz:
62 self.db.user.set(userid, timezone='US/Eastern')
64 def testBadEmailAddresses(self):
65 userid = self.db.user.lookup('kyle')
66 self.assertRaises(ValueError, self.db.user.set, userid, address='kyle @ example.com')
67 self.assertRaises(ValueError, self.db.user.set, userid, address='one@example.com,two@example.com')
68 self.assertRaises(ValueError, self.db.user.set, userid, address='weird@@example.com')
69 self.assertRaises(ValueError, self.db.user.set, userid, address='embedded\nnewline@example.com')
70 # verify that we check alternates as well
71 self.assertRaises(ValueError, self.db.user.set, userid, alternate_addresses='kyle @ example.com')
72 # make sure we accept local style addresses
73 self.db.user.set(userid, address='kyle')
74 # verify we are case insensitive
75 self.db.user.set(userid, address='kyle@EXAMPLE.COM')
77 def testUniqueEmailAddresses(self):
78 self.db.user.create(username='kenny', address='kenny@example.com', alternate_addresses='sp_ken@example.com')
79 self.assertRaises(ValueError, self.db.user.create, username='test_user01', address='kenny@example.com')
80 uid = self.db.user.create(username='eric', address='eric@example.com')
81 self.assertRaises(ValueError, self.db.user.set, uid, address='kenny@example.com')
83 # make sure we check alternates
84 self.assertRaises(ValueError, self.db.user.set, uid, address='kenny@example.com')
85 self.assertRaises(ValueError, self.db.user.set, uid, address='sp_ken@example.com')
86 self.assertRaises(ValueError, self.db.user.set, uid, alternate_addresses='kenny@example.com')
88 def testBadRoles(self):
89 userid = self.db.user.lookup('kyle')
90 self.assertRaises(ValueError, self.db.user.set, userid, roles='BadRole')
91 self.assertRaises(ValueError, self.db.user.set, userid, roles='User,BadRole')
93 def testGoodRoles(self):
94 userid = self.db.user.lookup('kyle')
95 # make sure we handle commas in weird places
96 self.db.user.set(userid, roles='User,')
97 self.db.user.set(userid, roles=',User')
98 # make sure we strip whitespace
99 self.db.user.set(userid, roles=' User ')
100 # check for all-whitespace (treat as no role)
101 self.db.user.set(userid, roles=' ')
103 def test_suite():
104 suite = unittest.TestSuite()
105 suite.addTest(unittest.makeSuite(UserAuditorTest))
106 return suite
108 if __name__ == '__main__':
109 runner = unittest.TextTestRunner()
110 unittest.main(testRunner=runner)
112 # vim: filetype=python sts=4 sw=4 et si