changeset 4811:89497de6165d

Groups2009: GroupManager became compose backend and was moved to backends directory. Unnecessary tests were removed.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Sun, 14 Jun 2009 17:18:29 +0200
parents 3c0ce322472a
children a1018d829c3e
files MoinMoin/_tests/wikiconfig_groups.py MoinMoin/groups/__init__.py MoinMoin/groups/_tests/test_group_manager.py MoinMoin/groups/_tests/test_group_manager_acl.py MoinMoin/groups/backends/_tests/__init__.py MoinMoin/groups/backends/_tests/test_compose_group.py MoinMoin/groups/backends/_tests/test_config_group.py MoinMoin/groups/backends/_tests/test_wiki_group.py MoinMoin/groups/backends/compose_group.py MoinMoin/wsgiapp.py
diffstat 10 files changed, 159 insertions(+), 194 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/wikiconfig_groups.py	Sun Jun 14 17:07:02 2009 +0200
+++ b/MoinMoin/_tests/wikiconfig_groups.py	Sun Jun 14 17:18:29 2009 +0200
@@ -7,9 +7,8 @@
 """
 
 from wikiconfig import LocalConfig
-from MoinMoin.groups import GroupManager
 from MoinMoin.groups.backends import wiki_group
 
 class Config(LocalConfig):
-    group_manager_init = lambda self, request: GroupManager(wiki_group.Backend(request))
+    group_manager_init = lambda self, request: wiki_group.Backend(request)
 
--- a/MoinMoin/groups/__init__.py	Sun Jun 14 17:07:02 2009 +0200
+++ b/MoinMoin/groups/__init__.py	Sun Jun 14 17:18:29 2009 +0200
@@ -6,60 +6,3 @@
 @license: GPL, see COPYING for details
 """
 
-
-class GroupManager(object):
-    """
-    GroupManager manages several group backends.
-    """
-
-    def __init__(self, *backends):
-        """
-        Create a group manager object.
-
-        @param backends: list of group backends which are used to get
-                         access to the group definitions.
-        """
-        self._backends = backends
-
-    def __getitem__(self, group_name):
-        """
-        Get a group by its name. First match counts.
-
-        @param group_name: name of the group [unicode]
-        """
-        for backend in self._backends:
-            if group_name in backend:
-                return backend[group_name]
-        raise KeyError("There is no such group %s" % group_name)
-
-    def __iter__(self):
-        """
-        Iterate over group names in all backends (filtering duplicates).
-        """
-        yielded_groups = set()
-
-        for backend in self._backends:
-            for group_name in backend:
-                if group_name not in yielded_groups:
-                    yield group_name
-                    yielded_groups.add(group_name)
-
-    def __contains__(self, group_name):
-        """
-        Check if a group called group_name is available in any of the backends.
-
-        @param group_name: name of the group [unicode]
-        """
-        for backend in self._backends:
-            if group_name in backend:
-                return True
-        return False
-
-    def membergroups(self, member):
-        # Dirty hack just to make code works, after GroupManager
-        # becomes a backend itself, no need in this.
-        return self._backends[0].groups_with_member(member)
-
-    def __repr__(self):
-        return "<%s backends=%s>" % (self.__class__, self._backends)
-
--- a/MoinMoin/groups/_tests/test_group_manager.py	Sun Jun 14 17:07:02 2009 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,78 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-
-"""
-MoinMoin.groups.GroupManager test
-
-@copyright: 2009 MoinMoin:DmitrijsMilajevs
-            2008 MoinMoin: MelitaMihaljevic
-@license: GPL, see COPYING for details
-"""
-
-from py.test import raises
-
-from MoinMoin.groups import GroupManager
-from MoinMoin.groups.backends import config_group
-from MoinMoin._tests import wikiconfig
-
-
-class TestGroupManager(object):
-
-    class Config(wikiconfig.Config):
-
-        admin_group = frozenset([u'Admin', u'JohnDoe'])
-        editor_group = frozenset([u'MainEditor', u'JohnDoe'])
-        fruit_group = frozenset([u'Apple', u'Banana', u'Cherry'])
-
-        first_backend_groups = {u'AdminGroup': admin_group,
-                                u'EditorGroup': editor_group,
-                                u'FruitGroup': fruit_group}
-
-        user_group = frozenset([u'JohnDoe', u'Bob', u'Joe'])
-        city_group = frozenset([u'Bolzano', u'Riga', u'London'])
-
-        # Suppose, someone hacked second backend and added himself to AdminGroup
-        second_admin_group = frozenset([u'TheHacker'])
-
-        second_backend_groups = {u'UserGroup': user_group,
-                                 u'CityGroup': city_group,
-                                 # Here group name clash occurs.
-                                 # AdminGroup is defined in both
-                                 # first_backend and second_backend.
-                                 u'AdminGroup': second_admin_group}
-
-        def group_manager_init(self, request):
-            return GroupManager(config_group.Backend(request, self.first_backend_groups),
-                                config_group.Backend(request, self.second_backend_groups))
-
-    def setup_method(self, method):
-        self.groups = self.request.groups
-
-    def test_getitem(self):
-        raises(KeyError, lambda: self.groups[u'not existing group'])
-
-    def test_clashed_getitem(self):
-        """
-        Check the case when groups of the same name are defined in multiple
-        backends. __getitem__ should return the first match (backends are
-        considered in the order they are given in the backends list).
-        """
-        admin_group = self.groups[u'AdminGroup']
-
-        # TheHacker added himself to the second backend, but that must not be
-        # taken into consideration, because AdminGroup is defined in first
-        # backend and we only use the first match.
-        assert u'TheHacker' not in admin_group
-
-    def test_iter(self):
-        all_group_names = [group_name for group_name in self.groups]
-
-        assert 5 == len(all_group_names)
-        # There are no duplicates
-        assert len(set(all_group_names)) == len(all_group_names)
-
-    def test_contains(self):
-        assert u'UserGroup' in self.groups
-        assert u'not existing group' not in self.groups
-
-
-coverage_modules = ['MoinMoin.groups']
--- a/MoinMoin/groups/_tests/test_group_manager_acl.py	Sun Jun 14 17:07:02 2009 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,47 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-"""
-MoinMoin.groups.GroupManager ACL Tests
-
-@copyright: 2009 MoinMoin:DmitrijsMilajevs
-            2008 MoinMoin:MelitaMihaljevic
-@license: GPL, see COPYING for details
-"""
-
-from MoinMoin import security
-from MoinMoin.groups import GroupManager
-from MoinMoin.groups.backends import config_group
-from MoinMoin._tests import wikiconfig
-
-
-class TestGroupManagerACL:
-    """
-    Test how GroupManager works with the acl code.
-    """
-
-    class Config(wikiconfig.Config):
-
-        def group_manager_init(self, request):
-            groups = {u'FirstGroup': frozenset([u"ExampleUser", u"SecondUser", u"JoeDoe"]),
-                      u'SecondGroup': frozenset([u"ExampleUser", u"ThirdUser"])}
-
-            return GroupManager(config_group.Backend(request, groups))
-
-    def testConfigBackendAcl(self):
-        """
-        test if the group config backend works with acl code
-        """
-        # define acl rights for FirstGroup, members of group can read and write
-        acl_rights = ["FirstGroup:admin,read,write"]
-        acl = security.AccessControlList(self.request.cfg, acl_rights)
-
-        allow = acl.may(self.request, u"JoeDoe", "admin")
-        # JoeDoe has admin rights because he is a member of group FirstGroup
-        assert allow
-
-        allow = acl.may(self.request, u"AnotherUser", "admin")
-        # AnotherUser has no read rights because he is not a member of group FirstGroup
-        assert not allow
-
-
-coverage_modules = ['MoinMoin.groups']
-
--- a/MoinMoin/groups/backends/_tests/__init__.py	Sun Jun 14 17:07:02 2009 +0200
+++ b/MoinMoin/groups/backends/_tests/__init__.py	Sun Jun 14 17:18:29 2009 +0200
@@ -42,7 +42,6 @@
         groups = self.request.groups
 
         for group, members in self.expanded_groups.iteritems():
-            print group
             assert group in groups
             for member in members:
                 assert member in groups[group]
@@ -53,15 +52,15 @@
         groups = self.request.groups
 
         for group, members in self.expanded_groups.iteritems():
-            returned_members = [x for x in groups[group]]
+            returned_members = list(groups[group])
             assert len(returned_members) == len(members)
             for member in members:
                 assert member in returned_members
 
-    def test_membergroups(self):
+    def test_groups_with_member(self):
         groups = self.request.groups
 
-        john_groups = list(groups.membergroups(u'John'))
+        john_groups = list(groups.groups_with_member(u'John'))
         assert 2 == len(john_groups)
         assert u'EditorGroup' in john_groups
         assert u'AdminGroup' in john_groups
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/groups/backends/_tests/test_compose_group.py	Sun Jun 14 17:18:29 2009 +0200
@@ -0,0 +1,91 @@
+# -*- coding: iso-8859-1 -*-
+
+"""
+MoinMoin.groups.GroupManager test
+
+@copyright: 2009 MoinMoin:DmitrijsMilajevs
+            2008 MoinMoin: MelitaMihaljevic
+@license: GPL, see COPYING for details
+"""
+
+from py.test import raises
+
+from  MoinMoin.groups.backends._tests import BackendTest
+from MoinMoin.groups.backends import config_group, compose_group
+from MoinMoin._tests import wikiconfig
+from MoinMoin import security
+
+
+class TestConfigBackend(BackendTest):
+
+    class Config(wikiconfig.Config):
+
+        def group_manager_init(self, request):
+            groups = BackendTest.test_groups
+            return compose_group.Backend(request, config_group.Backend(request, groups))
+
+
+class TestConfigGroup(object):
+
+    class Config(wikiconfig.Config):
+
+        admin_group = frozenset([u'Admin', u'JohnDoe'])
+        editor_group = frozenset([u'MainEditor', u'JohnDoe'])
+        fruit_group = frozenset([u'Apple', u'Banana', u'Cherry'])
+
+        first_backend_groups = {u'AdminGroup': admin_group,
+                                u'EditorGroup': editor_group,
+                                u'FruitGroup': fruit_group}
+
+        user_group = frozenset([u'JohnDoe', u'Bob', u'Joe'])
+        city_group = frozenset([u'Bolzano', u'Riga', u'London'])
+
+        # Suppose, someone hacked second backend and added himself to AdminGroup
+        second_admin_group = frozenset([u'TheHacker'])
+
+        second_backend_groups = {u'UserGroup': user_group,
+                                 u'CityGroup': city_group,
+                                 # Here group name clash occurs.
+                                 # AdminGroup is defined in both
+                                 # first_backend and second_backend.
+                                 u'AdminGroup': second_admin_group}
+
+        def group_manager_init(self, request):
+            return compose_group.Backend(request,
+                                         config_group.Backend(request,
+                                                              self.first_backend_groups),
+                                         config_group.Backend(request,
+                                                              self.second_backend_groups))
+
+    def setup_method(self, method):
+        self.groups = self.request.groups
+
+    def test_getitem(self):
+        raises(KeyError, lambda: self.groups[u'NotExistingGroup'])
+
+    def test_clashed_getitem(self):
+        """
+        Check the case when groups of the same name are defined in multiple
+        backends. __getitem__ should return the first match (backends are
+        considered in the order they are given in the backends list).
+        """
+        admin_group = self.groups[u'AdminGroup']
+
+        # TheHacker added himself to the second backend, but that must not be
+        # taken into consideration, because AdminGroup is defined in first
+        # backend and we only use the first match.
+        assert u'TheHacker' not in admin_group
+
+    def test_iter(self):
+        all_group_names = [group_name for group_name in self.groups]
+
+        assert 5 == len(all_group_names)
+        # There are no duplicates
+        assert len(set(all_group_names)) == len(all_group_names)
+
+    def test_contains(self):
+        assert u'UserGroup' in self.groups
+        assert u'not existing group' not in self.groups
+
+
+# coverage_modules = ['MoinMoin.groups.backend.compose_group']
--- a/MoinMoin/groups/backends/_tests/test_config_group.py	Sun Jun 14 17:07:02 2009 +0200
+++ b/MoinMoin/groups/backends/_tests/test_config_group.py	Sun Jun 14 17:18:29 2009 +0200
@@ -12,7 +12,6 @@
 
 from  MoinMoin.groups.backends._tests import BackendTest
 from MoinMoin.groups.backends import config_group
-from MoinMoin.groups import GroupManager
 from MoinMoin._tests import wikiconfig
 
 
@@ -22,7 +21,7 @@
 
         def group_manager_init(self, request):
             groups = BackendTest.test_groups
-            return GroupManager(config_group.Backend(request, groups))
+            return config_group.Backend(request, groups)
 
 
 coverage_modules = ['MoinMoin.groups.backends.config_group']
--- a/MoinMoin/groups/backends/_tests/test_wiki_group.py	Sun Jun 14 17:07:02 2009 +0200
+++ b/MoinMoin/groups/backends/_tests/test_wiki_group.py	Sun Jun 14 17:18:29 2009 +0200
@@ -19,7 +19,6 @@
 from MoinMoin.PageEditor import PageEditor
 from MoinMoin.user import User
 from MoinMoin._tests import append_page, become_trusted, create_page, create_random_string_list, nuke_page, nuke_user, wikiconfig
-from MoinMoin.groups import GroupManager
 
 
 class TestWikiGroupPageParser(object):
@@ -30,7 +29,7 @@
     class Config(wikiconfig.Config):
 
         def group_manager_init(self, request):
-            return GroupManager(wiki_group.Backend(request))
+            return wiki_group.Backend(request)
 
     def test_CamelCase(self):
         text = """
@@ -88,7 +87,7 @@
     class Config(wikiconfig.Config):
 
         def group_manager_init(self, request):
-            return GroupManager(wiki_group.Backend(request))
+            return wiki_group.Backend(request)
 
     def setup_class(self):
         become_trusted(self.request)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/groups/backends/compose_group.py	Sun Jun 14 17:18:29 2009 +0200
@@ -0,0 +1,61 @@
+# -*- coding: iso-8859-1 -*-
+"""
+MoinMoin - group access via various backends.
+
+@copyright: 2009 DmitrijsMilajevs
+@license: GPL, see COPYING for details
+"""
+
+from MoinMoin.groups.backends import BaseBackend
+
+
+class Backend(BaseBackend):
+    """
+    Manage several group backends.
+    """
+
+    def __init__(self, request, *backends):
+        """
+        @param backends: list of group backends which are used to get
+                         access to the group definitions.
+        """
+        super(Backend, self).__init__(request)
+        self._backends = backends
+
+    def __getitem__(self, group_name):
+        """
+        Get a group by its name. First match counts.
+
+        @param group_name: name of the group [unicode]
+        """
+        for backend in self._backends:
+            if group_name in backend:
+                return backend[group_name]
+        raise KeyError("There is no such group %s" % group_name)
+
+    def __iter__(self):
+        """
+        Iterate over group names in all backends (filtering duplicates).
+        """
+        yielded_groups = set()
+
+        for backend in self._backends:
+            for group_name in backend:
+                if group_name not in yielded_groups:
+                    yield group_name
+                    yielded_groups.add(group_name)
+
+    def __contains__(self, group_name):
+        """
+        Check if a group called group_name is available in any of the backends.
+
+        @param group_name: name of the group [unicode]
+        """
+        for backend in self._backends:
+            if group_name in backend:
+                return True
+        return False
+
+    def __repr__(self):
+        return "<%s backends=%s>" % (self.__class__, self._backends)
+
--- a/MoinMoin/wsgiapp.py	Sun Jun 14 17:07:02 2009 +0200
+++ b/MoinMoin/wsgiapp.py	Sun Jun 14 17:18:29 2009 +0200
@@ -44,7 +44,6 @@
     return context
 
 def init_group_manager(context):
-    from MoinMoin.groups import GroupManager
     context.groups = context.cfg.group_manager_init(context)
 
 def run(context):