Changeset 144:6786830f3cd5


Ignore:
Timestamp:
Aug 6, 2011, 2:26:32 PM (9 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
wokkel-muc-client-support-24
Message:

Cleanup some tests, add a few tests, fix subject.

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r143 r144  
    12411241        """
    12421242        msg = GroupChat(roomJID.userhostJID(), subject=subject)
    1243         self.xmlstream.send(msg)
     1243        self.xmlstream.send(msg.toElement())
    12441244
    12451245
  • wokkel/test/test_muc.py

    r143 r144  
    4848        self.protocol.xmlstream = self.stub.xmlstream
    4949        self.protocol.connectionInitialized()
    50         self.test_room = 'test'
    51         self.test_srv  = 'conference.example.org'
    52         self.test_nick = 'Nick'
    53 
    54         self.room_jid = JID(tuple=(self.test_room,
    55                                    self.test_srv,
    56                                    self.test_nick))
    57         self.user_jid = JID('test@jabber.org/Testing')
     50        self.roomIdentifier = 'test'
     51        self.service  = 'conference.example.org'
     52        self.nick = 'Nick'
     53
     54        self.occupantJID = JID(tuple=(self.roomIdentifier,
     55                                      self.service,
     56                                      self.nick))
     57        self.roomJID = self.occupantJID.userhostJID()
     58        self.userJID = JID('test@example.org/Testing')
    5859
    5960
     
    6364        """
    6465        # create a room
    65         self.current_room = muc.Room(self.test_room,
    66                                      self.test_srv,
    67                                      self.test_nick)
    68         self.protocol._addRoom(self.current_room)
     66        room = muc.Room(self.roomIdentifier,
     67                        self.service,
     68                        self.nick)
     69        self.protocol._addRoom(room)
    6970
    7071
     
    7879    def test_userJoinedRoom(self):
    7980        """
    80         The client receives presence from an entity joining the room.
    81 
    82         This tests the class L{muc.UserPresence} and the userJoinedRoom event method.
    83 
    84         The test sends the user presence and tests if the event method is called.
    85 
     81        Joins by others to a room we're in are passed to userJoinedRoom
    8682        """
    8783        xml = """
     
    9187              </x>
    9288            </presence>
    93         """ % (self.user_jid.full(), self.room_jid.full())
     89        """ % (self.userJID.full(), self.occupantJID.full())
    9490
    9591        # create a room
     
    9793
    9894        def userJoinedRoom(room, user):
    99             self.assertEquals(self.test_room, room.roomIdentifier,
     95            self.assertEquals(self.roomIdentifier, room.roomIdentifier,
    10096                              'Wrong room name')
    10197            self.assertTrue(room.inRoster(user), 'User not in roster')
     
    106102
    107103
    108     def test_groupChat(self):
    109         """
    110         The client receives a groupchat message from an entity in the room.
    111         """
    112         xml = """
     104    def test_receivedGroupChat(self):
     105        """
     106        Messages received from a room we're in are passed to receivedGroupChat.
     107        """
     108        xml = u"""
    113109            <message to='test@test.com' from='%s' type='groupchat'>
    114110              <body>test</body>
    115111            </message>
    116         """ % (self.room_jid.full())
    117 
    118         self._createRoom()
    119 
    120         def groupChat(room, user, message):
     112        """ % (self.occupantJID)
     113
     114        self._createRoom()
     115
     116        def receivedGroupChat(room, user, message):
    121117            self.assertEquals('test', message.body, "Wrong group chat message")
    122             self.assertEquals(self.test_room, room.roomIdentifier,
     118            self.assertEquals(self.roomIdentifier, room.roomIdentifier,
    123119                              'Wrong room name')
    124120
    125         d, self.protocol.receivedGroupChat = calledAsync(groupChat)
    126         self.stub.send(parseXml(xml))
    127         return d
    128 
    129 
    130     def test_groupChatNoNick(self):
    131         """
    132         Message received from the room itself, no nickname
    133         """
    134         xml = """
     121        d, self.protocol.receivedGroupChat = calledAsync(receivedGroupChat)
     122        self.stub.send(parseXml(xml))
     123        return d
     124
     125
     126    def test_receivedGroupChatRoom(self):
     127        """
     128        Messages received from the room itself have C{user} set to C{None}.
     129        """
     130        xml = u"""
    135131            <message to='test@test.com' from='%s' type='groupchat'>
    136132              <body>test</body>
    137133            </message>
    138         """ % (self.room_jid.userhost())
    139 
    140         self._createRoom()
    141 
    142         def groupChat(room, user, message):
    143             self.assertEquals('test', message.body,
    144                               "Wrong group chat message")
    145             self.assertEquals(self.test_room, room.roomIdentifier,
    146                               'Wrong room name')
    147 
    148 
    149         d, self.protocol.receivedGroupChat = calledAsync(groupChat)
     134        """ % (self.roomJID)
     135
     136        self._createRoom()
     137
     138        def receivedGroupChat(room, user, message):
     139            self.assertIdentical(None, user)
     140
     141        d, self.protocol.receivedGroupChat = calledAsync(receivedGroupChat)
    150142        self.stub.send(parseXml(xml))
    151143        return d
     
    154146    def test_joinRoom(self):
    155147        """
    156         Joining a room
     148        Joining a room waits for confirmation, deferred fires room.
    157149        """
    158150
    159151        def cb(room):
    160             self.assertEquals(self.test_room, room.roomIdentifier)
    161 
    162         d = self.protocol.join(self.test_srv, self.test_room, self.test_nick)
     152            self.assertEquals(self.roomIdentifier, room.roomIdentifier)
     153
     154        d = self.protocol.join(self.service, self.roomIdentifier, self.nick)
    163155        d.addCallback(cb)
    164156
     
    174166              </x>
    175167            </presence>
    176         """ % (self.test_room, self.test_srv, self.test_nick)
     168        """ % (self.roomIdentifier, self.service, self.nick)
    177169        self.stub.send(parseXml(xml))
    178170        return d
     
    188180                              'Wrong muc condition')
    189181
    190         d = self.protocol.join(self.test_srv, self.test_room, self.test_nick)
     182        d = self.protocol.join(self.service, self.roomIdentifier, self.nick)
    191183        d.addBoth(cb)
    192184
     
    196188
    197189        # send back error, forbidden
    198         xml = """
     190        xml = u"""
    199191            <presence from='%s' type='error'>
    200192              <error type='auth'>
     
    202194              </error>
    203195            </presence>
    204         """ % (self.room_jid.full())
     196        """ % (self.occupantJID)
    205197        self.stub.send(parseXml(xml))
    206198        return d
     
    216208                              'Wrong muc condition')
    217209
    218         d = self.protocol.join(self.test_srv, self.test_room, self.test_nick)
     210        d = self.protocol.join(self.service, self.roomIdentifier, self.nick)
    219211        d.addBoth(cb)
    220212
     
    224216
    225217        # send back error, bad JID
    226         xml = """
     218        xml = u"""
    227219            <presence from='%s' type='error'>
    228220              <error type='modify'>
     
    230222              </error>
    231223            </presence>
    232         """ % (self.room_jid.full())
     224        """ % (self.occupantJID)
    233225        self.stub.send(parseXml(xml))
    234226        return d
     
    243235
    244236        self._createRoom()
    245         d = self.protocol.leave(self.room_jid)
     237        d = self.protocol.leave(self.occupantJID)
    246238        d.addCallback(cb)
    247239
     
    251243                          'Unavailable is not being sent')
    252244
    253         xml = """
    254             <presence to='test@jabber.org' from='%s' type='unavailable'/>
    255         """ % (self.room_jid.full())
     245        xml = u"""
     246            <presence to='%s' from='%s' type='unavailable'/>
     247        """ % (self.userJID, self.occupantJID)
    256248        self.stub.send(parseXml(xml))
    257249        return d
     
    263255        """
    264256
    265         xml = """
     257        xml = u"""
    266258            <presence to='%s' from='%s' type='unavailable'/>
    267         """ % (self.user_jid.full(), self.room_jid.full())
     259        """ % (self.userJID, self.occupantJID)
    268260
    269261        # create a room
     
    271263
    272264        # add user to room
    273         user = muc.User(self.room_jid.resource)
    274         room = self.protocol._getRoom(self.room_jid)
     265        user = muc.User(self.nick)
     266        room = self.protocol._getRoom(self.occupantJID)
    275267        room.addUser(user)
    276268
    277269        def userPresence(room, user):
    278             self.assertEquals(self.test_room, room.roomIdentifier,
     270            self.assertEquals(self.roomIdentifier, room.roomIdentifier,
    279271                              'Wrong room name')
    280272            self.assertFalse(room.inRoster(user), 'User in roster')
     
    294286            self.assertTrue(banned, 'Did not ban user')
    295287
    296         d = self.protocol.ban(self.room_jid, banned, reason='Spam',
    297                               sender=self.user_jid)
     288        d = self.protocol.ban(self.occupantJID, banned, reason='Spam',
     289                              sender=self.userJID)
    298290        d.addCallback(cb)
    299291
     
    301293
    302294        self.assertTrue(xpath.matches(
    303                 "/iq[@type='set' and @to='%s']/query/item"
    304                     "[@affiliation='outcast']" % (self.room_jid.userhost(),),
     295                u"/iq[@type='set' and @to='%s']/query/item"
     296                    "[@affiliation='outcast']" % (self.roomJID,),
    305297                iq),
    306298            'Wrong ban stanza')
     
    321313            self.assertTrue(kicked, 'Did not kick user')
    322314
    323         d = self.protocol.kick(self.room_jid, nick, reason='Spam',
    324                                sender=self.user_jid)
     315        d = self.protocol.kick(self.occupantJID, nick, reason='Spam',
     316                               sender=self.userJID)
    325317        d.addCallback(cb)
    326318
     
    328320
    329321        self.assertTrue(xpath.matches(
    330                 "/iq[@type='set' and @to='%s']/query/item"
    331                     "[@affiliation='none']" % (self.room_jid.userhost(),),
     322                u"/iq[@type='set' and @to='%s']/query/item"
     323                    "[@affiliation='none']" % (self.roomJID,),
    332324                iq),
    333325            'Wrong kick stanza')
     
    344336        """
    345337
    346         self.protocol.password(self.room_jid, 'secret')
     338        self.protocol.password(self.occupantJID, 'secret')
    347339
    348340        prs = self.stub.output[-1]
    349341
    350342        self.assertTrue(xpath.matches(
    351                 "/presence[@to='%s']/x/password"
    352                     "[text()='secret']" % (self.room_jid.full(),),
     343                u"/presence[@to='%s']/x/password"
     344                    "[text()='secret']" % (self.occupantJID,),
    353345                prs),
    354346            'Wrong presence stanza')
     
    359351        Receiving history on room join.
    360352        """
    361         xml = """
     353        xml = u"""
    362354            <message to='test@test.com' from='%s' type='groupchat'>
    363355              <body>test</body>
     
    365357                                            from="%s"/>
    366358            </message>
    367         """ % (self.room_jid.full(), self.user_jid.full())
     359        """ % (self.occupantJID, self.userJID)
    368360
    369361        self._createRoom()
     
    408400                                              tzinfo=tzutc())})
    409401
    410         self.protocol.history(self.room_jid, archive)
     402        self.protocol.history(self.occupantJID, archive)
    411403
    412404
     
    422414        Invite a user to a room
    423415        """
    424         bareRoomJID = self.room_jid.userhostJID()
    425         invitee = JID('test@jabber.org')
    426 
    427         self.protocol.invite(bareRoomJID, invitee, 'This is a test')
    428 
    429         msg = self.stub.output[-1]
    430 
    431         query = u"/message[@to='%s']/x/invite/reason" % bareRoomJID
    432         self.assertTrue(xpath.matches(query, msg), 'Wrong message type')
    433 
    434 
    435     def test_privateMessage(self):
     416        invitee = JID('other@example.org')
     417
     418        self.protocol.invite(self.roomJID, invitee, u'This is a test')
     419
     420        message = self.stub.output[-1]
     421
     422        self.assertEquals('message', message.name)
     423        self.assertEquals(self.roomJID.full(), message.getAttribute('to'))
     424        self.assertEquals(muc.NS_MUC_USER, message.x.uri)
     425        self.assertEquals(muc.NS_MUC_USER, message.x.invite.uri)
     426        self.assertEquals(invitee.full(), message.x.invite.getAttribute('to'))
     427        self.assertEquals(muc.NS_MUC_USER, message.x.invite.reason.uri)
     428        self.assertEquals(u'This is a test', unicode(message.x.invite.reason))
     429
     430
     431    def test_groupChat(self):
    436432        """
    437433        Send private messages to muc entities.
    438434        """
    439         other_nick = JID(self.room_jid.userhost()+'/OtherNick')
    440 
    441         self.protocol.chat(other_nick, 'This is a test')
    442 
    443         msg = self.stub.output[-1]
    444 
    445         query = "/message[@type='chat' and @to='%s']/body" % other_nick.full()
    446         self.assertTrue(xpath.matches(query, msg), 'Wrong message type')
     435        self.protocol.groupChat(self.roomJID, u'This is a test')
     436
     437        message = self.stub.output[-1]
     438
     439        self.assertEquals('message', message.name)
     440        self.assertEquals(self.roomJID.full(), message.getAttribute('to'))
     441        self.assertEquals('groupchat', message.getAttribute('type'))
     442        self.assertEquals(u'This is a test', unicode(message.body))
     443
     444
     445    def test_chat(self):
     446        """
     447        Send private messages to muc entities.
     448        """
     449        otherOccupantJID = JID(self.occupantJID.userhost()+'/OtherNick')
     450
     451        self.protocol.chat(otherOccupantJID, u'This is a test')
     452
     453        message = self.stub.output[-1]
     454
     455        self.assertEquals('message', message.name)
     456        self.assertEquals(otherOccupantJID.full(), message.getAttribute('to'))
     457        self.assertEquals('chat', message.getAttribute('type'))
     458        self.assertEquals(u'This is a test', unicode(message.body))
    447459
    448460
     
    453465        http://xmpp.org/extensions/xep-0045.html#register
    454466        """
     467
     468        # FIXME: this doesn't really test the registration
    455469
    456470        def cb(iq):
     
    458472            self.assertEquals('result', iq['type'], 'We did not get a result')
    459473
    460         d = self.protocol.register(self.room_jid)
     474        d = self.protocol.register(self.roomJID)
    461475        d.addCallback(cb)
    462476
     
    474488        Client requesting voice for a room.
    475489        """
    476         self.protocol.voice(self.room_jid)
     490        self.protocol.voice(self.occupantJID)
    477491
    478492        m = self.stub.output[-1]
     
    496510        fields.append(data_form.Field(label='Natural-Language Room Name',
    497511                                      var='muc#roomconfig_roomname',
    498                                       value=self.test_room))
    499 
    500         d = self.protocol.configure(self.room_jid.userhost(), fields)
     512                                      value=self.roomIdentifier))
     513
     514        d = self.protocol.configure(self.roomJID, fields)
    501515        d.addCallback(cb)
    502516
     
    510524
    511525
    512     def test_roomDestroy(self):
     526    def test_destroy(self):
    513527        """
    514528        Destroy a room.
     
    518532            self.assertTrue(destroyed, 'Room not destroyed.')
    519533
    520         d = self.protocol.destroy(self.room_jid)
     534        d = self.protocol.destroy(self.occupantJID)
    521535        d.addCallback(cb)
    522536
     
    530544
    531545
     546    def test_subject(self):
     547        """
     548        Change subject of the room.
     549        """
     550        self.protocol.subject(self.roomJID, u'This is a test')
     551
     552        message = self.stub.output[-1]
     553
     554        self.assertEquals('message', message.name)
     555        self.assertEquals(self.roomJID.full(), message.getAttribute('to'))
     556        self.assertEquals('groupchat', message.getAttribute('type'))
     557        self.assertEquals(u'This is a test', unicode(message.subject))
     558
     559
    532560    def test_nickChange(self):
    533561        """
     
    539567
    540568        def cb(room):
    541             self.assertEquals(self.test_room, room.roomIdentifier)
     569            self.assertEquals(self.roomIdentifier, room.roomIdentifier)
    542570            self.assertEquals(test_nick, room.nick)
    543571
    544         d = self.protocol.nick(self.room_jid, test_nick)
     572        d = self.protocol.nick(self.occupantJID, test_nick)
    545573        d.addCallback(cb)
    546574
     
    556584              </x>
    557585            </presence>
    558         """ % (self.test_room, self.test_srv, test_nick)
     586        """ % (self.roomIdentifier, self.service, test_nick)
    559587        self.stub.send(parseXml(xml))
    560588        return d
     
    570598            self.assertTrue(give_voice, 'Did not give voice user')
    571599
    572         d = self.protocol.grantVoice(self.room_jid, nick, sender=self.user_jid)
     600        d = self.protocol.grantVoice(self.occupantJID, nick, sender=self.userJID)
    573601        d.addCallback(cb)
    574602
    575603        iq = self.stub.output[-1]
    576604
    577         query = ("/iq[@type='set' and @to='%s']/query/item"
    578                      "[@role='participant']") % self.room_jid.userhost()
     605        query = (u"/iq[@type='set' and @to='%s']/query/item"
     606                     "[@role='participant']") % self.roomJID
    579607        self.assertTrue(xpath.matches(query, iq), 'Wrong voice stanza')
    580608
     
    589617        """
    590618        self._createRoom()
    591         room = self.protocol._getRoom(self.room_jid)
    592         user = muc.User(self.room_jid.resource)
     619        room = self.protocol._getRoom(self.occupantJID)
     620        user = muc.User(self.nick)
    593621        room.addUser(user)
    594622
    595623        def cb(room):
    596             self.assertEquals(self.test_room, room.roomIdentifier)
    597             user = room.getUser(self.room_jid.resource)
     624            self.assertEquals(self.roomIdentifier, room.roomIdentifier)
     625            user = room.getUser(self.nick)
    598626            self.assertNotIdentical(None, user, 'User not found')
    599627            self.assertEquals('testing MUC', user.status, 'Wrong status')
    600628            self.assertEquals('xa', user.show, 'Wrong show')
    601629
    602         d = self.protocol.status(self.room_jid, 'xa', 'testing MUC')
     630        d = self.protocol.status(self.occupantJID, 'xa', 'testing MUC')
    603631        d.addCallback(cb)
    604632
     
    609637
    610638        # send back user presence, status changed
    611         xml = """
     639        xml = u"""
    612640            <presence from='%s'>
    613641              <x xmlns='http://jabber.org/protocol/muc#user'>
     
    617645              <status>testing MUC</status>
    618646            </presence>
    619         """ % self.room_jid.full()
     647        """ % self.occupantJID
    620648        self.stub.send(parseXml(xml))
    621649        return d
     
    632660
    633661        self._createRoom()
    634         bareRoomJID = self.room_jid.userhostJID()
    635         d = self.protocol.getMemberList(bareRoomJID)
     662        d = self.protocol.getMemberList(self.roomJID)
    636663        d.addCallback(cb)
    637664
     
    651678
    652679        return d
    653 
    654 
Note: See TracChangeset for help on using the changeset viewer.