def test_getNotificationsMultipleNodes(self): """ Ensure that entities that subscribe to a leaf node as well as the root node get exactly one notification. """ item = pubsub.Item() sub = pubsub.Subscription('test', OWNER, 'subscribed') subRoot = pubsub.Subscription('', OWNER, 'subscribed') class TestNode: def getSubscriptions(self, state=None): return [sub] class TestRootNode: def getSubscriptions(self, state=None): return [subRoot] def cb(result): self.assertEquals(1, len(result)) subscriber, subscriptions, items = result[-1] self.assertEquals(OWNER, subscriber) self.assertEquals(set([sub, subRoot]), subscriptions) self.assertEquals([item], items) self.storage = self.NodeStore({'test': TestNode(), '': TestRootNode()}) self.backend = backend.BackendService(self.storage) d = self.backend.getNotifications('test', [item]) d.addCallback(cb) return d
def test_publishNoID(self): """ Test publish request with an item without a node identifier. """ class TestNode: nodeType = 'leaf' nodeIdentifier = 'node' def getAffiliation(self, entity): if entity.userhostJID() == OWNER: return defer.succeed('owner') def getConfiguration(self): return { 'pubsub#deliver_payloads': True, 'pubsub#persist_items': False } class TestStorage: def getNode(self, nodeIdentifier): return defer.succeed(TestNode()) def checkID(notification): self.assertNotIdentical(None, notification['items'][0]['id']) self.storage = TestStorage() self.backend = backend.BackendService(self.storage) self.storage.backend = self.backend self.backend.registerNotifier(checkID) items = [pubsub.Item()] d = self.backend.publish('node', items, OWNER_FULL) return d
def test_getNodeConfiguration(self): class testNode: nodeIdentifier = 'node' def getConfiguration(self): return { 'pubsub#deliver_payloads': True, 'pubsub#persist_items': False } class testStorage: def getNode(self, nodeIdentifier): return defer.succeed(testNode()) def cb(options): self.assertIn("pubsub#deliver_payloads", options) self.assertEqual(True, options["pubsub#deliver_payloads"]) self.assertIn("pubsub#persist_items", options) self.assertEqual(False, options["pubsub#persist_items"]) self.storage = testStorage() self.backend = backend.BackendService(self.storage) self.storage.backend = self.backend d = self.backend.getNodeConfiguration('node') d.addCallback(cb) return d
def test_getNotificationsRoot(self): """ Ensure subscribers to the root node show up in the notification list for leaf nodes. This assumes a flat node relationship model with exactly one collection node: the root node. Each leaf node is automatically a child node of the root node. """ item = pubsub.Item() subRoot = pubsub.Subscription('', OWNER, 'subscribed') class TestNode: def getSubscriptions(self, state=None): return [] class TestRootNode: def getSubscriptions(self, state=None): return [subRoot] def cb(result): self.assertEquals(1, len(result)) subscriber, subscriptions, items = result[-1] self.assertEquals(OWNER, subscriber) self.assertEquals(set([subRoot]), subscriptions) self.assertEquals([item], items) self.storage = self.NodeStore({'test': TestNode(), '': TestRootNode()}) self.backend = backend.BackendService(self.storage) d = self.backend.getNotifications('test', [item]) d.addCallback(cb) return d
def test_notifyOnSubscription(self): """ Test notification of last published item on subscription. """ ITEM = "<item xmlns='%s' id='1'/>" % NS_PUBSUB class TestNode: implements(iidavoll.ILeafNode) nodeIdentifier = 'node' nodeType = 'leaf' def getAffiliation(self, entity): if entity is OWNER: return defer.succeed('owner') def getConfiguration(self): return { 'pubsub#deliver_payloads': True, 'pubsub#persist_items': False, 'pubsub#send_last_published_item': 'on_sub' } def getItems(self, maxItems): return [ITEM] def addSubscription(self, subscriber, state, options): self.subscription = pubsub.Subscription( 'node', subscriber, state, options) return defer.succeed(None) def getSubscription(self, subscriber): return defer.succeed(self.subscription) class TestStorage: def getNode(self, nodeIdentifier): return defer.succeed(TestNode()) def cb(data): self.assertEquals('node', data['nodeIdentifier']) self.assertEquals([ITEM], data['items']) self.assertEquals(OWNER, data['subscription'].subscriber) self.storage = TestStorage() self.backend = backend.BackendService(self.storage) self.storage.backend = self.backend d1 = defer.Deferred() d1.addCallback(cb) self.backend.registerNotifier(d1.callback) d2 = self.backend.subscribe('node', OWNER, OWNER_FULL) return defer.gatherResults([d1, d2])
def test_deleteNodeRedirect(self): uri = 'xmpp:%s?;node=test2' % (SERVICE.full(), ) class TestNode: nodeIdentifier = 'to-be-deleted' def getAffiliation(self, entity): if entity.userhostJID() == OWNER: return defer.succeed('owner') class TestStorage: def __init__(self): self.deleteCalled = [] def getNode(self, nodeIdentifier): return defer.succeed(TestNode()) def deleteNode(self, nodeIdentifier): if nodeIdentifier in ['to-be-deleted']: self.deleteCalled.append(nodeIdentifier) return defer.succeed(None) else: return defer.fail(error.NodeNotFound()) def preDelete(data): self.assertFalse(self.storage.deleteCalled) preDeleteCalled.append(data) return defer.succeed(None) def cb(result): self.assertEquals(1, len(preDeleteCalled)) data = preDeleteCalled[-1] self.assertEquals('to-be-deleted', data['nodeIdentifier']) self.assertEquals(uri, data['redirectURI']) self.assertTrue(self.storage.deleteCalled) self.storage = TestStorage() self.backend = backend.BackendService(self.storage) preDeleteCalled = [] self.backend.registerPreDelete(preDelete) d = self.backend.deleteNode('to-be-deleted', OWNER, redirectURI=uri) d.addCallback(cb) return d
def test_setNodeConfiguration(self): class testNode: nodeIdentifier = 'node' def getAffiliation(self, entity): if entity.userhostJID() == OWNER: return defer.succeed('owner') def setConfiguration(self, options): self.options = options class testStorage: def __init__(self): self.nodes = {'node': testNode()} def getNode(self, nodeIdentifier): return defer.succeed(self.nodes[nodeIdentifier]) def checkOptions(node): options = node.options self.assertIn("pubsub#deliver_payloads", options) self.assertEqual(True, options["pubsub#deliver_payloads"]) self.assertIn("pubsub#persist_items", options) self.assertEqual(False, options["pubsub#persist_items"]) def cb(result): d = self.storage.getNode('node') d.addCallback(checkOptions) return d self.storage = testStorage() self.backend = backend.BackendService(self.storage) self.storage.backend = self.backend options = { 'pubsub#deliver_payloads': True, 'pubsub#persist_items': False } d = self.backend.setNodeConfiguration('node', options, OWNER_FULL) d.addCallback(cb) return d
def test_getDefaultConfiguration(self): """ L{backend.BackendService.getDefaultConfiguration} should return a deferred that fires a dictionary with configuration values. """ class TestStorage: def getDefaultConfiguration(self, nodeType): return { "pubsub#persist_items": True, "pubsub#deliver_payloads": True } def cb(options): self.assertIn("pubsub#persist_items", options) self.assertEqual(True, options["pubsub#persist_items"]) self.backend = backend.BackendService(TestStorage()) d = self.backend.getDefaultConfiguration('leaf') d.addCallback(cb) return d
def test_createNodeNoID(self): """ Test creation of a node without a given node identifier. """ class TestStorage: def getDefaultConfiguration(self, nodeType): return {} def createNode(self, nodeIdentifier, requestor, config): self.nodeIdentifier = nodeIdentifier return defer.succeed(None) self.storage = TestStorage() self.backend = backend.BackendService(self.storage) self.storage.backend = self.backend def checkID(nodeIdentifier): self.assertNotIdentical(None, nodeIdentifier) self.assertIdentical(self.storage.nodeIdentifier, nodeIdentifier) d = self.backend.createNode(None, OWNER_FULL) d.addCallback(checkID) return d
def test_getNotifications(self): """ Ensure subscribers show up in the notification list. """ item = pubsub.Item() sub = pubsub.Subscription('test', OWNER, 'subscribed') class TestNode: def getSubscriptions(self, state=None): return [sub] def cb(result): self.assertEquals(1, len(result)) subscriber, subscriptions, items = result[-1] self.assertEquals(OWNER, subscriber) self.assertEquals(set([sub]), subscriptions) self.assertEquals([item], items) self.storage = self.NodeStore({'test': TestNode()}) self.backend = backend.BackendService(self.storage) d = self.backend.getNotifications('test', [item]) d.addCallback(cb) return d
def test_interfaceIBackend(self): self.assertTrue( verifyObject(iidavoll.IBackendService, backend.BackendService(None)))