view MoinMoin/_tests/ @ 1974:87272c032485

remove unused imports
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 10 Feb 2013 23:54:13 +0100
parents 286e315935ae
children 384555088cab
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 socket
from StringIO import StringIO

from flask import g as flaskg

from MoinMoin.constants.contenttypes import CHARSET
from MoinMoin.constants.keys import NAME, CONTENTTYPE
from MoinMoin.items import Item
from MoinMoin.util.crypto import random_string

# 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, ]
    flaskg.user.may.names = [username, ]  # see security.DefaultSecurityPolicy class
    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(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))