~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Functions to help with the testing of views."""

__metaclass__ = type
__all__ = [
    'create_view',
    'create_initialized_view',
    'YUITestFileView',
    ]

import os

from zope.component import getUtility, getMultiAdapter
from zope.security.management import endInteraction, newInteraction

from canonical.config import config
from canonical.lazr import ExportedFolder
from canonical.launchpad.layers import setFirstLayer
from canonical.launchpad.webapp.interfaces import IPlacelessAuthUtility
from canonical.launchpad.webapp.servers import LaunchpadTestRequest


def create_view(context, name, form=None, layer=None, server_url=None,
                method='GET', principal=None, query_string='', cookie='',
                request=None, path_info='/', current_request=False, **kwargs):
    """Return a view based on the given arguments.

    :param context: The context for the view.
    :param name: The web page the view should handle.
    :param form: A dictionary with the form keys.
    :param layer: The layer where the page we are interested in is located.
    :param server_url: The URL from where this request was done.
    :param method: The method used in the request. Defaults to 'GET'.
    :param principal: The principal for the request, default to the
        unauthenticated principal.
    :param query_string: The query string for the request.
    :patam cookie: The HTTP_COOKIE value for the request.
    :param request: Use this request instead of creating a new one.
    :param path_info: The PATH_INFO value for the request.
    :param current_request: If True, the request will be set as the current
        interaction.
    :param **kwargs: Any other parameter for the request.
    :return: The view class for the given context and the name.
    """
    if request is None:
        request = LaunchpadTestRequest(
            form=form, SERVER_URL=server_url, QUERY_STRING=query_string,
            HTTP_COOKIE=cookie, method=method, PATH_INFO=path_info, **kwargs)
    if principal is not None:
        request.setPrincipal(principal)
    else:
        request.setPrincipal(
            getUtility(IPlacelessAuthUtility).unauthenticatedPrincipal())
    if layer is not None:
        setFirstLayer(request, layer)
    if current_request:
        endInteraction()
        newInteraction(request)
    return getMultiAdapter((context, request), name=name)


def create_initialized_view(context, name, form=None, layer=None,
                            server_url=None, method=None, principal=None,
                            query_string=None, cookie=None, request=None,
                            path_info='/'):
    """Return a view that has already been initialized."""
    if method is None:
        if form is None:
            method = 'GET'
        else:
            method = 'POST'
    view = create_view(
        context, name, form, layer, server_url, method, principal,
        query_string, cookie, request, path_info)
    view.initialize()
    return view


class YUITestFileView(ExportedFolder):
    """Export the lib directory where the test assets reside."""

    folder = os.path.join(config.root, 'lib/')
    export_subdirectories = True