source: ralphm-patches/roster_server.patch @ 51:6edeb69e910c

Last change on this file since 51:6edeb69e910c was 51:6edeb69e910c, checked in by Ralph Meijer <ralphm@…>, 10 years ago

Redo RosterServerProtocol? with IQHandlerMixin, add tests.

File size: 4.1 KB
  • wokkel/im.py

    Add server side support for the roster protocol.
    
    diff -r 6604e0dc687c wokkel/im.py
    a b  
    2323NS_ROSTER = 'jabber:iq:roster'
    2424
    2525XPATH_ROSTER_SET = "/iq[@type='set']/query[@xmlns='%s']" % NS_ROSTER
     26XPATH_ROSTER_GET = "/iq[@type='get']/query[@xmlns='%s']" % NS_ROSTER
    2627
    2728
    2829
     
    549550        @param entity: The entity for which the roster item has been removed.
    550551        @type entity: L{jid.JID}
    551552        """
     553
     554
     555
     556class RosterServerProtocol(XMPPHandler, IQHandlerMixin):
     557    """
     558    XMPP subprotocol handler for the roster, server side.
     559    """
     560
     561    iqHandlers = {
     562            XPATH_ROSTER_GET: '_onRosterGet',
     563            # XPATH_ROSTER_SET: '_onRosterSet',
     564            }
     565
     566    def connectionInitialized(self):
     567        self.xmlstream.addObserver(XPATH_ROSTER_GET, self.handleRequest)
     568        self.xmlstream.addObserver(XPATH_ROSTER_SET, self.handleRequest)
     569
     570
     571    def _toRosterReply(self, roster, request):
     572        response = domish.Element((NS_ROSTER, 'query'))
     573
     574        for item in roster:
     575            response.addChild(item.toElement())
     576
     577        return response
     578
     579
     580    def _onRosterGet(self, iq):
     581        request = Stanza.fromElement(iq)
     582
     583        d = self.getRoster(request)
     584        d.addCallback(self._toRosterReply, request)
     585        return d
     586
     587
     588    def getRoster(self, entity):
     589        raise NotImplementedError()
  • wokkel/test/test_im.py

    diff -r 6604e0dc687c wokkel/test/test_im.py
    a b  
    844844        d.addCallback(cb)
    845845        return d
    846846
     847
     848
     849class RosterServerProtocolTest(unittest.TestCase, TestableRequestHandlerMixin):
     850    """
     851    Tests for L{im.RosterServerProtocol}.
     852    """
     853
     854    def setUp(self):
     855        self.stub = XmlStreamStub()
     856        self.service = im.RosterServerProtocol()
     857        self.service.makeConnection(self.stub.xmlstream)
     858        self.service.connectionInitialized()
     859
     860
     861    def test_onRosterGet(self):
     862        """
     863        A roster get request should trigger getRoster with the request.
     864        """
     865        xml = """
     866          <iq type='get' from='user@example.org' to='user@example.org'>
     867            <query xmlns='jabber:iq:roster'/>
     868          </iq>
     869        """
     870
     871        def getRoster(request):
     872            self.assertEqual(JID('user@example.org'), request.sender)
     873            item = im.RosterItem(JID('other@example.org'), True, False,
     874                                 'The User')
     875            return defer.succeed([item])
     876
     877        def cb(element):
     878            self.assertEquals('query', element.name)
     879            self.assertEquals(NS_ROSTER, element.uri)
     880            itemElements = list(domish.generateElementsQNamed(
     881                element.children, 'item', NS_ROSTER))
     882            self.assertEqual(1, len(itemElements))
     883            item = itemElements[0]
     884            self.assertEquals(u'other@example.org', item.getAttribute('jid'))
     885            self.assertEquals(u'to', item.getAttribute('subscription'))
     886            self.assertEquals(u'The User', item.getAttribute('name'))
     887
     888        self.service.getRoster = getRoster
     889        d = self.handleRequest(xml)
     890        d.addCallback(cb)
     891        return d
     892
     893
     894    def test_onRosterGetNotOverridden(self):
     895        """
     896        If getRoster is not overridden, return feature-not-implemented.
     897        """
     898        xml = """
     899          <iq type='get' from='user@example.org' to='user@example.org'>
     900            <query xmlns='jabber:iq:roster'/>
     901          </iq>
     902        """
     903
     904        d = self.handleRequest(xml)
     905        self.assertFailure(d, NotImplementedError)
     906        return d
     907
     908
     909    def test_onRosterSet(self):
     910        """
     911        Roster set is not yet supported.
     912        """
     913        xml = """
     914          <iq type='set' from='user@example.org' to='user@example.org'>
     915            <query xmlns='jabber:iq:roster'>
     916              <item jid='other@example.org' name='Other User'/>
     917            </query>
     918          </iq>
     919        """
     920
     921        d = self.handleRequest(xml)
     922        self.assertFailure(d, NotImplementedError)
     923        return d
Note: See TracBrowser for help on using the repository browser.