Changeset 135:516de1d26876 for wokkel


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

Rework existing patches to use XML snippets to prevent inderdependence.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/test/test_muc.py

    r132 r135  
    1313from twisted.words.protocols.jabber.jid import JID
    1414
    15 from wokkel import data_form, iwokkel, muc, disco
     15from wokkel import data_form, iwokkel, muc
     16from wokkel.generic import parseXml
    1617from wokkel.test.helpers import XmlStreamStub
    1718
     
    3536
    3637    return d, func
    37 
    38 
    3938
    4039class MucClientTest(unittest.TestCase):
     
    5049        self.test_nick = 'Nick'
    5150
    52         self.room_jid = JID(self.test_room+'@'+self.test_srv+'/'+self.test_nick)
    53 
     51        self.room_jid = JID(tuple=(self.test_room,
     52                                   self.test_srv,
     53                                   self.test_nick))
    5454        self.user_jid = JID('test@jabber.org/Testing')
    5555
     
    6060        """
    6161        # create a room
    62         self.current_room = muc.Room(self.test_room, self.test_srv, self.test_nick)
     62        self.current_room = muc.Room(self.test_room,
     63                                     self.test_srv,
     64                                     self.test_nick)
    6365        self.protocol._addRoom(self.current_room)
    6466
     
    8082
    8183        """
    82         p = muc.UserPresence()
    83         p['to'] = self.user_jid.full()
    84         p['from'] = self.room_jid.full()
     84        xml = """
     85            <presence to='%s' from='%s'>
     86              <x xmlns='http://jabber.org/protocol/muc#user'>
     87                <item affiliation='member' role='participant'/>
     88              </x>
     89            </presence>
     90        """ % (self.user_jid.full(), self.room_jid.full())
    8591
    8692        # create a room
    8793        self._createRoom()
    8894
    89         def userPresence(room, user):
    90             self.assertEqual(self.test_room, room.roomIdentifier,
    91                              'Wrong room name')
     95        def userJoinedRoom(room, user):
     96            self.assertEquals(self.test_room, room.roomIdentifier,
     97                              'Wrong room name')
    9298            self.assertTrue(room.inRoster(user), 'User not in roster')
    9399
    94 
    95         d, self.protocol.userJoinedRoom = calledAsync(userPresence)
    96         self.stub.send(p)
     100        d, self.protocol.userJoinedRoom = calledAsync(userJoinedRoom)
     101        self.stub.send(parseXml(xml))
    97102        return d
    98103
     
    102107        The client receives a groupchat message from an entity in the room.
    103108        """
    104         m = muc.GroupChat('test@test.com',body='test')
    105         m['from'] = self.room_jid.full()
     109        xml = """
     110            <message to='test@test.com' from='%s' type='groupchat'>
     111              <body>test</body>
     112            </message>
     113        """ % (self.room_jid.full())
    106114
    107115        self._createRoom()
    108116
    109117        def groupChat(room, user, message):
    110             self.failUnless(message=='test', "Wrong group chat message")
    111             self.failUnless(room.roomIdentifier==self.test_room, 'Wrong room name')
    112 
     118            self.assertEquals('test', message, "Wrong group chat message")
     119            self.assertEquals(self.test_room, room.roomIdentifier,
     120                              'Wrong room name')
    113121
    114122        d, self.protocol.receivedGroupChat = calledAsync(groupChat)
    115         self.stub.send(m)
     123        self.stub.send(parseXml(xml))
    116124        return d
    117125
     
    129137
    130138        prs = self.stub.output[-1]
    131         self.failUnless(prs.name=='presence', "Need to be presence")
    132         self.failUnless(getattr(prs, 'x', None), 'No muc x element')
     139        self.assertEquals('presence', prs.name, "Need to be presence")
     140        self.assertNotIdentical(None, prs.x, 'No muc x element')
    133141
    134142        # send back user presence, they joined
    135         response = muc.UserPresence(frm=self.test_room+'@'+self.test_srv+'/'+self.test_nick)
    136         self.stub.send(response)
     143        xml = """
     144            <presence from='%s@%s/%s'>
     145              <x xmlns='http://jabber.org/protocol/muc#user'>
     146                <item affiliation='member' role='participant'/>
     147              </x>
     148            </presence>
     149        """ % (self.test_room, self.test_srv, self.test_nick)
     150        self.stub.send(parseXml(xml))
    137151        return d
    138152
     
    144158
    145159        def cb(error):
    146 
    147             self.failUnless(error.value.mucCondition=='forbidden','Wrong muc condition')
    148 
    149 
     160            self.assertEquals('forbidden', error.value.mucCondition,
     161                              'Wrong muc condition')
    150162
    151163        d = self.protocol.join(self.test_srv, self.test_room, self.test_nick)
     
    153165
    154166        prs = self.stub.output[-1]
    155         self.failUnless(prs.name=='presence', "Need to be presence")
    156         self.failUnless(getattr(prs, 'x', None), 'No muc x element')
    157         # send back user presence, they joined
    158 
    159         response = muc.PresenceError(error=muc.MUCError('auth',
    160                                                         'forbidden'
    161                                                         ),
    162                                      frm=self.room_jid.full())
    163         self.stub.send(response)
    164         return d
    165 
    166 
    167     def test_joinRoomBadJid(self):
    168         """
    169         Client joining a room and getting a forbidden error.
     167        self.assertEquals('presence', prs.name, "Need to be presence")
     168        self.assertNotIdentical(None, prs.x, 'No muc x element')
     169
     170        # send back error, forbidden
     171        xml = """
     172            <presence from='%s' type='error'>
     173              <error type='auth'>
     174                <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
     175              </error>
     176            </presence>
     177        """ % (self.room_jid.full())
     178        self.stub.send(parseXml(xml))
     179        return d
     180
     181
     182    def test_joinRoomBadJID(self):
     183        """
     184        Client joining a room and getting a jid-malformed error.
    170185        """
    171186
    172187        def cb(error):
    173 
    174             self.failUnless(error.value.mucCondition=='jid-malformed','Wrong muc condition')
    175 
    176 
     188            self.assertEquals('jid-malformed', error.value.mucCondition,
     189                              'Wrong muc condition')
    177190
    178191        d = self.protocol.join(self.test_srv, self.test_room, self.test_nick)
     
    180193
    181194        prs = self.stub.output[-1]
    182         self.failUnless(prs.name=='presence', "Need to be presence")
    183         self.failUnless(getattr(prs, 'x', None), 'No muc x element')
    184         # send back user presence, they joined
    185 
    186         response = muc.PresenceError(error=muc.MUCError('modify',
    187                                                         'jid-malformed'
    188                                                         ),
    189                                      frm=self.room_jid.full())
    190         self.stub.send(response)
     195        self.assertEquals('presence', prs.name, "Need to be presence")
     196        self.assertNotIdentical(None, prs.x, 'No muc x element')
     197
     198        # send back error, bad JID
     199        xml = """
     200            <presence from='%s' type='error'>
     201              <error type='modify'>
     202                <jid-malformed xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
     203              </error>
     204            </presence>
     205        """ % (self.room_jid.full())
     206        self.stub.send(parseXml(xml))
    191207        return d
    192208
     
    197213        """
    198214        def cb(left):
    199             self.failUnless(left, 'did not leave room')
    200 
     215            self.assertTrue(left, 'did not leave room')
    201216
    202217        self._createRoom()
     
    206221        prs = self.stub.output[-1]
    207222
    208         self.failUnless(prs['type']=='unavailable', 'Unavailable is not being sent')
    209 
    210         response = prs
    211         response['from'] = response['to']
    212         response['to'] = 'test@jabber.org'
    213 
    214         self.stub.send(response)
     223        self.assertEquals('unavailable', prs['type'],
     224                          'Unavailable is not being sent')
     225
     226        xml = """
     227            <presence to='test@jabber.org' from='%s' type='unavailable'/>
     228        """ % (self.room_jid.full())
     229        self.stub.send(parseXml(xml))
    215230        return d
    216231
     
    221236        """
    222237
    223         p = muc.UnavailableUserPresence()
    224         p['to'] = self.user_jid.full()
    225         p['from'] = self.room_jid.full()
     238        xml = """
     239            <presence to='%s' from='%s' type='unavailable'/>
     240        """ % (self.user_jid.full(), self.room_jid.full())
    226241
    227242        # create a room
    228243        self._createRoom()
     244
    229245        # add user to room
    230246        user = muc.User(self.room_jid.resource)
    231 
    232247        room = self.protocol._getRoom(self.room_jid)
    233248        room.addUser(user)
    234249
    235250        def userPresence(room, user):
    236             self.failUnless(room.roomIdentifier==self.test_room, 'Wrong room name')
    237             self.failUnless(room.inRoster(user)==False, 'User in roster')
     251            self.assertEquals(self.test_room, room.roomIdentifier,
     252                              'Wrong room name')
     253            self.assertFalse(room.inRoster(user), 'User in roster')
    238254
    239255        d, self.protocol.userLeftRoom = calledAsync(userPresence)
    240         self.stub.send(p)
     256        self.stub.send(parseXml(xml))
    241257        return d
    242258
     
    246262        Ban an entity in a room.
    247263        """
    248         banned = JID('ban@jabber.org/TroubleMakger')
     264        banned = JID('ban@jabber.org/TroubleMaker')
     265
    249266        def cb(banned):
    250             self.failUnless(banned, 'Did not ban user')
    251 
     267            self.assertTrue(banned, 'Did not ban user')
    252268
    253269        d = self.protocol.ban(self.room_jid, banned, reason='Spam',
     
    257273        iq = self.stub.output[-1]
    258274
    259         self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='outcast']" % (self.room_jid.userhost(),), iq), 'Wrong ban stanza')
     275        self.assertTrue(xpath.matches(
     276                "/iq[@type='set' and @to='%s']/query/item"
     277                    "[@affiliation='outcast']" % (self.room_jid.userhost(),),
     278                iq),
     279            'Wrong ban stanza')
    260280
    261281        response = toResponse(iq, 'result')
    262 
    263282        self.stub.send(response)
    264283
     
    270289        Kick an entity from a room.
    271290        """
    272         nick = 'TroubleMakger'
     291        nick = 'TroubleMaker'
    273292
    274293        def cb(kicked):
    275             self.failUnless(kicked, 'Did not kick user')
     294            self.assertTrue(kicked, 'Did not kick user')
    276295
    277296        d = self.protocol.kick(self.room_jid, nick, reason='Spam',
     
    281300        iq = self.stub.output[-1]
    282301
    283         self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='none']" % (self.room_jid.userhost(),), iq), 'Wrong kick stanza')
     302        self.assertTrue(xpath.matches(
     303                "/iq[@type='set' and @to='%s']/query/item"
     304                    "[@affiliation='none']" % (self.room_jid.userhost(),),
     305                iq),
     306            'Wrong kick stanza')
    284307
    285308        response = toResponse(iq, 'result')
    286 
    287309        self.stub.send(response)
    288310
     
    299321        prs = self.stub.output[-1]
    300322
    301         self.failUnless(xpath.matches("/presence[@to='%s']/x/password[text()='secret']" % (self.room_jid.full(),), prs), 'Wrong presence stanza')
    302 
    303 
    304     def test_history(self):
     323        self.assertTrue(xpath.matches(
     324                "/presence[@to='%s']/x/password"
     325                    "[text()='secret']" % (self.room_jid.full(),),
     326                prs),
     327            'Wrong presence stanza')
     328
     329
     330    def test_historyReceived(self):
    305331        """
    306332        Receiving history on room join.
    307333        """
    308         m = muc.HistoryMessage(self.room_jid.userhost(), self.protocol._makeTimeStamp(), body='test')
    309         m['from'] = self.room_jid.full()
    310 
    311         self._createRoom()
    312 
    313         def roomHistory(room, user, body, stamp, frm=None):
    314             self.failUnless(body=='test', "wrong message body")
    315             self.failUnless(stamp, 'Does not have a history stamp')
    316 
    317 
    318         d, self.protocol.receivedHistory = calledAsync(roomHistory)
    319         self.stub.send(m)
     334        xml = """
     335            <message to='test@test.com' from='%s' type='groupchat'>
     336              <body>test</body>
     337              <delay xmlns='urn:xmpp:delay' stamp="2002-10-13T23:58:37Z"
     338                                            from="%s"/>
     339            </message>
     340        """ % (self.room_jid.full(), self.user_jid.full())
     341
     342        self._createRoom()
     343
     344
     345        def historyReceived(room, user, body, stamp, frm=None):
     346            self.assertTrue(body=='test', "wrong message body")
     347            self.assertTrue(stamp, 'Does not have a history stamp')
     348
     349        d, self.protocol.receivedHistory = calledAsync(historyReceived)
     350        self.stub.send(parseXml(xml))
    320351        return d
    321352
     
    350381            m = self.stub.output.pop()
    351382            # check for delay element
    352             self.failUnless(m.name=='message', 'Wrong stanza')
    353             self.failUnless(xpath.matches("/message/delay", m), 'Invalid history stanza')
     383            self.assertTrue(m.name=='message', 'Wrong stanza')
     384            self.assertTrue(xpath.matches("/message/delay", m), 'Invalid history stanza')
    354385
    355386
     
    366397
    367398        query = u"/message[@to='%s']/x/invite/reason" % bareRoomJID
    368         self.failUnless(xpath.matches(query, msg), 'Wrong message type')
     399        self.assertTrue(xpath.matches(query, msg), 'Wrong message type')
    369400
    370401
     
    379410        msg = self.stub.output[-1]
    380411
    381         self.failUnless(xpath.matches("/message[@type='chat' and @to='%s']/body" % (other_nick,), msg), 'Wrong message type')
     412        query = "/message[@type='chat' and @to='%s']/body" % other_nick
     413        self.assertTrue(xpath.matches(query, msg), 'Wrong message type')
    382414
    383415
    384416    def test_register(self):
    385417        """
    386         Client registering with a room. http://xmpp.org/extensions/xep-0045.html#register
    387 
     418        Client registering with a room.
     419
     420        http://xmpp.org/extensions/xep-0045.html#register
    388421        """
    389422
    390423        def cb(iq):
    391424            # check for a result
    392             self.failUnless(iq['type']=='result', 'We did not get a result')
     425            self.assertTrue(iq['type']=='result', 'We did not get a result')
    393426
    394427        d = self.protocol.register(self.room_jid)
     
    396429
    397430        iq = self.stub.output[-1]
    398         self.failUnless(xpath.matches("/iq/query[@xmlns='%s']" % (muc.NS_REQUEST), iq), 'Invalid iq register request')
     431        query = "/iq/query[@xmlns='%s']" % muc.NS_REQUEST
     432        self.assertTrue(xpath.matches(query, iq), 'Invalid iq register request')
    399433
    400434        response = toResponse(iq, 'result')
    401 
    402435        self.stub.send(response)
    403436        return d
     
    412445        m = self.stub.output[-1]
    413446
    414         self.failUnless(xpath.matches("/message/x[@type='submit']/field/value[text()='%s']" % (muc.NS_MUC_REQUEST,), m), 'Invalid voice message stanza')
     447        query = ("/message/x[@type='submit']/field/value"
     448                    "[text()='%s']") % muc.NS_MUC_REQUEST
     449        self.assertTrue(xpath.matches(query, m), 'Invalid voice message stanza')
    415450
    416451
     
    421456
    422457        def cb(iq):
    423             self.failUnless(iq['type']=='result', 'Not a result')
     458            self.assertTrue(iq['type']=='result', 'Not a result')
    424459
    425460
     
    434469
    435470        iq = self.stub.output[-1]
    436         self.failUnless(xpath.matches("/iq/query[@xmlns='%s']/x"% (muc.NS_MUC_OWNER,), iq), 'Bad configure request')
     471        query = "/iq/query[@xmlns='%s']/x"% muc.NS_MUC_OWNER
     472        self.assertTrue(xpath.matches(query, iq), 'Bad configure request')
    437473
    438474        response = toResponse(iq, 'result')
     
    447483
    448484        def cb(destroyed):
    449             self.failUnless(destroyed==True, 'Room not destroyed.')
     485            self.assertTrue(destroyed==True, 'Room not destroyed.')
    450486
    451487        d = self.protocol.destroy(self.room_jid)
     
    453489
    454490        iq = self.stub.output[-1]
    455         self.failUnless(xpath.matches("/iq/query[@xmlns='%s']/destroy"% (muc.NS_MUC_OWNER,), iq), 'Bad configure request')
     491        query = "/iq/query[@xmlns='%s']/destroy"% muc.NS_MUC_OWNER
     492        self.assertTrue(xpath.matches(query, iq), 'Bad configure request')
    456493
    457494        response = toResponse(iq, 'result')
     
    469506
    470507        def cb(room):
    471             self.assertEqual(self.test_room, room.roomIdentifier)
    472             self.assertEqual(test_nick, room.nick)
     508            self.assertEquals(self.test_room, room.roomIdentifier)
     509            self.assertEquals(test_nick, room.nick)
    473510
    474511        d = self.protocol.nick(self.room_jid, test_nick)
     
    476513
    477514        prs = self.stub.output[-1]
    478         self.failUnless(prs.name=='presence', "Need to be presence")
    479         self.failUnless(getattr(prs, 'x', None), 'No muc x element')
    480 
    481         # send back user presence, they joined
    482         response = muc.UserPresence(frm=self.test_room+'@'+self.test_srv+'/'+test_nick)
    483 
     515        self.assertEquals('presence', prs.name, "Need to be presence")
     516        self.assertNotIdentical(None, prs.x, 'No muc x element')
     517
     518        # send back user presence, nick changed
     519        xml = """
     520            <presence from='%s@%s/%s'>
     521              <x xmlns='http://jabber.org/protocol/muc#user'>
     522                <item affiliation='member' role='participant'/>
     523              </x>
     524            </presence>
     525        """ % (self.test_room, self.test_srv, test_nick)
     526        self.stub.send(parseXml(xml))
     527        return d
     528
     529
     530    def test_grantVoice(self):
     531        """
     532        Test granting voice to a user.
     533
     534        """
     535        nick = 'TroubleMaker'
     536        def cb(give_voice):
     537            self.assertTrue(give_voice, 'Did not give voice user')
     538
     539        d = self.protocol.grantVoice(self.room_jid, nick, sender=self.user_jid)
     540        d.addCallback(cb)
     541
     542        iq = self.stub.output[-1]
     543
     544        query = ("/iq[@type='set' and @to='%s']/query/item"
     545                     "[@role='participant']") % self.room_jid.userhost()
     546        self.assertTrue(xpath.matches(query, iq), 'Wrong voice stanza')
     547
     548        response = toResponse(iq, 'result')
    484549        self.stub.send(response)
    485         return d
    486 
    487 
    488     def test_grantVoice(self):
    489         """
    490         Test granting voice to a user.
    491 
    492         """
    493         nick = 'TroubleMakger'
    494         def cb(give_voice):
    495             self.failUnless(give_voice, 'Did not give voice user')
    496 
    497 
    498         d = self.protocol.grantVoice(self.room_jid, nick, sender=self.user_jid)
    499         d.addCallback(cb)
    500 
    501         iq = self.stub.output[-1]
    502 
    503         self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@role='participant']" % (self.room_jid.userhost(),), iq), 'Wrong voice stanza')
    504 
    505         response = toResponse(iq, 'result')
    506 
    507         self.stub.send(response)
    508 
    509550        return d
    510551
     
    534575        self.assertTrue(getattr(prs, 'x', None), 'No muc x element')
    535576
    536         # send back user presence, they joined
    537         response = muc.UserPresence(frm=self.room_jid.full())
    538         response.addElement('show', None, 'xa')
    539         response.addElement('status', None, 'testing MUC')
    540         self.stub.send(response)
     577        # send back user presence, status changed
     578        xml = """
     579            <presence from='%s'>
     580              <x xmlns='http://jabber.org/protocol/muc#user'>
     581                <item affiliation='member' role='participant'/>
     582              </x>
     583              <show>xa</show>
     584              <status>testing MUC</status>
     585            </presence>
     586        """ % self.room_jid.full()
     587        self.stub.send(parseXml(xml))
    541588        return d
    542589
Note: See TracChangeset for help on using the changeset viewer.