Back to index

moin  1.9.0~rc2
__init__.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - MoinMoin.datastruct.backends base test classes.
00004 
00005     @copyright: 2003-2004 by Juergen Hermann <jh@web.de>,
00006                 2007 by MoinMoin:ThomasWaldmann
00007                 2008 by MoinMoin:MelitaMihaljevic
00008                 2009 by MoinMoin:DmitrijsMilajevs
00009     @license: GNU GPL, see COPYING for details.
00010 
00011 """
00012 
00013 from py.test import raises
00014 
00015 from MoinMoin import security
00016 from MoinMoin.datastruct import GroupDoesNotExistError
00017 
00018 
00019 class GroupsBackendTest(object):
00020 
00021     test_groups = {u'EditorGroup': [u'AdminGroup', u'John', u'JoeDoe', u'Editor1', u'John'],
00022                    u'AdminGroup': [u'Admin1', u'Admin2', u'John'],
00023                    u'OtherGroup': [u'SomethingOther'],
00024                    u'RecursiveGroup': [u'Something', u'OtherRecursiveGroup'],
00025                    u'OtherRecursiveGroup': [u'RecursiveGroup', u'Anything', u'NotExistingGroup'],
00026                    u'ThirdRecursiveGroup': [u'ThirdRecursiveGroup', u'Banana'],
00027                    u'EmptyGroup': [],
00028                    u'CheckNotExistingGroup': [u'NotExistingGroup']}
00029 
00030 
00031     expanded_groups = {u'EditorGroup': [u'Admin1', u'Admin2', u'John',
00032                                         u'JoeDoe', u'Editor1'],
00033                        u'AdminGroup': [u'Admin1', u'Admin2', u'John'],
00034                        u'OtherGroup': [u'SomethingOther'],
00035                        u'RecursiveGroup': [u'Anything', u'Something', u'NotExistingGroup'],
00036                        u'OtherRecursiveGroup': [u'Anything', u'Something', u'NotExistingGroup'],
00037                        u'ThirdRecursiveGroup': [u'Banana'],
00038                        u'EmptyGroup': [],
00039                        u'CheckNotExistingGroup': [u'NotExistingGroup']}
00040 
00041     def test_contains(self):
00042         """
00043         Test group_wiki Backend and Group containment methods.
00044         """
00045         groups = self.request.groups
00046 
00047         for group, members in self.expanded_groups.iteritems():
00048             assert group in groups
00049             for member in members:
00050                 assert member in groups[group]
00051 
00052         raises(GroupDoesNotExistError, lambda: groups[u'NotExistingGroup'])
00053 
00054     def test_contains_group(self):
00055         groups = self.request.groups
00056 
00057         assert u'AdminGroup' in groups[u'EditorGroup']
00058         assert u'EditorGroup' not in groups[u'AdminGroup']
00059 
00060     def test_iter(self):
00061         groups = self.request.groups
00062 
00063         for group, members in self.expanded_groups.iteritems():
00064             returned_members = list(groups[group])
00065             assert len(returned_members) == len(members)
00066             for member in members:
00067                 assert member in returned_members
00068 
00069     def test_get(self):
00070         groups = self.request.groups
00071 
00072         assert groups.get(u'AdminGroup')
00073         assert u'NotExistingGroup' not in groups
00074         assert groups.get(u'NotExistingGroup') is None
00075         assert groups.get(u'NotExistingGroup', []) == []
00076 
00077     def test_groups_with_member(self):
00078         groups = self.request.groups
00079 
00080         john_groups = list(groups.groups_with_member(u'John'))
00081         assert 2 == len(john_groups)
00082         assert u'EditorGroup' in john_groups
00083         assert u'AdminGroup' in john_groups
00084         assert u'ThirdGroup' not in john_groups
00085 
00086     def test_backend_acl_allow(self):
00087         """
00088         Test if the wiki group backend works with acl code.
00089         Check user which has rights.
00090         """
00091         request = self.request
00092 
00093         acl_rights = ["AdminGroup:admin,read,write"]
00094         acl = security.AccessControlList(request.cfg, acl_rights)
00095 
00096         for user in self.expanded_groups['AdminGroup']:
00097             for permission in ["read", "write", "admin"]:
00098                 assert acl.may(request, u"Admin1", permission), '%s must have %s permission because he is member of the AdminGroup' % (user, permission)
00099 
00100     def test_backend_acl_deny(self):
00101         """
00102         Test if the wiki group backend works with acl code.
00103         Check user which does not have rights.
00104         """
00105         request = self.request
00106 
00107         acl_rights = ["AdminGroup:read,write"]
00108         acl = security.AccessControlList(request.cfg, acl_rights)
00109 
00110         assert u"SomeUser" not in request.groups['AdminGroup']
00111         for permission in ["read", "write"]:
00112             assert not acl.may(request, u"SomeUser", permission), 'SomeUser must not have %s permission because he is not listed in the AdminGroup' % permission
00113 
00114         assert u'Admin1' in request.groups['AdminGroup']
00115         assert not acl.may(request, u"Admin1", "admin")
00116 
00117     def test_backend_acl_with_all(self):
00118         request = self.request
00119 
00120         acl_rights = ["EditorGroup:read,write,delete,admin All:read"]
00121         acl = security.AccessControlList(request.cfg, acl_rights)
00122 
00123         for member in self.expanded_groups[u'EditorGroup']:
00124             for permission in ["read", "write", "delete", "admin"]:
00125                 assert acl.may(request, member, permission)
00126 
00127         assert acl.may(request, u"Someone", "read")
00128         for permission in ["write", "delete", "admin"]:
00129             assert not acl.may(request, u"Someone", permission)
00130 
00131     def test_backend_acl_not_existing_group(self):
00132         request = self.request
00133         assert u'NotExistingGroup' not in request.groups
00134 
00135         acl_rights = ["NotExistingGroup:read,write,delete,admin All:read"]
00136         acl = security.AccessControlList(request.cfg, acl_rights)
00137 
00138         assert not acl.may(request, u"Someone", "write")
00139 
00140 
00141 class DictsBackendTest(object):
00142 
00143     dicts = {u'SomeTestDict': {u'First': u'first item',
00144                                u'text with spaces': u'second item',
00145                                u'Empty string': u'',
00146                                u'Last': u'last item'},
00147              u'SomeOtherTestDict': {u'One': '1',
00148                                     u'Two': '2'}}
00149 
00150     def test_getitem(self):
00151         expected_dicts = self.dicts
00152         dicts = self.request.dicts
00153 
00154         for dict_name, expected_dict in expected_dicts.items():
00155             test_dict = dicts[dict_name]
00156             assert len(test_dict) == len(expected_dict)
00157             for key, value in expected_dict.items():
00158                 assert test_dict[key] == value
00159 
00160     def test_contains(self):
00161         dicts = self.request.dicts
00162 
00163         for key in self.dicts:
00164             assert key in dicts
00165 
00166         assert u'SomeNotExistingDict' not in dicts
00167 
00168     def test_update(self):
00169         dicts = self.request.dicts
00170 
00171         d = {}
00172         d.update(dicts['SomeTestDict'])
00173 
00174         assert u'First' in d
00175 
00176     def test_get(self):
00177         dicts = self.request.dicts
00178 
00179         for dict_name in self.dicts:
00180             assert dicts.get(dict_name)
00181 
00182         assert u'SomeNotExistingDict' not in dicts
00183         assert dicts.get(u'SomeNotExistingDict') is None
00184         assert dicts.get(u'SomeNotExistingDict', {}) == {}
00185 
00186 
00187         for dict_name, expected_dict in self.dicts.items():
00188             test_dict = dicts[dict_name]
00189             for key, value in expected_dict.items():
00190                 assert u'SomeNotExistingKey' not in test_dict
00191                 assert test_dict.get(u'SomeNotExistingKey') is None
00192                 assert test_dict.get(u'SomeNotExistingKey', {}) == {}
00193