Back to index

moin  1.9.0~rc2
test_composite_groups.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 
00003 """
00004 MoinMoin.datastruct.backends.composite_groups test
00005 
00006 @copyright: 2009 MoinMoin:DmitrijsMilajevs
00007 @license: GPL, see COPYING for details
00008 """
00009 
00010 from py.test import raises
00011 
00012 from MoinMoin.datastruct.backends._tests import GroupsBackendTest
00013 from MoinMoin.datastruct import ConfigGroups, CompositeGroups, GroupDoesNotExistError
00014 from MoinMoin._tests import wikiconfig
00015 from MoinMoin import security
00016 
00017 
00018 class TestCompositeGroupsBackend(GroupsBackendTest):
00019 
00020     class Config(wikiconfig.Config):
00021 
00022         def groups(self, request):
00023             groups = GroupsBackendTest.test_groups
00024             return CompositeGroups(request, ConfigGroups(request, groups))
00025 
00026 
00027 class TestCompositeGroup(object):
00028 
00029     class Config(wikiconfig.Config):
00030 
00031         admin_group = frozenset([u'Admin', u'JohnDoe'])
00032         editor_group = frozenset([u'MainEditor', u'JohnDoe'])
00033         fruit_group = frozenset([u'Apple', u'Banana', u'Cherry'])
00034 
00035         first_backend_groups = {u'AdminGroup': admin_group,
00036                                 u'EditorGroup': editor_group,
00037                                 u'FruitGroup': fruit_group}
00038 
00039         user_group = frozenset([u'JohnDoe', u'Bob', u'Joe'])
00040         city_group = frozenset([u'Bolzano', u'Riga', u'London'])
00041 
00042         # Suppose, someone hacked second backend and added himself to AdminGroup
00043         second_admin_group = frozenset([u'TheHacker'])
00044 
00045         second_backend_groups = {u'UserGroup': user_group,
00046                                  u'CityGroup': city_group,
00047                                  # Here group name clash occurs.
00048                                  # AdminGroup is defined in both
00049                                  # first_backend and second_backend.
00050                                  u'AdminGroup': second_admin_group}
00051 
00052         def groups(self, request):
00053             return CompositeGroups(request,
00054                                    ConfigGroups(request, self.first_backend_groups),
00055                                    ConfigGroups(request, self.second_backend_groups))
00056 
00057     def setup_method(self, method):
00058         self.groups = self.request.groups
00059 
00060     def test_getitem(self):
00061         raises(GroupDoesNotExistError, lambda: self.groups[u'NotExistingGroup'])
00062 
00063     def test_clashed_getitem(self):
00064         """
00065         Check the case when groups of the same name are defined in multiple
00066         backends. __getitem__ should return the first match (backends are
00067         considered in the order they are given in the backends list).
00068         """
00069         admin_group = self.groups[u'AdminGroup']
00070 
00071         # TheHacker added himself to the second backend, but that must not be
00072         # taken into consideration, because AdminGroup is defined in first
00073         # backend and we only use the first match.
00074         assert u'TheHacker' not in admin_group
00075 
00076     def test_iter(self):
00077         all_group_names = list(self.groups)
00078 
00079         assert 5 == len(all_group_names)
00080         # There are no duplicates
00081         assert len(set(all_group_names)) == len(all_group_names)
00082 
00083     def test_contains(self):
00084         assert u'UserGroup' in self.groups
00085         assert u'not existing group' not in self.groups
00086 
00087 
00088 coverage_modules = ['MoinMoin.datastruct.backends.composite_groups']