changeset 4800:3b05bf18c0d1

Groups2009: The BackendTest class provides basic test methods for testing backends. It should be inherited to test backends.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Wed, 10 Jun 2009 20:32:42 +0200
parents fde7471cb2a3
children 1d90fe5f1bcf
files MoinMoin/groups/backends/__init__.py MoinMoin/groups/backends/_tests/__init__.py MoinMoin/groups/backends/_tests/test_config_group.py MoinMoin/groups/backends/_tests/test_wiki_group.py MoinMoin/groups/backends/config_group.py
diffstat 5 files changed, 197 insertions(+), 174 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/groups/backends/__init__.py	Wed Jun 10 20:32:37 2009 +0200
+++ b/MoinMoin/groups/backends/__init__.py	Wed Jun 10 20:32:42 2009 +0200
@@ -20,6 +20,8 @@
         self.request = request
         self.name = name
         self._backend = backend
+        self.members = None
+        self.member_groups = None
 
         self.to_backend_name = backend.to_backend_name
         self.to_group_name = backend.to_group_name
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/groups/backends/_tests/__init__.py	Wed Jun 10 20:32:42 2009 +0200
@@ -0,0 +1,150 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - MoinMoin.groups.backends base test classes.
+
+    @copyright: 2003-2004 by Juergen Hermann <jh@web.de>,
+                2007 by MoinMoin:ThomasWaldmann
+                2008 by MoinMoin:MelitaMihaljevic
+                2009 by MoinMoin:DmitrijsMilajevs
+    @license: GNU GPL, see COPYING for details.
+
+"""
+
+from py.test import raises
+
+from MoinMoin import security
+from MoinMoin._tests import wikiconfig
+
+class Config(wikiconfig.Config):
+    # Simply drop the "Group" postfix for group names given to a backend.
+    # Note: in the real world, this would not work good enough:
+    to_backend_name = lambda self, group_name:  group_name[:-5]
+    # Add "Group" postfix for group names received from a backend.
+    # Note: in the real world, this would not work good enough:
+    to_group_name = lambda self, backend_name:'%sGroup' % backend_name
+
+class BackendTest(object):
+
+    test_groups = {u'EditorGroup': [u'AdminGroup', u'John', u'JoeDoe', u'Editor1'],
+                   u'AdminGroup': [u'Admin1', u'Admin2', u'John'],
+                   u'OtherGroup': [u'SomethingOther'],
+                   u'RecursiveGroup': [u'Something', u'OtherRecursiveGroup'],
+                   u'OtherRecursiveGroup': [u'RecursiveGroup', u'Anything'],
+                   u'ThirdRecursiveGroup': [u'ThirdRecursiveGroup', u'Banana']}
+
+    expanded_groups = {u'EditorGroup': [u'Admin1', u'Admin2', u'John',
+                                        u'JoeDoe', u'Editor1'],
+                       u'AdminGroup': [u'Admin1', u'Admin2', u'John'],
+                       u'OtherGroup': [u'SomethingOther'],
+                       u'RecursiveGroup': [u'Anything', u'Something'],
+                       u'OtherRecursiveGroup': [u'Anything', u'Something'],
+                       u'ThirdRecursiveGroup': [u'Banana']}
+
+    # These backend group names do not follow moin convention:
+    mapped_groups = {u'Editor': [u'Admin', u'John', u'JoeDoe', u'Editor1'],
+                     u'Admin': [u'Admin1', u'Admin2', u'John'],
+                     u'Other': [u'SomethingOther'],
+                     u'Recursive': [u'Something', u'OtherRecursive'],
+                     u'OtherRecursive': [u'Recursive', u'Anything'],
+                     u'ThirdRecursive': [u'ThirdRecursive', u'Banana']}
+
+    def test_contains(self):
+        """
+        Test group_wiki Backend and Group containment methods.
+        """
+        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]
+
+        raises(KeyError, lambda: groups[u'NotExistingGroup'])
+
+    def test_iter(self):
+        groups = self.request.groups
+
+        for (group, members) in self.expanded_groups.iteritems():
+            returned_members = [x for x in groups[group]]
+            assert len(returned_members) == len(members)
+            for member in members:
+                assert member in returned_members
+
+    def test_membergroups(self):
+        groups = self.request.groups
+
+        john_groups = groups.membergroups(u'John')
+        assert 2 == len(john_groups)
+        assert u'EditorGroup' in john_groups
+        assert u'AdminGroup' in john_groups
+        assert u'ThirdGroup' not in john_groups
+
+    def test_backend_acl_allow(self):
+        """
+        Test if the wiki group backend works with acl code.
+        Check user which has rights.
+        """
+        request = self.request
+
+        acl_rights = ["AdminGroup:admin,read,write"]
+        acl = security.AccessControlList(request.cfg, acl_rights)
+
+        allow = acl.may(request, u"Admin1", "admin")
+
+        assert allow, 'Admin has read rights because he is member of AdminGroup'
+
+    def test_backend_acl_deny(self):
+        """
+        Test if the wiki group backend works with acl code.
+        Check user which does not have rights.
+        """
+        request = self.request
+
+        acl_rights = ["AdminGroup:read,write"]
+        acl = security.AccessControlList(request.cfg, acl_rights)
+
+        other_user_allow = acl.may(request, u"OtherUser", "admin")
+        some_user_allow = acl.may(request, u"SomeUser", "read")
+
+        assert not other_user_allow, 'OtherUser does not have admin rights because it is not listed in acl'
+        assert not some_user_allow, 'SomeUser does not have admin read right because he is not listed in the AdminGroup'
+
+    def test_wiki_backend_page_acl_with_all(self):
+        request = self.request
+
+        acl_rights = ["EditorGroup:read,write,delete,admin All:read"]
+        acl = security.AccessControlList(request.cfg, acl_rights)
+
+
+        for member in self.expanded_groups[u'EditorGroup']:
+            assert acl.may(request, member, "read")
+            assert acl.may(request, member, "write")
+            assert acl.may(request, member, "delete")
+            assert acl.may(request, member, "admin")
+
+        assert acl.may(request, u"Someone", "read")
+        assert not acl.may(request, u"Someone", "write")
+        assert not acl.may(request, u"Someone", "delete")
+        assert not acl.may(request, u"Someone", "admin")
+
+class BackendTestMapping(BackendTest):
+    """
+    Test group name mapping:
+        moin -> backend (e.g. "AdminGroup" -> "ImportantAdminGroup")
+        backend -> moin (e.g. "ImportantAdminGroup" -> "AdminGroup")
+
+    Moin expects group names to match the page_group_regex (e.g. "AdminGroup"),
+    but a backend might want to use different group names (e.g. just "ImportantAdminGroup").
+    """
+    test_groups = {u'ImportantEditorGroup': [u'ImportantAdminGroup', u'John',
+                                             u'JoeDoe', u'Editor'],
+                   u'ImportantAdminGroup': [u'Admin', u'Admin2', u'John'],
+                   u'ImportantOtherGroup': [u'SomethingOther'],
+                   u'ImportantRecursiveGroup': [u'Something', u'ImportantOtherRecursiveGroup'],
+                   u'ImportantOtherRecursiveGroup': [u'ImportantRecursiveGroup', u'Anything'],
+                   u'ImportantThirdRecursiveGroup': [u'ImportantThirdRecursiveGroup', u'Banana']}
+
+
+coverage_modules = ['MoinMoin.groups.backends.config_group']
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/groups/backends/_tests/test_config_group.py	Wed Jun 10 20:32:42 2009 +0200
@@ -0,0 +1,36 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - MoinMoin.groups.backends.wiki_group tests
+
+    @copyright: 2003-2004 by Juergen Hermann <jh@web.de>,
+                2007 by MoinMoin:ThomasWaldmann
+                2008 by MoinMoin:MelitaMihaljevic
+                2009 by MoinMoin:DmitrijsMilajevs
+    @license: GNU GPL, see COPYING for details.
+
+"""
+
+from  MoinMoin.groups.backends._tests import BackendTest, BackendTestMapping, Config
+from MoinMoin.groups.backends import config_group
+from MoinMoin.groups import GroupManager
+
+class TestConfigBackend(BackendTest):
+
+    class Config(Config):
+        def group_manager_init(self, request):
+            self.config_groups = BackendTest.test_groups
+            return GroupManager(backends=[config_group.Backend(request)])
+
+class TestConfigBackendMapping(TestConfigBackend):
+
+    class Config(Config):
+        def group_manager_init(self, request):
+            backend = config_group.Backend(request, TestConfigBackend.mapped_groups)
+
+            backend.to_backend_name = self.to_backend_name
+            backend.to_group_name = self.to_group_name
+
+            return GroupManager(backends=[backend])
+
+coverage_modules = ['MoinMoin.groups.backends.config_group']
+
--- a/MoinMoin/groups/backends/_tests/test_wiki_group.py	Wed Jun 10 20:32:37 2009 +0200
+++ b/MoinMoin/groups/backends/_tests/test_wiki_group.py	Wed Jun 10 20:32:42 2009 +0200
@@ -13,19 +13,20 @@
 from py.test import raises
 import re, shutil
 
+from  MoinMoin.groups.backends._tests import BackendTest, BackendTestMapping, Config
 from MoinMoin.groups.backends import wiki_group
 from MoinMoin import Page, security
 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._tests import append_page, become_trusted, create_page, create_random_string_list, nuke_page, nuke_user
 from MoinMoin.groups import GroupManager
 
-class TestWikiGroupPage:
+class TestWikiGroupPageParser(object):
     """
     Test what backend extracts from a group page and what is ignored.
     """
 
-    class Config(wikiconfig.Config):
+    class Config(Config):
         def group_manager_init(self, request):
             return GroupManager(backends=[wiki_group.Backend(request)])
 
@@ -80,72 +81,25 @@
         return group
 
 
-class TestWikiGroupBackend:
+class TestWikiGroupBackend(BackendTest):
 
-    class Config(wikiconfig.Config):
+    class Config(Config):
         def group_manager_init(self, request):
             return GroupManager(backends=[wiki_group.Backend(request)])
 
     def setup_class(self):
         become_trusted(self.request)
 
-        test_groups = {u'EditorGroup': [u'AdminGroup', u'John', u'JoeDoe', u'Editor'],
-                       u'AdminGroup': [u'Admin', u'Admin2', u'John'],
-                       u'OtherGroup': [u'SomethingOther'],
-                       u'RecursiveGroup': [u'Something', u'OtherRecursiveGroup'],
-                       u'OtherRecursiveGroup': [u'RecursiveGroup', u'Anything'],
-                       u'ThirdRecursiveGroup': [u'ThirdRecursiveGroup', u'Banana']}
-
-        self.expanded_groups = {u'EditorGroup': [u'Admin', u'Admin2', u'John',
-                                                 u'JoeDoe', u'Editor'],
-                                u'AdminGroup': [u'Admin', u'Admin2', u'John'],
-                                u'OtherGroup': [u'SomethingOther'],
-                                u'RecursiveGroup': [u'Anything', u'Something'],
-                                u'OtherRecursiveGroup': [u'Anything', u'Something'],
-                                u'ThirdRecursiveGroup': [u'Banana']}
-
-        for (group, members) in test_groups.iteritems():
+        for (group, members) in self.test_groups.iteritems():
             page_text = ' * %s' % '\n * '.join(members)
             create_page(self.request, group, page_text)
 
     def teardown_class(self):
         become_trusted(self.request)
 
-        for group in self.expanded_groups:
+        for group in self.test_groups:
             nuke_page(self.request, group)
 
-    def test_contains(self):
-        """
-        Test group_wiki Backend and Group containment methods.
-        """
-        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]
-
-        raises(KeyError, lambda: groups[u'NotExistingGroup'])
-
-    def test_iter(self):
-        groups = self.request.groups
-
-        for (group, members) in self.expanded_groups.iteritems():
-            returned_members = [x for x in groups[group]]
-            assert len(returned_members) == len(members)
-            for member in members:
-                assert member in returned_members
-
-    def test_membergroups(self):
-        groups = self.request.groups
-
-        john_groups = groups.membergroups(u'John')
-        assert 2 == len(john_groups)
-        assert u'EditorGroup' in john_groups
-        assert u'AdminGroup' in john_groups
-        assert u'ThirdGroup' not in john_groups
-
     def test_rename_group_page(self):
         """
         Tests if the groups cache is refreshed after renaming a Group page.
@@ -263,46 +217,6 @@
 
         assert result
 
-    def test_wiki_backend_acl_allow(self):
-        """
-        Test if the wiki group backend works with acl code.
-        Check user which has rights.
-        """
-        request = self.request
-        become_trusted(request)
-
-        create_page(request, u'FirstGroup', u" * OtherUser")
-
-        acl_rights = ["FirstGroup:admin,read,write"]
-        acl = security.AccessControlList(request.cfg, acl_rights)
-
-        allow = acl.may(request, u"OtherUser", "admin")
-
-        nuke_page(request, u'FirstGroup')
-
-        assert allow, 'OtherUser has read rights because he is member of FirstGroup'
-
-    def test_wiki_backend_acl_deny(self):
-        """
-        Test if the wiki group backend works with acl code.
-        Check user which does not have rights.
-        """
-        request = self.request
-        become_trusted(request)
-
-        create_page(request, u'FirstGroup', u" * OtherUser")
-
-        acl_rights = ["FirstGroup:read,write"]
-        acl = security.AccessControlList(request.cfg, acl_rights)
-
-        other_user_allow = acl.may(request, u"OtherUser", "admin")
-        some_user_allow = acl.may(request, u"SomeUser", "read")
-
-        nuke_page(request, u'FirstGroup')
-
-        assert not other_user_allow, 'OtherUser does not have admin rights because it is not listed in acl'
-        assert not some_user_allow, 'SomeUser does not have admin read right because he is not listed in the FirstGroup'
-
     def test_wiki_backend_page_acl_append_page(self):
         """
         Test if the wiki group backend works with acl code.
@@ -329,84 +243,5 @@
         assert not has_rights_before, 'AnotherUser has no read rights because in the beginning he is not a member of a group page NewGroup'
         assert has_rights_after, 'AnotherUser must have read rights because after appendage he is member of NewGroup'
 
-
-    def test_wiki_backend_page_acl_with_all(self):
-        request = self.request
-        become_trusted(request)
-
-        acl_rights = ["EditorGroup:read,write,delete,admin All:read"]
-        acl = security.AccessControlList(request.cfg, acl_rights)
-
-
-        for member in self.expanded_groups[u'EditorGroup']:
-            assert acl.may(request, member, "read")
-            assert acl.may(request, member, "write")
-            assert acl.may(request, member, "delete")
-            assert acl.may(request, member, "admin")
-
-        assert acl.may(request, u"Someone", "read")
-        assert not acl.may(request, u"Someone", "write")
-        assert not acl.may(request, u"Someone", "delete")
-        assert not acl.may(request, u"Someone", "admin")
-
-
-class TestWikiGroupNameMapping(object):
-    """
-    Test group name mapping:
-        moin -> backend (e.g. "AdminGroup" -> "VeryImportantAdminGroup")
-        backend -> moin (e.g. "VeryImportantAdminGroup" -> "AdminGroup")
-
-    Moin expects group names to match the page_group_regex (e.g. "AdminGroup"),
-    but a backend might want to use different group names (e.g. just "Admin").
-    """
-
-    class Config(wikiconfig.Config):
-
-        def group_manager_init(self, request):
-            backend = wiki_group.Backend(request)
-            backend.to_backend_name = lambda group_name: 'VeryImportant%s' % group_name
-            # Get rid of the prefix VeryImportant
-            # Note: In real set up it is more complicated.
-            backend.to_group_name = lambda backend_name: backend_name[13:]
-
-            return GroupManager(backends=[backend])
-
-    def setup_class(self):
-        become_trusted(self.request)
-
-        test_groups = {u'VeryImportantEditorGroup': [u'VeryImportantAdminGroup', u'John', u'JoeDoe', u'Editor'],
-                       u'VeryImportantAdminGroup': [u'Admin', u'Admin2', u'John']}
-
-        self.expanded_groups = {u'EditorGroup': [u'Admin', u'Admin2', u'John',
-                                                 u'JoeDoe', u'Editor'],
-                                u'AdminGroup': [u'Admin', u'Admin2', u'John']}
-
-        for (group, members) in test_groups.iteritems():
-            page_text = ' * %s' % '\n * '.join(members)
-            create_page(self.request, group, page_text)
-
-    def test_contains(self):
-        """
-        Test group_wiki Backend and Group containment methods.
-        """
-        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]
-
-        raises(KeyError, lambda: groups[u'NotExistingGroup'])
-
-    def test_iter(self):
-        groups = self.request.groups
-
-        for (group, members) in self.expanded_groups.iteritems():
-            returned_members = [x for x in groups[group]]
-            assert len(returned_members) == len(members)
-            for member in members:
-                assert member in returned_members
-
 coverage_modules = ['MoinMoin.groups.backends.wiki_group']
 
--- a/MoinMoin/groups/backends/config_group.py	Wed Jun 10 20:32:37 2009 +0200
+++ b/MoinMoin/groups/backends/config_group.py	Wed Jun 10 20:32:42 2009 +0200
@@ -19,7 +19,7 @@
         member_groups = set()
 
         for member in self._backend._groups[backend_group_name]:
-            if member in self._backend:
+            if member in self._backend._groups:
                 member_groups.add(self.to_group_name(member))
             else:
                 members_final.add(member)