class TestWebsocketCallbacks(unittest.TestCase): def setUp(self): self.serial_queue = Queue.Queue() self.update_queue = Queue.Queue() self.websocket = WSClient("ws://echo.websocket.org", self.serial_queue, self.update_queue, "websockettest") self.websocket.connect() def tearDown(self): self.websocket.close() def test_opened(self): WSClient.initialise = MagicMock('mock_init') WSClient.update_server_location = MagicMock('mock_location') with LogCapture() as l: self.websocket.opened() l.check(('root', 'INFO', "websockettest - Connection opened: ws://echo.websocket.org"),) self.assertTrue(WSClient.initialise.called) self.assertTrue(WSClient.update_server_location.called) def test_received_valid_serial_message(self): ear_serial = "EARMOV L 10\r\n" WSClient.json_to_serial = MagicMock('mock_json_to_serial', return_value=ear_serial) ear_message = Message(OPCODE_TEXT, data=json.dumps({"ear": "L", "pos": 10})) with LogCapture() as l: self.websocket.received_message(ear_message) l.check(('root', 'INFO', 'websockettest - Message received: {"ear": "L", "pos": 10}'),) self.assertTrue(WSClient.json_to_serial.called) self.assertEquals(self.serial_queue.get(), ear_serial) @patch('subprocess.Popen') def test_received_valid_speech_message(self, mock_popen): ear_message = Message(OPCODE_TEXT, data=json.dumps({"text": "String to speak", "speak": 1})) with LogCapture() as l: self.websocket.received_message(ear_message) l.check(('root', 'INFO', 'websockettest - Message received: {"text": "String to speak", "speak": 1}'),) self.assertTrue(mock_popen.called) def test_received_invalid_message(self): ear_serial = "EARMOV L 10\r\n" WSClient.json_to_serial = MagicMock('mock_json_to_serial', return_value=ear_serial) ear_message = Message(OPCODE_TEXT, data='{"ear": "L" "pos": 10}') with LogCapture() as l: self.websocket.received_message(ear_message) l.check(('root', 'INFO', 'websockettest - Message received: {"ear": "L" "pos": 10}'), ('root', 'ERROR', 'websockettest - Error: Expecting , delimiter: line 1 column 13 (char 12)')) def test_closed(self): with LogCapture() as l: self.websocket.closed(1006) l.check(('root', 'INFO', 'websockettest - Connection closed with code: 1006'))
class TestUpdateServerLocation(unittest.TestCase): def setUp(self): self.serial_queue = Queue.Queue() self.update_queue = Queue.Queue() self.websocket = WSClient("ws://echo.websocket.org", self.serial_queue, self.update_queue, "websockettest") self.websocket.connect() httpretty.enable() def tearDown(self): self.websocket.close() httpretty.disable() httpretty.reset() def test_api_available(self): response = {"lat": 50.936850899999996, "lon": -1.3972685} httpretty.register_uri( httpretty.GET, "http://localhost/nabaztag/api/location", body=json.dumps(response), content_type="application/json" ) self.websocket.update_server_location() update = self.update_queue.get() response.update({"location": 1}) self.assertEquals(update, response) def test_api_unavailable(self): response = {"status": 503, "message": "Location service temporarily unavailable"} httpretty.register_uri( httpretty.GET, "http://localhost/nabaztag/api/location", body=json.dumps(response), content_type="application/json" ) self.websocket.update_server_location() update = self.update_queue.get() self.assertEquals(update, {"location": 1, "unavailable": 1})