def _makeView(self, entry): """Create view for a queue entry.""" request = LaunchpadTestRequest() setFirstLayer(request, TranslationsLayer) view = getMultiAdapter((entry, request), name='+index') view.initialize() return view
def _makeView(self, entry): """Create view for a queue entry.""" request = LaunchpadTestRequest() setFirstLayer(request, TranslationsLayer) view = getMultiAdapter((entry, request), name='+index') view.initialize() return view
def test_XMLRPCRequest_uses_MasterPolicy(self): """XMLRPC should always use the master flavor, since they always use POST and do not support session cookies. """ request = LaunchpadTestRequest(SERVER_URL="http://xmlrpc-private.launchpad.dev") setFirstLayer(request, IXMLRPCRequest) policy = getAdapter(request, IDatabasePolicy) self.failUnless(isinstance(policy, MasterDatabasePolicy), "Expected MasterDatabasePolicy, not %s." % policy)
def test_FeedsLayer_uses_SlaveDatabasePolicy(self): """FeedsRequest should use the SlaveDatabasePolicy since they are read-only in nature. Also we don't want to send session cookies over them. """ request = LaunchpadTestRequest(SERVER_URL='http://feeds.launchpad.dev') setFirstLayer(request, FeedsLayer) policy = IDatabasePolicy(request) self.assertIsInstance(policy, SlaveOnlyDatabasePolicy)
def test_FeedsLayer_uses_SlaveDatabasePolicy(self): """FeedsRequest should use the SlaveDatabasePolicy since they are read-only in nature. Also we don't want to send session cookies over them. """ request = LaunchpadTestRequest(SERVER_URL="http://feeds.launchpad.dev") setFirstLayer(request, FeedsLayer) policy = IDatabasePolicy(request) self.assertIsInstance(policy, SlaveOnlyDatabasePolicy)
def test_XMLRPCRequest_uses_MasterPolicy(self): """XMLRPC should always use the master flavor, since they always use POST and do not support session cookies. """ request = LaunchpadTestRequest( SERVER_URL='http://xmlrpc-private.launchpad.dev') setFirstLayer(request, IXMLRPCRequest) policy = getAdapter(request, IDatabasePolicy) self.assertTrue(isinstance(policy, MasterDatabasePolicy), "Expected MasterDatabasePolicy, not %s." % policy)
def test_WebServiceRequest_uses_MasterDatabasePolicy(self): """WebService requests should always use the master flavor, since it's likely that clients won't support cookies and thus mixing read and write requests will result in incoherent views of the data. XXX 20090320 Stuart Bishop bug=297052: This doesn't scale of course and will meltdown when the API becomes popular. """ api_prefix = getUtility(IWebServiceConfiguration).active_versions[0] server_url = 'http://api.launchpad.dev/%s' % api_prefix request = LaunchpadTestRequest(SERVER_URL=server_url) setFirstLayer(request, WebServiceLayer) policy = IDatabasePolicy(request) self.assertIsInstance(policy, MasterDatabasePolicy)
def test_WebServiceRequest_uses_MasterDatabasePolicy(self): """WebService requests should always use the master flavor, since it's likely that clients won't support cookies and thus mixing read and write requests will result in incoherent views of the data. XXX 20090320 Stuart Bishop bug=297052: This doesn't scale of course and will meltdown when the API becomes popular. """ api_prefix = getUtility(IWebServiceConfiguration).active_versions[0] server_url = "http://api.launchpad.dev/%s" % api_prefix request = LaunchpadTestRequest(SERVER_URL=server_url) setFirstLayer(request, WebServiceLayer) policy = IDatabasePolicy(request) self.assertIsInstance(policy, MasterDatabasePolicy)
def create_view(context, name, form=None, layer=None, server_url=None, method='GET', principal=None, query_string='', cookie='', request=None, path_info='/', current_request=False, rootsite=None, **kwargs): """Return a view based on the given arguments. :param context: The context for the view. :param name: The web page the view should handle. :param form: A dictionary with the form keys. :param layer: The layer where the page we are interested in is located. :param server_url: The URL from where this request was done. :param method: The method used in the request. Defaults to 'GET'. :param principal: The principal for the request, default to the unauthenticated principal. :param query_string: The query string for the request. :param cookie: The HTTP_COOKIE value for the request. :param request: Use this request instead of creating a new one. :param path_info: The PATH_INFO value for the request. :param current_request: If True, the request will be set as the current interaction. :param **kwargs: Any other parameter for the request. :return: The view class for the given context and the name. """ if request is None: request = LaunchpadTestRequest( form=form, SERVER_URL=server_url, QUERY_STRING=query_string, HTTP_COOKIE=cookie, method=method, PATH_INFO=path_info, **kwargs) if principal is not None: request.setPrincipal(principal) else: request.setPrincipal( getUtility(IPlacelessAuthUtility).unauthenticatedPrincipal()) if layer is None: # If a layer hasn't been specified, try to get the layer for the # rootsite. if rootsite is None: # If we haven't been told a site, try to get it from the canonical # url data of the object. obj_urldata = ICanonicalUrlData(context, None) if obj_urldata is not None: rootsite = obj_urldata.rootsite layer = layer_for_rootsite(rootsite) if layer is not None: setFirstLayer(request, layer) if current_request: endInteraction() newInteraction(request) return getMultiAdapter((context, request), name=name)
def test_WebServiceRequest_uses_LaunchpadDatabasePolicy(self): """WebService requests with a session cookie will use the standard LaunchpadDatabasePolicy so their database queries can be outsourced to a slave database when possible. """ api_prefix = getUtility(IWebServiceConfiguration).active_versions[0] server_url = 'http://api.launchpad.dev/%s' % api_prefix request = LaunchpadTestRequest(SERVER_URL=server_url) newInteraction(request) try: # First, generate a valid session cookie. ISession(request)['whatever']['whatever'] = 'whatever' # Then stuff it into the request where we expect to # find it. The database policy is only interested if # a session cookie was sent with the request, not it # one has subsequently been set in the response. request._cookies = request.response._cookies setFirstLayer(request, WebServiceLayer) policy = IDatabasePolicy(request) self.assertIsInstance(policy, LaunchpadDatabasePolicy) finally: endInteraction()
def test_WebServiceRequest_uses_LaunchpadDatabasePolicy(self): """WebService requests with a session cookie will use the standard LaunchpadDatabasePolicy so their database queries can be outsourced to a slave database when possible. """ api_prefix = getUtility(IWebServiceConfiguration).active_versions[0] server_url = "http://api.launchpad.dev/%s" % api_prefix request = LaunchpadTestRequest(SERVER_URL=server_url) newInteraction(request) try: # First, generate a valid session cookie. ISession(request)["whatever"]["whatever"] = "whatever" # Then stuff it into the request where we expect to # find it. The database policy is only interested if # a session cookie was sent with the request, not it # one has subsequently been set in the response. request._cookies = request.response._cookies setFirstLayer(request, WebServiceLayer) policy = IDatabasePolicy(request) self.assertIsInstance(policy, LaunchpadDatabasePolicy) finally: endInteraction()
def _publishTraverse(self, request, name): """Traverse, like zope wants.""" # First, set a new layer if there is one. This is important to do # first so that if there's an error, we get the error page for # this request. if self.newlayer is not None: setFirstLayer(request, self.newlayer) # Next, see if we're being asked to stepto somewhere. stepto_traversals = self.stepto_traversals if stepto_traversals is not None: if name in stepto_traversals: handler = stepto_traversals[name] try: nextobj = handler(self) except NotFoundError: nextobj = None return self._handle_next_object(nextobj, request, name) # Next, see if we have at least two path steps in total to traverse; # that is, the current name and one on the request's traversal stack. # If so, see if the name is in the namespace_traversals, and if so, # dispatch to the appropriate function. We can optimise by changing # the order of these checks around a bit. # If the next path step is a zope namespace eg ++model++, then we # actually do not want to process the path steps as a stepthrough # traversal so we just ignore it here. namespace_traversals = self.stepthrough_traversals if namespace_traversals is not None: if name in namespace_traversals: stepstogo = request.stepstogo if stepstogo: # First peek at the nextstep to see if we should ignore it. nextstep = stepstogo.peek() if not RESERVED_NAMESPACE.match(nextstep): nextstep = stepstogo.consume() handler = namespace_traversals[name] try: nextobj = handler(self, nextstep) except NotFoundError: nextobj = None else: # Circular import; breaks make. from lp.services.webapp.breadcrumb import ( Breadcrumb, ) stepthrough_page = queryMultiAdapter( (self.context, self.request), name=name) if stepthrough_page: # Not all stepthroughs have a page; if they # don't, there's no need for a breadcrumb. page_title = getattr( stepthrough_page, 'page_title', None) label = getattr( stepthrough_page, 'label', None) stepthrough_text = page_title or label if isinstance(stepthrough_text, Message): stepthrough_text = i18n.translate( stepthrough_text, context=self.request) stepthrough_url = canonical_url( self.context, view_name=name) stepthrough_breadcrumb = Breadcrumb( context=self.context, url=stepthrough_url, text=stepthrough_text) self.request.traversed_objects.append( stepthrough_breadcrumb) return self._handle_next_object(nextobj, request, nextstep) # Next, look up views on the context object. If a view exists, # use it. view = queryMultiAdapter((self.context, request), name=name) if view is not None: return view # Next, look up redirections. Note that registered views take # priority over redirections, because you can always make your # view redirect, but you can't make your redirection 'view'. redirections = self.redirections if redirections is not None: if name in redirections: urlto, status = redirections[name] return RedirectionView(urlto(self), request, status=status) # Finally, use the self.traverse() method. This can return # an object to be traversed, or raise NotFoundError. It must not # return None. try: nextobj = self.traverse(name) except NotFoundError: nextobj = None return self._handle_next_object(nextobj, request, name)
def create_view(context, name, form=None, layer=None, server_url=None, method='GET', principal=None, query_string='', cookie='', request=None, path_info='/', current_request=False, rootsite=None, **kwargs): """Return a view based on the given arguments. :param context: The context for the view. :param name: The web page the view should handle. :param form: A dictionary with the form keys. :param layer: The layer where the page we are interested in is located. :param server_url: The URL from where this request was done. :param method: The method used in the request. Defaults to 'GET'. :param principal: The principal for the request, default to the unauthenticated principal. :param query_string: The query string for the request. :param cookie: The HTTP_COOKIE value for the request. :param request: Use this request instead of creating a new one. :param path_info: The PATH_INFO value for the request. :param current_request: If True, the request will be set as the current interaction. :param **kwargs: Any other parameter for the request. :return: The view class for the given context and the name. """ if request is None: request = LaunchpadTestRequest(form=form, SERVER_URL=server_url, QUERY_STRING=query_string, HTTP_COOKIE=cookie, method=method, PATH_INFO=path_info, **kwargs) if principal is not None: request.setPrincipal(principal) else: request.setPrincipal( getUtility(IPlacelessAuthUtility).unauthenticatedPrincipal()) if layer is None: # If a layer hasn't been specified, try to get the layer for the # rootsite. if rootsite is None: # If we haven't been told a site, try to get it from the canonical # url data of the object. obj_urldata = ICanonicalUrlData(context, None) if obj_urldata is not None: rootsite = obj_urldata.rootsite layer = layer_for_rootsite(rootsite) if layer is not None: setFirstLayer(request, layer) if current_request: endInteraction() newInteraction(request) return getMultiAdapter((context, request), name=name)