Changeset 147:aa2cfee614e7


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

Use generic.Request for room destruction request, add TestableStreamManager?.

Location:
wokkel
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r145 r147  
    105105
    106106
    107 class OwnerRequest(xmlstream.IQ):
    108     """
    109     A basic owner iq request.
    110 
    111     @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
    112     @type method: C{str}
    113     """
    114 
    115     def __init__(self, xs, method='get'):
    116         xmlstream.IQ.__init__(self, xs, method)
    117         q = self.addElement((NS_MUC_OWNER, 'query'))
     107class DestructionRequest(generic.Request):
     108    """
     109    Room destruction request.
     110
     111    @param reason: Optional reason for the destruction of this room.
     112    @type reason: C{unicode}.
     113
     114    @param alternate: Optional room JID of an alternate venue.
     115    @type alternate: L{jid.JID}
     116
     117    @param password: Optional password for entering the alternate venue.
     118    @type password: C{unicode}
     119    """
     120
     121    stanzaType = 'set'
     122
     123    def __init__(self, recipient, sender=None, reason=None, alternate=None,
     124                       password=None):
     125        generic.Request.__init__(self, recipient, sender)
     126        self.reason = reason
     127        self.alternate = alternate
     128        self.password = password
     129
     130
     131    def toElement(self):
     132        element = generic.Request.toElement(self)
     133        element.addElement((NS_MUC_OWNER, 'query'))
     134        element.query.addElement('destroy')
     135
     136        if self.alternate:
     137            element.query.destroy['jid'] = self.alternate.full()
     138
     139            if self.password:
     140                element.query.destroy.addElement('password',
     141                                                 content=self.password)
     142
     143        if self.reason:
     144            element.query.destroy.addElement('reason', content=self.reason)
     145
     146        return element
    118147
    119148
     
    11551184
    11561185
    1157     def destroy(self, roomJID, reason=None, alternate=None):
     1186    def destroy(self, roomJID, reason=None, alternate=None, password=None):
    11581187        """
    11591188        Destroy a room.
    11601189
    1161         @param roomJID: The bare JID of the room.
    1162         @type roomJID: L{jid.JID}
    1163 
    1164         @param reason: The reason we are destroying the room.
     1190        @param roomJID: The JID of the room.
     1191        @type roomJID: L{jid.JID}
     1192
     1193        @param reason: The reason for the destruction of the room.
    11651194        @type reason: C{unicode}
    11661195
    1167         @param alternate: The bare JID of the room suggested as an alternate
    1168                           venue.
     1196        @param alternate: The JID of the room suggested as an alternate venue.
    11691197        @type alternate: L{jid.JID}
    11701198
     
    11721200        def destroyed(iq):
    11731201            self._removeRoom(roomJID)
    1174             return True
    1175 
    1176         iq = OwnerRequest(self.xmlstream, method='set')
    1177         iq['to'] = roomJID.userhost()
    1178         d = iq.query.addElement('destroy')
    1179 
    1180         if alternate is not None:
    1181             d['jid'] = alternate.userhost()
    1182         if reason is not None:
    1183             d.addElement('reason', None, reason)
    1184 
    1185         return iq.send().addCallback(destroyed)
     1202
     1203        request = DestructionRequest(recipient=roomJID, reason=reason,
     1204                                     alternate=alternate, password=password)
     1205
     1206        d = self.request(request)
     1207        d.addCallback(destroyed)
     1208        return d
    11861209
    11871210
  • wokkel/test/helpers.py

    r96 r147  
    66"""
    77
    8 from twisted.internet import defer
     8from twisted.internet import defer, task
    99from twisted.words.xish import xpath
    1010from twisted.words.xish.utility import EventDispatcher
    1111
    1212from wokkel.generic import parseXml
     13from wokkel.subprotocols import StreamManager
    1314
    1415class XmlStreamStub(object):
     
    9394
    9495        return d
     96
     97
     98class TestableStreamManager(StreamManager):
     99    """
     100    Stream manager for testing subprotocol handlers.
     101    """
     102
     103    def __init__(self, reactor=None):
     104        class DummyFactory(object):
     105            def addBootstrap(self, event, fn):
     106                pass
     107
     108        factory = DummyFactory()
     109        StreamManager.__init__(self, factory, reactor)
     110        self.stub = XmlStreamStub()
     111        self._connected(self.stub.xmlstream)
     112        self._authd(self.stub.xmlstream)
  • wokkel/test/test_muc.py

    r146 r147  
    2020from wokkel import data_form, iwokkel, muc
    2121from wokkel.generic import parseXml
    22 from wokkel.test.helpers import XmlStreamStub
     22from wokkel.test.helpers import XmlStreamStub, TestableStreamManager
    2323
    2424
     
    4747
    4848    def setUp(self):
    49         self.stub = XmlStreamStub()
    5049        self.clock = task.Clock()
     50        self.sessionManager = TestableStreamManager(reactor=self.clock)
     51        self.stub = self.sessionManager.stub
    5152        self.protocol = muc.MUCClient(reactor=self.clock)
    52         self.protocol.xmlstream = self.stub.xmlstream
    53         self.protocol.connectionInitialized()
     53        self.protocol.setHandlerParent(self.sessionManager)
     54
    5455        self.roomIdentifier = 'test'
    5556        self.service  = 'conference.example.org'
     
    620621        Destroy a room.
    621622        """
    622 
    623         def cb(destroyed):
    624             self.assertTrue(destroyed, 'Room not destroyed.')
    625 
    626         d = self.protocol.destroy(self.occupantJID)
    627         d.addCallback(cb)
     623        d = self.protocol.destroy(self.occupantJID, reason='Time to leave',
     624                                  alternate=JID('other@%s' % self.service),
     625                                  password='secret')
    628626
    629627        iq = self.stub.output[-1]
    630         query = "/iq/query[@xmlns='%s']/destroy"% muc.NS_MUC_OWNER
    631         self.assertTrue(xpath.matches(query, iq), 'Bad configure request')
     628
     629        query = ("/iq/query[@xmlns='%s']/destroy[@xmlns='%s']" %
     630                 (muc.NS_MUC_OWNER, muc.NS_MUC_OWNER))
     631
     632        nodes = xpath.queryForNodes(query, iq)
     633        self.assertNotIdentical(None, nodes, 'Bad configure request')
     634        destroy = nodes[0]
     635        self.assertEquals('Time to leave', unicode(destroy.reason))
    632636
    633637        response = toResponse(iq, 'result')
Note: See TracChangeset for help on using the changeset viewer.