2023-12-29 19:17:15 +00:00
|
|
|
from json import dumps as json_dumps
|
|
|
|
from json import loads as json_loads
|
|
|
|
|
2024-01-18 17:32:26 +00:00
|
|
|
import aiohttp
|
2024-01-18 17:51:50 +00:00
|
|
|
import pytest
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
from kasa.credentials import Credentials
|
|
|
|
from kasa.deviceconfig import (
|
|
|
|
DeviceConfig,
|
|
|
|
)
|
2024-02-21 15:52:55 +00:00
|
|
|
from kasa.exceptions import KasaException
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
|
2024-01-18 17:32:26 +00:00
|
|
|
async def test_serialization():
|
|
|
|
config = DeviceConfig(host="Foo", http_client=aiohttp.ClientSession())
|
2023-12-29 19:17:15 +00:00
|
|
|
config_dict = config.to_dict()
|
|
|
|
config_json = json_dumps(config_dict)
|
|
|
|
config2_dict = json_loads(config_json)
|
|
|
|
config2 = DeviceConfig.from_dict(config2_dict)
|
|
|
|
assert config == config2
|
2024-01-03 21:46:08 +00:00
|
|
|
|
|
|
|
|
2024-01-18 17:51:50 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("input_value", "expected_msg"),
|
|
|
|
[
|
|
|
|
({"Foo": "Bar"}, "Cannot create dataclass from dict, unknown key: Foo"),
|
|
|
|
("foobar", "Invalid device config data: foobar"),
|
|
|
|
],
|
|
|
|
ids=["invalid-dict", "not-dict"],
|
|
|
|
)
|
|
|
|
def test_deserialization_errors(input_value, expected_msg):
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException, match=expected_msg):
|
2024-01-18 17:51:50 +00:00
|
|
|
DeviceConfig.from_dict(input_value)
|
|
|
|
|
|
|
|
|
2024-01-18 17:32:26 +00:00
|
|
|
async def test_credentials_hash():
|
2024-01-03 21:46:08 +00:00
|
|
|
config = DeviceConfig(
|
|
|
|
host="Foo",
|
2024-01-18 17:32:26 +00:00
|
|
|
http_client=aiohttp.ClientSession(),
|
2024-01-03 21:46:08 +00:00
|
|
|
credentials=Credentials("foo", "bar"),
|
|
|
|
)
|
|
|
|
config_dict = config.to_dict(credentials_hash="credhash")
|
|
|
|
config_json = json_dumps(config_dict)
|
|
|
|
config2_dict = json_loads(config_json)
|
|
|
|
config2 = DeviceConfig.from_dict(config2_dict)
|
|
|
|
assert config2.credentials_hash == "credhash"
|
|
|
|
assert config2.credentials is None
|
|
|
|
|
|
|
|
|
2024-01-18 17:32:26 +00:00
|
|
|
async def test_blank_credentials_hash():
|
2024-01-10 19:47:30 +00:00
|
|
|
config = DeviceConfig(
|
|
|
|
host="Foo",
|
2024-01-18 17:32:26 +00:00
|
|
|
http_client=aiohttp.ClientSession(),
|
2024-01-10 19:47:30 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2024-01-18 17:32:26 +00:00
|
|
|
async def test_exclude_credentials():
|
2024-01-03 21:46:08 +00:00
|
|
|
config = DeviceConfig(
|
|
|
|
host="Foo",
|
2024-01-18 17:32:26 +00:00
|
|
|
http_client=aiohttp.ClientSession(),
|
2024-01-03 21:46:08 +00:00
|
|
|
credentials=Credentials("foo", "bar"),
|
|
|
|
)
|
|
|
|
config_dict = config.to_dict(exclude_credentials=True)
|
|
|
|
config_json = json_dumps(config_dict)
|
|
|
|
config2_dict = json_loads(config_json)
|
|
|
|
config2 = DeviceConfig.from_dict(config2_dict)
|
|
|
|
assert config2.credentials is None
|