def setUp(self): self.siteStore = Store(filesdir=self.mktemp()) Mantissa().installSite(self.siteStore, u"example.com", u"", False) self.userAccount = Create().addAccount( self.siteStore, u'testuser', u'example.com', u'password') self.userStore = self.userAccount.avatars.open() self.privapp = PrivateApplication(store=self.userStore) installOn(self.privapp, self.userStore) self.webViewer = IWebViewer(self.userStore)
def setUp(self): """ Set up an inequality model (by way of L{TestableInequalityModel}) backed by a user store with some sample data, and an L{IWebTranslator} powerup, to provide a somewhat realistic test setup. The data provided has some duplicates in the sort column, and it is intentionally inserted out of order, so that storeID order and sort column ordering will not coincide. """ self.store = Store() privApp = PrivateApplication(store=self.store) installOn(privApp, self.store) self.data = [] for a, b, c in [ (9, 1928, u'nine'), (1, 983, u'one'), (8, 843, u'eight'), (2, 827, u'two'), # (8, 1874, u'eight (DUP)'), (2, 294, u'two (DUP)'), (7, 18, u'seven'), (3, 19, u'three'), (6, 218, u'six'), (4, 2198, u'four'), (5, 1982, u'five'), (0, 10, u'zero') ]: self.data.append(DataThunk(store=self.store, a=a, b=b, c=c)) self.data.sort(key=lambda item: (item.a, item.storeID)) self.model = TestableInequalityModel( self.store, DataThunk, None, [DataThunk.a, DataThunk.b, DataThunk.c], DataThunk.a, True)
def test_partDisplayerScrubbedContentLength(self): """ Test that L{PartDisplayer} sets the C{Content-Length} header to the length of the content after it has been transformed by the scrubber. """ s = Store() installOn(PrivateApplication(store=s), s) body = u'<div><script>haha</script>this is ok</div>' part = PartItem(store=s, contentType=u'text/html', bodyLength=len(body), body=body) partDisplayer = PartDisplayer(None) partDisplayer.item = part req = makeRequest() D = deferredRender(partDisplayer, req) def checkLength(renderedBody): self.assertEqual(int(req.headers.get('content-length')), len(renderedBody)) D.addCallback(checkLength) return D
def getScrollingWidget(self, howManyElements=0): store = Store() installOn(PrivateApplication(store=store), store) for n in xrange(howManyElements): testMessageFactory(store, spam=False) f = MailboxScrollingFragment(store) f.docFactory = getLoader(f.fragmentName) f.setFragmentParent(self) return f
def addUser(self, username=u'nobody', domain=u'nowhere'): """ Add a user to the site store, and install a L{PrivateApplication}. @return: the user store of the added user. """ userStore = self.siteStore.findUnique(LoginSystem).addAccount( username, domain, u'asdf', internal=True).avatars.open() installOn(PrivateApplication(store=userStore), userStore) return userStore
def testZipFileName(self): """ Test L{xquotient.exmess.MessageDetail._getZipFileName} """ s = Store() installOn(PrivateApplication(store=s), s) installOn(QuotientPreferenceCollection(store=s), s) md = MessageDetail( Message(store=s, subject=u'a/b/c', sender=u'foo@bar')) self.assertEqual(md.zipFileName, '*****@*****.**')
def test_setTabURLs(self): """ Check that L{webnav.setTabURLs} correctly sets the C{linkURL} attribute of L{webnav.Tab} instances to the result of passing tab.storeID to L{xmantissa.ixmantissa.IWebTranslator.linkTo} if C{linkURL} is not set, and that it leaves it alone if it is """ s = Store() privapp = PrivateApplication(store=s) installOn(privapp,s) tabs = [webnav.Tab('PrivateApplication', privapp.storeID, 0), webnav.Tab('Something Else', None, 0, linkURL='/foo/bar')] webnav.setTabURLs(tabs, privapp) self.assertEqual(tabs[0].linkURL, privapp.linkTo(privapp.storeID)) self.assertEqual(tabs[1].linkURL, '/foo/bar')
def setUp(self): self.store = Store() installOn(PrivateApplication(store=self.store), self.store) self.six = DataThunk(a=6, b=8157, c=u'six', store=self.store) self.three = DataThunk(a=3, b=821375, c=u'three', store=self.store) self.seven = DataThunk(a=7, b=4724, c=u'seven', store=self.store) self.eight = DataThunk(a=8, b=61, c=u'eight', store=self.store) self.one = DataThunk(a=1, b=435716, c=u'one', store=self.store) self.two = DataThunk(a=2, b=67145, c=u'two', store=self.store) self.four = DataThunk(a=4, b=6327, c=u'four', store=self.store) self.five = DataThunk(a=5, b=91856, c=u'five', store=self.store) self.scrollFragment = self.getScrollFragment()
def setUp(self): self.store = Store() privApp = PrivateApplication(store=self.store) installOn(privApp, self.store) self.model = TestableInequalityModel(self.store, DataThunkWithIndex, None, [DataThunkWithIndex.a], DataThunkWithIndex.a, True, privApp) self.counter = QueryCounter(self.store) self.data = [] for i in range(4): self.data.append(DataThunkWithIndex(store=self.store, a=i))
def getScrollingWidget(self, key, rowCount=10): store = Store() installOn(PrivateApplication(store=store), store) elements = [ ScrollElement(store=store, column=i) for i in range(rowCount) ] columns = [ScrollElement.column] f = SequenceScrollingFragment(store, elements, columns) f.docFactory = getLoader(f.fragmentName) f.setFragmentParent(self) self.perTestData[key] = (store, elements, f) return f
def test_setTabURLs(self): """ Check that L{webnav.setTabURLs} correctly sets the C{linkURL} attribute of L{webnav.Tab} instances to the result of passing tab.storeID to L{xmantissa.ixmantissa.IWebTranslator.linkTo} if C{linkURL} is not set, and that it leaves it alone if it is """ s = Store() privapp = PrivateApplication(store=s) installOn(privapp, s) tabs = [ webnav.Tab("PrivateApplication", privapp.storeID, 0), webnav.Tab("Something Else", None, 0, linkURL="/foo/bar"), ] webnav.setTabURLs(tabs, privapp) self.assertEqual(tabs[0].linkURL, privapp.linkTo(privapp.storeID)) self.assertEqual(tabs[1].linkURL, "/foo/bar")
def createDatabase(store): """ Instantiate a L{PrivateApplication} in C{store} and install it. """ loginSystem = LoginSystem(store=store) installOn(loginSystem, store) account = loginSystem.addAccount(USERNAME, DOMAIN, None) subStore = account.avatars.open() app = PrivateApplication( store=subStore, preferredTheme=PREFERRED_THEME, privateKey=PRIVATE_KEY) installOn(app, subStore)
def setUp(self): self.store = Store() privApp = PrivateApplication(store=self.store) installOn(privApp, self.store) # Create some data to test with in an order which is not naturally # sorted in any way. # 4 1s, 4 0s, 4 2s self.data = [] for number in [1, 0, 2, 2, 0, 1, 1, 0, 2, 0, 2, 1]: self.data.append(DataThunk(store=self.store, a=number)) # But order it for the sake of simplicity while testing. self.data.sort(key=lambda item: (item.a, item.storeID)) self.model = TestableInequalityModel( self.store, DataThunk, None, [DataThunk.a, DataThunk.b, DataThunk.c], DataThunk.a, True)
def setUp(self): """ Set up a fake objects and methods for the password reset tests. """ siteStore = Store(filesdir=self.mktemp()) Mantissa().installSite(siteStore, u"divmod.com", u"", False) self.loginSystem = siteStore.findUnique(userbase.LoginSystem) self.site = siteStore.findUnique(SiteConfiguration) la = self.loginSystem.addAccount( u'joe', u'divmod.com', u'secret', internal=True) la.addLoginMethod( u'joe', u'external.com', protocol=u'zombie dance', verified=False, internal=False) # create an account with no external mail address account = self.loginSystem.addAccount( u'jill', u'divmod.com', u'secret', internal=True) account.addLoginMethod( u'jill', u'divmod.com', protocol=u'zombie dance', verified=True, internal=True) self.siteStore = siteStore # Set up the user store to have all the elements necessary to redirect # in the case where the user is already logged in. substore = la.avatars.open() installOn(PrivateApplication(store=substore), substore) self.userStore = substore self.loginAccount = la self.nonExternalAccount = account self.reset = PasswordResetResource(self.siteStore)
def test_partDisplayerContentLength(self): """ Test that L{PartDisplayer} sets the C{Content-Length} header on the request. """ s = Store() installOn(PrivateApplication(store=s), s) part = PartItem(store=s, contentType=u'text/plain', bodyLength=31, body=u'x' * 31) partDisplayer = PartDisplayer(None) partDisplayer.item = part req = makeRequest() D = deferredRender(partDisplayer, req) def checkLength(ign): self.assertEqual(int(req.headers.get('content-length')), 31) D.addCallback(checkLength) return D
def _testPartDisplayerScrubbing(self, input, scrub=True): """ Set up a store, a PartItem with a body of C{input}, pass it to the PartDisplayer, render it, and return a deferred that'll fire with the string result of the rendering. @param scrub: if False, the noscrub URL arg will be added to the PartDisplayer request """ s = Store() installOn(PrivateApplication(store=s), s) part = PartItem(store=s, contentType=u'text/html', body=input) pd = PartDisplayer(None) pd.item = part req = makeRequest() if not scrub: req.args = {'noscrub': True} return deferredRender(pd, req)
class WebIDLocationTest(TestCase): def setUp(self): store = Store() ss = SubStore.createNew(store, ['test']).open() self.pa = PrivateApplication(store=ss) installOn(self.pa, ss) self.webViewer = IWebViewer(ss) def test_powersUpTemplateNameResolver(self): """ L{PrivateApplication} implements L{ITemplateNameResolver} and should power up the store it is installed on for that interface. """ self.assertIn( self.pa, self.pa.store.powerupsFor(ITemplateNameResolver)) def test_suchWebID(self): """ Verify that retrieving a webID gives the correct resource. """ i = FakeResourceItem(store=self.pa.store) wid = self.pa.toWebID(i) ctx = FakeRequest() res = self.pa.createResourceWith(self.webViewer) self.assertEqual(res.locateChild(ctx, [wid]), (i, [])) def test_noSuchWebID(self): """ Verify that non-existent private URLs generate 'not found' responses. """ ctx = FakeRequest() for segments in [ # something that looks like a valid webID ['0000000000000000'], # something that doesn't ["nothing-here"], # more than one segment ["two", "segments"]]: res = self.pa.createResourceWith(self.webViewer) self.assertEqual(res.locateChild(ctx, segments), rend.NotFound) def test_webIDForFragment(self): """ Retrieving a webID that specifies a fragment gives the correct resource. """ class FakeView(record("model")): "A fake view that wraps a FakeModelItem." class FakeWebViewer(object): def wrapModel(self, model): return FakeView(model) i = FakeModelItem(store=self.pa.store) wid = self.pa.toWebID(i) ctx = FakeRequest() res = self.pa.createResourceWith(FakeWebViewer()) child, segs = res.locateChild(ctx, [wid]) self.assertIsInstance(child, FakeView) self.assertIdentical(child.model, i)
def setUp(self): store = Store() ss = SubStore.createNew(store, ['test']).open() self.pa = PrivateApplication(store=ss) installOn(self.pa, ss) self.webViewer = IWebViewer(ss)
class GenericNavigationAthenaPageTests(TestCase, AuthenticatedNavigationTestMixin): """ Tests for L{GenericNavigationAthenaPage}. """ def setUp(self): """ Set up a site store, user store, and page instance to test with. """ self.siteStore = Store(filesdir=self.mktemp()) def siteStoreTxn(): Mantissa().installSite(self.siteStore, u"localhost", u"", False) self.userStore = SubStore.createNew( self.siteStore, ['child', 'lookup']).open() self.siteStore.transact(siteStoreTxn) def userStoreTxn(): self.privateApp = PrivateApplication(store=self.userStore) installOn(self.privateApp, self.userStore) self.navpage = self.createPage(None) self.userStore.transact(userStoreTxn) def createPage(self, username): """ Create a L{GenericNavigationAthenaPage} for the given user. """ return GenericNavigationAthenaPage( self.privateApp, TestElement(), self.privateApp.getPageComponents(), username) def rootURL(self, request): """ Return the root URL as reported by C{self.website}. """ return ISiteURLGenerator(self.siteStore).rootURL(request) def test_childLookup(self): """ L{GenericNavigationAthenaPage} should delegate to its fragment and its L{LivePageFactory} when it cannot find a child itself. """ self.navpage.factory = tcf = TestClientFactory( 'client-of-livepage', 'I AM A CLIENT OF THE LIVEPAGE') self.assertEqual(self.navpage.locateChild(None, ('child-of-fragment',)), ('I AM A CHILD OF THE FRAGMENT', ())) self.assertEqual(self.navpage.locateChild(None, (tcf.magicSegment,)), (tcf.returnValue, ())) def test_jsModuleLocation(self): """ L{GenericNavigationAthenaPage.beforeRender} should should call L{xmantissa.website.MantissaLivePage.beforeRender}, which shares its Athena JavaScript module location with all other pages that use L{xmantissa.cachejs}, and provide links to /__jsmodule__/. """ ctx = WovenContext() req = FakeRequest() ctx.remember(req, IRequest) self.navpage.beforeRender(ctx) urlObj = self.navpage.getJSModuleURL('Mantissa') self.assertEqual(urlObj.pathList()[0], '__jsmodule__') def test_beforeRenderDelegation(self): """ L{GenericNavigationAthenaPage.beforeRender} should call C{beforeRender} on the wrapped fragment, if it's defined, and return its result. """ contexts = [] result = defer.succeed(None) def beforeRender(ctx): contexts.append(ctx) return result self.navpage.fragment.beforeRender = beforeRender ctx = WovenContext() ctx.remember(FakeRequest(), IRequest) self.assertIdentical( self.navpage.beforeRender(ctx), result) self.assertEqual(contexts, [ctx])
def userStoreTxn(): self.privateApp = PrivateApplication(store=self.userStore) installOn(self.privateApp, self.userStore) self.navpage = self.createPage(None)
class PrivateApplicationTestCase(TestCase): """ Tests for L{PrivateApplication}. """ def setUp(self): self.siteStore = Store(filesdir=self.mktemp()) Mantissa().installSite(self.siteStore, u"example.com", u"", False) self.userAccount = Create().addAccount( self.siteStore, u'testuser', u'example.com', u'password') self.userStore = self.userAccount.avatars.open() self.privapp = PrivateApplication(store=self.userStore) installOn(self.privapp, self.userStore) self.webViewer = IWebViewer(self.userStore) def test_createResourceUsername(self): """ L{PrivateApplication.createResourceWith} should figure out the right username and pass it to L{_PrivateRootPage}. """ rootPage = self.privapp.createResourceWith(self.webViewer) self.assertEqual(rootPage.username, u'*****@*****.**') def test_getDocFactory(self): """ L{PrivateApplication.getDocFactory} finds a document factory for the specified template name from among the installed themes. """ # Get something from the Mantissa theme self.assertNotIdentical(self.privapp.getDocFactory('shell'), None) # Get rid of the Mantissa offering and make sure the template is no # longer found. self.siteStore.query(InstalledOffering).deleteFromStore() # And flush the cache. :/ -exarkun theThemeCache.emptyCache() self.assertIdentical(self.privapp.getDocFactory('shell'), None) def test_powersUpWebViewer(self): """ L{PrivateApplication} should provide an indirected L{IWebViewer} powerup, and its indirected powerup should be the default provider of that interface. """ webViewer = IWebViewer(self.privapp.store) self.assertIsInstance(webViewer, _AuthenticatedWebViewer) self.assertIdentical(webViewer._privateApplication, self.privapp) def test_producePrivateRoot(self): """ L{PrivateApplication.produceResource} should return a L{_PrivateRootPage} when asked for '/private'. """ rsrc, segments = self.privapp.produceResource(FakeRequest(), tuple(['private']), None) self.assertIsInstance(rsrc, _PrivateRootPage) self.assertEqual(segments, ()) def test_produceRedirect(self): """ L{_PrivateRootPage.produceResource} should return a redirect to '/private/<default-private-id>' when asked for '/'. This is a bad way to do it, because it isn't optional; all logged-in users are instantly redirected to their private page, even if the application has something interesting to display. See ticket #2708 for details. """ item = FakeModelItem(store=self.userStore) class TabThingy(object): implements(INavigableElement) def getTabs(self): return [Tab("supertab", item.storeID, 1.0)] tt = TabThingy() self.userStore.inMemoryPowerUp(tt, INavigableElement) rsrc, segments = self.privapp.produceResource( FakeRequest(), tuple(['']), None) self.assertIsInstance(rsrc, _PrivateRootPage) self.assertEqual(segments, tuple([''])) url, newSegs = rsrc.locateChild(FakeRequest(), ('',)) self.assertEqual(newSegs, ()) req = FakeRequest() target = self.privapp.linkTo(item.storeID) self.assertEqual('/'+url.path, target) def test_produceNothing(self): """ L{_PrivateRootPage.produceResource} should return None when asked for a resources other than '/' and '/private'. """ self.assertIdentical( self.privapp.produceResource(FakeRequest(), tuple(['hello', 'world']), None), None) def test_privateRootHasWebViewer(self): """ The L{_PrivateRootPage} returned from L{PrivateApplication.produceResource} should refer to an L{IWebViewer}. """ webViewer = object() rsrc, segments = self.privapp.produceResource( FakeRequest(), tuple(['private']), webViewer) self.assertIdentical(webViewer, rsrc.webViewer)
def userStoreTxn(): self.privateApp = PrivateApplication(store=self.userStore) installOn(self.privateApp, self.userStore)
def createDatabase(s): PrivateApplication(store=s)