Back to index

moin  1.9.0~rc2
__init__.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - some common code for testing
00004 
00005     @copyright: 2007 MoinMoin:KarolNowak,
00006                 2008 MoinMoin:ThomasWaldmann, MoinMoin:ReimarBauer
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 import os, shutil
00011 
00012 from MoinMoin.parser.text import Parser
00013 from MoinMoin.formatter.text_html import Formatter
00014 from MoinMoin.Page import Page
00015 from MoinMoin.PageEditor import PageEditor
00016 from MoinMoin.util import random_string
00017 from MoinMoin import caching, user
00018 from MoinMoin.action import AttachFile
00019 
00020 # Promoting the test user -------------------------------------------
00021 # Usually the tests run as anonymous user, but for some stuff, you
00022 # need more privs...
00023 
00024 def become_valid(request, username=u"ValidUser"):
00025     """ modify request.user to make the user valid.
00026         Note that a valid user will only be in ACL special group "Known", if
00027         we have a user profile for this user as the ACL system will check if
00028         there is a userid for this username.
00029         Thus, for testing purposes (e.g. if you need delete rights), it is
00030         easier to use become_trusted().
00031     """
00032     request.user.name = username
00033     request.user.may.name = username
00034     request.user.valid = 1
00035 
00036 
00037 def become_trusted(request, username=u"TrustedUser"):
00038     """ modify request.user to make the user valid and trusted, so it is in acl group Trusted """
00039     become_valid(request, username)
00040     request.user.auth_method = request.cfg.auth_methods_trusted[0]
00041 
00042 
00043 def become_superuser(request):
00044     """ modify request.user so it is in the superuser list,
00045         also make the user valid (see notes in become_valid()),
00046         also make the user trusted (and thus in "Trusted" ACL pseudo group).
00047 
00048         Note: being superuser is completely unrelated to ACL rights,
00049               especially it is not related to ACL admin rights.
00050     """
00051     su_name = u"SuperUser"
00052     become_trusted(request, su_name)
00053     if su_name not in request.cfg.superuser:
00054         request.cfg.superuser.append(su_name)
00055 
00056 def nuke_user(request, username):
00057     """ completely delete a user """
00058     user_dir = request.cfg.user_dir
00059     user_id = user.getUserId(request, username)
00060     # really get rid of the user
00061     fpath = os.path.join(user_dir, user_id)
00062     os.remove(fpath)
00063     # delete cache
00064     arena = 'user'
00065     key = 'name2id'
00066     caching.CacheEntry(request, arena, key, scope='wiki').remove()
00067 
00068 # Creating and destroying test pages --------------------------------
00069 
00070 def create_page(request, pagename, content, do_editor_backup=False):
00071     """ create a page with some content """
00072     # make sure there is nothing already there:
00073     nuke_page(request, pagename)
00074     # now create from scratch:
00075     page = PageEditor(request, pagename, do_editor_backup=do_editor_backup)
00076     page.saveText(content, 0)
00077     return page
00078 
00079 def append_page(request, pagename, content, do_editor_backup=False):
00080     """ appends some conetent to an existing page """
00081     # reads the raw text of the existing page
00082     raw = Page(request, pagename).get_raw_body()
00083     # adds the new content to the old
00084     content = "%s\n%s\n"% (raw, content)
00085     page = PageEditor(request, pagename, do_editor_backup=do_editor_backup)
00086     page.saveText(content, 0)
00087     return page
00088 
00089 def nuke_eventlog(request):
00090     """ removes event-log file """
00091     fpath = request.rootpage.getPagePath('event-log', isfile=1)
00092     if os.path.exists(fpath):
00093         os.remove(fpath)
00094 
00095 def nuke_page(request, pagename):
00096     """ completely delete a page, everything in the pagedir """
00097     attachments = AttachFile._get_files(request, pagename)
00098     for attachment in attachments:
00099         AttachFile.remove_attachment(request, pagename, attachment)
00100     page = PageEditor(request, pagename, do_editor_backup=False)
00101     page.deletePage()
00102     # really get rid of everything there:
00103     fpath = page.getPagePath(check_create=0)
00104     shutil.rmtree(fpath, True)
00105 
00106 def create_random_string_list(length=14, count=10):
00107     """ creates a list of random strings """
00108     chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
00109     return [u"%s" % random_string(length, chars) for counter in range(count)]
00110 
00111 def make_macro(request, page):
00112     """ creates the macro """
00113     from MoinMoin import macro
00114     p = Parser("##\n", request)
00115     p.formatter = Formatter(request)
00116     p.formatter.page = page
00117     request.page = page
00118     request.formatter = p.formatter
00119     p.form = request.form
00120     m = macro.Macro(p)
00121     return m
00122 
00123 def nuke_xapian_index(request):
00124     """ completely delete everything in xapian index dir """
00125     fpath = os.path.join(request.cfg.cache_dir, 'xapian')
00126     if os.path.exists(fpath):
00127         shutil.rmtree(fpath, True)