From 1a751406bfee0ba4791bf445f6ef9ea2ec8f02f9 Mon Sep 17 00:00:00 2001 From: mattsaxon Date: Sun, 2 Feb 2020 12:32:32 +0000 Subject: [PATCH] Add failing tests for incorrect api key --- pysonofflanr3/client.py | 17 +++++++++++++++++ pysonofflanr3/sonoffdevice.py | 5 +++++ tests/test_cli.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/pysonofflanr3/client.py b/pysonofflanr3/client.py index b86d5f1..701979e 100644 --- a/pysonofflanr3/client.py +++ b/pysonofflanr3/client.py @@ -247,12 +247,29 @@ def update_service(self, zeroconf, type, name): self.device_id, format(ex) ) + asyncio.run_coroutine_threadsafe( + self.event_handler(None), self.loop) + + except TypeError as ex: + self.logger.error( + "Error updating service for device %s: %s" + " Probably missing API key.", + self.device_id, format(ex) + ) + + asyncio.run_coroutine_threadsafe( + self.event_handler(None), self.loop) + + except Exception as ex: self.logger.error( "Error updating service for device %s: %s, %s", self.device_id, format(ex), traceback.format_exc() ) + asyncio.run_coroutine_threadsafe( + self.event_handler(None), self.loop) + def retry_connection(self): while True: diff --git a/pysonofflanr3/sonoffdevice.py b/pysonofflanr3/sonoffdevice.py index b8f70eb..b808856 100644 --- a/pysonofflanr3/sonoffdevice.py +++ b/pysonofflanr3/sonoffdevice.py @@ -281,6 +281,11 @@ def update_params(self, params): self.logger.debug("unnecessary update received, ignoring") async def handle_message(self, message): + + # Null message shuts us down + if message is None: + self.shutdown_event_loop() + """ Receive message sent by the device and handle it, either updating state or storing basic device info diff --git a/tests/test_cli.py b/tests/test_cli.py index 2273fff..9e6acba 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -181,6 +181,36 @@ def test_cli_discover_debug(self): "Looking for all eWeLink devices on local network" in result.output ) + def test_cli_no_key(self): + + start_device("PlugEncryptMock2", "plug", "testkey") + + """Test the CLI.""" + runner = CliRunner() + result = runner.invoke( + cli.cli, + ["--device_id", "PlugEncryptMock2", "state"], + ) + + print(result.output) + + assert "encoding without a string argument" in result.output + + def test_cli_wrong_key(self): + + start_device("PlugEncryptMock3", "plug", "testkey") + + """Test the CLI.""" + runner = CliRunner() + result = runner.invoke( + cli.cli, + ["--device_id", "PlugEncryptMock3", "--api_key", "badkey", "state"], + ) + + print(result.output) + + assert "Padding is incorrect" in result.output + if __name__ == "__main__": unittest.main()