Exemplo n.º 1
0
 def test_callTraversalHooks_appends_only_once_to_traversed_objects(self):
     # callTraversalHooks() may be called more than once for a given
     # traversed object, but if that's the case we won't add the same
     # object twice to traversed_objects.
     obj1 = obj2 = object()
     request = LaunchpadTestRequest()
     publication = LaunchpadBrowserPublication(None)
     publication.callTraversalHooks(request, obj1)
     publication.callTraversalHooks(request, obj2)
     self.assertEqual(request.traversed_objects, [obj1])
Exemplo n.º 2
0
 def test_callTraversalHooks_appends_to_traversed_objects(self):
     # Traversed objects are appended to request.traversed_objects in the
     # order they're traversed.
     obj1 = object()
     obj2 = object()
     request = LaunchpadTestRequest()
     publication = LaunchpadBrowserPublication(None)
     publication.callTraversalHooks(request, obj1)
     publication.callTraversalHooks(request, obj2)
     self.assertEqual(request.traversed_objects, [obj1, obj2])
 def test_callTraversalHooks_appends_only_once_to_traversed_objects(self):
     # callTraversalHooks() may be called more than once for a given
     # traversed object, but if that's the case we won't add the same
     # object twice to traversed_objects.
     obj1 = obj2 = object()
     request = LaunchpadTestRequest()
     publication = LaunchpadBrowserPublication(None)
     publication.callTraversalHooks(request, obj1)
     publication.callTraversalHooks(request, obj2)
     self.assertEquals(request.traversed_objects, [obj1])
 def test_callTraversalHooks_appends_to_traversed_objects(self):
     # Traversed objects are appended to request.traversed_objects in the
     # order they're traversed.
     obj1 = object()
     obj2 = object()
     request = LaunchpadTestRequest()
     publication = LaunchpadBrowserPublication(None)
     publication.callTraversalHooks(request, obj1)
     publication.callTraversalHooks(request, obj2)
     self.assertEquals(request.traversed_objects, [obj1, obj2])
Exemplo n.º 5
0
class LaunchpadBrowserPublicationPageIDTestCase(TestCase):
    """Ensure that the web service enhances the page ID correctly."""

    def setUp(self):
        super(LaunchpadBrowserPublicationPageIDTestCase, self).setUp()
        self.publication = LaunchpadBrowserPublication(db=None)
        self.view = FakeView()
        self.context = FakeContext()

    def test_pageid_without_context(self):
        # The pageid is an empty string if there is no context.
        self.assertEqual('', self.publication.constructPageID(self.view, None))

    def test_pageid_view_without_name(self):
        # The view. __class__.__name__ is used if the view does not have a
        # __name__ attribute.
        self.assertEqual(
            'FakeContext:FakeView',
            self.publication.constructPageID(self.view, self.context))

    def test_pageid_view_with_name(self):
        # The view.__name__ is used when it exists.
        self.view.__name__ = '+snarf'
        self.assertEqual(
            'FakeContext:+snarf',
            self.publication.constructPageID(self.view, self.context))

    def test_pageid_context_is_view_from_template(self):
        # When the context is a dynamic view class of a page template,
        # such as adapting a form view to ++model++, the method recurses
        # the views to locate the true context.
        class FakeView2(FakeView):
            pass

        class FakeViewView(FakeView):
            __name__ = '++model++'

            def __init__(self):
                self.request = FakeRequest()
                self.context = FakeView2()

        self.view = FakeViewView()
        self.context = self.view.context
        self.context.__name__ = '+bugs'
        self.context.__class__.__name__ = 'SimpleViewClass from template.pt'
        self.assertEqual(
            'FakeContext:+bugs:++model++',
            self.publication.constructPageID(self.view, self.context))
Exemplo n.º 6
0
class LaunchpadBrowserPublicationPageIDTestCase(TestCase):
    """Ensure that the web service enhances the page ID correctly."""
    def setUp(self):
        super(LaunchpadBrowserPublicationPageIDTestCase, self).setUp()
        self.publication = LaunchpadBrowserPublication(db=None)
        self.view = FakeView()
        self.context = FakeContext()

    def test_pageid_without_context(self):
        # The pageid is an empty string if there is no context.
        self.assertEqual('', self.publication.constructPageID(self.view, None))

    def test_pageid_view_without_name(self):
        # The view. __class__.__name__ is used if the view does not have a
        # __name__ attribute.
        self.assertEqual(
            'FakeContext:FakeView',
            self.publication.constructPageID(self.view, self.context))

    def test_pageid_view_with_name(self):
        # The view.__name__ is used when it exists.
        self.view.__name__ = '+snarf'
        self.assertEqual(
            'FakeContext:+snarf',
            self.publication.constructPageID(self.view, self.context))

    def test_pageid_context_is_view_from_template(self):
        # When the context is a dynamic view class of a page template,
        # such as adapting a form view to ++model++, the method recurses
        # the views to locate the true context.
        class FakeView2(FakeView):
            pass

        class FakeViewView(FakeView):
            __name__ = '++model++'

            def __init__(self):
                self.request = FakeRequest()
                self.context = FakeView2()

        self.view = FakeViewView()
        self.context = self.view.context
        self.context.__name__ = '+bugs'
        self.context.__class__.__name__ = 'SimpleViewClass from template.pt'
        self.assertEqual(
            'FakeContext:+bugs:++model++',
            self.publication.constructPageID(self.view, self.context))
Exemplo n.º 7
0
 def test_LaunchpadBrowserPub_fires_FinishReadOnlyRequestEvent(self):
     # LaunchpadBrowserPublication.finishReadOnlyRequest() issues an
     # IFinishReadOnlyRequestEvent and aborts the transaction.
     publication = LaunchpadBrowserPublication(None)
     self._test_publication(publication, ["ABORT"])
Exemplo n.º 8
0
 def setUp(self):
     super(LaunchpadBrowserPublicationPageIDTestCase, self).setUp()
     self.publication = LaunchpadBrowserPublication(db=None)
     self.view = FakeView()
     self.context = FakeContext()
Exemplo n.º 9
0
 def setUp(self):
     super(LaunchpadBrowserPublicationPageIDTestCase, self).setUp()
     self.publication = LaunchpadBrowserPublication(db=None)
     self.view = FakeView()
     self.context = FakeContext()
Exemplo n.º 10
0
def canonical_url(
    obj, request=None, rootsite=None, path_only_if_possible=False,
    view_name=None, force_local_path=False):
    """Return the canonical URL string for the object.

    If the canonical url configuration for the given object binds it to a
    particular root site, then we use that root URL.

    (There is an assumption here that traversal works the same way on
     different sites.  When that isn't so, we need to specify the url
     in full in the canonical url configuration.  We may want to
     register canonical url configuration *for* particular sites in the
     future, to allow more flexibility for traversal.
     I foresee a refactoring where we'll combine the concepts of
     sites, layers, URLs and so on.)

    Otherwise, we attempt to take the protocol, host and port from
    the request.  If a request is not provided, but a web-request is in
    progress, the protocol, host and port are taken from the current request.

    :param request: The web request; if not provided, canonical_url attempts
        to guess at the current request, using the protocol, host, and port
        taken from the root_url given in launchpad.conf.
    :param path_only_if_possible: If the protocol and hostname can be omitted
        for the current request, return a url containing only the path.
    :param view_name: Provide the canonical url for the specified view,
        rather than the default view.
    :param force_local_path: Strip off the site no matter what.
    :raises: NoCanonicalUrl if a canonical url is not available.
    """
    urlparts = [urldata.path
                for urldata in canonical_urldata_iterator(obj)
                if urldata.path]

    if rootsite is None:
        obj_urldata = ICanonicalUrlData(obj, None)
        if obj_urldata is None:
            raise NoCanonicalUrl(obj, obj)
        rootsite = obj_urldata.rootsite

    # The request is needed when there's no rootsite specified.
    if request is None:
        # Look for a request from the interaction.
        current_request = get_current_browser_request()
        if current_request is not None:
            if WebServiceLayer.providedBy(current_request):
                from lp.services.webapp.publication import (
                    LaunchpadBrowserPublication)
                from lp.services.webapp.servers import (
                    LaunchpadBrowserRequest)
                current_request = LaunchpadBrowserRequest(
                    current_request.bodyStream.getCacheStream(),
                    dict(current_request.environment))
                current_request.setPublication(
                    LaunchpadBrowserPublication(None))
                current_request.setVirtualHostRoot(names=[])
                main_root_url = current_request.getRootURL(
                    'mainsite')
                current_request._app_server = main_root_url.rstrip('/')

            request = current_request

    if view_name is not None:
        # Make sure that the view is registered for the site requested.
        fake_request = FakeRequest()
        directlyProvides(fake_request, layer_for_rootsite(rootsite))
        # Look first for a view.
        if queryMultiAdapter((obj, fake_request), name=view_name) is None:
            # Look if this is a special name defined by Navigation.
            navigation = queryMultiAdapter(
                (obj, fake_request), IBrowserPublisher)
            if isinstance(navigation, Navigation):
                all_names = navigation.all_traversal_and_redirection_names
            else:
                all_names = []
            if view_name not in all_names:
                raise AssertionError(
                    'Name "%s" is not registered as a view or navigation '
                    'step for "%s" on "%s".' % (
                        view_name, obj.__class__.__name__, rootsite))
        urlparts.insert(0, view_name)

    if request is None:
        # Yes this really does need to be here, as rootsite can be None, and
        # we don't want to make the getRootURL from the request break.
        if rootsite is None:
            rootsite = 'mainsite'
        root_url = allvhosts.configs[rootsite].rooturl
    else:
        root_url = request.getRootURL(rootsite)

    path = u'/'.join(reversed(urlparts))
    if ((path_only_if_possible and
         request is not None and
         root_url.startswith(request.getApplicationURL()))
        or force_local_path):
        return unicode('/' + path)
    return unicode(root_url + path)