Ignore:
Timestamp:
Aug 8, 2011, 1:43:09 PM (9 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
wokkel-muc-client-support-24
Message:

Add more tests, remove code redundancy and cleanup variable names.

This adds tests for timeouts in response to joins, nick and presence changes,
error responses from the room JID instead of the occupant JID and receiving
room subject changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/test/test_muc.py

    r144 r145  
    1212
    1313from twisted.trial import unittest
    14 from twisted.internet import defer
     14from twisted.internet import defer, task
    1515from twisted.words.xish import domish, xpath
    1616from twisted.words.protocols.jabber.jid import JID
     17from twisted.words.protocols.jabber.error import StanzaError
     18from twisted.words.protocols.jabber.xmlstream import TimeoutError, toResponse
    1719
    1820from wokkel import data_form, iwokkel, muc
     
    2022from wokkel.test.helpers import XmlStreamStub
    2123
    22 from twisted.words.protocols.jabber.xmlstream import toResponse
    2324
    2425NS_MUC_ADMIN = 'http://jabber.org/protocol/muc#admin'
     
    4041    return d, func
    4142
    42 class MucClientTest(unittest.TestCase):
     43
     44
     45class MUCClientTest(unittest.TestCase):
    4346    timeout = 2
    4447
    4548    def setUp(self):
    4649        self.stub = XmlStreamStub()
    47         self.protocol = muc.MUCClient()
     50        self.clock = task.Clock()
     51        self.protocol = muc.MUCClient(reactor=self.clock)
    4852        self.protocol.xmlstream = self.stub.xmlstream
    4953        self.protocol.connectionInitialized()
     
    102106
    103107
     108    def test_receivedSubject(self):
     109        """
     110        Subject received from a room we're in are passed to receivedSubject.
     111        """
     112        xml = u"""
     113            <message to='%s' from='%s' type='groupchat'>
     114              <subject>test</subject>
     115            </message>
     116        """ % (self.userJID, self.occupantJID)
     117
     118        self._createRoom()
     119
     120        # add user to room
     121        user = muc.User(self.nick)
     122        room = self.protocol._getRoom(self.roomJID)
     123        room.addUser(user)
     124
     125        def receivedSubject(room, user, subject):
     126            self.assertEquals('test', subject, "Wrong group chat message")
     127            self.assertEquals(self.roomIdentifier, room.roomIdentifier,
     128                              'Wrong room name')
     129            self.assertEquals(self.nick, user.nick)
     130
     131        d, self.protocol.receivedSubject = calledAsync(receivedSubject)
     132        self.stub.send(parseXml(xml))
     133        return d
     134
     135
    104136    def test_receivedGroupChat(self):
    105137        """
     
    144176
    145177
    146     def test_joinRoom(self):
     178    def test_join(self):
    147179        """
    148180        Joining a room waits for confirmation, deferred fires room.
     
    155187        d.addCallback(cb)
    156188
    157         prs = self.stub.output[-1]
    158         self.assertEquals('presence', prs.name, "Need to be presence")
    159         self.assertNotIdentical(None, prs.x, 'No muc x element')
     189        element = self.stub.output[-1]
     190        self.assertEquals('presence', element.name, "Need to be presence")
     191        self.assertNotIdentical(None, element.x, 'No muc x element')
    160192
    161193        # send back user presence, they joined
     
    171203
    172204
    173     def test_joinRoomForbidden(self):
    174         """
    175         Client joining a room and getting a forbidden error.
     205    def test_joinForbidden(self):
     206        """
     207        A forbidden error in response to a join errbacks with L{StanzaError}.
    176208        """
    177209
    178210        def cb(error):
    179             self.assertEquals('forbidden', error.value.condition,
     211            self.assertEquals('forbidden', error.condition,
    180212                              'Wrong muc condition')
    181213
    182214        d = self.protocol.join(self.service, self.roomIdentifier, self.nick)
    183         d.addBoth(cb)
    184 
    185         prs = self.stub.output[-1]
    186         self.assertEquals('presence', prs.name, "Need to be presence")
    187         self.assertNotIdentical(None, prs.x, 'No muc x element')
     215        self.assertFailure(d, StanzaError)
     216        d.addCallback(cb)
    188217
    189218        # send back error, forbidden
     
    199228
    200229
    201     def test_joinRoomBadJID(self):
     230    def test_joinForbiddenFromRoomJID(self):
     231        """
     232        An error response to a join sent from the room JID should errback.
     233
     234        Some service implementations send error stanzas from the room JID
     235        instead of the JID the join presence was sent to.
     236        """
     237
     238        d = self.protocol.join(self.service, self.roomIdentifier, self.nick)
     239        self.assertFailure(d, StanzaError)
     240
     241        # send back error, forbidden
     242        xml = u"""
     243            <presence from='%s' type='error'>
     244              <error type='auth'>
     245                <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
     246              </error>
     247            </presence>
     248        """ % (self.roomJID)
     249        self.stub.send(parseXml(xml))
     250        return d
     251
     252
     253    def test_joinBadJID(self):
    202254        """
    203255        Client joining a room and getting a jid-malformed error.
     
    205257
    206258        def cb(error):
    207             self.assertEquals('jid-malformed', error.value.condition,
     259            self.assertEquals('jid-malformed', error.condition,
    208260                              'Wrong muc condition')
    209261
    210262        d = self.protocol.join(self.service, self.roomIdentifier, self.nick)
    211         d.addBoth(cb)
    212 
    213         prs = self.stub.output[-1]
    214         self.assertEquals('presence', prs.name, "Need to be presence")
    215         self.assertNotIdentical(None, prs.x, 'No muc x element')
     263        self.assertFailure(d, StanzaError)
     264        d.addCallback(cb)
    216265
    217266        # send back error, bad JID
     
    227276
    228277
    229     def test_partRoom(self):
     278    def test_joinTimeout(self):
     279        """
     280        After not receiving a response to a join, errback with L{TimeoutError}.
     281        """
     282
     283        d = self.protocol.join(self.service, self.roomIdentifier, self.nick)
     284        self.assertFailure(d, TimeoutError)
     285        self.clock.advance(muc.DEFER_TIMEOUT)
     286        return d
     287
     288
     289    def test_leave(self):
    230290        """
    231291        Client leaves a room
     
    235295
    236296        self._createRoom()
    237         d = self.protocol.leave(self.occupantJID)
    238         d.addCallback(cb)
    239 
    240         prs = self.stub.output[-1]
    241 
    242         self.assertEquals('unavailable', prs['type'],
     297        d = self.protocol.leave(self.roomJID)
     298        d.addCallback(cb)
     299
     300        element = self.stub.output[-1]
     301
     302        self.assertEquals('unavailable', element['type'],
    243303                          'Unavailable is not being sent')
    244304
     
    250310
    251311
    252     def test_userPartsRoom(self):
    253         """
    254         An entity leaves the room, a presence of type unavailable is received by the client.
     312    def test_userLeftRoom(self):
     313        """
     314        Unavailable presence from a participant removes it from the room.
    255315        """
    256316
     
    264324        # add user to room
    265325        user = muc.User(self.nick)
    266         room = self.protocol._getRoom(self.occupantJID)
     326        room = self.protocol._getRoom(self.roomJID)
    267327        room.addUser(user)
    268328
    269         def userPresence(room, user):
     329        def userLeftRoom(room, user):
    270330            self.assertEquals(self.roomIdentifier, room.roomIdentifier,
    271331                              'Wrong room name')
    272332            self.assertFalse(room.inRoster(user), 'User in roster')
    273333
    274         d, self.protocol.userLeftRoom = calledAsync(userPresence)
     334        d, self.protocol.userLeftRoom = calledAsync(userLeftRoom)
    275335        self.stub.send(parseXml(xml))
    276336        return d
     
    338398        self.protocol.password(self.occupantJID, 'secret')
    339399
    340         prs = self.stub.output[-1]
     400        element = self.stub.output[-1]
    341401
    342402        self.assertTrue(xpath.matches(
    343403                u"/presence[@to='%s']/x/password"
    344404                    "[text()='secret']" % (self.occupantJID,),
    345                 prs),
     405                element),
    346406            'Wrong presence stanza')
    347407
    348408
    349     def test_historyReceived(self):
     409    def test_receivedHistory(self):
    350410        """
    351411        Receiving history on room join.
     
    362422
    363423
    364         def historyReceived(room, user, message):
     424        def receivedHistory(room, user, message):
    365425            self.assertEquals('test', message.body, "wrong message body")
    366426            stamp = datetime(2002, 10, 13, 23, 58, 37, tzinfo=tzutc())
     
    368428                             'Does not have a history stamp')
    369429
    370         d, self.protocol.receivedHistory = calledAsync(historyReceived)
     430        d, self.protocol.receivedHistory = calledAsync(receivedHistory)
    371431        self.stub.send(parseXml(xml))
    372432        return d
     
    380440        thread = "e0ffe42b28561960c6b12b944a092794b9683a38"
    381441        # create messages
    382         msg = domish.Element((None, 'message'))
    383         msg['to'] = 'testing@example.com'
    384         msg['type'] = 'chat'
    385         msg.addElement('body', None, 'test')
    386         msg.addElement('thread', None, thread)
    387 
    388         archive.append({'stanza': msg,
     442        element = domish.Element((None, 'message'))
     443        element['to'] = 'testing@example.com'
     444        element['type'] = 'chat'
     445        element.addElement('body', None, 'test')
     446        element.addElement('thread', None, thread)
     447
     448        archive.append({'stanza': element,
    389449                        'timestamp': datetime(2002, 10, 13, 23, 58, 37,
    390450                                              tzinfo=tzutc())})
    391451
    392         msg = domish.Element((None, 'message'))
    393         msg['to'] = 'testing2@example.com'
    394         msg['type'] = 'chat'
    395         msg.addElement('body', None, 'yo')
    396         msg.addElement('thread', None, thread)
    397 
    398         archive.append({'stanza': msg,
     452        element = domish.Element((None, 'message'))
     453        element['to'] = 'testing2@example.com'
     454        element['type'] = 'chat'
     455        element.addElement('body', None, 'yo')
     456        element.addElement('thread', None, thread)
     457
     458        archive.append({'stanza': element,
    399459                        'timestamp': datetime(2002, 10, 13, 23, 58, 43,
    400460                                              tzinfo=tzutc())})
     
    404464
    405465        while len(self.stub.output)>0:
    406             m = self.stub.output.pop()
     466            element = self.stub.output.pop()
    407467            # check for delay element
    408             self.assertEquals('message', m.name, 'Wrong stanza')
    409             self.assertTrue(xpath.matches("/message/delay", m), 'Invalid history stanza')
     468            self.assertEquals('message', element.name, 'Wrong stanza')
     469            self.assertTrue(xpath.matches("/message/delay", element),
     470                            'Invalid history stanza')
    410471
    411472
     
    558619
    559620
    560     def test_nickChange(self):
     621    def test_nick(self):
    561622        """
    562623        Send a nick change to the server.
    563624        """
    564         test_nick = 'newNick'
     625        newNick = 'newNick'
    565626
    566627        self._createRoom()
     
    568629        def cb(room):
    569630            self.assertEquals(self.roomIdentifier, room.roomIdentifier)
    570             self.assertEquals(test_nick, room.nick)
    571 
    572         d = self.protocol.nick(self.occupantJID, test_nick)
    573         d.addCallback(cb)
    574 
    575         prs = self.stub.output[-1]
    576         self.assertEquals('presence', prs.name, "Need to be presence")
    577         self.assertNotIdentical(None, prs.x, 'No muc x element')
     631            self.assertEquals(newNick, room.nick)
     632
     633        d = self.protocol.nick(self.roomJID, newNick)
     634        d.addCallback(cb)
     635
     636        element = self.stub.output[-1]
     637        self.assertEquals('presence', element.name, "Need to be presence")
     638        self.assertNotIdentical(None, element.x, 'No muc x element')
    578639
    579640        # send back user presence, nick changed
    580         xml = """
    581             <presence from='%s@%s/%s'>
     641        xml = u"""
     642            <presence from='%s/%s'>
    582643              <x xmlns='http://jabber.org/protocol/muc#user'>
    583644                <item affiliation='member' role='participant'/>
    584645              </x>
    585646            </presence>
    586         """ % (self.roomIdentifier, self.service, test_nick)
     647        """ % (self.roomJID, newNick)
     648        self.stub.send(parseXml(xml))
     649        return d
     650
     651
     652    def test_nickConflict(self):
     653        """
     654        If the server finds the new nick in conflict, the errback is called.
     655        """
     656        newNick = 'newNick'
     657
     658        self._createRoom()
     659
     660        d = self.protocol.nick(self.roomJID, newNick)
     661        self.assertFailure(d, StanzaError)
     662
     663        element = self.stub.output[-1]
     664        self.assertEquals('presence', element.name, "Need to be presence")
     665        self.assertNotIdentical(None, element.x, 'No muc x element')
     666
     667        # send back user presence, nick changed
     668        xml = u"""
     669            <presence from='%s/%s' type='error'>
     670                <x xmlns='http://jabber.org/protocol/muc'/>
     671                <error type='cancel'>
     672                  <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
     673                </error>
     674            </presence>
     675        """ % (self.roomJID, newNick)
    587676        self.stub.send(parseXml(xml))
    588677        return d
     
    598687            self.assertTrue(give_voice, 'Did not give voice user')
    599688
    600         d = self.protocol.grantVoice(self.occupantJID, nick, sender=self.userJID)
     689        d = self.protocol.grantVoice(self.occupantJID, nick,
     690                                     sender=self.userJID)
    601691        d.addCallback(cb)
    602692
     
    612702
    613703
    614     def test_changeStatus(self):
     704    def test_status(self):
    615705        """
    616706        Change status
    617707        """
    618708        self._createRoom()
    619         room = self.protocol._getRoom(self.occupantJID)
     709        room = self.protocol._getRoom(self.roomJID)
    620710        user = muc.User(self.nick)
    621711        room.addUser(user)
     
    628718            self.assertEquals('xa', user.show, 'Wrong show')
    629719
    630         d = self.protocol.status(self.occupantJID, 'xa', 'testing MUC')
    631         d.addCallback(cb)
    632 
    633         prs = self.stub.output[-1]
    634 
    635         self.assertEquals('presence', prs.name, "Need to be presence")
    636         self.assertTrue(getattr(prs, 'x', None), 'No muc x element')
     720        d = self.protocol.status(self.roomJID, 'xa', 'testing MUC')
     721        d.addCallback(cb)
     722
     723        element = self.stub.output[-1]
     724
     725        self.assertEquals('presence', element.name, "Need to be presence")
     726        self.assertTrue(getattr(element, 'x', None), 'No muc x element')
    637727
    638728        # send back user presence, status changed
Note: See TracChangeset for help on using the changeset viewer.