def login(email, participation=None): """Simulates a login, using the specified email. If the lp.testing.ANONYMOUS constant is supplied as the email, you'll be logged in as the anonymous user. You can optionally pass in a participation to be used. If no participation is given, a LaunchpadTestRequest is used. If the participation provides IPublicationRequest, it must implement setPrincipal(), otherwise it must allow setting its principal attribute. """ if not isinstance(email, basestring): raise ValueError("Expected email parameter to be a string.") participation = _test_login_impl(participation) setupInteractionByEmail(email, participation)
def _generate_web_service_root(version, mimetype): """Generate the webservice description for the given version and mimetype. """ url = urlparse.urljoin(allvhosts.configs['api'].rooturl, version) # Since we want HTTPS URLs we have to munge the request URL. url = url.replace('http://', 'https://') request = WebServiceTestRequest(version=version, environ={ 'SERVER_URL': url, 'HTTP_HOST': allvhosts.configs['api'].hostname, 'HTTP_ACCEPT': mimetype, }) # We then bypass the usual publisher processing by associating # the request with the WebServicePublication (usually done by the # publisher) and then calling the root resource - retrieved # through getApplication(). request.setPublication(WebServicePublication(None)) setupInteractionByEmail(ANONYMOUS, request) return request.publication.getApplication(request)(request)
def login(self, user=ANONYMOUS): """Super-convenience method that avoids the import.""" setupInteractionByEmail(user)
def execute_zcml_for_scripts(use_web_security=False): """Execute the zcml rooted at launchpad/script.zcml If use_web_security is True, the same security policy as the web application uses will be used. Otherwise everything protected by a permission is allowed, and everything else denied. """ # When in testing mode, prevent some cases of erroneous layer usage. # But we don't want to import that module in production usage, thus # the conditional block. if 'lp.testing.layers' in sys.modules: from lp.testing.layers import (FunctionalLayer, BaseLayer, ZopelessLayer) assert not FunctionalLayer.isSetUp, \ 'Setting up Zopeless CA when Zopefull CA is already running' assert not BaseLayer.isSetUp or ZopelessLayer.isSetUp, """ execute_zcml_for_scripts should not be called from tests. Instead, your test should use the Zopeless layer. """ if config.isTestRunner(): scriptzcmlfilename = 'script-testing.zcml' else: scriptzcmlfilename = 'script.zcml' scriptzcmlfilename = os.path.abspath( os.path.join(config.root, 'zcml', scriptzcmlfilename)) from zope.configuration import xmlconfig # Hook up custom component architecture calls zope.site.hooks.setHooks() # Load server-independent site config context = ConfigurationMachine() xmlconfig.registerCommonDirectives(context) context = xmlconfig.file(scriptzcmlfilename, execute=True, context=context) if use_web_security: setSecurityPolicy(LaunchpadSecurityPolicy) else: setSecurityPolicy(LaunchpadPermissiveSecurityPolicy) # Register atexit handler to kill off mail delivery daemon threads, and # thus avoid spew at exit. See: # http://mail.python.org/pipermail/python-list/2003-October/192044.html # http://mail.python.org/pipermail/python-dev/2003-September/038151.html # http://mail.python.org/pipermail/python-dev/2003-September/038153.html def kill_queue_processor_threads(): for thread in threading.enumerate(): if isinstance(thread, zope.sendmail.delivery.QueueProcessorThread): thread.stop() thread.join(30) if thread.isAlive(): raise RuntimeError( "QueueProcessorThread did not shut down") atexit.register(kill_queue_processor_threads) # This is a convenient hack to set up a zope interaction, before we get # the proper API for having a principal / user running in scripts. setupInteractionByEmail(ANONYMOUS)
def execute_zcml_for_scripts(use_web_security=False): """Execute the zcml rooted at launchpad/script.zcml If use_web_security is True, the same security policy as the web application uses will be used. Otherwise everything protected by a permission is allowed, and everything else denied. """ # When in testing mode, prevent some cases of erroneous layer usage. # But we don't want to import that module in production usage, thus # the conditional block. if 'lp.testing.layers' in sys.modules: from lp.testing.layers import ( FunctionalLayer, BaseLayer, ZopelessLayer) assert not FunctionalLayer.isSetUp, \ 'Setting up Zopeless CA when Zopefull CA is already running' assert not BaseLayer.isSetUp or ZopelessLayer.isSetUp, """ execute_zcml_for_scripts should not be called from tests. Instead, your test should use the Zopeless layer. """ if config.isTestRunner(): scriptzcmlfilename = 'script-testing.zcml' else: scriptzcmlfilename = 'script.zcml' scriptzcmlfilename = os.path.abspath( os.path.join(config.root, 'zcml', scriptzcmlfilename)) from zope.configuration import xmlconfig # Hook up custom component architecture calls zope.site.hooks.setHooks() # Load server-independent site config context = ConfigurationMachine() xmlconfig.registerCommonDirectives(context) context = xmlconfig.file( scriptzcmlfilename, execute=True, context=context) if use_web_security: setSecurityPolicy(LaunchpadSecurityPolicy) else: setSecurityPolicy(LaunchpadPermissiveSecurityPolicy) # Register atexit handler to kill off mail delivery daemon threads, and # thus avoid spew at exit. See: # http://mail.python.org/pipermail/python-list/2003-October/192044.html # http://mail.python.org/pipermail/python-dev/2003-September/038151.html # http://mail.python.org/pipermail/python-dev/2003-September/038153.html def kill_queue_processor_threads(): for thread in threading.enumerate(): if isinstance( thread, zope.sendmail.delivery.QueueProcessorThread): thread.stop() thread.join(30) if thread.isAlive(): raise RuntimeError( "QueueProcessorThread did not shut down") atexit.register(kill_queue_processor_threads) # This is a convenient hack to set up a zope interaction, before we get # the proper API for having a principal / user running in scripts. setupInteractionByEmail(ANONYMOUS)