Changeset 51:6edeb69e910c in ralphm-patches


Ignore:
Timestamp:
Jun 1, 2011, 9:23:16 PM (9 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Redo RosterServerProtocol? with IQHandlerMixin, add tests.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • roster_server.patch

    r50 r51  
    1 diff -r 05330d69e16c wokkel/im.py
    2 --- a/wokkel/im.py      Wed May 25 20:20:50 2011 +0200
    3 +++ b/wokkel/im.py      Wed May 25 20:22:14 2011 +0200
    4 @@ -12,7 +12,7 @@
    5  
    6  from twisted.internet import defer
    7  from twisted.words.protocols.jabber import jid
    8 -from twisted.words.protocols.jabber import error
    9 +from twisted.words.protocols.jabber import error, xmlstream
    10  from twisted.words.xish import domish
    11  
    12  from wokkel.compat import IQ
     1Add server side support for the roster protocol.
     2
     3diff -r 6604e0dc687c wokkel/im.py
     4--- a/wokkel/im.py      Wed Jun 01 09:35:07 2011 +0200
     5+++ b/wokkel/im.py      Wed Jun 01 21:13:19 2011 +0200
    136@@ -23,6 +23,7 @@
    147 NS_ROSTER = 'jabber:iq:roster'
     
    1912 
    2013 
    21 @@ -550,3 +551,43 @@
     14@@ -549,3 +550,40 @@
    2215         @param entity: The entity for which the roster item has been removed.
    2316         @type entity: L{jid.JID}
     
    2619+
    2720+
    28 +class RosterServerProtocol(XMPPHandler):
     21+class RosterServerProtocol(XMPPHandler, IQHandlerMixin):
    2922+    """
    3023+    XMPP subprotocol handler for the roster, server side.
    3124+    """
    3225+
     26+    iqHandlers = {
     27+            XPATH_ROSTER_GET: '_onRosterGet',
     28+            # XPATH_ROSTER_SET: '_onRosterSet',
     29+            }
     30+
    3331+    def connectionInitialized(self):
    34 +        self.xmlstream.addObserver(XPATH_ROSTER_GET, self._onRosterGet)
    35 +        self.xmlstream.addObserver(XPATH_ROSTER_SET, self._onRosterSet)
     32+        self.xmlstream.addObserver(XPATH_ROSTER_GET, self.handleRequest)
     33+        self.xmlstream.addObserver(XPATH_ROSTER_SET, self.handleRequest)
    3634+
    3735+
    38 +    def _toRosterReply(self, roster, iq):
    39 +        response = xmlstream.toResponse(iq, 'result')
    40 +        response.addElement((NS_ROSTER, 'query'))
     36+    def _toRosterReply(self, roster, request):
     37+        response = domish.Element((NS_ROSTER, 'query'))
    4138+
    4239+        for item in roster:
    43 +            response.query.addChild(item.toElement())
     40+            response.addChild(item.toElement())
    4441+
    4542+        return response
     
    4744+
    4845+    def _onRosterGet(self, iq):
    49 +        iq.handled = True
     46+        request = Stanza.fromElement(iq)
    5047+
    51 +        d = self.getRoster(internJID(iq["from"]))
    52 +        d.addCallback(self._toRosterReply, iq)
    53 +        d.addErrback(lambda _: error.ErrorStanza('internal-error').toResponse(iq))
    54 +        d.addBoth(self.send)
    55 +
    56 +
    57 +    def _onRosterSet(self, iq):
    58 +        iq.handled = True
    59 +        response = error.StanzaError('bad-request').toResponse(iq)
    60 +        self.send(response)
     48+        d = self.getRoster(request)
     49+        d.addCallback(self._toRosterReply, request)
     50+        return d
    6151+
    6252+
    6353+    def getRoster(self, entity):
    64 +        raise NotImplemented
     54+        raise NotImplementedError()
     55diff -r 6604e0dc687c wokkel/test/test_im.py
     56--- a/wokkel/test/test_im.py    Wed Jun 01 09:35:07 2011 +0200
     57+++ b/wokkel/test/test_im.py    Wed Jun 01 21:13:19 2011 +0200
     58@@ -844,3 +844,80 @@
     59         d.addCallback(cb)
     60         return d
     61 
     62+
     63+
     64+class RosterServerProtocolTest(unittest.TestCase, TestableRequestHandlerMixin):
     65+    """
     66+    Tests for L{im.RosterServerProtocol}.
     67+    """
     68+
     69+    def setUp(self):
     70+        self.stub = XmlStreamStub()
     71+        self.service = im.RosterServerProtocol()
     72+        self.service.makeConnection(self.stub.xmlstream)
     73+        self.service.connectionInitialized()
     74+
     75+
     76+    def test_onRosterGet(self):
     77+        """
     78+        A roster get request should trigger getRoster with the request.
     79+        """
     80+        xml = """
     81+          <iq type='get' from='user@example.org' to='user@example.org'>
     82+            <query xmlns='jabber:iq:roster'/>
     83+          </iq>
     84+        """
     85+
     86+        def getRoster(request):
     87+            self.assertEqual(JID('user@example.org'), request.sender)
     88+            item = im.RosterItem(JID('other@example.org'), True, False,
     89+                                 'The User')
     90+            return defer.succeed([item])
     91+
     92+        def cb(element):
     93+            self.assertEquals('query', element.name)
     94+            self.assertEquals(NS_ROSTER, element.uri)
     95+            itemElements = list(domish.generateElementsQNamed(
     96+                element.children, 'item', NS_ROSTER))
     97+            self.assertEqual(1, len(itemElements))
     98+            item = itemElements[0]
     99+            self.assertEquals(u'other@example.org', item.getAttribute('jid'))
     100+            self.assertEquals(u'to', item.getAttribute('subscription'))
     101+            self.assertEquals(u'The User', item.getAttribute('name'))
     102+
     103+        self.service.getRoster = getRoster
     104+        d = self.handleRequest(xml)
     105+        d.addCallback(cb)
     106+        return d
     107+
     108+
     109+    def test_onRosterGetNotOverridden(self):
     110+        """
     111+        If getRoster is not overridden, return feature-not-implemented.
     112+        """
     113+        xml = """
     114+          <iq type='get' from='user@example.org' to='user@example.org'>
     115+            <query xmlns='jabber:iq:roster'/>
     116+          </iq>
     117+        """
     118+
     119+        d = self.handleRequest(xml)
     120+        self.assertFailure(d, NotImplementedError)
     121+        return d
     122+
     123+
     124+    def test_onRosterSet(self):
     125+        """
     126+        Roster set is not yet supported.
     127+        """
     128+        xml = """
     129+          <iq type='set' from='user@example.org' to='user@example.org'>
     130+            <query xmlns='jabber:iq:roster'>
     131+              <item jid='other@example.org' name='Other User'/>
     132+            </query>
     133+          </iq>
     134+        """
     135+
     136+        d = self.handleRequest(xml)
     137+        self.assertFailure(d, NotImplementedError)
     138+        return d
  • series

    r49 r51  
    22jid_cleanup.patch
    33roster_item.patch #+c2s
    4 roster_item_more.patch
     4roster_item_more.patch #+c2s
    55roster_server.patch #+c2s
    66xmpp_client_service.patch #+c2s
  • xmpp_client_service.patch

    r49 r51  
    972972+
    973973+
    974  class RosterServerProtocol(XMPPHandler):
     974 class RosterServerProtocol(XMPPHandler, IQHandlerMixin):
    975975     """
    976976     XMPP subprotocol handler for the roster, server side.
Note: See TracChangeset for help on using the changeset viewer.