def test_getting_static_style_css_works_with_script_name(self): environ = environ_from_url("/test/static/default.css") filestore = FileStore() context = ExecutionContext(script_name="/test") app = WikkidApp(filestore, execution_context=context) app(environ, self.assert_ok)
def test_context(app, url=None, environ=None): """Sets up a TurboGears context allowing to use turbogears functions outside of a real request. Everything inside the with statement will have a TurboGears context available In case ``app`` is a ``TGApp`` instance (as the one that can be retrieved through `configure_new_app` hook) a context for such application is set in place. In case of a WebTest application a request to ``/_test_vars`` is performed to setup the test variables. In case ``app`` is ``None`` a fake application is created for which to setup the context. ``url`` parameter is provided to simulate the context as being for that url and ``environ`` parameter is provided to allow changing WSGI environ entries for the context. """ if app is None: from ..configuration.app_config import AppConfig loadenv = AppConfig(minimal=True, root_controller=lambda *args: Response('HELLO') ).make_load_environment() app = TGApp(loadenv({}, {})) url = url or '/' from webob.request import environ_from_url wsgienviron = environ_from_url(url) wsgienviron.update(environ or {}) if isinstance(app, TGApp): return _TGTestContextManager(app, wsgienviron) else: return _WebTestTGTestContextManager(app, wsgienviron)
def test_context(app, url=None, environ=None): """Sets up a TurboGears context allowing to use turbogears functions outside of a real request. Everything inside the with statement will have a TurboGears context available In case ``app`` is a ``TGApp`` instance (as the one that can be retrieved through `configure_new_app` hook) a context for such application is set in place. In case of a WebTest application a request to ``/_test_vars`` is performed to setup the test variables. In case ``app`` is ``None`` a fake application is created for which to setup the context. ``url`` parameter is provided to simulate the context as being for that url and ``environ`` parameter is provided to allow changing WSGI environ entries for the context. """ if app is None: app = _BareTGAppMaker().make() url = url or '/' from webob.request import environ_from_url wsgienviron = environ_from_url(url) wsgienviron.update(environ or {}) if isinstance(app, TGApp): return _TGTestContextManager(app, wsgienviron) else: return _WebTestTGTestContextManager(app, wsgienviron)
def assertUrlIsView(self, url, view_type, script_name=None, store_content=None): environ = environ_from_url(url) filestore = FileStore(store_content) context = ExecutionContext(script_name=script_name) app = WikkidApp(filestore, execution_context=context) view = app.get_view(environ) self.assertThat(view, IsInstance(view_type))
def test_traverse_above_static_not_possible_with_relative_path(self): """ Traversal above the static folder, by forging a malicious request with a relative path for example, is not possible. """ environ = environ_from_url("/static/../page.html") filestore = FileStore() app = WikkidApp(filestore) app(environ, self.assert_not_found)
def test_traverse_above_static_not_possible_with_absolute_path(self): """ Traversal above the static folder, by forging a malicious request including an absolute path for example, is not possible. """ this_file = os.path.abspath(__file__) environ = environ_from_url("/static/" + this_file) filestore = FileStore() app = WikkidApp(filestore) app(environ, self.assert_not_found)
def __enter__(self): self._app.get('/_test_vars') if self._url is not None: from webob.request import environ_from_url request.environ.update(environ_from_url(self._url)) if self._environ is not None: request.environ.update(self._environ) return self._app
def create_wsgi_environ(url, request_method, request_body=None): wsgi_environ = environ_from_url(url) wsgi_environ.update({ 'REQUEST_METHOD': request_method, }) if request_body: content_type, request_body = build_http_body(request_body) wsgi_environ.update({ 'wsgi.input': StringIO(request_body), 'CONTENT_LENGTH': str(len(request_body)), 'CONTENT_TYPE': content_type, }) return wsgi_environ
def test_use_wsgi_filewrapper(self): class TestWrapper(object): def __init__(self, file, block_size): self.file = file self.block_size = block_size environ = environ_from_url('/') environ['wsgi.file_wrapper'] = TestWrapper app = static.FileApp(self.tempfile) app_iter = Request(environ).get_response(app).app_iter self.assertTrue(isinstance(app_iter, TestWrapper)) self.assertEqual(bytes_('import this\n'), app_iter.file.read()) self.assertEqual(static.BLOCK_SIZE, app_iter.block_size)
def test_use_wsgi_filewrapper(self): class TestWrapper: __slots__ = ("file", "block_size") def __init__(self, file, block_size): self.file = file self.block_size = block_size environ = environ_from_url("/") environ["wsgi.file_wrapper"] = TestWrapper app = static.FileApp(self.tempfile) app_iter = Request(environ).get_response(app).app_iter self.assertTrue(isinstance(app_iter, TestWrapper)) self.assertEqual(bytes_("import this\n"), app_iter.file.read()) self.assertEqual(static.BLOCK_SIZE, app_iter.block_size)
def __init__(self, *args, **kwargs): super(PyramidWebTestRequest, self).__init__(*args, **kwargs) manager.push({'request': self, 'registry': self._registry}) self._base_pyramid_request = self._pyramid_app.request_factory( environ_from_url('/'))
def get_response(app, path="/", **req_kw): """Convenient function to query an application""" req = Request(environ_from_url(path), **req_kw) return req.get_response(app)
def get_response(app, path='/', **req_kw): """Convenient function to query an application""" req = Request(environ_from_url(path), **req_kw) return req.get_response(app)
def test_getting_static_style_css_works(self): environ = environ_from_url("/static/default.css") filestore = FileStore() app = WikkidApp(filestore) app(environ, self.assert_ok)
def test_getting_static_style_css_works_with_script_name_multiple_segments(self): environ = environ_from_url("/p/project-name/wiki/static/default.css") filestore = FileStore() context = ExecutionContext(script_name="/p/project-name/wiki") app = WikkidApp(filestore, execution_context=context) app(environ, self.assert_ok)
def test_getting_anything_outside_script_name_fails(self): environ = environ_from_url("/foo/bar") filestore = FileStore() context = ExecutionContext(script_name="/test") app = WikkidApp(filestore, execution_context=context) app(environ, self.assert_not_found)