1
# Copyright 2009 Canonical Ltd. This software is licensed under the
1
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
4
"""Tests for traversal from the root branch object."""
10
10
from zope.component import getUtility
11
11
from zope.publisher.interfaces import NotFound
12
from zope.security.interfaces import Unauthorized
12
13
from zope.security.proxy import removeSecurityProxy
14
15
from canonical.launchpad.interfaces.account import AccountStatus
15
from canonical.launchpad.webapp import canonical_url
16
from canonical.launchpad.webapp.interfaces import BrowserNotificationLevel
16
from canonical.launchpad.webapp import (
19
from canonical.launchpad.webapp.interfaces import (
20
BrowserNotificationLevel,
17
23
from canonical.launchpad.webapp.servers import LaunchpadTestRequest
18
24
from canonical.launchpad.webapp.url import urlappend
19
25
from canonical.testing.layers import DatabaseFunctionalLayer
93
100
self.assertEqual(url, redirection.target)
95
102
def traverse(self, path, first_segment, use_default_referer=True):
96
"""Traverse to 'segments' using a 'LaunchpadRootNavigation' object.
103
"""Traverse to 'path' using a 'LaunchpadRootNavigation' object.
98
105
Using the Zope traversal machinery, traverse to the path given by
99
106
'segments', starting at a `LaunchpadRootNavigation` object.
101
:param segments: A list of path segments.
108
CAUTION: Prefer test_traverse to this method, because it correctly
109
establishes the global request.
111
:param path: A slash-delimited path.
102
112
:param use_default_referer: If True, set the referer attribute in the
103
113
request header to DEFAULT_REFERER = "http://launchpad.dev"
104
114
(otherwise it remains as None)
105
115
:return: The object found.
117
# XXX: What's the difference between first_segment and path? -- mbp
107
119
extra = {'PATH_INFO': urlappend('/%s' % first_segment, path)}
108
120
if use_default_referer:
109
121
extra['HTTP_REFERER'] = DEFAULT_REFERER
110
122
request = LaunchpadTestRequest(**extra)
111
123
segments = reversed(path.split('/'))
112
124
request.setTraversalStack(segments)
113
traverser = LaunchpadRootNavigation(None, request=request)
125
traverser = LaunchpadRootNavigation(
126
getUtility(ILaunchpadRoot), request=request)
114
127
return traverser.publishTraverse(request, first_segment)
368
381
login_person(self.any_user)
369
382
self.assertRaises(NotFound, self.traverse, segment, segment)
384
def test_self_url_traversal(self):
385
# Just /~/ expands to the current user. (Bug 785800).
386
person = self.factory.makePerson()
388
obj, view, req = test_traverse('http://launchpad.dev/~')
389
view = removeSecurityProxy(view)
391
canonical_url(person),
392
view.target.rstrip('/'))
394
def test_self_url_not_logged_in(self):
395
# /~/ when not logged in asks you to log in.
396
self.assertRaises(Unauthorized,
397
test_traverse, 'http://launchpad.dev/~')
399
def test_self_url_pathinfo(self):
400
# You can traverse below /~/.
401
person = self.factory.makePerson()
403
obj, view, req = test_traverse('http://launchpad.dev/~/+editsshkeys')
404
view = removeSecurityProxy(view)
406
canonical_url(person) + '/+editsshkeys',
409
def test_self_url_app_domain(self):
410
# You can traverse below /~/.
411
person = self.factory.makePerson()
413
obj, view, req = test_traverse('http://bugs.launchpad.dev/~')
414
view = removeSecurityProxy(view)
416
canonical_url(person, rootsite='bugs'),
417
view.target.rstrip('/'))
372
421
class TestErrorViews(TestCaseWithFactory):