def test_peer_on_not_connected_socket(self): ws = MagicMock() ws.ws_connection.stream.socket = socket.socket() ws.request.remote_ip = "127.0.0.1" client = ClientConnection(ws) client.id = 42 self.assertEqual(client.peer, "127.0.0.1:HACK|42")
def test_filter_by_property_value(self): connections = ConnectionDict() connections[1] = ClientConnection(websocket=None, name="Mario") connections[2] = ClientConnection(websocket=None, name="Goncalo") connections[3] = ClientConnection(websocket=None, name="Fredrik") answer = connections.filter_by_property_value("name", "Mario") self.assertEqual(answer, [connections[1]])
def test_connection_zombify(self, mock_datetime): mock_datetime.now.return_value = datetime(2002, 4, 4) connection = ClientConnection(websocket=None) self.assertFalse(connection.zombie) connection.zombify() self.assertEqual(connection.zombification_datetime, '2002-04-04T00:00:00') self.assertEqual(connection.topics, {'subscriber': {}, 'publisher': {}}) self.assertTrue(connection.zombie)
def test_add_publishing_channel(self): connection = ClientConnection(websocket=None) connection.add_publishing_channel(42, "reason.for.life") expected_topics = { "publisher": { "reason.for.life": 42 }, 'subscriber': {} } self.assertEqual(connection.topics, expected_topics)
def test_add_subscription_channel(self): connection = ClientConnection(websocket=None) connection.add_subscription_channel(7, "start.wars") expected_topics = { "subscriber": { "start.wars": 7 }, 'publisher': {} } self.assertEqual(connection.topics, expected_topics)
def test_get_connection(self): manager = TopicsManager() frodo = ClientConnection(None, name="Frodo") sam = ClientConnection(None, name="Sam") manager.add_subscriber("lord.of.the.rings", frodo, subscription_id=1) manager.add_publisher("lord.of.the.rings", sam, subscription_id=2) hopefully_frodo = manager.get_connection("lord.of.the.rings", 1) hopefully_sam = manager.get_connection("lord.of.the.rings", 2) self.assertEqual(frodo, hopefully_frodo) self.assertEqual(sam, hopefully_sam)
def test_add_subscriber(self): manager = TopicsManager() connection = ClientConnection(None, name="Dracula") manager.add_subscriber("romania", connection, 432) connection = manager["romania"].subscribers.pop(432) self.assertEqual(connection.name, "Dracula") self.assertTrue("romania" in connection.topics["subscriber"])
def test_add_publisher(self): manager = TopicsManager() connection = ClientConnection(None, name="Frankenstein") manager.add_publisher("gernsheim", connection, 123) connection = manager["gernsheim"].publishers.pop(123) self.assertEqual(connection.name, "Frankenstein") self.assertTrue("gernsheim" in connection.topics["publisher"])
def test_succeed(self): message = SubscribeMessage(request_id=123, topic="olympic.games") connection = ClientConnection(None) processor = SubscribeProcessor(message, connection) answer = processor.answer_message self.assertEqual(answer.code, Code.SUBSCRIBED) self.assertEqual(answer.request_id, 123)
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_get_publisher_topics(self): connection = ClientConnection(websocket=None) connection.add_publishing_channel(11, "dont.try.anything") connection.add_publishing_channel(42, "reason.for.life") expected_topics = ["dont.try.anything", "reason.for.life"] topics = connection.get_publisher_topics() self.assertEqual(sorted(topics), expected_topics)
def test_get_topics(self): connection = ClientConnection(websocket=None) connection.add_subscription_channel(12, "whiplash") connection.add_publishing_channel(21, "reload") topics = connection.get_topics() expected_topics = {'reload': 21, 'whiplash': 12} self.assertEqual(topics, expected_topics)
def test_get_publisher_topics(self): connection = ClientConnection(websocket=None) connection.add_subscription_channel(12, "again.do.nothing") connection.add_subscription_channel(32, "start.wars") expected_topics = ["again.do.nothing", "start.wars"] topics = connection.get_subscriber_topics() self.assertEqual(sorted(topics), expected_topics)
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_remove_subscriber(self): manager = TopicsManager() connection = ClientConnection(None, name="Dracula") manager.add_subscriber("romania", connection, 95) self.assertEqual(len(manager["romania"].subscribers), 1) self.assertTrue("romania" in connection.topics["subscriber"]) manager.remove_subscriber("romania", 95) self.assertEqual(len(manager["romania"].subscribers), 0) self.assertFalse("romania" in connection.topics["subscriber"])
def test_remove_publisher(self): manager = TopicsManager() connection = ClientConnection(None, name="Frankenstein") manager.add_publisher("gernsheim", connection, 123) self.assertEqual(len(manager["gernsheim"].publishers), 1) self.assertTrue("gernsheim" in connection.topics["publisher"]) manager.remove_publisher("gernsheim", 123) self.assertEqual(len(manager["gernsheim"].publishers), 0) self.assertFalse("gernsheim" in connection.topics["publisher"])
def test_fail(self, mock_authorize): message = SubscribeMessage(request_id=234, topic="olympic.games") connection = ClientConnection(None) processor = SubscribeProcessor(message, connection) answer = processor.answer_message self.assertEqual(answer.code, Code.ERROR) self.assertEqual(answer.request_id, 234) self.assertEqual(answer.request_code, Code.SUBSCRIBE) self.assertEqual(answer.uri, "tornwamp.subscribe.unauthorized")
def test_dict(self, mock_id, mock_id_2): manager = TopicsManager() mr_hyde = ClientConnection(None, name="Mr Hyde") mr_hyde.last_update = None dr_jekyll = ClientConnection(None, name="Dr Jekyll") dr_jekyll.last_update = None manager.add_subscriber("scotland", mr_hyde) manager.add_publisher("scotland", dr_jekyll) expected_dict = { 'scotland': { 'name': 'scotland', 'publishers': { 4: { 'id': 2, 'last_update': None, 'name': 'Dr Jekyll', 'topics': { 'subscriber': {}, 'publisher': { 'scotland': 4 } }, 'zombie': False, 'zombification_datetime': None } }, 'subscribers': { 3: { 'id': 1, 'last_update': None, 'name': 'Mr Hyde', 'topics': { 'subscriber': { 'scotland': 3 }, 'publisher': {} }, 'zombie': False, 'zombification_datetime': None } } } } self.assertEqual(manager.dict, expected_dict)
def test_remove_connection(self): manager = TopicsManager() connection = ClientConnection(None, name="Drakenstein") manager.add_publisher("gernsheim", connection) self.assertEqual(len(manager["gernsheim"].publishers), 1) self.assertTrue("gernsheim" in connection.topics["publisher"]) manager.add_subscriber("romania", connection) self.assertEqual(len(manager["romania"].subscribers), 1) self.assertTrue("romania" in connection.topics["subscriber"]) manager.remove_connection(connection) self.assertEqual(len(manager["romania"].subscribers), 0) self.assertEqual(len(manager["gernsheim"].publishers), 0)
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_client_connection_with_details(self, mock_global_id, mock_datetime): mock_datetime.now.return_value = datetime(1984, 5, 11) connection = ClientConnection(websocket=None, user_name="Alex", speaks_chinese=True) self.assertEqual(connection.user_name, "Alex") self.assertTrue(connection.speaks_chinese) expected_response = { "user_name": "Alex", "speaks_chinese": True, "id": 1111, "topics": {'subscriber': {}, 'publisher': {}}, "zombie": False, 'zombification_datetime': None, "last_update": '1984-05-11T00:00:00' } self.assertEqual(connection.dict, expected_response)
def test_connection_dict(self, mock_global_id, mock_datetime): mock_datetime.now.return_value = datetime(1950, 4, 6) connections = ConnectionDict() connections[2222] = ClientConnection(websocket=None, include=1, _exclude=True) expected_response = { 2222: { "include": 1, "id": 2222, "topics": {'subscriber': {}, 'publisher': {}}, "zombie": False, 'zombification_datetime': None, "last_update": '1950-04-06T00:00:00' } } self.assertEqual(connections.dict, expected_response)
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_deliver_event_messages_by_publishing(self): connection = ClientConnection(None, user_id=7475) with patch.object(self.subscriber_connection, "_websocket") as ws: msg = BroadcastMessage("education.first", EventMessage(subscription_id=1, publication_id=1), connection.id) tornwamp_topic.topics.get("education.first").publish(msg) ws.write_message.assert_called_once_with(EventMessage(subscription_id=18273, publication_id=1).json)
def test_get_subscription_id(self): connection = ClientConnection(websocket=None) connection.add_subscription_channel(5, "a.new.hope") connection.add_subscription_channel(6, "the.empire.strikes.back") subscription_id = connection.get_subscription_id("a.new.hope") self.assertEqual(subscription_id, 5)
def test_deliver_event_messages_empty_topic(self): connection = ClientConnection(None, user_id=7475) with patch.object(self.subscriber_connection, "_websocket") as ws: tornwamp_topic.customize.deliver_event_messages(Topic("education.second"), EventMessage(subscription_id=1, publication_id=1), connection.id) self.assertFalse(ws.write_message.called)
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)
def test_topics_by_subscription_id(self): connection = ClientConnection(None) connection.add_subscription_channel(10, "weird") self.assertEqual(connection.topics_by_subscription_id[10], "weird")
def test_deliver_event_messages_none_publisher_connection_id(self): connection = ClientConnection(None, user_id=7475) with patch.object(self.subscriber_connection, "_websocket") as ws: tornwamp_topic.customize.deliver_event_messages(self.topic, EventMessage(subscription_id=1, publication_id=1)) ws.write_message.assert_called_once_with(EventMessage(subscription_id=18273, publication_id=1).json)
def setUp(self): topic_name = "education.first" self.original_topics = topic.topics self.subscriber_connection = ClientConnection(None, user_id=7471) topic.topics.add_subscriber(topic_name, self.subscriber_connection, 18273)