~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/browser/tests/test_launchpad.py

  • Committer: mbp at canonical
  • Date: 2011-06-27 15:36:25 UTC
  • mto: This revision was merged to the branch mainline in revision 13315.
  • Revision ID: mbp@canonical.com-20110627153625-sqo912gravoccn6a
launchpad.net/~ now redirects you to your user page

Show diffs side-by-side

added added

removed removed

Lines of Context:
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).
3
3
 
4
4
"""Tests for traversal from the root branch object."""
9
9
 
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
13
14
 
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 (
 
17
    canonical_url,
 
18
    )
 
19
from canonical.launchpad.webapp.interfaces import (
 
20
    BrowserNotificationLevel,
 
21
    ILaunchpadRoot,
 
22
    )
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
32
38
    person_logged_in,
33
39
    TestCaseWithFactory,
34
40
    )
 
41
from lp.testing.publication import test_traverse
35
42
from lp.testing.views import create_view
36
43
 
37
44
# We set the request header HTTP_REFERER  when we want to simulate navigation
93
100
        self.assertEqual(url, redirection.target)
94
101
 
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.
97
104
 
98
105
        Using the Zope traversal machinery, traverse to the path given by
99
106
        'segments', starting at a `LaunchpadRootNavigation` object.
100
107
 
101
 
        :param segments: A list of path segments.
 
108
        CAUTION: Prefer test_traverse to this method, because it correctly
 
109
        establishes the global request.
 
110
 
 
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.
106
116
        """
 
117
        # XXX: What's the difference between first_segment and path? -- mbp
 
118
        # 2011-06-27.
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)
115
128
 
116
129
 
368
381
        login_person(self.any_user)
369
382
        self.assertRaises(NotFound, self.traverse, segment, segment)
370
383
 
 
384
    def test_self_url_traversal(self):
 
385
        # Just /~/ expands to the current user.  (Bug 785800).
 
386
        person = self.factory.makePerson()
 
387
        login_person(person)
 
388
        obj, view, req = test_traverse('http://launchpad.dev/~')
 
389
        view = removeSecurityProxy(view)
 
390
        self.assertEqual(
 
391
            canonical_url(person),
 
392
            view.target.rstrip('/'))
 
393
 
 
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/~')
 
398
 
 
399
    def test_self_url_pathinfo(self):
 
400
        # You can traverse below /~/.
 
401
        person = self.factory.makePerson()
 
402
        login_person(person)
 
403
        obj, view, req = test_traverse('http://launchpad.dev/~/+editsshkeys')
 
404
        view = removeSecurityProxy(view)
 
405
        self.assertEqual(
 
406
            canonical_url(person) + '/+editsshkeys',
 
407
            view.target)
 
408
 
 
409
    def test_self_url_app_domain(self):
 
410
        # You can traverse below /~/.
 
411
        person = self.factory.makePerson()
 
412
        login_person(person)
 
413
        obj, view, req = test_traverse('http://bugs.launchpad.dev/~')
 
414
        view = removeSecurityProxy(view)
 
415
        self.assertEqual(
 
416
            canonical_url(person, rootsite='bugs'),
 
417
            view.target.rstrip('/'))
 
418
 
 
419
 
371
420
 
372
421
class TestErrorViews(TestCaseWithFactory):
373
422