changeset 3042:353739af70fa

PageEditor: fixed copyPage for write restricted pages, tests added (ported from 1.6)
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Tue, 19 Feb 2008 22:06:50 +0100
parents 7400c1f2e20a
children fafaa6590b80
files MoinMoin/PageEditor.py MoinMoin/_tests/test_PageEditor.py
diffstat 2 files changed, 79 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/PageEditor.py	Thu Feb 14 17:15:04 2008 +0100
+++ b/MoinMoin/PageEditor.py	Tue Feb 19 22:06:50 2008 +0100
@@ -564,16 +564,9 @@
             self.error = None
             if not comment:
                 comment = u"## page was copied from %s" % self.page_name
-            if request.user.may.write(newpagename):
-                # Save page text with a comment about the old name and log entry
-                savetext = u"## page was copied from %s\n%s" % (self.page_name, savetext)
-                newpage.saveText(savetext, 0, comment=comment, extra=self.page_name, action='SAVE', notify=False)
-            else:
-                # if user is  not able to write to the page itselfs we set a log entry only
-                from MoinMoin import packages
-                rev = newpage.current_rev()
-                packages.edit_logfile_append(self, newpagename, newpath, rev, 'SAVENEW', logname='edit-log',
-                                       comment=comment, author=u"CopyPage action")
+            savetext = u"## page was copied from %s\n%s" % (self.page_name, savetext)
+            Page.__init__(self, request, newpagename)
+            self._write_file(savetext, "SAVENEW", comment)
 
             event = PageCopiedEvent(request, newpage, self, comment)
             send_event(event)
@@ -824,7 +817,6 @@
             # Strip trailing spaces if needed
             if kw.get('stripspaces', 0):
                 lines = [line.rstrip() for line in lines]
-
             # Add final newline if not present, better for diffs (does
             # not include former last line when just adding text to
             # bottom; idea by CliffordAdams)
--- a/MoinMoin/_tests/test_PageEditor.py	Thu Feb 14 17:15:04 2008 +0100
+++ b/MoinMoin/_tests/test_PageEditor.py	Tue Feb 19 22:06:50 2008 +0100
@@ -219,5 +219,81 @@
 
         assert result == expected
 
+class TestCopyPage(object):
+
+    pagename = u'AutoCreatedMoinMoinTemporaryTestPage'
+    copy_pagename = u'AutoCreatedMoinMoinTemporaryCopyTestPage'
+    shouldDeleteTestPage = True
+    text = u'Example'
+
+    def setup_method(self, method):
+        self.savedValid = self.request.user.valid
+        self.request.user.valid = 1
+
+    def teardown_method(self, method):
+        self.request.user.valid = self.savedValid
+        self.deleteTestPage()
+
+    def createTestPage(self):
+        """ Create temporary page, bypass logs, notification and backups
+
+        TODO: this code is very fragile, any change in the
+        implementation will break this test. Need to factor PageEditor
+        to make it possible to create page without loging and notifying.
+        """
+        import os
+        path = Page(self.request, self.pagename).getPagePath(check_create=0)
+        copy_path = Page(self.request, self.copy_pagename).getPagePath(check_create=0)
+
+        if os.path.exists(path) or os.path.exists(copy_path):
+            self.shouldDeleteTestPage = False
+            py.test.skip("%s or %s exists. Won't overwrite exiting page" % (self.pagename, self.copy_pagename))
+        try:
+            os.mkdir(path)
+            revisionsDir = os.path.join(path, 'revisions')
+            os.mkdir(revisionsDir)
+            current = '00000001'
+            file(os.path.join(path, 'current'), 'w').write('%s\n' % current)
+
+            file(os.path.join(revisionsDir, current), 'w').write(self.text)
+        except Exception, err:
+            py.test.skip("Can not be create test page: %s" % err)
+
+    def deleteTestPage(self):
+        """ Delete temporary page, bypass logs and notifications """
+        if self.shouldDeleteTestPage:
+            import shutil
+            shutil.rmtree(Page(self.request, self.pagename).getPagePath(), True)
+            shutil.rmtree(Page(self.request, self.copy_pagename).getPagePath(), True)
+
+    def test_copy_page(self):
+        """
+        Tests copying a page without restricted acls
+        """
+        self.createTestPage()
+        result, msg = PageEditor(self.request, self.pagename).copyPage(self.copy_pagename)
+        revision = Page(self.request, self.copy_pagename).current_rev()
+        assert result and revision is 2
+
+    def test_copy_page_acl_read(self):
+        """
+        Tests copying a page without write rights
+        """
+        self.text = u'#acl SomeUser:read,write,delete All:read\n'
+        self.createTestPage()
+        result, msg = PageEditor(self.request, self.pagename).copyPage(self.copy_pagename)
+        revision = Page(self.request, self.copy_pagename).current_rev()
+        assert result and revision is 2
+
+    def test_copy_page_acl_no_read(self):
+        """
+        Tests copying a page without read rights
+        """
+        self.text = u'#acl SomeUser:read,write,delete All:\n'
+        self.createTestPage()
+        result, msg = PageEditor(self.request, self.pagename).copyPage(self.copy_pagename)
+        revision = Page(self.request, self.copy_pagename).current_rev()
+        assert result and revision is 2
+
 coverage_modules = ['MoinMoin.PageEditor']