Skip to content

Commit

Permalink
Add failing tests for incorrect api key
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsaxon committed Feb 2, 2020
1 parent e423dc9 commit 1a75140
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pysonofflanr3/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions pysonofflanr3/sonoffdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 1a75140

Please sign in to comment.