Code

- using Zope3's test runner now, allowing GC checks, nicer controls and
[roundup.git] / test / test_security.py
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.6 2003-10-25 22:53:26 richard Exp $
23 import os, unittest, shutil
25 from roundup.password import Password
26 from db_test_base 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, 'admin')
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="View", klass="issue",
52                         description="User is allowed to access issues")
53         self.db.security.addPermissionToRole('User', ai)
55     def testGetPermission(self):
56         self.db.security.getPermission('Edit')
57         self.db.security.getPermission('View')
58         self.assertRaises(ValueError, self.db.security.getPermission, 'x')
59         self.assertRaises(ValueError, self.db.security.getPermission, 'Edit',
60             'fubar')
61         ei = self.db.security.addPermission(name="Edit", klass="issue",
62                         description="User is allowed to edit issues")
63         self.db.security.getPermission('Edit', 'issue')
64         ai = self.db.security.addPermission(name="View", klass="issue",
65                         description="User is allowed to access issues")
66         self.db.security.getPermission('View', 'issue')
68     def testDBinit(self):
69         self.db.user.create(username="anonymous", roles='User')
71     def testAccessControls(self):
72         self.testDBinit()
73         ei = self.db.security.addPermission(name="Edit", klass="issue",
74                         description="User is allowed to edit issues")
75         self.db.security.addPermissionToRole('User', ei)
77         # test class-level access
78         userid = self.db.user.lookup('admin')
79         self.assertEquals(self.db.security.hasPermission('Edit', userid,
80             'issue'), 1)
81         self.assertEquals(self.db.security.hasPermission('Edit', userid,
82             'user'), 1)
83         userid = self.db.user.lookup('anonymous')
84         self.assertEquals(self.db.security.hasPermission('Edit', userid,
85             'issue'), 1)
86         self.assertEquals(self.db.security.hasPermission('Edit', userid,
87             'user'), 0)
88         self.assertEquals(self.db.security.hasPermission('View', userid,
89             'issue'), 0)
91         # test node-level access
92         issueid = self.db.issue.create(title='foo', assignedto='admin')
93         userid = self.db.user.lookup('admin')
94         self.assertEquals(self.db.security.hasNodePermission('issue',
95             issueid, assignedto=userid), 1)
96         self.assertEquals(self.db.security.hasNodePermission('issue',
97             issueid, nosy=userid), 0)
98         self.db.issue.set(issueid, nosy=[userid])
99         self.assertEquals(self.db.security.hasNodePermission('issue',
100             issueid, nosy=userid), 1)
102 def test_suite():
103     suite = unittest.TestSuite()
104     suite.addTest(unittest.makeSuite(PermissionTest))
105     return suite
107 if __name__ == '__main__':
108     runner = unittest.TextTestRunner()
109     unittest.main(testRunner=runner)
111 # vim: set filetype=python ts=4 sw=4 et si