def process(self): """ Return SUBSCRIBE message based on the input HELLO message. """ received_message = PublishMessage(*self.message.value) allow, msg = customize.authorize_publication(received_message.topic, self.connection) answer = None if allow: publication_id = create_global_id() if received_message.options.get("acknowledge"): answer = PublishedMessage( request_id=received_message.request_id, publication_id=publication_id ) self.direct_messages = customize.get_publish_direct_messages( received_message, publication_id, self.connection ) else: answer = ErrorMessage( request_id=received_message.request_id, request_code=received_message.code, uri="tornwamp.publish.unauthorized" ) answer.error(msg) self.answer_message = answer
def test_succeed_with_acknowledge(self): options = {"acknowledge": True} message = PublishMessage(request_id=345, topic="world.cup", options=options) connection = ClientConnection(None) processor = PublishProcessor(message, connection) answer = processor.answer_message self.assertEqual(answer.code, Code.PUBLISHED) self.assertEqual(answer.request_id, 345)
def test_fail(self, mock_authorize): message = PublishMessage(request_id=456, topic="world.cup") connection = ClientConnection(None) processor = PublishProcessor(message, connection) answer = processor.answer_message self.assertEqual(answer.code, Code.ERROR) self.assertEqual(answer.request_id, 456) self.assertEqual(answer.request_code, Code.PUBLISH) self.assertEqual(answer.uri, "tornwamp.publish.unauthorized")
def test_use_customized_message_if_available(self): options = {"acknowledge": True} expected_answer = ErrorMessage( request_id=345, request_code=16, uri="something.is.wrong" ) def error(*args, **kwargs): return None, expected_answer customize.get_publish_messages = error message = PublishMessage(request_id=345, topic="world.cup", options=options) connection = ClientConnection(None) processor = PublishProcessor(message, connection) answer = processor.answer_message self.assertEqual(answer, expected_answer)
def test_get_publish_direct_messages(self): msg = PublishMessage(request_id=168206, topic="education.first", kwargs={"type": "someMessage"}) pub_id = 91537 other_connection = ClientConnection(None, user_id=123) items = customize.get_publish_direct_messages(msg, pub_id, other_connection) self.assertEqual(len(items), 1) message = items[0]["message"] self.assertEqual(items[0]["websocket"], None) self.assertEqual(message.code, Code.EVENT) self.assertEqual(message.subscription_id, 18273) self.assertEqual(message.publication_id, 91537) self.assertEqual(message.kwargs, {"type": "someMessage"})
def test_succeed_without_acknowledge(self): message = PublishMessage(request_id=345, topic="world.cup") connection = ClientConnection(None) processor = PublishProcessor(message, connection) answer = processor.answer_message self.assertEqual(answer, None)