Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)