source: wokkel/test/test_muc.py @ 116:66d9a0b47016

wokkel-muc-client-support-24
Last change on this file since 116:66d9a0b47016 was 116:66d9a0b47016, checked in by Christopher Zorn <tofu@…>, 14 years ago

change namespaces to be consistent

File size: 11.3 KB
RevLine 
[107]1# Copyright (c) 2003-2008 Ralph Meijer
2# See LICENSE for details.
3
4"""
5Tests for L{wokkel.muc}
6"""
7
8from zope.interface import verify
9
10from twisted.trial import unittest
11from twisted.internet import defer
12from twisted.words.xish import domish, xpath
13from twisted.words.protocols.jabber import error
14from twisted.words.protocols.jabber.jid import JID
15
[108]16from wokkel import data_form, iwokkel, muc, shim, disco
[107]17from wokkel.generic import parseXml
18from wokkel.test.helpers import XmlStreamStub
19
20try:
21    from twisted.words.protocols.jabber.xmlstream import toResponse
22except ImportError:
23    from wokkel.compat import toResponse
24
25
26def calledAsync(fn):
27    """
28    Function wrapper that fires a deferred upon calling the given function.
29    """
30    d = defer.Deferred()
31
32    def func(*args, **kwargs):
33        try:
34            result = fn(*args, **kwargs)
35        except:
36            d.errback()
37        else:
38            d.callback(result)
39
40    return d, func
41
42
43class MucClientTest(unittest.TestCase):
44    timeout = 2
45
46    def setUp(self):
47        self.stub = XmlStreamStub()
48        self.protocol = muc.MUCClient()
49        self.protocol.xmlstream = self.stub.xmlstream
50        self.protocol.connectionInitialized()
[110]51        self.test_room = 'test'
52        self.test_srv  = 'conference.example.org'
53        self.test_nick = 'Nick'
[107]54
[110]55        self.room_jid = JID(self.test_room+'@'+self.test_srv+'/'+self.test_nick)
[107]56
[112]57        self.user_jid = JID('test@jabber.org/Testing')
58
[113]59    def _createRoom(self):
60        # create a room
61        self.current_room = muc.Room(self.test_room, self.test_srv, self.test_nick)
62        self.protocol._setRoom(self.current_room)
63
64
[107]65    def test_interface(self):
66        """
67        Do instances of L{muc.MUCClient} provide L{iwokkel.IMUCClient}?
68        """
69        verify.verifyObject(iwokkel.IMUCClient, self.protocol)
70
71
[113]72    def test_userJoinedRoom(self):
73        """Test receiving a user joined event.
[109]74        """
75        p = muc.UserPresence()
[113]76        p['to'] = self.user_jid.full()
77        p['from'] = self.room_jid.full()
78
79        # create a room
80        self._createRoom()
81
82        def userPresence(room, user):
83            self.failUnless(room.name==self.test_room, 'Wrong room name')
84            self.failUnless(room.inRoster(user), 'User not in roster')
85                           
[109]86       
[113]87        d, self.protocol.userJoinedRoom = calledAsync(userPresence)
[109]88        self.stub.send(p)
89        return d
90
91
92    def test_groupChat(self):
93        """Test receiving room presence
94        """
95        m = muc.GroupChat('test@test.com',body='test')
[113]96        m['from'] = self.room_jid.full()
97
98        self._createRoom()
99
100        def groupChat(room, user, message):
101            self.failUnless(message=='test', "Wrong group chat message")
102            self.failUnless(room.name==self.test_room, 'Wrong room name')
[109]103                           
104       
105        d, self.protocol.receivedGroupChat = calledAsync(groupChat)
106        self.stub.send(m)
107        return d
108
[108]109
110    def test_discoServerSupport(self):
111        """Test for disco support from a server.
[107]112        """
[108]113        test_srv = 'shakespeare.lit'
[107]114
[108]115        def cb(query):
116            # check namespace
117            self.failUnless(query.uri==disco.NS_INFO, 'Wrong namespace')
[107]118           
119
[108]120        d = self.protocol.disco(test_srv)
121        d.addCallback(cb)
122
123        iq = self.stub.output[-1]
124       
125        # send back a response
126        response = toResponse(iq, 'result')
127        response.addElement('query', disco.NS_INFO)
128        # need to add information to response
[116]129        response.query.addChild(disco.DiscoFeature(muc.NS_MUC))
[108]130        response.query.addChild(disco.DiscoIdentity(category='conference',
131                                                    name='Macbeth Chat Service',
132                                                    type='text'))
133       
134        self.stub.send(response)
[107]135        return d
[108]136       
[107]137
[108]138       
[107]139    def test_joinRoom(self):
140        """Test joining a room
141        """
[110]142       
[107]143
144        def cb(room):
[110]145            self.assertEquals(self.test_room, room.name)
[107]146
[110]147        d = self.protocol.join(self.test_srv, self.test_room, self.test_nick)
[107]148        d.addCallback(cb)
149
150        prs = self.stub.output[-1]
151        self.failUnless(prs.name=='presence', "Need to be presence")
152        self.failUnless(getattr(prs, 'x', None), 'No muc x element')
[108]153
154        # send back user presence, they joined       
[110]155        response = muc.UserPresence(frm=self.test_room+'@'+self.test_srv+'/'+self.test_nick)
[108]156        self.stub.send(response)
157        return d
158
159
160    def test_joinRoomForbidden(self):
161        """Test joining a room and getting an error
162        """
163
164        def cb(error):
165            self.failUnless(isinstance(error.value,muc.PresenceError), 'Wrong type')
166            self.failUnless(error.value['type']=='error', 'Not an error returned')
167           
168           
[110]169        d = self.protocol.join(self.test_srv, self.test_room, self.test_nick)
[108]170        d.addBoth(cb)
171
172        prs = self.stub.output[-1]
173        self.failUnless(prs.name=='presence', "Need to be presence")
174        self.failUnless(getattr(prs, 'x', None), 'No muc x element')
[107]175        # send back user presence, they joined
176       
[108]177        response = muc.PresenceError(error=muc.MUCError('auth',
178                                                        'forbidden'
179                                                        ),
[110]180                                     frm=self.room_jid.full())
[107]181        self.stub.send(response)
[108]182        return d       
[109]183
[110]184    def test_partRoom(self):
[112]185       
186        def cb(left):
187            self.failUnless(left, 'did not leave room')
188
189
190        d = self.protocol.leave(self.room_jid)
191        d.addCallback(cb)
192
193        prs = self.stub.output[-1]
194       
195        self.failUnless(prs['type']=='unavailable', 'Unavailable is not being sent')
196       
197        response = prs
198        response['from'] = response['to']
199        response['to'] = 'test@jabber.org'
200
201        self.stub.send(response)
202        return d
[110]203       
204
205    def test_ban(self):
206       
[112]207        banned = JID('ban@jabber.org/TroubleMakger')
208        def cb(banned):
209            self.failUnless(banned, 'Did not ban user')
210
211           
212        d = self.protocol.ban(self.room_jid, banned, self.user_jid, reason='Spam')
213        d.addCallback(cb)
214
215        iq = self.stub.output[-1]
216       
217        self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='outcast']" % (self.room_jid.userhost(),), iq), 'Wrong ban stanza')
218
219        response = toResponse(iq, 'result')
220
221        self.stub.send(response)
222
223        return d
224
[110]225
226    def test_kick(self):
[112]227
228        kicked = JID('kick@jabber.org/TroubleMakger')
229        def cb(kicked):
230            self.failUnless(kicked, 'Did not kick user')
231
232           
233        d = self.protocol.kick(self.room_jid, kicked, self.user_jid, reason='Spam')
234        d.addCallback(cb)
235
236        iq = self.stub.output[-1]
237       
238        self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='none']" % (self.room_jid.userhost(),), iq), 'Wrong kick stanza')
239
240        response = toResponse(iq, 'result')
241
242        self.stub.send(response)
243
244        return d
245
[110]246       
247
248    def test_password(self):
249        """Test sending a password via presence to a password protected room.
250        """
251       
[112]252        self.protocol.password(self.room_jid, 'secret')
[110]253       
[112]254        prs = self.stub.output[-1]
255       
256        self.failUnless(xpath.matches("/presence[@to='%s']/x/password[text()='secret']" % (self.room_jid.full(),), prs), 'Wrong presence stanza')
257
[110]258
259    def test_history(self):
[112]260        """Test receiving history on room join.
261        """
262        m = muc.HistoryMessage(self.room_jid.userhost(), self.protocol._makeTimeStamp(), body='test')
[113]263        m['from'] = self.room_jid.full()
264       
265        self._createRoom()
266
267        def roomHistory(room, user, body, stamp, frm=None):
268            self.failUnless(body=='test', "wrong message body")
269            self.failUnless(stamp, 'Does not have a history stamp')
[112]270                   
271
272        d, self.protocol.receivedHistory = calledAsync(roomHistory)
273        self.stub.send(m)
274        return d
[110]275
276
277    def test_oneToOneChat(self):
278        """Test converting a one to one chat
279        """
280        archive = []
281        thread = "e0ffe42b28561960c6b12b944a092794b9683a38"
282        # create messages
283        msg = domish.Element((None, 'message'))
284        msg['to'] = 'testing@example.com'
285        msg['type'] = 'chat'
286        msg.addElement('body', None, 'test')
287        msg.addElement('thread', None, thread)
288
289        archive.append(msg)
290
291        msg = domish.Element((None, 'message'))
292        msg['to'] = 'testing2@example.com'
293        msg['type'] = 'chat'
294        msg.addElement('body', None, 'yo')
295        msg.addElement('thread', None, thread)
296
297        archive.append(msg)
298
299        self.protocol.history(self.room_jid.userhost(), archive)
300
301
302        while len(self.stub.output)>0:
303            m = self.stub.output.pop()
304            # check for delay element
305            self.failUnless(m.name=='message', 'Wrong stanza')
306            self.failUnless(xpath.matches("/message/delay", m), 'Invalid history stanza')
307       
308
309    def test_invite(self):
[112]310        other_jid = 'test@jabber.org'
311
312        self.protocol.invite(other_jid, 'This is a test')
313
314        msg = self.stub.output[-1]
315
316        self.failUnless(xpath.matches("/message[@to='%s']/x/invite/reason" % (other_jid,), msg), 'Wrong message type')
317
[110]318
319       
320    def test_privateMessage(self):
[112]321        """Test sending private messages to muc entities.
322        """
323        other_nick = self.room_jid.userhost()+'/OtherNick'
324
325        self.protocol.chat(other_nick, 'This is a test')
326
327        msg = self.stub.output[-1]
328
329        self.failUnless(xpath.matches("/message[@type='chat' and @to='%s']/body" % (other_nick,), msg), 'Wrong message type')
330
[110]331
332    def test_register(self):
[111]333        """Test client registering with a room. http://xmpp.org/extensions/xep-0045.html#register
334
335        """
336       
337        def cb(iq):
338            # check for a result
339            self.failUnless(iq['type']=='result', 'We did not get a result')
340       
341        d = self.protocol.register(self.room_jid.userhost())
342        d.addCallback(cb)
343
344        iq = self.stub.output[-1]
[116]345        print iq.toXml()
[111]346        self.failUnless(xpath.matches("/iq/query[@xmlns='%s']" % (muc.NS_REQUEST), iq), 'Invalid iq register request')
347       
348        response = toResponse(iq, 'result')
[116]349       
[111]350        self.stub.send(response)
351        return d
[110]352
353    def test_voice(self):
[111]354        """
355        """
[110]356        self.protocol.voice(self.room_jid.userhost())
357
358        m = self.stub.output[-1]
[111]359       
[116]360        self.failUnless(xpath.matches("/message/x[@type='submit']/field/value[text()='%s']" % (muc.NS_MUC_REQUEST,), m), 'Invalid voice message stanza')
[110]361
362
[109]363    def test_roomConfigure(self):
[111]364        """
365        """
[109]366
[111]367        def cb(iq):
368            self.failUnless(iq['type']=='result', 'Not a result')
369           
370
371        fields = []
372
373        fields.append(data_form.Field(label='Natural-Language Room Name',
374                                      var='muc#roomconfig_roomname',
375                                      value=self.test_room))
[109]376       
[111]377        d = self.protocol.configure(self.room_jid.userhost(), fields)
378        d.addCallback(cb)
[109]379
[111]380        iq = self.stub.output[-1]
[116]381        self.failUnless(xpath.matches("/iq/query[@xmlns='%s']/x"% (muc.NS_MUC_OWNER,), iq), 'Bad configure request')
[111]382       
383        response = toResponse(iq, 'result')
384        self.stub.send(response)
385        return d
386
387
Note: See TracBrowser for help on using the repository browser.