Back to index

moin  1.9.0~rc2
test_packages.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - MoinMoin.packages tests
00004 
00005     @copyright: 2005 MoinMoin:AlexanderSchremmer,
00006                 2007 Federico Lorenzi
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 import os
00011 import py
00012 import tempfile
00013 import zipfile
00014 
00015 from datetime import datetime
00016 from MoinMoin import user, wikiutil
00017 from MoinMoin.action import AttachFile
00018 from MoinMoin.action.PackagePages import PackagePages
00019 from MoinMoin.packages import Package, ScriptEngine, MOIN_PACKAGE_FILE, ZipPackage, packLine, unpackLine
00020 from MoinMoin._tests import become_trusted, become_superuser, create_page, nuke_page
00021 from MoinMoin.Page import Page
00022 from MoinMoin.PageEditor import PageEditor
00023 
00024 
00025 
00026 class DebugPackage(Package, ScriptEngine):
00027     """ Used for debugging, does not need a real .zip file. """
00028 
00029     def __init__(self, request, filename, script=None):
00030         Package.__init__(self, request)
00031         ScriptEngine.__init__(self)
00032         self.filename = filename
00033         self.script = script or u"""moinmoinpackage|1
00034 print|foo
00035 ReplaceUnderlay|testdatei|TestSeite2
00036 IgnoreExceptions|True
00037 DeletePage|TestSeiteDoesNotExist|Test ...
00038 DeletePage|FooPage|Test ...
00039 IgnoreExceptions|False
00040 AddRevision|foofile|FooPage
00041 AddRevision|foofile|FooPage
00042 setthemename|foo
00043 #foobar
00044 installplugin|foo|local|parser|testy
00045 """
00046 
00047     def extract_file(self, filename):
00048         if filename == MOIN_PACKAGE_FILE:
00049             return self.script.encode("utf-8")
00050         else:
00051             return "Hello world, I am the file " + filename.encode("utf-8")
00052 
00053     def filelist(self):
00054         return [MOIN_PACKAGE_FILE, "foo"]
00055 
00056     def isPackage(self):
00057         return True
00058 
00059 
00060 class TestUnsafePackage:
00061     """ Tests various things in the packages package. Note that this package does
00062         not care to clean up and needs to run in a test wiki because of that. """
00063 
00064     def setup_class(self):
00065         if not getattr(self.request.cfg, 'is_test_wiki', False):
00066             py.test.skip('This test needs to be run using the test wiki.')
00067 
00068     def teardown_class(self):
00069         DebugPackage(self.request, u"""moinmoinpackage|1
00070 DeletePage|FooPage|Test ...
00071 """).installPackage()
00072 
00073     def testBasicPackageThings(self):
00074         become_superuser(self.request)
00075         myPackage = DebugPackage(self.request, 'test')
00076         myPackage.installPackage()
00077         assert myPackage.msg == u'foo\nFooPage added \n'
00078         testseite2 = Page(self.request, 'TestSeite2')
00079         assert testseite2.getPageText() == "Hello world, I am the file testdatei"
00080         assert testseite2.isUnderlayPage()
00081 
00082 
00083 class TestQuoting:
00084 
00085     def testQuoting(self):
00086         for line in ([':foo', 'is\\', 'ja|', u't|�', u'baAz�'], [], ['', '']):
00087             assert line == unpackLine(packLine(line))
00088 
00089 
00090 class TestRealCreation:
00091 
00092     def testSearch(self):
00093         package = PackagePages(self.request.rootpage.page_name, self.request)
00094         assert package.searchpackage(self.request, "title:BadCon") == [u'BadContent']
00095 
00096     def testListCreate(self):
00097         package = PackagePages(self.request.rootpage.page_name, self.request)
00098         temp = tempfile.NamedTemporaryFile(suffix='.zip')
00099         package.collectpackage(['FrontPage'], temp)
00100         assert zipfile.is_zipfile(temp.name)
00101 
00102     def testAllCreate(self):
00103         package = PackagePages(self.request.rootpage.page_name, self.request)
00104         temp = tempfile.NamedTemporaryFile(suffix='.zip')
00105         package.collectpackage(self.request.rootpage.getPageList(
00106                                 include_underlay=False,
00107                                 filter=lambda name: not wikiutil.isSystemPage(self.request, name)),
00108                                 temp)
00109         if not package:
00110             py.test.skip("No user created pages in wiki!")
00111         assert zipfile.is_zipfile(temp.name)
00112 
00113     def testInvalidCreate(self):
00114         package = PackagePages(self.request.rootpage.page_name, self.request)
00115         temp = tempfile.NamedTemporaryFile(suffix='.zip')
00116         package.collectpackage(['___//THIS PAGE SHOULD NOT EXIST\\___'], temp)
00117         assert not zipfile.is_zipfile(temp.name)
00118 
00119 
00120 class TestRealPackageInstallation:
00121 
00122 
00123     def create_package(self, script, page=None):
00124         # creates the package example zip file
00125         userid = user.getUserIdentification(self.request)
00126         COMPRESSION_LEVEL = zipfile.ZIP_DEFLATED
00127         zip_file = tempfile.mkstemp(suffix='.zip')[1]
00128         zf = zipfile.ZipFile(zip_file, "w", COMPRESSION_LEVEL)
00129         if page:
00130             timestamp = wikiutil.version2timestamp(page.mtime_usecs())
00131             zi = zipfile.ZipInfo(filename="1", date_time=datetime.fromtimestamp(timestamp).timetuple()[:6])
00132             zi.compress_type = COMPRESSION_LEVEL
00133             zf.writestr(zi, page.get_raw_body().encode("utf-8"))
00134         zf.writestr("1_attachment", "sample attachment")
00135         zf.writestr(MOIN_PACKAGE_FILE, script.encode("utf-8"))
00136         zf.close()
00137         return zip_file
00138 
00139     def testAttachments_after_page_creation(self):
00140         become_trusted(self.request)
00141         pagename = u'PackageTestPageCreatedFirst'
00142         page = create_page(self.request, pagename, u"This page has not yet an attachments dir")
00143         script = u"""MoinMoinPackage|1
00144 AddRevision|1|%(pagename)s
00145 AddAttachment|1_attachment|my_test.txt|%(pagename)s
00146 Print|Thank you for using PackagePages!
00147 """ % {"pagename": pagename}
00148         zip_file = self.create_package(script, page)
00149         package = ZipPackage(self.request, zip_file)
00150         package.installPackage()
00151         assert Page(self.request, pagename).exists()
00152         assert AttachFile.exists(self.request, pagename, "my_test.txt")
00153 
00154         nuke_page(self.request, pagename)
00155         os.unlink(zip_file)
00156 
00157     def testAttachments_without_page_creation(self):
00158         become_trusted(self.request)
00159         pagename = u"PackageAttachmentAttachWithoutPageCreation"
00160         script = u"""MoinMoinPackage|1
00161 AddAttachment|1_attachment|my_test.txt|%(pagename)s
00162 Print|Thank you for using PackagePages!
00163 """ % {"pagename": pagename}
00164         zip_file = self.create_package(script)
00165         package = ZipPackage(self.request, zip_file)
00166         package.installPackage()
00167         assert not Page(self.request, pagename).exists()
00168         assert AttachFile.exists(self.request, pagename, "my_test.txt")
00169 
00170         nuke_page(self.request, pagename)
00171         os.unlink(zip_file)
00172 
00173 
00174 coverage_modules = ['MoinMoin.packages']
00175