def setUp(self): """ Set up a store with a valid offering to test against. """ SiteTestsMixin.setUp(self) self.store = self.siteStore self.site = ISiteURLGenerator(self.store) self.resource = IResource(self.store)
def setUp(self): """ Set up a store with a valid offering to test against. """ SiteTestsMixin.setUp(self) userStore = self.addUser() ws = userStore.findUnique(WebSite) self.store = ws.store self.resource = IResource(self.store)
def test_redirectToSettingsWhenLoggedIn(self): """ When a user is already logged in, navigating to /resetPassword should redirect to the settings page, since the user can change their password from there. """ self.assertNotIdentical(self.userStore.parent, None) # sanity check prefPage = ixmantissa.IPreferenceAggregator(self.userStore) urlPath = ixmantissa.IWebTranslator(self.userStore).linkTo(prefPage.storeID) request = FakeRequest(headers={"host": "example.com"}) app = IResource(self.userStore) rsc = IResource(app.locateChild(request, ("resetPassword",))[0]) d = renderPage(rsc, reqFactory=lambda : request) def rendered(result): self.assertEquals( 'http://example.com' + urlPath, request.redirected_to) d.addCallback(rendered) return d
def childFactory(self, ctx, name): for T in self.original.store.query( Ticket, AND(Ticket.booth == self.original, Ticket.nonce == unicode(name, 'ascii'))): something = T.claim() res = IResource(something) lgo = getattr(res, 'logout', lambda: None) ISession(ctx).setDefaultResource(res, lgo) return URL.fromContext(ctx).click("/private") return None
def _cbWrapChild(result): if result in [NotFound, errorMarker]: return result if isinstance(result, tuple): res, segments = result if isinstance(res, Deferred): return res.addCallback(lambda res: _cbWrapChild((res, segments))) return type(self)(IResource(res)), segments raise ValueError('Broken resource; locateChild returned %r' % (result,))
class _SecureWrapper(record('urlGenerator wrappedResource')): """ Helper class for L{SecuringWrapper} which preserves wrapping to the ultimate resource and which implements HTTPS redirect logic if necessary. @ivar urlGenerator: The L{ISiteURLGenerator} which will provide an HTTPS URL. @ivar wrappedResource: The resource which will be used to locate children or which will be rendered. """ implements(IResource) def __init__(self, *a, **k): super(_SecureWrapper, self).__init__(*a, **k) self.wrappedResource = IResource(self.wrappedResource) def locateChild(self, context, segments): """ Delegate child lookup to the wrapped resource but wrap whatever results in another instance of this wrapper. """ childDeferred = maybeDeferred( self.wrappedResource.locateChild, context, segments) def childLocated((resource, segments)): if (resource, segments) == NotFound: return NotFound return _SecureWrapper(self.urlGenerator, resource), segments childDeferred.addCallback(childLocated) return childDeferred def renderHTTP(self, context): """ Check to see if the wrapped resource wants to be rendered over HTTPS and generate a redirect if this is so, if HTTPS is available, and if the request is not already over HTTPS. """ if getattr(self.wrappedResource, 'needsSecure', False): request = IRequest(context) url = self.urlGenerator.encryptedRoot() if url is not None: for seg in request.prepath: url = url.child(seg) return url return self.wrappedResource.renderHTTP(context)
def wrapModel(self, model): """ Converts application-provided model objects to L{IResource} providers. """ res = IResource(model, None) if res is None: frag = INavigableFragment(model) fragmentName = getattr(frag, 'fragmentName', None) if fragmentName is not None: fragDocFactory = self._getDocFactory(fragmentName) if fragDocFactory is not None: frag.docFactory = fragDocFactory if frag.docFactory is None: raise CouldNotLoadFromThemes(frag, self._preferredThemes()) useAthena = isinstance(frag, (athena.LiveFragment, athena.LiveElement)) return self._wrapNavFrag(frag, useAthena) else: return res
class AnonymousSiteTests(SiteTestsMixin, TestCase): """ Tests for L{AnonymousSite}. """ def setUp(self): """ Set up a store with a valid offering to test against. """ SiteTestsMixin.setUp(self) self.store = self.siteStore self.site = ISiteURLGenerator(self.store) self.resource = IResource(self.store) def test_powersUpWebViewer(self): """ L{AnonymousSite} provides an indirected L{IWebViewer} powerup, and its indirected powerup should be the default provider of that interface. """ webViewer = IWebViewer(self.store) self.assertIsInstance(webViewer, _AnonymousWebViewer) self.assertIdentical(webViewer._siteStore, self.store) def test_login(self): """ L{AnonymousSite} has a I{login} child which returns a L{LoginPage} instance. """ host = "example.org" port = 1234 netloc = "%s:%d" % (host, port) request = FakeRequest(headers={"host": netloc}, uri="/login/foo", currentSegments=[], isSecure=False) self.site.hostname = host.decode("ascii") SSLPort(store=self.store, portNumber=port, factory=self.site) resource, segments = self.resource.locateChild(request, ("login",)) self.assertTrue(isinstance(resource, LoginPage)) self.assertIdentical(resource.store, self.store) self.assertEqual(resource.segments, ()) self.assertEqual(resource.arguments, {}) self.assertEqual(segments, ()) def test_resetPassword(self): """ L{AnonymousSite} has a I{resetPassword} child which returns a L{PasswordResetResource} instance. """ resource, segments = self.resource.locateChild(FakeRequest(headers={"host": "example.com"}), ("resetPassword",)) self.assertTrue(isinstance(resource, PasswordResetResource)) self.assertIdentical(resource.store, self.store) self.assertEqual(segments, ()) def test_users(self): """ L{AnonymousSite} has a I{users} child which returns a L{UserIndexPage} instance. """ resource, segments = self.resource.locateChild(FakeRequest(headers={"host": "example.com"}), ("users",)) self.assertTrue(isinstance(resource, UserIndexPage)) self.assertIdentical(resource.loginSystem, self.store.findUnique(LoginSystem)) self.assertEqual(segments, ()) def test_notFound(self): """ L{AnonymousSite.locateChild} returns L{NotFound} for requests it cannot find another response for. """ result = self.resource.locateChild(FakeRequest(headers={"host": "example.com"}), ("foo", "bar")) self.assertIdentical(result, NotFound)
def __init__(self, *a, **k): super(_SecureWrapper, self).__init__(*a, **k) self.wrappedResource = IResource(self.wrappedResource)
class AnonymousSiteTests(SiteTestsMixin, TestCase): """ Tests for L{AnonymousSite}. """ def setUp(self): """ Set up a store with a valid offering to test against. """ SiteTestsMixin.setUp(self) self.store = self.siteStore self.site = ISiteURLGenerator(self.store) self.resource = IResource(self.store) def test_powersUpWebViewer(self): """ L{AnonymousSite} provides an indirected L{IWebViewer} powerup, and its indirected powerup should be the default provider of that interface. """ webViewer = IWebViewer(self.store) self.assertIsInstance(webViewer, _AnonymousWebViewer) self.assertIdentical(webViewer._siteStore, self.store) def test_login(self): """ L{AnonymousSite} has a I{login} child which returns a L{LoginPage} instance. """ host = 'example.org' port = 1234 netloc = '%s:%d' % (host, port) request = FakeRequest(headers={'host': netloc}, uri='/login/foo', currentSegments=[], isSecure=False) self.site.hostname = host.decode('ascii') SSLPort(store=self.store, portNumber=port, factory=self.site) resource, segments = self.resource.locateChild(request, ("login", )) self.assertTrue(isinstance(resource, LoginPage)) self.assertIdentical(resource.store, self.store) self.assertEqual(resource.segments, ()) self.assertEqual(resource.arguments, {}) self.assertEqual(segments, ()) def test_resetPassword(self): """ L{AnonymousSite} has a I{resetPassword} child which returns a L{PasswordResetResource} instance. """ resource, segments = self.resource.locateChild( FakeRequest(headers={"host": "example.com"}), ("resetPassword", )) self.assertTrue(isinstance(resource, PasswordResetResource)) self.assertIdentical(resource.store, self.store) self.assertEqual(segments, ()) def test_users(self): """ L{AnonymousSite} has a I{users} child which returns a L{UserIndexPage} instance. """ resource, segments = self.resource.locateChild( FakeRequest(headers={"host": "example.com"}), ("users", )) self.assertTrue(isinstance(resource, UserIndexPage)) self.assertIdentical(resource.loginSystem, self.store.findUnique(LoginSystem)) self.assertEqual(segments, ()) def test_notFound(self): """ L{AnonymousSite.locateChild} returns L{NotFound} for requests it cannot find another response for. """ result = self.resource.locateChild( FakeRequest(headers={"host": "example.com"}), ("foo", "bar")) self.assertIdentical(result, NotFound)