comparison idavoll/test/test_backend.py @ 222:698af5d720ad

Reshape Idavoll as a PubSubResource. PubSubResource is Wokkel's newer interface for building (parts of) XMPP publish-subscribe services and replaces the old interface of PubSubService. It is more flexible for adding new protocol, allows for node-as-code (providing a specific backend per node), and permits accepting requests for different entities (virtual hosts or PEP-like settings). This moves over the current backend to use the new interface, so new code for previously unsupported protocol can be added down the line.
author Ralph Meijer <ralphm@ik.nu>
date Sat, 16 Oct 2010 21:03:38 +0200
parents e59b48f3f636
children 0eafdced5f24
comparison
equal deleted inserted replaced
221:a430976f2977 222:698af5d720ad
445 def registerPreDelete(self, preDeleteFn): 445 def registerPreDelete(self, preDeleteFn):
446 return 446 return
447 447
448 448
449 449
450 class PubSubServiceFromBackendTest(unittest.TestCase): 450 class PubSubResourceFromBackendTest(unittest.TestCase):
451 451
452 def test_interfaceIBackend(self): 452 def test_interface(self):
453 s = backend.PubSubServiceFromBackend(BaseTestBackend()) 453 resource = backend.PubSubResourceFromBackend(BaseTestBackend())
454 self.assertTrue(verifyObject(iwokkel.IPubSubService, s)) 454 self.assertTrue(verifyObject(iwokkel.IPubSubResource, resource))
455 455
456 456
457 def test_preDelete(self): 457 def test_preDelete(self):
458 """ 458 """
459 Test pre-delete sending out notifications to subscribers. 459 Test pre-delete sending out notifications to subscribers.
475 self.assertEqual([OWNER], subscribers) 475 self.assertEqual([OWNER], subscribers)
476 self.assertIdentical(None, redirectURI) 476 self.assertIdentical(None, redirectURI)
477 d1.callback(None) 477 d1.callback(None)
478 478
479 d1 = defer.Deferred() 479 d1 = defer.Deferred()
480 s = backend.PubSubServiceFromBackend(TestBackend()) 480 resource = backend.PubSubResourceFromBackend(TestBackend())
481 s.serviceJID = SERVICE 481 resource.serviceJID = SERVICE
482 s.notifyDelete = notifyDelete 482 resource.pubsubService = pubsub.PubSubService()
483 self.assertTrue(verifyObject(iwokkel.IPubSubService, s)) 483 resource.pubsubService.notifyDelete = notifyDelete
484 self.assertNotIdentical(None, s.backend.preDeleteFn) 484 self.assertTrue(verifyObject(iwokkel.IPubSubResource, resource))
485 self.assertNotIdentical(None, resource.backend.preDeleteFn)
485 data = {'nodeIdentifier': 'test'} 486 data = {'nodeIdentifier': 'test'}
486 d2 = s.backend.preDeleteFn(data) 487 d2 = resource.backend.preDeleteFn(data)
487 return defer.DeferredList([d1, d2], fireOnOneErrback=1) 488 return defer.DeferredList([d1, d2], fireOnOneErrback=1)
488 489
489 490
490 def test_preDeleteRedirect(self): 491 def test_preDeleteRedirect(self):
491 """ 492 """
510 self.assertEqual([OWNER], subscribers) 511 self.assertEqual([OWNER], subscribers)
511 self.assertEqual(uri, redirectURI) 512 self.assertEqual(uri, redirectURI)
512 d1.callback(None) 513 d1.callback(None)
513 514
514 d1 = defer.Deferred() 515 d1 = defer.Deferred()
515 s = backend.PubSubServiceFromBackend(TestBackend()) 516 resource = backend.PubSubResourceFromBackend(TestBackend())
516 s.serviceJID = SERVICE 517 resource.serviceJID = SERVICE
517 s.notifyDelete = notifyDelete 518 resource.pubsubService = pubsub.PubSubService()
518 self.assertTrue(verifyObject(iwokkel.IPubSubService, s)) 519 resource.pubsubService.notifyDelete = notifyDelete
519 self.assertNotIdentical(None, s.backend.preDeleteFn) 520 self.assertTrue(verifyObject(iwokkel.IPubSubResource, resource))
521 self.assertNotIdentical(None, resource.backend.preDeleteFn)
520 data = {'nodeIdentifier': 'test', 522 data = {'nodeIdentifier': 'test',
521 'redirectURI': uri} 523 'redirectURI': uri}
522 d2 = s.backend.preDeleteFn(data) 524 d2 = resource.backend.preDeleteFn(data)
523 return defer.DeferredList([d1, d2], fireOnOneErrback=1) 525 return defer.DeferredList([d1, d2], fireOnOneErrback=1)
524 526
525 527
526 def test_unsubscribeNotSubscribed(self): 528 def test_unsubscribeNotSubscribed(self):
527 """ 529 """
533 return defer.fail(error.NotSubscribed()) 535 return defer.fail(error.NotSubscribed())
534 536
535 def cb(e): 537 def cb(e):
536 self.assertEquals('unexpected-request', e.condition) 538 self.assertEquals('unexpected-request', e.condition)
537 539
538 s = backend.PubSubServiceFromBackend(TestBackend()) 540 resource = backend.PubSubResourceFromBackend(TestBackend())
539 d = s.unsubscribe(OWNER, SERVICE, 'test', OWNER) 541 request = pubsub.PubSubRequest()
542 request.sender = OWNER
543 request.recipient = SERVICE
544 request.nodeIdentifier = 'test'
545 request.subscriber = OWNER
546 d = resource.unsubscribe(request)
540 self.assertFailure(d, StanzaError) 547 self.assertFailure(d, StanzaError)
541 d.addCallback(cb) 548 d.addCallback(cb)
542 return d 549 return d
543 550
544 551
545 def test_getNodeInfo(self): 552 def test_getInfo(self):
546 """ 553 """
547 Test retrieving node information. 554 Test retrieving node information.
548 """ 555 """
549 556
550 class TestBackend(BaseTestBackend): 557 class TestBackend(BaseTestBackend):
558 self.assertIn('type', info) 565 self.assertIn('type', info)
559 self.assertEquals('leaf', info['type']) 566 self.assertEquals('leaf', info['type'])
560 self.assertIn('meta-data', info) 567 self.assertIn('meta-data', info)
561 self.assertEquals({'pubsub#persist_items': True}, info['meta-data']) 568 self.assertEquals({'pubsub#persist_items': True}, info['meta-data'])
562 569
563 s = backend.PubSubServiceFromBackend(TestBackend()) 570 resource = backend.PubSubResourceFromBackend(TestBackend())
564 d = s.getNodeInfo(OWNER, SERVICE, 'test') 571 d = resource.getInfo(OWNER, SERVICE, 'test')
565 d.addCallback(cb) 572 d.addCallback(cb)
566 return d 573 return d
567 574
568 575
569 def test_getConfigurationOptions(self): 576 def test_getConfigurationOptions(self):
575 "pubsub#deliver_payloads": 582 "pubsub#deliver_payloads":
576 {"type": "boolean", 583 {"type": "boolean",
577 "label": "Deliver payloads with event notifications"} 584 "label": "Deliver payloads with event notifications"}
578 } 585 }
579 586
580 s = backend.PubSubServiceFromBackend(TestBackend()) 587 resource = backend.PubSubResourceFromBackend(TestBackend())
581 options = s.getConfigurationOptions() 588 options = resource.getConfigurationOptions()
582 self.assertIn("pubsub#persist_items", options) 589 self.assertIn("pubsub#persist_items", options)
583 590
584 591
585 def test_getDefaultConfiguration(self): 592 def test_default(self):
586 class TestBackend(BaseTestBackend): 593 class TestBackend(BaseTestBackend):
587 def getDefaultConfiguration(self, nodeType): 594 def getDefaultConfiguration(self, nodeType):
588 options = {"pubsub#persist_items": True, 595 options = {"pubsub#persist_items": True,
589 "pubsub#deliver_payloads": True, 596 "pubsub#deliver_payloads": True,
590 "pubsub#send_last_published_item": 'on_sub', 597 "pubsub#send_last_published_item": 'on_sub',
592 return defer.succeed(options) 599 return defer.succeed(options)
593 600
594 def cb(options): 601 def cb(options):
595 self.assertEquals(True, options["pubsub#persist_items"]) 602 self.assertEquals(True, options["pubsub#persist_items"])
596 603
597 s = backend.PubSubServiceFromBackend(TestBackend()) 604 resource = backend.PubSubResourceFromBackend(TestBackend())
598 d = s.getDefaultConfiguration(OWNER, SERVICE, 'leaf') 605 request = pubsub.PubSubRequest()
599 d.addCallback(cb) 606 request.sender = OWNER
600 return d 607 request.recipient = SERVICE
608 request.nodeType = 'leaf'
609 d = resource.default(request)
610 d.addCallback(cb)
611 return d