1 # Copyright (c) 2002 ekit.com Inc (http://www.ekit-inc.com/)
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 # SOFTWARE.
21 # $Id: test_security.py,v 1.1 2002-07-25 07:14:06 richard Exp $
23 import os, unittest, shutil
25 from roundup.password import Password
26 from test_db import setupSchema, MyTestCase, config
28 class PermissionTest(MyTestCase):
29 def setUp(self):
30 from roundup.backends import anydbm
31 # remove previous test, ignore errors
32 if os.path.exists(config.DATABASE):
33 shutil.rmtree(config.DATABASE)
34 os.makedirs(config.DATABASE + '/files')
35 self.db = anydbm.Database(config, 'test')
36 setupSchema(self.db, 1, anydbm)
38 def testInterfaceSecurity(self):
39 ' test that the CGI and mailgw have initialised security OK '
40 # TODO: some asserts
42 def testInitialiseSecurity(self):
43 ''' Create some Permissions and Roles on the security object
45 This function is directly invoked by security.Security.__init__()
46 as a part of the Security object instantiation.
47 '''
48 ei = self.db.security.addPermission(name="Edit", klass="issue",
49 description="User is allowed to edit issues")
50 self.db.security.addPermissionToRole('User', ei)
51 ai = self.db.security.addPermission(name="Assign", klass="issue",
52 description="User may be assigned to issues")
53 self.db.security.addPermissionToRole('User', ai)
55 def testDBinit(self):
56 r = str(self.db.role.lookup('Admin'))
57 self.db.user.create(username="admin", roles=[r])
58 r = str(self.db.role.lookup('User'))
59 self.db.user.create(username="anonymous", roles=[r])
61 def testAccess(self):
62 self.testDBinit()
63 self.testInitialiseSecurity()
65 # test class-level access
66 userid = self.db.user.lookup('admin')
67 self.assertEquals(self.db.security.hasClassPermission('issue',
68 'Edit', userid), 1)
69 self.assertEquals(self.db.security.hasClassPermission('user',
70 'Edit', userid), 1)
71 userid = self.db.user.lookup('anonymous')
72 self.assertEquals(self.db.security.hasClassPermission('issue',
73 'Edit', userid), 1)
74 self.assertEquals(self.db.security.hasClassPermission('user',
75 'Edit', userid), 0)
77 # test node-level access
78 issueid = self.db.issue.create(title='foo', assignedto='admin')
79 userid = self.db.user.lookup('admin')
80 self.assertEquals(self.db.security.hasNodePermission('issue',
81 issueid, assignedto=userid), 1)
82 self.assertEquals(self.db.security.hasNodePermission('issue',
83 issueid, nosy=userid), 0)
84 self.db.issue.set(issueid, nosy=[userid])
85 self.assertEquals(self.db.security.hasNodePermission('issue',
86 issueid, nosy=userid), 1)
88 def suite():
89 return unittest.makeSuite(PermissionTest)
92 #
93 # $Log: not supported by cvs2svn $
94 # Revision 1.1 2002/07/10 06:40:01 richard
95 # ehem, forgot to add
96 #
97 #
98 #
99 # vim: set filetype=python ts=4 sw=4 et si