Back to index

moin  1.9.0~rc2
test_PageEditor.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - MoinMoin.PageEditor Tests
00004 
00005     @copyright: 2003-2004 by Juergen Hermann <jh@web.de>,
00006                 2007 MoinMoin:ThomasWaldmann
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 import py
00011 
00012 from MoinMoin import wikiutil
00013 from MoinMoin.Page import Page
00014 from MoinMoin.PageEditor import PageEditor
00015 
00016 # TODO: check if and where we can use the helpers:
00017 from MoinMoin._tests import become_trusted, create_page, nuke_page
00018 
00019 class TestExpandVars(object):
00020     """PageEditor: testing page editor"""
00021     pagename = u'AutoCreatedMoinMoinTemporaryTestPage'
00022 
00023     _tests = (
00024         # Variable,             Expanded
00025         ("@PAGE@", pagename),
00026         ("em@PAGE@bedded", "em%sbedded" % pagename),
00027         ("@NOVAR@", "@NOVAR@"),
00028         ("case@Page@sensitive", "case@Page@sensitive"),
00029         )
00030 
00031     def setup_method(self, method):
00032         self.page = PageEditor(self.request, self.pagename)
00033 
00034     def testExpandVariables(self):
00035         """ PageEditor: expand general variables """
00036         for var, expected in self._tests:
00037             result = self.page._expand_variables(var)
00038             assert result == expected
00039 
00040 
00041 class TestExpandUserName(object):
00042     """ Base class for user name tests
00043 
00044     Set user name during tests.
00045     """
00046     pagename = u'AutoCreatedMoinMoinTemporaryTestPage'
00047     variable = u'@USERNAME@'
00048 
00049     def setup_method(self, method):
00050         self.page = PageEditor(self.request, self.pagename)
00051         self.savedName = self.request.user.name
00052         self.request.user.name = self.name
00053 
00054     def teardown_method(self, method):
00055         self.request.user.name = self.savedName
00056 
00057     def expand(self):
00058         return self.page._expand_variables(self.variable)
00059 
00060 
00061 class TestExpandCamelCaseName(TestExpandUserName):
00062 
00063     name = u'UserName'
00064 
00065     def testExpandCamelCaseUserName(self):
00066         """ PageEditor: expand @USERNAME@ CamelCase """
00067         assert self.expand() == self.name
00068 
00069 
00070 class TestExpandExtendedName(TestExpandUserName):
00071 
00072     name = u'user name'
00073 
00074     def testExtendedNamesEnabled(self):
00075         """ PageEditor: expand @USERNAME@ extended name - enabled """
00076         assert self.expand() == u'[[%s]]' % self.name
00077 
00078 
00079 class TestExpandMailto(TestExpandUserName):
00080 
00081     variable = u'@MAILTO@'
00082     name = u'user name'
00083     email = 'user@example.com'
00084 
00085     def setup_method(self, method):
00086         super(TestExpandMailto, self).setup_method(method)
00087         self.savedValid = self.request.user.valid
00088         self.request.user.valid = 1
00089         self.savedEmail = self.request.user.email
00090         self.request.user.email = self.email
00091 
00092     def teardown_method(self, method):
00093         super(TestExpandMailto, self).teardown_method(method)
00094         self.request.user.valid = self.savedValid
00095         self.request.user.email = self.savedEmail
00096 
00097     def testMailto(self):
00098         """ PageEditor: expand @MAILTO@ """
00099         assert self.expand() == u'<<MailTo(%s)>>' % self.email
00100 
00101 
00102 class TestExpandPrivateVariables(TestExpandUserName):
00103 
00104     variable = u'@ME@'
00105     name = u'AutoCreatedMoinMoinTemporaryTestUser'
00106     dictPage = name + '/MyDict'
00107     shouldDeleteTestPage = True
00108 
00109     def setup_method(self, method):
00110         super(TestExpandPrivateVariables, self).setup_method(method)
00111         self.savedValid = self.request.user.valid
00112         self.request.user.valid = 1
00113         self.createTestPage()
00114         self.deleteCaches()
00115 
00116     def teardown_method(self, method):
00117         super(TestExpandPrivateVariables, self).teardown_method(method)
00118         self.request.user.valid = self.savedValid
00119         self.deleteTestPage()
00120 
00121     def testPrivateVariables(self):
00122         """ PageEditor: expand user variables """
00123         assert self.expand() == self.name
00124 
00125     def createTestPage(self):
00126         """ Create temporary page, bypass logs, notification and backups
00127 
00128         TODO: this code is very fragile, any change in the
00129         implementation will break this test. Need to factor PageEditor
00130         to make it possible to create page without loging and notifying.
00131         """
00132         import os
00133         path = self.dictPagePath()
00134         if os.path.exists(path):
00135             self.shouldDeleteTestPage = False
00136             py.test.skip("%s exists. Won't overwrite exiting page" % self.dictPage)
00137         try:
00138             os.mkdir(path)
00139             revisionsDir = os.path.join(path, 'revisions')
00140             os.mkdir(revisionsDir)
00141             current = '00000001'
00142             file(os.path.join(path, 'current'), 'w').write('%s\n' % current)
00143             text = u' ME:: %s\n' % self.name
00144             file(os.path.join(revisionsDir, current), 'w').write(text)
00145         except Exception, err:
00146             py.test.skip("Can not be create test page: %s" % err)
00147 
00148     def deleteCaches(self):
00149         """ Force the wiki to scan the test page into the dicts """
00150         # New dicts does not require cache refresh.
00151 
00152     def deleteTestPage(self):
00153         """ Delete temporary page, bypass logs and notifications """
00154         if self.shouldDeleteTestPage:
00155             import shutil
00156             shutil.rmtree(self.dictPagePath(), True)
00157 
00158     def dictPagePath(self):
00159         page = Page(self.request, self.dictPage)
00160         return page.getPagePath(use_underlay=0, check_create=0)
00161 
00162 
00163 class TestSave(object):
00164 
00165     def setup_method(self, method):
00166         self.old_handlers = self.request.cfg.event_handlers
00167         become_trusted(self.request)
00168 
00169     def teardown_method(self, method):
00170         self.request.cfg.event_handlers = self.old_handlers
00171         nuke_page(self.request, u'AutoCreatedMoinMoinTemporaryTestPageFortestSave')
00172 
00173     def testSaveAbort(self):
00174         """Test if saveText() is interrupted if PagePreSave event handler returns Abort"""
00175 
00176         def handler(event):
00177             from MoinMoin.events import Abort
00178             return Abort("This is just a test")
00179 
00180         pagename = u'AutoCreatedMoinMoinTemporaryTestPageFortestSave'
00181         testtext = u'ThisIsSomeStupidTestPageText!'
00182 
00183         self.request.cfg.event_handlers = [handler]
00184 
00185         page = Page(self.request, pagename)
00186         if page.exists():
00187             deleter = PageEditor(self.request, pagename)
00188             deleter.deletePage()
00189 
00190         editor = PageEditor(self.request, pagename)
00191         editor.saveText(testtext, 0)
00192 
00193         print "PageEditor can't save a page if Abort is returned from PreSave event handlers"
00194         page = Page(self.request, pagename)
00195         assert page.body != testtext
00196 
00197 
00198 class TestDictPageDeletion(object):
00199 
00200     def testCreateDictAndDeleteDictPage(self):
00201         """
00202         simple test if it is possible to delete a Dict page after creation
00203         """
00204         become_trusted(self.request)
00205         pagename = u'SomeDict'
00206         page = PageEditor(self.request, pagename, do_editor_backup=0)
00207         body = u"This is an example text"
00208         page.saveText(body, 0)
00209 
00210         success_i, result = page.deletePage()
00211 
00212         expected = u'Page "SomeDict" was successfully deleted!'
00213 
00214         assert result == expected
00215 
00216 class TestCopyPage(object):
00217 
00218     pagename = u'AutoCreatedMoinMoinTemporaryTestPage'
00219     copy_pagename = u'AutoCreatedMoinMoinTemporaryCopyTestPage'
00220     shouldDeleteTestPage = True
00221     text = u'Example'
00222 
00223     def setup_method(self, method):
00224         self.savedValid = self.request.user.valid
00225         self.request.user.valid = 1
00226 
00227     def teardown_method(self, method):
00228         self.request.user.valid = self.savedValid
00229         self.deleteTestPage()
00230 
00231     def createTestPage(self):
00232         """ Create temporary page, bypass logs, notification and backups
00233 
00234         TODO: this code is very fragile, any change in the
00235         implementation will break this test. Need to factor PageEditor
00236         to make it possible to create page without loging and notifying.
00237         """
00238         import os
00239         path = Page(self.request, self.pagename).getPagePath(check_create=0)
00240         copy_path = Page(self.request, self.copy_pagename).getPagePath(check_create=0)
00241 
00242         if os.path.exists(path) or os.path.exists(copy_path):
00243             self.shouldDeleteTestPage = False
00244             py.test.skip("%s or %s exists. Won't overwrite exiting page" % (self.pagename, self.copy_pagename))
00245         try:
00246             os.mkdir(path)
00247             revisionsDir = os.path.join(path, 'revisions')
00248             os.mkdir(revisionsDir)
00249             current = '00000001'
00250             file(os.path.join(path, 'current'), 'w').write('%s\n' % current)
00251 
00252             file(os.path.join(revisionsDir, current), 'w').write(self.text)
00253         except Exception, err:
00254             py.test.skip("Can not be create test page: %s" % err)
00255 
00256     def deleteTestPage(self):
00257         """ Delete temporary page, bypass logs and notifications """
00258         if self.shouldDeleteTestPage:
00259             import shutil
00260             shutil.rmtree(Page(self.request, self.pagename).getPagePath(), True)
00261             shutil.rmtree(Page(self.request, self.copy_pagename).getPagePath(), True)
00262 
00263     def test_copy_page(self):
00264         """
00265         Tests copying a page without restricted acls
00266         """
00267         self.createTestPage()
00268         result, msg = PageEditor(self.request, self.pagename).copyPage(self.copy_pagename)
00269         revision = Page(self.request, self.copy_pagename).current_rev()
00270         assert result and revision is 2
00271 
00272     def test_copy_page_acl_read(self):
00273         """
00274         Tests copying a page without write rights
00275         """
00276         self.text = u'#acl SomeUser:read,write,delete All:read\n'
00277         self.createTestPage()
00278         result, msg = PageEditor(self.request, self.pagename).copyPage(self.copy_pagename)
00279         revision = Page(self.request, self.copy_pagename).current_rev()
00280         assert result and revision is 2
00281 
00282     def test_copy_page_acl_no_read(self):
00283         """
00284         Tests copying a page without read rights
00285         """
00286         self.text = u'#acl SomeUser:read,write,delete All:\n'
00287         self.createTestPage()
00288         result, msg = PageEditor(self.request, self.pagename).copyPage(self.copy_pagename)
00289         revision = Page(self.request, self.copy_pagename).current_rev()
00290         assert result and revision is 2
00291 
00292 coverage_modules = ['MoinMoin.PageEditor']