~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/testing/tests/test_publication.py

[r=gary][ui=none][bug=611570] Add tests for the test_traverse helper
        and make it set up its own interaction properly

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Tests for the helpers in `lp.testing.publication`."""
 
5
 
 
6
__metaclass__ = type
 
7
 
 
8
from zope.app.pagetemplate.simpleviewclass import simple
 
9
from zope.component import getSiteManager, getUtility
 
10
from zope.interface import Interface
 
11
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
 
12
from zope.security.checker import CheckerPublic, Checker, defineChecker
 
13
 
 
14
from canonical.launchpad.interfaces.launchpad import ILaunchpadRoot
 
15
from canonical.launchpad.webapp.interfaces import ILaunchBag
 
16
from canonical.launchpad.webapp.publisher import get_current_browser_request
 
17
from canonical.launchpad.webapp.servers import LaunchpadTestRequest
 
18
from canonical.testing import DatabaseFunctionalLayer
 
19
from lp.testing import ANONYMOUS, login, login_person, TestCaseWithFactory
 
20
from lp.testing.publication import test_traverse
 
21
 
 
22
 
 
23
class TestTestTraverse(TestCaseWithFactory):
 
24
    # Tests for `test_traverse`
 
25
 
 
26
    layer = DatabaseFunctionalLayer
 
27
 
 
28
    def registerViewCallable(self, view_callable):
 
29
        """Return a URL traversing to which will call `view_callable`.
 
30
 
 
31
        :param view_callable: Will be called with no arguments during
 
32
            traversal.
 
33
        """
 
34
        # This method is completely out of control.  Thanks, Zope.
 
35
        name = '+' + self.factory.getUniqueString()
 
36
        class new_class(simple):
 
37
            def __init__(self, context, request):
 
38
                view_callable()
 
39
        required = {}
 
40
        for n in ('browserDefault', '__call__', 'publishTraverse'):
 
41
            required[n] = CheckerPublic
 
42
        defineChecker(new_class, Checker(required))
 
43
        getSiteManager().registerAdapter(
 
44
            new_class, (ILaunchpadRoot, IDefaultBrowserLayer), Interface, 
 
45
            name)
 
46
        self.addCleanup(
 
47
            getSiteManager().unregisterAdapter, new_class,
 
48
            (ILaunchpadRoot, IDefaultBrowserLayer), Interface, name)
 
49
        return 'https://launchpad.dev/' + name
 
50
 
 
51
    def test_traverse_simple(self):
 
52
        # test_traverse called with a product URL returns the product
 
53
        # as the traversed object.
 
54
        login(ANONYMOUS)
 
55
        product = self.factory.makeProduct()
 
56
        context, view, request = test_traverse(
 
57
            'https://launchpad.dev/' + product.name)
 
58
        self.assertEqual(product, context)
 
59
 
 
60
    def test_request_is_current_during_traversal(self):
 
61
        # The request that test_traverse creates is current during
 
62
        # traversal in the sense of get_current_browser_request.
 
63
        login(ANONYMOUS)
 
64
        requests = []
 
65
        def record_current_request():
 
66
            requests.append(get_current_browser_request())
 
67
        context, view, request = test_traverse(
 
68
            self.registerViewCallable(record_current_request))
 
69
        self.assertEqual(1, len(requests))
 
70
        self.assertIs(request, requests[0])
 
71
 
 
72
    def test_participation_restored(self):
 
73
        # test_traverse restores the interaction (and hence
 
74
        # participation) that was present before it was called.
 
75
        request = LaunchpadTestRequest()
 
76
        login(ANONYMOUS, request)
 
77
        product = self.factory.makeProduct()
 
78
        test_traverse('https://launchpad.dev/' + product.name)
 
79
        self.assertIs(request, get_current_browser_request())
 
80
 
 
81
    def test_uses_current_user(self):
 
82
        # test_traverse performs the traversal as the currently logged
 
83
        # in user.
 
84
        person = self.factory.makePerson()
 
85
        login_person(person)
 
86
        users = []
 
87
        def record_user():
 
88
            users.append(getUtility(ILaunchBag).user)
 
89
        context, view, request = test_traverse(
 
90
            self.registerViewCallable(record_user))
 
91
        self.assertEqual(1, len(users))
 
92
        self.assertEqual(person, users[0])