changeset 4782:4634eaf01ed4

Groups2009: wiki_group test refinements. lower_case_with_underscores for method names. ACL related tests were added. Test for recursive groups definitions.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Wed, 03 Jun 2009 20:26:10 +0200
parents cee722c2097c
children d89ff2851ade
files MoinMoin/groups/backends/_tests/test_wiki_group.py
diffstat 1 files changed, 161 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/groups/backends/_tests/test_wiki_group.py	Wed Jun 03 12:10:14 2009 +0200
+++ b/MoinMoin/groups/backends/_tests/test_wiki_group.py	Wed Jun 03 20:26:10 2009 +0200
@@ -1,9 +1,10 @@
 # -*- coding: iso-8859-1 -*-
 """
-    MoinMoin - MoinMoin.wikidicts tests
+    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.
 
@@ -16,7 +17,7 @@
 from py.test import raises
 
 from MoinMoin.groups.backends import wiki_group
-from MoinMoin import Page
+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
@@ -31,49 +32,49 @@
         def group_manager_init(self, request):
             return GroupManager(backends=[wiki_group.Backend(request)])
 
-    def testCamelCase(self):
+    def test_CamelCase(self):
         text = """
  * CamelCase
 """
-        assert u'CamelCase' in self.getGroup(text)
+        assert u'CamelCase' in self.get_group(text)
 
-    def testExtendedName(self):
+    def test_extended_name(self):
         text = """
  * extended name
 """
-        assert u'extended name' in self.getGroup(text)
+        assert u'extended name' in self.get_group(text)
 
-    def testExtendedLink(self):
+    def test_extended_link(self):
         text = """
  * [[extended link]]
 """
-        assert u'extended link' in self.getGroup(text)
+        assert u'extended link' in self.get_group(text)
 
-    def testIgnoreSecondLevelList(self):
+    def test_ignore_second_level_list(self):
         text = """
   * second level
    * third level
     * forth level
      * and then some...
 """
-        assert len([x for x in self.getGroup(text)]) == 0
+        assert len([x for x in self.get_group(text)]) == 0
 
-    def testIgnoreOther(self):
+    def test_ignore_other(self):
         text = """
 = ignore this =
  * take this
 
 Ignore previous line and this text.
 """
-        assert u'take this' in self.getGroup(text)
+        assert u'take this' in self.get_group(text)
 
-    def testStripWhitespace(self):
+    def test_strip_whitespace(self):
         text = """
  *   take this
 """
-        assert u'take this' in self.getGroup(text)
+        assert u'take this' in self.get_group(text)
 
-    def getGroup(self, text):
+    def get_group(self, text):
         request = self.request
         become_trusted(request)
         create_page(request, u'SomeTestGroup', text)
@@ -116,29 +117,38 @@
         nuke_page(self.request, self.other_group_page_name)
         nuke_page(self.request, self.third_group_page_name)
 
-    def testContainment(self):
+    def test_contains(self):
+        """
+        Test group_wiki Backend and Group containment methods.
+        """
         groups = self.request.groups
 
+        assert u'TestWikiGroup' in groups
         assert u'Banana' in groups[u'TestWikiGroup']
         assert u'Apple' in groups[u'TestWikiGroup']
+        assert u'Admin' in groups[u'TestWikiGroup'], 'Groups must be automatically expanded'
 
+        assert u'OtherGroup' in groups
         assert u'Apple' in groups[u'OtherGroup']
         assert u'Admin' in groups[u'OtherGroup']
 
+        assert u'NotExistingGroup' not in groups
+        raises(KeyError, lambda: groups[u'NotExistingGroup'])
+
+        assert u'ThirdGroup' in groups
+
+    def test_membergroups(self):
+        groups = self.request.groups
+
         apple_groups = groups.membergroups(u'Apple')
-        assert 2 == len(apple_groups), 'Groups must be automatically expanded'
+        assert 2 == len(apple_groups)
         assert u'TestWikiGroup' in apple_groups
         assert u'OtherGroup' in apple_groups
         assert u'ThirdGroup' not in apple_groups
 
-        raises(KeyError, lambda: groups[u'NotExistingssssGroup'])
-
-        assert u'ThirdGroup' in groups
-
-    def testRenameGroupPage(self):
+    def test_rename_group_page(self):
         """
-         tests if the dict cache for groups is refreshed after
-         renaming a Group page
+        Tests if the groups cache is refreshed after renaming a Group page.
         """
         request = self.request
         become_trusted(request)
@@ -151,9 +161,9 @@
 
         assert result is True
 
-    def testCopyGroupPage(self):
+    def test_copy_group_page(self):
         """
-         tests if the dict cache for groups is refreshed after copying a Group page
+        Tests if the groups cache is refreshed after copying a Group page.
         """
         request = self.request
         become_trusted(request)
@@ -168,84 +178,174 @@
 
         assert result is True
 
-    def testAppendingGroupPage(self):
+    def test_appending_group_page(self):
         """
-         tests scalability by appending a name to a large list of
-         group members
+        Test scalability by appending a name to a large list of group members.
         """
+        request = self.request
+        become_trusted(request)
+
         # long list of users
         page_content = [u" * %s" % member for member in create_random_string_list(length=15, count=30000)]
-        request = self.request
-        become_trusted(request)
         test_user = create_random_string_list(length=15, count=1)[0]
-        page = create_page(request, u'UserGroup', "\n".join(page_content))
-        page = append_page(request, u'UserGroup', u' * %s' % test_user)
+        create_page(request, u'UserGroup', "\n".join(page_content))
+        append_page(request, u'UserGroup', u' * %s' % test_user)
         result = test_user in request.groups['UserGroup']
         nuke_page(request, u'UserGroup')
 
-        assert result is True
+        assert result
 
-    def testUserAppendingGroupPage(self):
+    def test_user_addition_to_group_page(self):
         """
-         tests appending a username to a large list of group members
-         and user creation
+        Test addition of a username to a large list of group members.
         """
-        # long list of users
-        page_content = [u" * %s" % member for member in create_random_string_list()]
         request = self.request
         become_trusted(request)
-        test_user = create_random_string_list(length=15, count=1)[0]
-        page = create_page(request, u'UserGroup', "\n".join(page_content))
-        page = append_page(request, u'UserGroup', u' * %s' % test_user)
-
-        # now shortly later we create a user object
-        user = User(request, name=test_user)
-        if not user.exists():
-            User(request, name=test_user, password=test_user).save()
 
-        result = test_user in request.groups[u'UserGroup']
-        nuke_page(request, u'UserGroup')
-        nuke_user(request, test_user)
+        # long list of users
+        page_content = [u" * %s" % member for member in create_random_string_list()]
+        create_page(request, u'UserGroup', "\n".join(page_content))
 
-        assert result is True
+        new_user = create_random_string_list(length=15, count=1)[0]
+        append_page(request, u'UserGroup', u' * %s' % new_user)
+        user = User(request, name=new_user)
+        if not user.exists():
+            User(request, name=new_user, password=new_user).save()
 
-    def testMemberRemovedFromGroupPage(self):
+        result = new_user in request.groups[u'UserGroup']
+        nuke_page(request, u'UserGroup')
+        nuke_user(request, new_user)
+
+        assert result
+
+    def test_member_removed_from_group_page(self):
         """
-         tests appending a member to a large list of group members and
-         recreating the page without the member
+        Tests appending a member to a large list of group members and
+        recreating the page without the member.
         """
+        request = self.request
+        become_trusted(request)
+
         # long list of users
         page_content = [u" * %s" % member for member in create_random_string_list()]
         page_content = "\n".join(page_content)
-        request = self.request
-        become_trusted(request)
+        create_page(request, u'UserGroup', page_content)
+
         test_user = create_random_string_list(length=15, count=1)[0]
-        page = create_page(request, u'UserGroup', page_content)
         page = append_page(request, u'UserGroup', u' * %s' % test_user)
+
         # saves the text without test_user
         page.saveText(page_content, 0)
         result = test_user in request.groups[u'UserGroup']
         nuke_page(request, u'UserGroup')
 
-        assert result is False
+        assert not result
 
-    def testGroupPageTrivialChange(self):
+    def test_group_page_user_addition_trivial_change(self):
         """
-         tests appending a username to a group page by trivial change
+        Test addition of a user to a group page by trivial change.
         """
         request = self.request
         become_trusted(request)
+
         test_user = create_random_string_list(length=15, count=1)[0]
         member = u" * %s\n" % test_user
         page = create_page(request, u'UserGroup', member)
+
         # next member saved  as trivial change
         test_user = create_random_string_list(length=15, count=1)[0]
         member = u" * %s\n" % test_user
         page.saveText(member, 0, trivial=1)
+
         result = test_user in request.groups[u'UserGroup']
+
         nuke_page(request, u'UserGroup')
 
-        assert result is True
+        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.
+        First check acl rights of a user that is not a member of group
+        then add user member to a page group and check acl rights
+        """
+        request = self.request
+        become_trusted(request)
+
+        create_page(request, u'NewGroup', u" * ExampleUser")
+
+        acl_rights = ["NewGroup:read,write"]
+        acl = security.AccessControlList(request.cfg, acl_rights)
+
+        has_rights_before = acl.may(request, u"AnotherUser", "read")
+
+        # update page - add AnotherUser to a page group NewGroup
+        append_page(request, u'NewGroup', u" * AnotherUser")
+
+        has_rights_after = acl.may(request, u"AnotherUser", "read")
+
+        nuke_page(request, u'NewGroup')
+
+        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 = ["OtherGroup:read,write,delete,admin All:read"]
+        acl = security.AccessControlList(request.cfg, acl_rights)
+
+        assert acl.may(request, u"Editor", "read")
+        assert acl.may(request, u"Editor", "write")
+        assert acl.may(request, u"Editor", "delete")
+        assert acl.may(request, u"Editor", "admin")
+
+        assert acl.may(request, u"Banana", "read")
+        assert not acl.may(request, u"Banana", "write")
+        assert not acl.may(request, u"Banana", "delete")
+        assert not acl.may(request, u"Banana", "admin")
 
 
 coverage_modules = ['MoinMoin.groups.backends.wiki_group']