def test_router_session_internal_error_onAuthenticate(self): """ similar to above, but during _RouterSession's onMessage handling, where it calls self.onAuthenticate) """ # setup transport = mock.MagicMock() transport.get_channel_id = mock.MagicMock(return_value=b'deadbeef') the_exception = RuntimeError("kerblam") def boom(*args, **kw): raise the_exception session = self.session_factory( ) # __call__ on the _RouterSessionFactory session.onAuthenticate = boom session.onOpen(transport) msg = message.Authenticate('bogus signature') # do the test; should call onHello which is now "boom", above session.onMessage(msg) errors = self.flushLoggedErrors() self.assertEqual(1, len(errors), "Expected just one error: {}".format(errors)) self.assertTrue(the_exception in [fail.value for fail in errors])
def test_router_session_internal_error_onAuthenticate(self): """ similar to above, but during _RouterSession's onMessage handling, where it calls self.onAuthenticate) """ # setup transport = mock.MagicMock() transport.get_channel_id = mock.MagicMock(return_value=b'deadbeef') the_exception = RuntimeError("kerblam") def boom(*args, **kw): raise the_exception session = self.session_factory() # __call__ on the _RouterSessionFactory session.onAuthenticate = boom session.onOpen(transport) msg = message.Authenticate(u'bogus signature') # XXX think: why isn't this using _RouterSession.log? from crossbar.router.session import RouterSession with mock.patch.object(RouterSession, 'log') as logger: # do the test; should call onHello which is now "boom", above session.onMessage(msg) # check we got the right log.failure() call self.assertTrue(len(logger.method_calls) > 0) call = logger.method_calls[0] # for a MagicMock call-object, 0th thing is the method-name, 1st # thing is the arg-tuple, 2nd thing is the kwargs. self.assertEqual(call[0], 'failure') self.assertTrue('failure' in call[2]) self.assertEqual(call[2]['failure'].value, the_exception)
def _on_message(self, inc_msg): self.log.debug('WampMQTTServerProtocol._on_message(inc_msg={inc_msg})', inc_msg=inc_msg) if isinstance(inc_msg, message.Challenge): assert inc_msg.method == u"ticket" msg = message.Authenticate(signature=self._pw_challenge) del self._pw_challenge self._wamp_session.onMessage(msg) elif isinstance(inc_msg, message.Welcome): self._waiting_for_connect.callback((0, False)) elif isinstance(inc_msg, message.Abort): self._waiting_for_connect.callback((1, False)) elif isinstance(inc_msg, message.Subscribed): # Successful subscription! mqtt_id = self._subrequest_to_mqtt_subrequest[inc_msg.request] self._inflight_subscriptions[mqtt_id][inc_msg.request]["response"] = 0 self._topic_lookup[inc_msg.subscription] = self._inflight_subscriptions[mqtt_id][inc_msg.request]["topic"] if -1 not in [x["response"] for x in self._inflight_subscriptions[mqtt_id].values()]: self._subrequest_callbacks[mqtt_id].callback(None) elif (isinstance(inc_msg, message.Error) and inc_msg.request_type == message.Subscribe.MESSAGE_TYPE): # Failed subscription :( mqtt_id = self._subrequest_to_mqtt_subrequest[inc_msg.request] self._inflight_subscriptions[mqtt_id][inc_msg.request]["response"] = 128 if -1 not in [x["response"] for x in self._inflight_subscriptions[mqtt_id].values()]: self._subrequest_callbacks[mqtt_id].callback(None) elif isinstance(inc_msg, message.Event): topic = inc_msg.topic or self._topic_lookup[inc_msg.subscription] try: payload_format, mapped_topic, payload = yield self.factory.transform_wamp(topic, inc_msg) except: self.log.failure() else: self._mqtt.send_publish(mapped_topic, 0, payload, retained=inc_msg.retained or False) elif isinstance(inc_msg, message.Goodbye): if self._mqtt.transport: self._mqtt.transport.loseConnection() self._mqtt.transport = None else: self.log.warn('cannot process unimplemented message: {inc_msg}', inc_msg=inc_msg)
def onMessage(self, msg): if isinstance(msg, message.Challenge): # TODO: verify that this can *only* come from the router! # Would seem better if we could get a deferred from the # HELLO and attach this function to that... if msg.method == 'userpass': return self._transport.send( message.Authenticate( json.dumps({ 'method': msg.method, 'username': self.factory.credentials['username'], 'password': self.factory.credentials['password'], }).decode('utf-8') # WTF! )) return wamp.ApplicationSession.onMessage(self, msg)
def success(signature): if not isinstance(signature, six.text_type): raise Exception('signature must be unicode') reply = message.Authenticate(signature) self._transport.send(reply)
def success(signature): reply = message.Authenticate(signature) self._transport.send(reply)
def _on_message(self, inc_msg): if isinstance(inc_msg, message.Challenge): assert inc_msg.method == u"ticket" msg = message.Authenticate(signature=self._pw_challenge) del self._pw_challenge self._wamp_session.onMessage(msg) elif isinstance(inc_msg, message.Welcome): self._waiting_for_connect.callback((0, False)) elif isinstance(inc_msg, message.Abort): self._waiting_for_connect.callback((1, False)) elif isinstance(inc_msg, message.Subscribed): # Successful subscription! mqtt_id = self._subrequest_to_mqtt_subrequest[inc_msg.request] self._inflight_subscriptions[mqtt_id][ inc_msg.request]["response"] = 0 self._topic_lookup[ inc_msg.subscription] = self._inflight_subscriptions[mqtt_id][ inc_msg.request]["topic"] if -1 not in [ x["response"] for x in self._inflight_subscriptions[mqtt_id].values() ]: self._subrequest_callbacks[mqtt_id].callback(None) elif (isinstance(inc_msg, message.Error) and inc_msg.request_type == message.Subscribe.MESSAGE_TYPE): # Failed subscription :( mqtt_id = self._subrequest_to_mqtt_subrequest[inc_msg.request] self._inflight_subscriptions[mqtt_id][ inc_msg.request]["response"] = 128 if -1 not in [ x["response"] for x in self._inflight_subscriptions[mqtt_id].values() ]: self._subrequest_callbacks[mqtt_id].callback(None) elif isinstance(inc_msg, message.Event): topic = inc_msg.topic or self._topic_lookup[inc_msg.subscription] body = wamp_payload_transform( self._wamp_session._router._mqtt_payload_format, inc_msg) self._mqtt.send_publish(u"/".join(tokenise_wamp_topic(topic)), 0, body, retained=inc_msg.retained or False) elif isinstance(inc_msg, message.Goodbye): if self._mqtt.transport: self._mqtt.transport.loseConnection() self._mqtt.transport = None else: print("Got something we don't understand yet:") print(inc_msg)