Changeset 24:ada867483400


Ignore:
Timestamp:
Jun 25, 2008, 1:06:55 PM (13 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@54
Message:

PubSubService?.items should return a list of Elements representing items.

Location:
wokkel
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • wokkel/generic.py

    r20 r24  
    1313from twisted.words.protocols.jabber import error
    1414from twisted.words.protocols.jabber.xmlstream import toResponse
     15from twisted.words.xish import domish
    1516
    1617from wokkel import disco
     
    2324NS_VERSION = 'jabber:iq:version'
    2425VERSION = IQ_GET + '/query[@xmlns="' + NS_VERSION + '"]'
     26
     27def parseXml(string):
     28    """
     29    Parse serialized XML into a DOM structure.
     30
     31    @param string: The serialized XML to be parsed, UTF-8 encoded.
     32    @type string: C{str}.
     33    @return: The DOM structure, or C{None} on empty or incomplete input.
     34    @rtype: L{domish.Element}
     35    """
     36    roots = []
     37    results = []
     38    elementStream = domish.elementStream()
     39    elementStream.DocumentStartEvent = roots.append
     40    elementStream.ElementEvent = lambda elem: roots[0].addChild(elem)
     41    elementStream.DocumentEndEvent = lambda: results.append(roots[0])
     42    elementStream.parse(string)
     43    return results and results[0] or None
     44
     45
    2546
    2647class FallbackHandler(XMPPHandler):
  • wokkel/iwokkel.py

    r16 r24  
    156156        """
    157157
    158     def deleteReceived(recipient, service, nodeIdentifier, items):
     158    def deleteReceived(recipient, service, nodeIdentifier):
    159159        """
    160160        Called when a deletion notification has been received for a node.
     
    168168        """
    169169
    170     def purgeReceived(recipient, service, nodeIdentifier, items):
     170    def purgeReceived(recipient, service, nodeIdentifier):
    171171        """
    172172        Called when a purge notification has been received for a node.
  • wokkel/pubsub.py

    r23 r24  
    712712
    713713            for item in result:
    714                 items.addRawXml(item)
     714                items.addChild(item)
    715715
    716716            return response
  • wokkel/test/test_pubsub.py

    r19 r24  
    55Tests for L{wokkel.pubsub}
    66"""
     7from zope.interface import verify
    78
    89from twisted.trial import unittest
    910from twisted.internet import defer
    10 from twisted.words.xish import domish
     11from twisted.words.xish import domish, xpath
    1112from twisted.words.protocols.jabber import error
    1213from twisted.words.protocols.jabber.jid import JID
    1314
    14 from wokkel import pubsub
     15from wokkel import iwokkel, pubsub
     16from wokkel.generic import parseXml
    1517from wokkel.test.helpers import XmlStreamStub
    1618
     
    5153
    5254
    53     def test_event_items(self):
     55    def test_interface(self):
     56        """
     57        Do instances of L{pubsub.PubSubClient} provide L{iwokkel.IPubSubClient}?
     58        """
     59        verify.verifyObject(iwokkel.IPubSubClient, self.protocol)
     60
     61
     62    def test_eventItems(self):
    5463        """
    5564        Test receiving an items event resulting in a call to itemsReceived.
     
    432441
    433442class PubSubServiceTest(unittest.TestCase):
    434 
    435     def setUp(self):
    436         self.output = []
    437 
    438     def send(self, obj):
    439         self.output.append(obj)
     443    """
     444    Tests for L{pubsub.PubSubService}.
     445    """
     446
     447    def handleRequest(self, handler, iq):
     448        """
     449        Find a handler and call it directly
     450        """
     451        for queryString, method in handler.iqHandlers.iteritems():
     452            if xpath.internQuery(queryString).matches(iq):
     453                handler = getattr(handler, method)
     454
     455        if handler:
     456            d = defer.maybeDeferred(handler, iq)
     457        else:
     458            d = defer.fail(NotImplementedError())
     459
     460        return d
     461
     462
     463    def test_interface(self):
     464        """
     465        Do instances of L{pubsub.PubSubService} provide L{iwokkel.IPubSubService}?
     466        """
     467        verify.verifyObject(iwokkel.IPubSubService, pubsub.PubSubService())
     468
    440469
    441470    def test_onPublishNoNode(self):
     471        """
     472        The root node is always a collection, publishing is a bad request.
     473        """
     474        xml = """
     475        <iq type='set' to='pubsub.example.org'
     476                       from='user@example.org'>
     477          <pubsub xmlns='http://jabber.org/protocol/pubsub'>
     478            <publish/>
     479          </pubsub>
     480        </iq>
     481        """
     482
     483        def cb(result):
     484            self.assertEquals('bad-request', result.condition)
     485
    442486        handler = pubsub.PubSubService()
    443         handler.parent = self
    444         iq = domish.Element((None, 'iq'))
    445         iq['from'] = 'user@example.org'
    446         iq['to'] = 'pubsub.example.org'
    447         iq['type'] = 'set'
    448         iq.addElement((NS_PUBSUB, 'pubsub'))
    449         iq.pubsub.addElement('publish')
    450         handler.handleRequest(iq)
    451 
    452         e = error.exceptionFromStanza(self.output[-1])
    453         self.assertEquals('bad-request', e.condition)
     487        d = self.handleRequest(handler, parseXml(xml))
     488        self.assertFailure(d, error.StanzaError)
     489        d.addCallback(cb)
     490        return d
    454491
    455492    def test_onPublish(self):
     493        """
     494        A publish request should result in L{PubSubService.publish} being
     495        called.
     496        """
    456497        class Handler(pubsub.PubSubService):
    457             def publish(self, *args, **kwargs):
     498            def publish(self, requestor, service, nodeIdentifier, items):
     499                return defer.succeed((requestor, service,nodeIdentifier,
     500                                      items))
     501
     502        xml = """
     503        <iq type='set' to='pubsub.example.org'
     504                       from='user@example.org'>
     505          <pubsub xmlns='http://jabber.org/protocol/pubsub'>
     506            <publish node='test'/>
     507          </pubsub>
     508        </iq>
     509        """
     510
     511        def cb(result):
     512            self.assertEqual((JID('user@example.org'),
     513                              JID('pubsub.example.org'),'test', []), result)
     514
     515        handler = Handler()
     516        d = self.handleRequest(handler, parseXml(xml))
     517        d.addCallback(cb)
     518        return d
     519
     520
     521    def test_onOptionsGet(self):
     522        """
     523        Subscription options are not supported.
     524        """
     525
     526        handler = pubsub.PubSubService()
     527        xml = """
     528        <iq type='get' to='pubsub.example.org'
     529                       from='user@example.org'>
     530          <pubsub xmlns='http://jabber.org/protocol/pubsub'>
     531            <options/>
     532          </pubsub>
     533        </iq>
     534        """
     535
     536        def cb(result):
     537            self.assertEquals('feature-not-implemented', result.condition)
     538            self.assertEquals('unsupported', result.appCondition.name)
     539            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
     540
     541        handler = pubsub.PubSubService()
     542        d = self.handleRequest(handler, parseXml(xml))
     543        self.assertFailure(d, error.StanzaError)
     544        d.addCallback(cb)
     545        return d
     546
     547
     548    def test_onItems(self):
     549        """
     550        On a items request, return all items for the given node.
     551        """
     552        class Handler(pubsub.PubSubService):
     553            def items(self, *args, **kwargs):
    458554                self.args = args
    459555                self.kwargs = kwargs
     556                return defer.succeed([pubsub.Item('current')])
     557
     558        xml = """
     559        <iq type='get' to='pubsub.example.org'
     560                       from='user@example.org'>
     561          <pubsub xmlns='http://jabber.org/protocol/pubsub'>
     562            <items node='test'/>
     563          </pubsub>
     564        </iq>
     565        """
     566
     567        def cb(element):
     568            self.assertEqual((JID('user@example.org'),
     569                              JID('pubsub.example.org'), 'test', None, []),
     570                             handler.args)
     571
     572            self.assertEqual(NS_PUBSUB, element.uri)
     573            self.assertEqual(NS_PUBSUB, element.items.uri)
     574            self.assertEqual(1, len(element.items.children))
     575            item = element.items.children[-1]
     576            self.assertTrue(domish.IElement.providedBy(item))
     577            self.assertEqual('item', item.name)
     578            self.assertEqual(NS_PUBSUB, item.uri)
     579            self.assertEqual('current', item['id'])
    460580
    461581        handler = Handler()
    462         handler.parent = self
    463         iq = domish.Element((None, 'iq'))
    464         iq['type'] = 'set'
    465         iq['from'] = 'user@example.org'
    466         iq['to'] = 'pubsub.example.org'
    467         iq.addElement((NS_PUBSUB, 'pubsub'))
    468         iq.pubsub.addElement('publish')
    469         iq.pubsub.publish['node'] = 'test'
    470         handler.handleRequest(iq)
    471 
    472         self.assertEqual((JID('user@example.org'),
    473                           JID('pubsub.example.org'), 'test', []), handler.args)
    474 
    475     def test_onOptionsGet(self):
    476         handler = pubsub.PubSubService()
    477         handler.parent = self
    478         iq = domish.Element((None, 'iq'))
    479         iq['from'] = 'user@example.org'
    480         iq['to'] = 'pubsub.example.org'
    481         iq['type'] = 'get'
    482         iq.addElement((NS_PUBSUB, 'pubsub'))
    483         iq.pubsub.addElement('options')
    484         handler.handleRequest(iq)
    485 
    486         e = error.exceptionFromStanza(self.output[-1])
    487         self.assertEquals('feature-not-implemented', e.condition)
    488         self.assertEquals('unsupported', e.appCondition.name)
    489         self.assertEquals(NS_PUBSUB_ERRORS, e.appCondition.uri)
     582        d = self.handleRequest(handler, parseXml(xml))
     583        d.addCallback(cb)
     584        return d
Note: See TracChangeset for help on using the changeset viewer.