changeset 4774:fb0cce9e8621

Groups 2009: Moin.groups.GroupManager is initialized with a request initialization.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Sun, 31 May 2009 21:17:34 +0200
parents c3e904a9c00b
children 0a0387a9eb1e
files MoinMoin/_tests/wikiconfig.py MoinMoin/conftest.py MoinMoin/groups/_tests/test_group_manager.py MoinMoin/groups/_tests/test_group_manager_acl.py MoinMoin/security/__init__.py MoinMoin/wsgiapp.py
diffstat 6 files changed, 41 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/wikiconfig.py	Sat May 30 08:22:09 2009 +0200
+++ b/MoinMoin/_tests/wikiconfig.py	Sun May 31 21:17:34 2009 +0200
@@ -25,7 +25,7 @@
     data_dir = os.path.join(_base_dir, "data")
     data_underlay_dir = os.path.join(_base_dir, "underlay")
 
-    group_manager = lambda self, request: GroupManager([])
+    group_manager_init = lambda self, request: GroupManager([])
 
     #show_hosts = 1
 
--- a/MoinMoin/conftest.py	Sat May 30 08:22:09 2009 +0200
+++ b/MoinMoin/conftest.py	Sun May 31 21:17:34 2009 +0200
@@ -31,7 +31,7 @@
 
 from MoinMoin.support.python_compatibility import set
 from MoinMoin.web.request import TestRequest, Client
-from MoinMoin.wsgiapp import Application, init
+from MoinMoin.wsgiapp import Application, init, init_group_manager
 from MoinMoin._tests import maketestwiki, wikiconfig
 
 coverage_modules = set()
@@ -72,6 +72,7 @@
     request = TestRequest()
     request.given_config = given_config
     request = init(request)
+    init_group_manager(request)
     return request
 
 
@@ -115,4 +116,3 @@
         if coverage is not None:
             coverage_modules.update(getattr(self.obj, 'coverage_modules', []))
         return super(Module, self).run(*args, **kwargs)
-
--- a/MoinMoin/groups/_tests/test_group_manager.py	Sat May 30 08:22:09 2009 +0200
+++ b/MoinMoin/groups/_tests/test_group_manager.py	Sun May 31 21:17:34 2009 +0200
@@ -12,6 +12,7 @@
 
 from MoinMoin.groups import BackendManager, GroupManager
 
+
 class TestGroupManagerAPI(object):
     """
     Performs test of the API of GroupManager.
@@ -19,42 +20,39 @@
 
     from MoinMoin._tests import wikiconfig
     class Config(wikiconfig.Config):
-        pass
+        admin_group = frozenset([u'Admin', u'JohnDoe'])
+        editor_group = frozenset([u'MainEditor', u'JohnDoe'])
+        fruit_group = frozenset([u'Apple', u'Banana', u'Cherry'])
 
-    def setup_class(self):
-        self.admin_group = frozenset([u'Admin', u'JohnDoe'])
-        self.editor_group = frozenset([u'MainEditor', u'JohnDoe'])
-        self.fruit_group = frozenset([u'Apple', u'Banana', u'Cherry'])
+        first_backend_groups = {u'AdminGroup': admin_group,
+                                u'EditorGroup': editor_group,
+                                u'FruitGroup': fruit_group}
 
-        first_backend_groups = {u'AdminGroup': self.admin_group,
-                                u'EditorGroup': self.editor_group,
-                                u'FruitGroup': self.fruit_group}
-
-        self.user_group = frozenset([u'JohnDoe', u'Bob', u'Joe'])
-        self.city_group = frozenset([u'Bolzano', u'Riga', u'London'])
+        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
-        self.second_admin_group = frozenset([u'TheHacker'])
+        second_admin_group = frozenset([u'TheHacker'])
 
-        second_backend_groups = {u'UserGroup': self.user_group,
-                                 u'CityGroup': self.city_group,
+        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': self.second_admin_group}
-
-        self.Config.group_manager = lambda self, request: GroupManager(backends=[BackendManager(request, first_backend_groups),
-                                                                                 BackendManager(request, second_backend_groups)])
+                                 u'AdminGroup': second_admin_group}
+        def group_manager_init(self, request):
+            return GroupManager(backends=[BackendManager(request, self.first_backend_groups),
+                                          BackendManager(request, self.second_backend_groups)])
 
     def setup_method(self, method):
-        self.group_manager = self.request.cfg.group_manager(self.request)
+        self.groups = self.request.groups
 
     def test_getitem(self):
         """
         Tests __getitem__ API method. It should return a group by its name.
         """
-        assert self.fruit_group == self.group_manager[u'FruitGroup']
-        raises(KeyError, lambda: self.group_manager[u'not existing group'])
+        assert self.request.cfg.fruit_group == self.groups[u'FruitGroup']
+        raises(KeyError, lambda: self.groups[u'not existing group'])
 
     def test_clashed_getitem(self):
         """
@@ -63,9 +61,9 @@
         backend must be taken in consideration, that backend which is
         defined first in the backends list.
         """
-        admin_group = self.group_manager[u'AdminGroup']
+        admin_group = self.groups[u'AdminGroup']
 
-        assert self.admin_group == admin_group
+        assert self.request.cfg.admin_group == admin_group
 
         # Nevertheless, TheHacker added himself to the second backend,
         # it must not be taken into consideration, because AdminGroup is defined
@@ -77,7 +75,7 @@
         Tests __iter__ API method. It should iterate over all groups
         available via backends. It should avoid group name clashes.
         """
-        all_group_names = [group_name for group_name in self.group_manager]
+        all_group_names = [group_name for group_name in self.groups]
 
         assert 5 == len(all_group_names)
         # There are no duplicates
@@ -88,8 +86,8 @@
         Tests __contains__ API method. It should check if a group
         called group_name is available via some backend.
         """
-        assert u'UserGroup' in self.group_manager
-        assert u'not existing group' not in self.group_manager
+        assert u'UserGroup' in self.groups
+        assert u'not existing group' not in self.groups
 
     def test_membergroups(self):
         """
@@ -97,12 +95,12 @@
         where member is a member of. It should return a list of group
         names.
         """
-        apple_groups = self.group_manager.membergroups(u'Apple')
+        apple_groups = self.groups.membergroups(u'Apple')
         assert 1 == len(apple_groups)
         assert u'FruitGroup' in apple_groups
         assert u'AdminGroup' not in apple_groups
 
-        john_doe_groups = self.group_manager.membergroups(u'JohnDoe')
+        john_doe_groups = self.groups.membergroups(u'JohnDoe')
         assert 3 == len(john_doe_groups)
         assert u'EditorGroup' in john_doe_groups
         assert u'AdminGroup' in john_doe_groups
--- a/MoinMoin/groups/_tests/test_group_manager_acl.py	Sat May 30 08:22:09 2009 +0200
+++ b/MoinMoin/groups/_tests/test_group_manager_acl.py	Sun May 31 21:17:34 2009 +0200
@@ -18,16 +18,11 @@
 
     from MoinMoin._tests import wikiconfig
     class Config(wikiconfig.Config):
-        pass
+        def group_manager_init(self, request):
+            groups = {u'FirstGroup': frozenset([u"ExampleUser", u"SecondUser", u"JoeDoe"]),
+                      u'SecondGroup': frozenset([u"ExampleUser", u"ThirdUser"])}
 
-    def setup_class(self):
-        groups = {u'FirstGroup': frozenset([u"ExampleUser", u"SecondUser", u"JoeDoe"]),
-                  u'SecondGroup': frozenset([u"ExampleUser", u"ThirdUser"])}
-
-        self.Config.group_manager = lambda self, request: GroupManager(backends=[BackendManager(request=request, backend=groups)])
-
-    def setup_method(self, method):
-        self.group_manager = self.request.cfg.group_manager(self.request)
+            return GroupManager(backends=[BackendManager(request=request, backend=groups)])
 
     def testConfigBackendAcl(self):
         """
@@ -45,33 +40,5 @@
         # AnotherUser has no read rights because he is not a member of group FirstGroup
         assert not allow
 
-    def testConfigBackend(self):
-        """
-        tests getting a group from the group manager, does group
-        membership tests.
-        """
-        # define config groups
-        groups = {'A': set(['a1', 'a2']),
-                  'B': set(['b1', 'b2']),
-                 }
-
-        # create config group manager backend object
-        group_manager_backend = GroupManager(BackendManager(request=self.request,
-                                                            backend=[groups]))
-
-        # check that a group named 'A' is available via the config backend
-        assert 'A' in group_manager_backend
-
-        # check that a group named 'C' is not available via the config backend
-        assert 'C' not in group_manager_backend
-
-        # get group object for a group named 'A'
-        group_A = group_manager_backend['A']
-
-        # check that a1 is a member of group A
-        assert 'a1' in group_A
-
-        # check that b1 is not a member of group A
-        assert 'b1' not in group_A
 
 coverage_modules = ['MoinMoin.groups']
--- a/MoinMoin/security/__init__.py	Sat May 30 08:22:09 2009 +0200
+++ b/MoinMoin/security/__init__.py	Sun May 31 21:17:34 2009 +0200
@@ -308,15 +308,15 @@
         else: # we have a #acl on the page (self.acl can be [] if #acl is empty!)
             acl = self.acl
 
-        group_manager = request.cfg.group_manager(request)
+        groups = request.groups
 
         allowed = None
         for entry, rightsdict in acl:
             if entry in self.special_users:
                 handler = getattr(self, "_special_"+entry, None)
                 allowed = handler(request, name, dowhat, rightsdict)
-            elif entry in group_manager:
-                if name in group_manager[entry]:
+            elif entry in groups:
+                if name in groups[entry]:
                     allowed = rightsdict.get(dowhat)
                 else:
                     for special in self.special_users:
--- a/MoinMoin/wsgiapp.py	Sat May 30 08:22:09 2009 +0200
+++ b/MoinMoin/wsgiapp.py	Sun May 31 21:17:34 2009 +0200
@@ -43,6 +43,10 @@
     context.clock.stop('init')
     return context
 
+def init_group_manager(context):
+    from MoinMoin.groups import GroupManager
+    context.groups = context.cfg.group_manager_init(context)
+
 def run(context):
     """ Run a context trough the application. """
     context.clock.start('run')
@@ -241,6 +245,7 @@
         try:
             request = self.Request(environ)
             context = init(request)
+            init_group_manager(context)
             response = run(context)
             context.clock.stop('total')
         except HTTPException, e: