~launchpad-pqm/launchpad/devel

11626.3.7 by Curtis Hovey
Fixed copyright dates.
1
# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
8687.15.18 by Karl Fogel
Add the copyright header block to files under lib/canonical/.
2
# GNU Affero General Public License version 3 (see the file LICENSE).
6743.1.6 by Maris Fogels
Pulled the summarise_tal_links() function out into a test helpers file.
3
4
"""Helpers for testing menus."""
5
6
__metaclass__ = type
7
__all__ = [
8
    'summarise_tal_links',
6767.5.40 by Maris Fogels
Rework based on reviewer feedback.
9
    'make_fake_request',
6743.1.6 by Maris Fogels
Pulled the summarise_tal_links() function out into a test helpers file.
10
    ]
11
13931.2.1 by Steve Kowalik
Chip away at canonical.lazr a little more.
12
from lazr.restful.utils import safe_hasattr
6743.1.6 by Maris Fogels
Pulled the summarise_tal_links() function out into a test helpers file.
13
from zope.security.proxy import isinstance as zope_isinstance
6767.5.35 by Maris Fogels
Extracted the make_fake_request() helper into the lazr menu helpers module.
14
from zope.security.management import endInteraction, newInteraction
6743.1.6 by Maris Fogels
Pulled the summarise_tal_links() function out into a test helpers file.
15
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
16
from lp.services.webapp import urlsplit
17
from lp.services.webapp.interfaces import ILink
18
from lp.services.webapp.servers import LaunchpadTestRequest
6743.1.6 by Maris Fogels
Pulled the summarise_tal_links() function out into a test helpers file.
19
6767.5.40 by Maris Fogels
Rework based on reviewer feedback.
20
6743.1.6 by Maris Fogels
Pulled the summarise_tal_links() function out into a test helpers file.
21
def summarise_tal_links(links):
22
    """List the links and their attributes in the dict or list.
23
24
    :param links: A dictionary or list of menu links returned by
11626.3.2 by Curtis Hovey
Move tales gto lp.app.
25
        `lp.app.browser.tales.MenuAPI`.
6743.1.6 by Maris Fogels
Pulled the summarise_tal_links() function out into a test helpers file.
26
    """
27
    is_dict = zope_isinstance(links, dict)
28
    if is_dict:
29
        keys = sorted(links)
30
    else:
31
        keys = links
9101.1.1 by Tim Penhey
Menu changes.
32
    for key in keys:
6743.1.6 by Maris Fogels
Pulled the summarise_tal_links() function out into a test helpers file.
33
        if is_dict:
9101.1.1 by Tim Penhey
Menu changes.
34
            link = links[key]
35
        else:
36
            link = key
37
        if ILink.providedBy(link):
38
            print 'link %s' % link.name
39
            attributes = ('url', 'enabled', 'menu', 'selected', 'linked')
40
            for attrname in attributes:
41
                if not safe_hasattr(link, attrname):
42
                    continue
43
                print '    %s:' % attrname, getattr(link, attrname)
44
        else:
45
            print 'attribute %s: %s' % (key, link)
6767.5.35 by Maris Fogels
Extracted the make_fake_request() helper into the lazr menu helpers module.
46
6767.5.40 by Maris Fogels
Rework based on reviewer feedback.
47
6767.5.35 by Maris Fogels
Extracted the make_fake_request() helper into the lazr menu helpers module.
48
def make_fake_request(url, traversed_objects=None):
49
    """Return a fake request object for menu testing.
50
51
    :param traversed_objects: A list of objects that becomes the request's
52
        traversed_objects attribute.
53
    """
54
    url_parts = urlsplit(url)
55
    server_url = '://'.join(url_parts[0:2])
56
    path_info = url_parts[2]
57
    request = LaunchpadTestRequest(
58
        SERVER_URL=server_url,
59
        PATH_INFO=path_info)
60
    request._traversed_names = path_info.split('/')[1:]
9087.4.1 by Guilherme Salgado
crackful attempt, using adapters
61
    if traversed_objects is not None:
9322.10.15 by Guilherme Salgado
Fix a bunch of things
62
        request.traversed_objects = traversed_objects[:]
6767.5.35 by Maris Fogels
Extracted the make_fake_request() helper into the lazr menu helpers module.
63
    # After making the request, setup a new interaction.
64
    endInteraction()
65
    newInteraction(request)
66
    return request