source: wokkel/test/test_muc.py @ 107:c1566d326c58

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

just saving what I started, still needs lots of work, needs message and iq support

File size: 3.3 KB
Line 
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
16from wokkel import data_form, iwokkel, muc, shim
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()
51
52
53    def test_interface(self):
54        """
55        Do instances of L{muc.MUCClient} provide L{iwokkel.IMUCClient}?
56        """
57        verify.verifyObject(iwokkel.IMUCClient, self.protocol)
58
59
60    def test_presence(self):
61        """Test receiving room presence
62        """
63        p = muc.UserPresence()
64
65        def userPresence(prs):
66            self.failUnless(len(prs.children)==1, 'Not enough children')
67            self.failUnless(getattr(prs,'x',None), 'No x element')
68           
69
70        d, self.protocol.userPresence = calledAsync(userPresence)
71        self.stub.send(p)
72        return d
73
74    def test_joinRoom(self):
75        """Test joining a room
76        """
77        test_room = 'test'
78        test_srv  = 'conference.example.org'
79        test_nick = 'Nick'
80
81        # p = muc.BasicPresenc(to=)
82
83        def cb(room):
84            self.assertEquals(test_room, room.name)
85
86        d = self.protocol.joinRoom(test_srv, test_room, test_nick)
87        d.addCallback(cb)
88
89        prs = self.stub.output[-1]
90        self.failUnless(prs.name=='presence', "Need to be presence")
91        self.failUnless(getattr(prs, 'x', None), 'No muc x element')
92        # send back user presence, they joined
93       
94        response = muc.UserPresence()
95        print response.toXml()
96        self.stub.send(response)
97        return d
98
99class MucServiceTest(unittest.TestCase):
100    """
101    Tests for L{muc.MUCService}.
102    """
103
104    def setUp(self):
105        self.service = muc.MUCService()
106
107    def handleRequest(self, xml):
108        """
109        Find a handler and call it directly
110        """
111        handler = None
112        iq = parseXml(xml)
113        for queryString, method in self.service.iqHandlers.iteritems():
114            if xpath.internQuery(queryString).matches(iq):
115                handler = getattr(self.service, method)
116
117        if handler:
118            d = defer.maybeDeferred(handler, iq)
119        else:
120            d = defer.fail(NotImplementedError())
121
122        return d
123
124
125    def test_interface(self):
126        """
127        Do instances of L{muc.MucService} provide L{iwokkel.IMucService}?
128        """
129        verify.verifyObject(iwokkel.IMUCService, self.service)
130
131
Note: See TracBrowser for help on using the repository browser.