def render(resource, query_args): """ Render (in the manner of the Nevow appserver) a Nevow ``Page`` or a Twisted ``Resource`` against a request with the given query arguments . :param resource: The page or resource to render. :param query_args: The query arguments to put into the request being rendered. A mapping from ``bytes`` to ``list`` of ``bytes``. :return Deferred: A Deferred that fires with the rendered response body as ``bytes``. """ ctx = WebContext(tag=resource) req = FakeRequest(args=query_args) ctx.remember(DefaultExceptionHandler(), ICanHandleException) ctx.remember(req, IRequest) ctx.remember(None, IData) def maybe_concat(res): if isinstance(res, bytes): return req.v + res return req.v resource = INevowResource(resource) d = maybeDeferred(resource.renderHTTP, ctx) d.addErrback(processingFailed, req, ctx) d.addCallback(maybe_concat) return d
class LoginPageTests(TestCase): """ Tests for functionality related to login. """ domain = u"example.com" def setUp(self): """ Create a L{Store}, L{WebSite} and necessary request-related objects to test L{LoginPage}. """ self.siteStore = Store(filesdir=self.mktemp()) Mantissa().installSite(self.siteStore, self.domain, u"", False) self.site = self.siteStore.findUnique(SiteConfiguration) installOn( TCPPort(store=self.siteStore, factory=self.site, portNumber=80), self.siteStore) self.context = WebContext() self.request = FakeRequest() self.context.remember(self.request) def test_fromRequest(self): """ L{LoginPage.fromRequest} should return a two-tuple of the class it is called on and an empty tuple. """ request = FakeRequest(uri='/foo/bar/baz', currentSegments=['foo'], args={'quux': ['corge']}) class StubLoginPage(LoginPage): def __init__(self, store, segments, arguments): self.store = store self.segments = segments self.arguments = arguments page = StubLoginPage.fromRequest(self.siteStore, request) self.assertTrue(isinstance(page, StubLoginPage)) self.assertIdentical(page.store, self.siteStore) self.assertEqual(page.segments, ['foo', 'bar']) self.assertEqual(page.arguments, {'quux': ['corge']}) def test_staticShellContent(self): """ The L{IStaticShellContent} adapter for the C{store} argument to L{LoginPage.__init__} should become its C{staticContent} attribute. """ originalInterface = publicweb.IStaticShellContent adaptions = [] result = object() def stubInterface(object, default): adaptions.append((object, default)) return result publicweb.IStaticShellContent = stubInterface try: page = LoginPage(self.siteStore) finally: publicweb.IStaticShellContent = originalInterface self.assertEqual(len(adaptions), 1) self.assertIdentical(adaptions[0][0], self.siteStore) self.assertIdentical(page.staticContent, result) def test_segments(self): """ L{LoginPage.beforeRender} should fill the I{login-action} slot with an L{URL} which includes all the segments given to the L{LoginPage}. """ segments = ('foo', 'bar') page = LoginPage(self.siteStore, segments) page.beforeRender(self.context) loginAction = self.context.locateSlotData('login-action') expectedLocation = URL.fromString('/') for segment in (LOGIN_AVATAR, ) + segments: expectedLocation = expectedLocation.child(segment) self.assertEqual(loginAction, expectedLocation) def test_queryArguments(self): """ L{LoginPage.beforeRender} should fill the I{login-action} slot with an L{URL} which includes all the query arguments given to the L{LoginPage}. """ args = {'foo': ['bar']} page = LoginPage(self.siteStore, (), args) page.beforeRender(self.context) loginAction = self.context.locateSlotData('login-action') expectedLocation = URL.fromString('/') expectedLocation = expectedLocation.child(LOGIN_AVATAR) expectedLocation = expectedLocation.add('foo', 'bar') self.assertEqual(loginAction, expectedLocation) def test_locateChildPreservesSegments(self): """ L{LoginPage.locateChild} should create a new L{LoginPage} with segments extracted from the traversal context. """ segments = ('foo', 'bar') page = LoginPage(self.siteStore) child, remaining = page.locateChild(self.context, segments) self.assertTrue(isinstance(child, LoginPage)) self.assertEqual(remaining, ()) self.assertEqual(child.segments, segments) def test_locateChildPreservesQueryArguments(self): """ L{LoginPage.locateChild} should create a new L{LoginPage} with query arguments extracted from the traversal context. """ self.request.args = {'foo': ['bar']} page = LoginPage(self.siteStore) child, remaining = page.locateChild(self.context, None) self.assertTrue(isinstance(child, LoginPage)) self.assertEqual(child.arguments, self.request.args)
class LoginPageTests(TestCase): """ Tests for functionality related to login. """ domain = u"example.com" def setUp(self): """ Create a L{Store}, L{WebSite} and necessary request-related objects to test L{LoginPage}. """ self.siteStore = Store(filesdir=self.mktemp()) Mantissa().installSite(self.siteStore, self.domain, u"", False) self.site = self.siteStore.findUnique(SiteConfiguration) installOn( TCPPort(store=self.siteStore, factory=self.site, portNumber=80), self.siteStore) self.context = WebContext() self.request = FakeRequest() self.context.remember(self.request) def test_fromRequest(self): """ L{LoginPage.fromRequest} should return a two-tuple of the class it is called on and an empty tuple. """ request = FakeRequest( uri='/foo/bar/baz', currentSegments=['foo'], args={'quux': ['corge']}) class StubLoginPage(LoginPage): def __init__(self, store, segments, arguments): self.store = store self.segments = segments self.arguments = arguments page = StubLoginPage.fromRequest(self.siteStore, request) self.assertTrue(isinstance(page, StubLoginPage)) self.assertIdentical(page.store, self.siteStore) self.assertEqual(page.segments, ['foo', 'bar']) self.assertEqual(page.arguments, {'quux': ['corge']}) def test_staticShellContent(self): """ The L{IStaticShellContent} adapter for the C{store} argument to L{LoginPage.__init__} should become its C{staticContent} attribute. """ originalInterface = publicweb.IStaticShellContent adaptions = [] result = object() def stubInterface(object, default): adaptions.append((object, default)) return result publicweb.IStaticShellContent = stubInterface try: page = LoginPage(self.siteStore) finally: publicweb.IStaticShellContent = originalInterface self.assertEqual(len(adaptions), 1) self.assertIdentical(adaptions[0][0], self.siteStore) self.assertIdentical(page.staticContent, result) def test_segments(self): """ L{LoginPage.beforeRender} should fill the I{login-action} slot with an L{URL} which includes all the segments given to the L{LoginPage}. """ segments = ('foo', 'bar') page = LoginPage(self.siteStore, segments) page.beforeRender(self.context) loginAction = self.context.locateSlotData('login-action') expectedLocation = URL.fromString('/') for segment in (LOGIN_AVATAR,) + segments: expectedLocation = expectedLocation.child(segment) self.assertEqual(loginAction, expectedLocation) def test_queryArguments(self): """ L{LoginPage.beforeRender} should fill the I{login-action} slot with an L{URL} which includes all the query arguments given to the L{LoginPage}. """ args = {'foo': ['bar']} page = LoginPage(self.siteStore, (), args) page.beforeRender(self.context) loginAction = self.context.locateSlotData('login-action') expectedLocation = URL.fromString('/') expectedLocation = expectedLocation.child(LOGIN_AVATAR) expectedLocation = expectedLocation.add('foo', 'bar') self.assertEqual(loginAction, expectedLocation) def test_locateChildPreservesSegments(self): """ L{LoginPage.locateChild} should create a new L{LoginPage} with segments extracted from the traversal context. """ segments = ('foo', 'bar') page = LoginPage(self.siteStore) child, remaining = page.locateChild(self.context, segments) self.assertTrue(isinstance(child, LoginPage)) self.assertEqual(remaining, ()) self.assertEqual(child.segments, segments) def test_locateChildPreservesQueryArguments(self): """ L{LoginPage.locateChild} should create a new L{LoginPage} with query arguments extracted from the traversal context. """ self.request.args = {'foo': ['bar']} page = LoginPage(self.siteStore) child, remaining = page.locateChild(self.context, None) self.assertTrue(isinstance(child, LoginPage)) self.assertEqual(child.arguments, self.request.args)