Ignore:
Timestamp:
Oct 18, 2008, 12:18:02 PM (14 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@112
Message:

Track changes to Twisted branch for the XMPP router.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/test/test_component.py

    r39 r40  
    2424
    2525    def setUp(self):
    26         self.router = component.RouterService()
     26        self.router = component.Router()
    2727        self.component = component.InternalComponent(self.router, 'component')
    2828
     
    114114        message = domish.Element((None, 'message'))
    115115
     116        self.router.route = fn
    116117        self.component.startService()
    117         self.router.routes['component'].addObserver('/message', fn)
    118118        self.component.send(message)
    119119
     
    122122
    123123
    124 class RouterServiceTest(unittest.TestCase):
    125     """
    126     Tests for L{component.RouterService}.
     124class RouterTest(unittest.TestCase):
     125    """
     126    Tests for L{component.Router}.
    127127    """
    128128
     
    131131        Test route registration and routing on incoming stanzas.
    132132        """
    133         router = component.RouterService()
     133        router = component.Router()
    134134        routed = []
    135135        router.route = lambda element: routed.append(element)
     
    151151        component1 = XmlPipe()
    152152        component2 = XmlPipe()
    153         router = component.RouterService()
     153        router = component.Router()
    154154        router.addRoute('component1.example.org', component1.sink)
    155155        router.addRoute('component2.example.org', component2.sink)
     
    158158        component2.source.addObserver('/*',
    159159                                      lambda element: outgoing.append(element))
    160         stanza = domish.Element((None, 'route'))
     160        stanza = domish.Element((None, 'presence'))
    161161        stanza['from'] = 'component1.example.org'
    162162        stanza['to'] = 'component2.example.org'
    163         stanza.addElement('presence')
    164163        component1.source.send(stanza)
    165164        self.assertEquals([stanza], outgoing)
     
    176175        component1 = XmlPipe()
    177176        s2s = XmlPipe()
    178         router = component.RouterService()
     177        router = component.Router()
    179178        router.addRoute('component1.example.org', component1.sink)
    180179        router.addRoute(None, s2s.sink)
     
    182181        outgoing = []
    183182        s2s.source.addObserver('/*', lambda element: outgoing.append(element))
    184         stanza = domish.Element((None, 'route'))
     183        stanza = domish.Element((None, 'presence'))
    185184        stanza['from'] = 'component1.example.org'
    186185        stanza['to'] = 'example.com'
    187         stanza.addElement('presence')
    188186        component1.source.send(stanza)
    189187        self.assertEquals([stanza], outgoing)
     
    211209
    212210    def test_streamStarted(self):
     211        """
     212        The received stream header should set several attributes.
     213        """
    213214        observers = []
    214215
     
    249250
    250251    def test_streamStartedNoTo(self):
     252        """
     253        The received stream header should have a 'to' attribute.
     254        """
    251255        streamErrors = []
    252256
     
    274278        self.assertEqual('1234', handshakes[-1])
    275279
     280    def test_onElementNotHandshake(self):
     281        """
     282        Reject elements that are not handshakes
     283        """
     284        handshakes = []
     285        streamErrors = []
     286
     287        xs = self.xmlstream
     288        xs.authenticator.onHandshake = handshakes.append
     289        xs.sendStreamError = streamErrors.append
     290
     291        element = domish.Element(('jabber:component:accept', 'message'))
     292        xs.authenticator.onElement(element)
     293        self.assertFalse(handshakes)
     294        self.assertEquals('not-authorized', streamErrors[-1].condition)
     295
     296
    276297    def test_onHandshake(self):
    277         xs = self.xmlstream
     298        """
     299        Receiving a handshake matching the secret authenticates the stream.
     300        """
     301        authd = []
     302
     303        def authenticated(xs):
     304            authd.append(xs)
     305
     306        xs = self.xmlstream
     307        xs.addOnetimeObserver(xmlstream.STREAM_AUTHD_EVENT, authenticated)
    278308        xs.sid = '1234'
    279309        theHash = '32532c0f7dbf1253c095b18b18e36d38d94c1256'
    280310        xs.authenticator.onHandshake(theHash)
    281311        self.assertEqual('<handshake/>', self.output[-1])
     312        self.assertEquals(1, len(authd))
    282313
    283314
    284315    def test_onHandshakeWrongHash(self):
     316        """
     317        Receiving a bad handshake should yield a stream error.
     318        """
    285319        streamErrors = []
    286320        authd = []
    287321
    288         def authenticated(self, xs):
     322        def authenticated(xs):
    289323            authd.append(xs)
    290324
     
    301335
    302336
    303 class ComponentServerTest(unittest.TestCase):
    304     """
    305     Tests for L{component.ComponentServer}.
     337class XMPPComponentServerFactoryTest(unittest.TestCase):
     338    """
     339    Tests for L{component.XMPPComponentServerFactory}.
    306340    """
    307341
    308342    def setUp(self):
    309         self.router = component.RouterService()
    310         self.server = component.ComponentServer(self.router)
    311         self.xmlstream = self.server.factory.buildProtocol(None)
     343        self.router = component.Router()
     344        self.factory = component.XMPPComponentServerFactory(self.router,
     345                                                            'secret')
     346        self.xmlstream = self.factory.buildProtocol(None)
    312347        self.xmlstream.thisEntity = JID('component.example.org')
    313348
     
    320355                                xmlstream.STREAM_CONNECTED_EVENT)
    321356        self.assertEqual(0, self.xmlstream.serial)
    322         self.assertEqual(1, self.server.serial)
     357        self.assertEqual(1, self.factory.serial)
    323358        self.assertIdentical(None, self.xmlstream.rawDataInFn)
    324359        self.assertIdentical(None, self.xmlstream.rawDataOutFn)
     
    329364        Setting logTraffic should set up raw data loggers.
    330365        """
    331         self.server.logTraffic = True
     366        self.factory.logTraffic = True
    332367        self.xmlstream.dispatch(self.xmlstream,
    333368                                xmlstream.STREAM_CONNECTED_EVENT)
Note: See TracChangeset for help on using the changeset viewer.