def _store(self): store = Store(self.mktemp()) store.inMemoryPowerUp(NullUploadScheduler(), IUploadScheduler) contentStore = ContentStore(store=store) store.powerUp(contentStore, IContentStore) object.__setattr__(contentStore, '_deferToThreadPool', execute) return contentStore
class CredentialsCheckerTests(SynchronousTestCase): """ Test cases for the password credentials checker. """ def setUp(self): self.rootStore = Store() self.checker = password.CredentialsChecker(store=self.rootStore) self.userStore = Store() self.user = common.User(store=self.userStore, identifier="uid") hashedPassword = FakeUsernameHashedPassword("password") self.userStore.inMemoryPowerUp(hashedPassword, IUsernameHashedPassword) directory = FakeWriteLockDirectory(self.userStore) self.rootStore.inMemoryPowerUp(directory, IWriteLockDirectory) def _requestAvatarId(self, username, password): """ Requests an avatar id with the given username and password (which should be UTF-8 encoded bytestrings). """ credentials = UsernamePassword(username, password) return defer.maybeDeferred(self.checker.requestAvatarId, credentials) def test_requestAvatarId(self): """ A user can request an avatar id using a user identifier and password. """ d = self._requestAvatarId("uid", "password") uid = self.successResultOf(d) self.assertIdentical(uid, "uid") def test_requestAvatarIdWithBadPassword(self): """ Requests an avatar id for a user that exists, but with the wrong password. """ d = self._requestAvatarId("uid", "BOGUS") self.failureResultOf(d, UnauthorizedLogin) def test_requestAvatarIdForMissingUser(self): """ Requests an avatar id for a missing user. """ d = self._requestAvatarId("BOGUS", "BOGUS") self.failureResultOf(d, UnauthorizedLogin)
class TestSiteTemplateResolver(TestCase): """ Tests for L{SiteTemplateResolver} """ def setUp(self): """ Create a L{Store} with a fake L{IOfferingTechnician} powerup which allows fine-grained control of template name resolution. """ self.offeringTech = FakeOfferingTechnician() self.store = Store() self.store.inMemoryPowerUp(self.offeringTech, IOfferingTechnician) self.siteResolver = SiteTemplateResolver(self.store) def getDocFactoryWithoutCaching(self, templateName): """ Use C{self.siteResolver} to get a loader for the named template, flushing the template cache first in order to make the result reflect any changes which in offering or theme availability which may have happened since the last call. """ webtheme.theThemeCache.emptyCache() return self.siteResolver.getDocFactory(templateName) def test_getDocFactory(self): """ L{SiteTemplateResolver.getDocFactory} should return only installed themes for its store. """ class FakeTheme(object): priority = 0 def getDocFactory(self, templateName, default=None): if templateName == 'shell': return object() return default self.assertIdentical(self.getDocFactoryWithoutCaching('shell'), None) self.offeringTech.installOffering( Offering( u'an offering', None, [], [], [], [], [FakeTheme()])) self.assertNotIdentical(self.getDocFactoryWithoutCaching('shell'), None)
class TestSiteTemplateResolver(TestCase): """ Tests for L{SiteTemplateResolver} """ def setUp(self): """ Create a L{Store} with a fake L{IOfferingTechnician} powerup which allows fine-grained control of template name resolution. """ self.offeringTech = FakeOfferingTechnician() self.store = Store() self.store.inMemoryPowerUp(self.offeringTech, IOfferingTechnician) self.siteResolver = SiteTemplateResolver(self.store) def getDocFactoryWithoutCaching(self, templateName): """ Use C{self.siteResolver} to get a loader for the named template, flushing the template cache first in order to make the result reflect any changes which in offering or theme availability which may have happened since the last call. """ webtheme.theThemeCache.emptyCache() return self.siteResolver.getDocFactory(templateName) def test_getDocFactory(self): """ L{SiteTemplateResolver.getDocFactory} should return only installed themes for its store. """ class FakeTheme(object): priority = 0 def getDocFactory(self, templateName, default=None): if templateName == 'shell': return object() return default self.assertIdentical(self.getDocFactoryWithoutCaching('shell'), None) self.offeringTech.installOffering( Offering(u'an offering', None, [], [], [], [], [FakeTheme()])) self.assertNotIdentical(self.getDocFactoryWithoutCaching('shell'), None)
class UnguardedWrapperTests(TestCase): """ Tests for L{UnguardedWrapper}. """ def setUp(self): """ Set up a store with a valid offering to test against. """ self.store = Store() installOffering(self.store, baseOffering, {}) self.site = ISiteURLGenerator(self.store) def test_live(self): """ L{UnguardedWrapper} has a I{live} child which returns a L{LivePage} instance. """ request = FakeRequest(uri='/live/foo', currentSegments=[]) wrapper = UnguardedWrapper(self.store, None) resource = wrapper.child_live(request) self.assertTrue(isinstance(resource, LivePage)) def test_jsmodules(self): """ L{UnguardedWrapper} has a I{__jsmodules__} child which returns a L{LivePage} instance. """ request = FakeRequest(uri='/__jsmodule__/foo', currentSegments=[]) wrapper = UnguardedWrapper(None, None) resource = wrapper.child___jsmodule__(request) # This is weak. Identity of this object doesn't matter. The caching # and jsmodule serving features are what matter. -exarkun self.assertIdentical(resource, theHashModuleProvider) def test_static(self): """ L{UnguardedWrapper} has a I{static} child which returns a L{StaticContent} instance. """ request = FakeRequest(uri='/static/extra', currentSegments=[]) wrapper = UnguardedWrapper(self.store, None) resource = wrapper.child_static(request) self.assertTrue(isinstance(resource, StaticContent)) self.assertEqual(resource.staticPaths, {baseOffering.name: baseOffering.staticContentPath}) def test_sessionlessPlugin(self): """ L{UnguardedWrapper.locateChild} looks up L{ISessionlessSiteRootPlugin} powerups on its store, and invokes their C{sessionlessProduceResource} methods to discover resources. """ wrapper = UnguardedWrapper(self.store, None) req = FakeRequest() segments = ('foo', 'bar') calledWith = [] result = object() class SiteRootPlugin(object): def sessionlessProduceResource(self, request, segments): calledWith.append((request, segments)) return result self.store.inMemoryPowerUp(SiteRootPlugin(), ISessionlessSiteRootPlugin) resource = wrapper.locateChild(req, segments) self.assertEqual(calledWith, [(req, ("foo", "bar"))]) def test_sessionlessLegacyPlugin(self): """ L{UnguardedWrapper.locateChild} honors old-style L{ISessionlessSiteRootPlugin} providers that only implement a C{resourceFactory} method. """ wrapper = UnguardedWrapper(self.store, None) req = FakeRequest() segments = ('foo', 'bar') calledWith = [] result = object() class SiteRootPlugin(object): def resourceFactory(self, segments): calledWith.append(segments) return result self.store.inMemoryPowerUp(SiteRootPlugin(), ISessionlessSiteRootPlugin) resource = wrapper.locateChild(req, segments) self.assertEqual(calledWith, [segments]) def test_confusedNewPlugin(self): """ If L{UnguardedWrapper.locateChild} discovers a plugin that implements both C{sessionlessProduceResource} and C{resourceFactory}, it should prefer the new C{sessionlessProduceResource} method and return that resource. """ wrapper = UnguardedWrapper(self.store, None) req = FakeRequest() test = self segments = ('foo', 'bar') result = object() calledWith = [] class SiteRootPlugin(object): def resourceFactory(self, segments): test.fail("Don't call this.") def sessionlessProduceResource(self, request, segments): calledWith.append((request, segments)) return result, segments[1:] self.store.inMemoryPowerUp(SiteRootPlugin(), ISessionlessSiteRootPlugin) resource, resultSegments = wrapper.locateChild(req, segments) self.assertEqual(calledWith, [(req, segments)]) self.assertIdentical(resource, result) self.assertEqual(resultSegments, ('bar', ))
class UnguardedWrapperTests(TestCase): """ Tests for L{UnguardedWrapper}. """ def setUp(self): """ Set up a store with a valid offering to test against. """ self.store = Store() installOffering(self.store, baseOffering, {}) self.site = ISiteURLGenerator(self.store) def test_live(self): """ L{UnguardedWrapper} has a I{live} child which returns a L{LivePage} instance. """ request = FakeRequest(uri='/live/foo', currentSegments=[]) wrapper = UnguardedWrapper(self.store, None) resource = wrapper.child_live(request) self.assertTrue(isinstance(resource, LivePage)) def test_jsmodules(self): """ L{UnguardedWrapper} has a I{__jsmodules__} child which returns a L{LivePage} instance. """ request = FakeRequest(uri='/__jsmodule__/foo', currentSegments=[]) wrapper = UnguardedWrapper(None, None) resource = wrapper.child___jsmodule__(request) # This is weak. Identity of this object doesn't matter. The caching # and jsmodule serving features are what matter. -exarkun self.assertIdentical(resource, theHashModuleProvider) def test_static(self): """ L{UnguardedWrapper} has a I{static} child which returns a L{StaticContent} instance. """ request = FakeRequest(uri='/static/extra', currentSegments=[]) wrapper = UnguardedWrapper(self.store, None) resource = wrapper.child_static(request) self.assertTrue(isinstance(resource, StaticContent)) self.assertEqual( resource.staticPaths, {baseOffering.name: baseOffering.staticContentPath}) def test_sessionlessPlugin(self): """ L{UnguardedWrapper.locateChild} looks up L{ISessionlessSiteRootPlugin} powerups on its store, and invokes their C{sessionlessProduceResource} methods to discover resources. """ wrapper = UnguardedWrapper(self.store, None) req = FakeRequest() segments = ('foo', 'bar') calledWith = [] result = object() class SiteRootPlugin(object): def sessionlessProduceResource(self, request, segments): calledWith.append((request, segments)) return result self.store.inMemoryPowerUp(SiteRootPlugin(), ISessionlessSiteRootPlugin) wrapper.locateChild(req, segments) self.assertEqual(calledWith, [(req, ("foo", "bar"))]) def test_sessionlessLegacyPlugin(self): """ L{UnguardedWrapper.locateChild} honors old-style L{ISessionlessSiteRootPlugin} providers that only implement a C{resourceFactory} method. """ wrapper = UnguardedWrapper(self.store, None) req = FakeRequest() segments = ('foo', 'bar') calledWith = [] result = object() class SiteRootPlugin(object): def resourceFactory(self, segments): calledWith.append(segments) return result self.store.inMemoryPowerUp(SiteRootPlugin(), ISessionlessSiteRootPlugin) wrapper.locateChild(req, segments) self.assertEqual(calledWith, [segments]) def test_confusedNewPlugin(self): """ If L{UnguardedWrapper.locateChild} discovers a plugin that implements both C{sessionlessProduceResource} and C{resourceFactory}, it should prefer the new C{sessionlessProduceResource} method and return that resource. """ wrapper = UnguardedWrapper(self.store, None) req = FakeRequest() test = self segments = ('foo', 'bar') result = object() calledWith = [] class SiteRootPlugin(object): def resourceFactory(self, segments): test.fail("Don't call this.") def sessionlessProduceResource(self, request, segments): calledWith.append((request, segments)) return result, segments[1:] self.store.inMemoryPowerUp(SiteRootPlugin(), ISessionlessSiteRootPlugin) resource, resultSegments = wrapper.locateChild(req, segments) self.assertEqual(calledWith, [(req, segments)]) self.assertIdentical(resource, result) self.assertEqual(resultSegments, ('bar',))
class StoreBackendTests(TestCase): """ Tests for content store backend functionality. """ def setUp(self): self.store = Store(self.mktemp()) self.contentStore1 = ContentStore(store=self.store) object.__setattr__(self.contentStore1, '_deferToThreadPool', execute) self.contentStore1.storeObject(content='somecontent', contentType=u'application/octet-stream') self.testObject = self.store.findUnique(ImmutableObject) self.contentStore2 = ContentStore(store=self.store) object.__setattr__(self.contentStore2, '_deferToThreadPool', execute) def test_getSiblingExists(self): """ Calling getSiblingObject with an object ID that is present in the local store just returns the local object. """ d = self.contentStore1.getSiblingObject(self.testObject.objectId) def _cb(o): self.o = o d.addCallback(_cb) self.assertIdentical(self.o, self.testObject) def _retrievalTest(self): o = self.successResultOf( self.contentStore2.getSiblingObject(self.testObject.objectId)) self.assertEquals( self.successResultOf(o.getContent()), 'somecontent') o2 = self.successResultOf( self.contentStore2.getObject(self.testObject.objectId)) self.assertIdentical(o, o2) def test_getSiblingExistsRemote(self): """ Calling getSiblingObject with an object ID that is missing locally, but present in one of the sibling stores, will retrieve the object, as well as inserting it into the local store. """ self.store.powerUp(self.contentStore1, ISiblingStore) self._retrievalTest() def test_getSiblingExistsBackend(self): """ If an object is missing in local and sibling stores, but present in a backend store, the object will be retrieved from the backend store. """ self.store.powerUp(self.contentStore1, IBackendStore) self._retrievalTest() def test_siblingBeforeBackend(self): """ When looking for a missing object, sibling stores are tried before backend stores. """ events = [] siblingStore = MockContentStore(store=self.store, events=events) self.store.powerUp(siblingStore, ISiblingStore) backendStore = MockContentStore(store=self.store, events=events) self.store.powerUp(backendStore, IBackendStore) def _cb(e): self.assertEquals( events, [('getObject', siblingStore, u'sha256:aoeuaoeu'), ('getObject', backendStore, u'sha256:aoeuaoeu')]) return self.assertFailure( self.contentStore2.getSiblingObject(u'sha256:aoeuaoeu'), NonexistentObject).addCallback(_cb) def test_getSiblingMissing(self): """ Calling getSiblingObject with an object ID that is missing everywhere raises L{NonexistentObject}. """ self.store.powerUp(self.contentStore1, ISiblingStore) objectId = u'sha256:NOSUCHOBJECT' d = self.contentStore2.getSiblingObject(objectId) return self.assertFailure(d, NonexistentObject ).addCallback(lambda e: self.assertEquals(e.objectId, objectId)) def test_storeObject(self): """ Storing an object also causes it to be scheduled for storing in all backend stores. """ contentStore = ContentStore(store=self.store) backendStore = MockContentStore(store=self.store) self.store.powerUp(backendStore, IBackendStore) backendStore2 = MockContentStore(store=self.store) self.store.powerUp(backendStore2, IBackendStore) scheduler = MockUploadScheduler() self.store.inMemoryPowerUp(scheduler, IUploadScheduler) contentStore.storeObject(content='somecontent', contentType=u'application/octet-stream') testObject = self.store.findUnique(ImmutableObject) pu = scheduler.uploads self.assertEquals(len(pu), 2) self.assertEquals(pu[0][0], testObject.objectId) self.assertEquals(pu[1][0], testObject.objectId) for objectId, backend in pu: if backend is backendStore: break else: self.fail('No pending upload for backendStore') for objectId, backend in pu: if backend is backendStore2: break else: self.fail('No pending upload for backendStore2')