view MoinMoin/_tests/ @ 1918:f0b346ac6338

added return_rev param to store_revision() to avoid returning a revision object without need
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 20 Jan 2013 23:36:37 +0100
parents 1972c90b0201
children 2bd864ed640e
line wrap: on
line source
# Copyright: 2007 MoinMoin:KarolNowak
# Copyright: 2008 MoinMoin:ThomasWaldmann
# Copyright: 2008, 2010 MoinMoin:ReimarBauer
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

    MoinMoin - some common code for testing

import os, shutil
import socket, errno
from StringIO import StringIO

from flask import current_app as app
from flask import g as flaskg

from MoinMoin import config, security, user
from MoinMoin.config import NAME, CONTENTTYPE
from MoinMoin.items import Item
from MoinMoin.util.crypto import random_string
from import ItemAlreadyExistsError

# Promoting the test user -------------------------------------------
# Usually the tests run as anonymous user, but for some stuff, you
# need more privs...

def become_valid(username=u"ValidUser"):
    """ modify flaskg.user to make the user valid.
        Note that a valid user will only be in ACL special group "Known", if
        we have a user profile for this user as the ACL system will check if
        there is a userid for this username.
        Thus, for testing purposes (e.g. if you need delete rights), it is
        easier to use become_trusted().
    flaskg.user.profile[NAME] = username = username
    flaskg.user.valid = 1

def become_trusted(username=u"TrustedUser"):
    """ modify flaskg.user to make the user valid and trusted, so it is in acl group Trusted """
    flaskg.user.trusted = True

# Creating and destroying test items --------------------------------

def update_item(name, meta, data):
    """ creates or updates an item  """
    if isinstance(data, unicode):
        data = data.encode(config.charset)
    item =[name]

    meta = meta.copy()
    if NAME not in meta:
        meta[NAME] = name
    if CONTENTTYPE not in meta:
        meta[CONTENTTYPE] = u'application/octet-stream'
    rev = item.store_revision(meta, StringIO(data), return_rev=True)
    return rev

def create_random_string_list(length=14, count=10):
    """ creates a list of random strings """
    chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    return [u"{0}".format(random_string(length, chars)) for counter in range(count)]

def nuke_item(name):
    """ complete destroys an item """
    item = Item.create(name)

def check_connection(port, host=''):
    Check if we can make a connection to host:port.

    If not, raise Exception with a meaningful msg.
        s = socket.create_connection((host, port))
    except socket.error as err:
        raise Exception("connecting to {0}:{1:d}, error: {2!s}".format(host, port, err))