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
This commit is contained in:
Steven B 2024-01-10 19:47:30 +00:00 committed by GitHub
parent d5a6fd8e73
commit fd2170c82c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -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

View File

@ -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(),