From fd2170c82c6392142e4790f4e9bf097cf3c41324 Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Wed, 10 Jan 2024 19:47:30 +0000 Subject: [PATCH] Update config to_dict to exclude credentials if the hash is empty string (#626) * Update config to_dict to exclude credentials if the hash is empty string * Add test --- kasa/deviceconfig.py | 2 +- kasa/tests/test_deviceconfig.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/kasa/deviceconfig.py b/kasa/deviceconfig.py index f317ccb1..4ea255a4 100644 --- a/kasa/deviceconfig.py +++ b/kasa/deviceconfig.py @@ -165,7 +165,7 @@ class DeviceConfig: exclude_credentials: bool = False, ) -> Dict[str, Dict[str, str]]: """Convert device config to dict.""" - if credentials_hash or exclude_credentials: + if credentials_hash is not None or exclude_credentials: self.credentials = None if credentials_hash: self.credentials_hash = credentials_hash diff --git a/kasa/tests/test_deviceconfig.py b/kasa/tests/test_deviceconfig.py index 22d42b81..43282915 100644 --- a/kasa/tests/test_deviceconfig.py +++ b/kasa/tests/test_deviceconfig.py @@ -35,7 +35,21 @@ def test_credentials_hash(): assert config2.credentials is None -def test_no_credentials_serialization(): +def test_blank_credentials_hash(): + config = DeviceConfig( + host="Foo", + http_client=httpx.AsyncClient(), + credentials=Credentials("foo", "bar"), + ) + config_dict = config.to_dict(credentials_hash="") + config_json = json_dumps(config_dict) + config2_dict = json_loads(config_json) + config2 = DeviceConfig.from_dict(config2_dict) + assert config2.credentials_hash is None + assert config2.credentials is None + + +def test_exclude_credentials(): config = DeviceConfig( host="Foo", http_client=httpx.AsyncClient(),