1
# Copyright 2010 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for the helpers in `lp.testing.publication`."""
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
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
23
class TestTestTraverse(TestCaseWithFactory):
24
# Tests for `test_traverse`
26
layer = DatabaseFunctionalLayer
28
def registerViewCallable(self, view_callable):
29
"""Return a URL traversing to which will call `view_callable`.
31
:param view_callable: Will be called with no arguments during
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):
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,
47
getSiteManager().unregisterAdapter, new_class,
48
(ILaunchpadRoot, IDefaultBrowserLayer), Interface, name)
49
return 'https://launchpad.dev/' + name
51
def test_traverse_simple(self):
52
# test_traverse called with a product URL returns the product
53
# as the traversed object.
55
product = self.factory.makeProduct()
56
context, view, request = test_traverse(
57
'https://launchpad.dev/' + product.name)
58
self.assertEqual(product, context)
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.
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])
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())
81
def test_uses_current_user(self):
82
# test_traverse performs the traversal as the currently logged
84
person = self.factory.makePerson()
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])