diff --git a/tests/test_unsubscribe.py b/tests/test_unsubscribe.py index 0819c2e..5dd128b 100644 --- a/tests/test_unsubscribe.py +++ b/tests/test_unsubscribe.py @@ -9,11 +9,22 @@ 'room': room_car, } +subscribe_car_2 = { + 'requestId': 'car_2', + 'type': 'subscribe', + 'room': room_car, +} + unsubscribe_car = { 'requestId': 'car', 'type': 'unsubscribe', } +unsubscribe_car_2 = { + 'requestId': 'car_2', + 'type': 'unsubscribe', +} + class TestUnsubscribe(TestCase): def setUp(self): @@ -56,11 +67,45 @@ def test_no_more_publish(self): g.switch() self.assertEqual(4, len(ws.outgoing_messages)) - # test doesnt receive messages - # - # test room closes if empty - # - # test subscription removed from connection - # - # test cannot unsubscribe from non subscription + def test_sub_removed_from_room(self): + ws1 = MockWebSocket() + ws1.mock_incoming_message(json.dumps(subscribe_car)) + ws2 = MockWebSocket() + ws2.mock_incoming_message(json.dumps(subscribe_car_2)) + + g1 = greenlet(self.client.open_connection) + g1.switch(ws1) + g2 = greenlet(self.client.open_connection) + g2.switch(ws2) + + room = self.getHubRoomByDict(room_car) + c1 = room.connections[0] + c2 = room.connections[1] + self.assertHubRoomsEqual([room_car]) + self.assertEqual([c1, c2], room.connections) + self.assertEqual([room], [s.room for s in c1.subscriptions.values()]) + self.assertEqual([room], [s.room for s in c2.subscriptions.values()]) + + ws2.mock_incoming_message(json.dumps(unsubscribe_car_2)) + g2.switch() + + self.assertHubRoomsEqual([room_car]) + self.assertEqual([c1], room.connections) + self.assertEqual([room], [s.room for s in c1.subscriptions.values()]) + self.assertEqual(0, len(c2.subscriptions)) + + ws1.mock_incoming_message(json.dumps(unsubscribe_car)) + g1.switch() + + self.assertHubRoomsEqual([]) + self.assertEqual(0, len(c1.subscriptions)) + self.assertEqual(0, len(c2.subscriptions)) + + def test_cannot_unsubscribe_when_not_subscribed(self): + ws = MockWebSocket() + ws.mock_incoming_message(json.dumps(unsubscribe_car)) + + self.client.open_connection(ws) + self.assertEqual(2, len(ws.outgoing_messages)) + self.assertEqual({'requestId': 'car', 'code': 'error', 'message': 'not-subscribed'}, json.loads(ws.outgoing_messages[1]))