Skip to content

Commit

Permalink
Add tests to cloudinterface
Browse files Browse the repository at this point in the history
  • Loading branch information
rytilahti committed Jan 29, 2023
1 parent 6ac771f commit 33d424c
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 0 deletions.
116 changes: 116 additions & 0 deletions miio/tests/fixtures/micloud_devices_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
[
{
"did": "1234",
"token": "token1",
"longitude": "0.0",
"latitude": "0.0",
"name": "device 1",
"pid": "0",
"localip": "192.168.xx.xx",
"mac": "xx:xx:xx:xx:xx:xx",
"ssid": "ssid",
"bssid": "xx:xx:xx:xx:xx:xx",
"parent_id": "",
"parent_model": "",
"show_mode": 1,
"model": "some.model.v2",
"adminFlag": 1,
"shareFlag": 0,
"permitLevel": 16,
"isOnline": false,
"desc": "description",
"extra": {
"isSetPincode": 0,
"pincodeType": 0,
"fw_version": "1.2.3",
"needVerifyCode": 0,
"isPasswordEncrypt": 0
},
"prop": {
"power": "off"
},
"uid": 1111,
"pd_id": 211,
"method": [
{
"allow_values": "",
"name": "set_power"
}
],
"password": "",
"p2p_id": "",
"rssi": -55,
"family_id": 0,
"reset_flag": 0,
"locale": "de"
},
{
"did": "4321",
"token": "token2",
"longitude": "0.0",
"latitude": "0.0",
"name": "device 2",
"pid": "0",
"localip": "192.168.xx.xx",
"mac": "yy:yy:yy:yy:yy:yy",
"ssid": "HomeNet",
"bssid": "yy:yy:yy:yy:yy:yy",
"parent_id": "",
"parent_model": "",
"show_mode": 1,
"model": "some.model.v2",
"adminFlag": 1,
"shareFlag": 0,
"permitLevel": 16,
"isOnline": false,
"desc": "description",
"extra": {
"isSetPincode": 0,
"pincodeType": 0,
"fw_version": "1.2.3",
"needVerifyCode": 0,
"isPasswordEncrypt": 0
},
"uid": 1111,
"pd_id": 2222,
"password": "",
"p2p_id": "",
"rssi": 0,
"family_id": 0,
"reset_flag": 0,
"locale": "us"
},
{
"did": "lumi.12341234",
"token": "",
"longitude": "0.0",
"latitude": "0.0",
"name": "example child device",
"pid": "3",
"localip": "",
"mac": "",
"ssid": "ssid",
"bssid": "xx:xx:xx:xx:xx:xx",
"parent_id": "654321",
"parent_model": "some.model.v3",
"show_mode": 1,
"model": "lumi.some.child",
"adminFlag": 1,
"shareFlag": 0,
"permitLevel": 16,
"isOnline": false,
"desc": "description",
"extra": {
"isSetPincode": 0,
"pincodeType": 0
},
"uid": 1111,
"pd_id": 753,
"password": "",
"p2p_id": "",
"rssi": 0,
"family_id": 0,
"reset_flag": 0,
"locale": "cn"
}
]
103 changes: 103 additions & 0 deletions miio/tests/test_cloud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import json
from pathlib import Path

import pytest
from micloud.micloudexception import MiCloudAccessDenied

from miio import CloudException, CloudInterface


def load_fixture(filename: str) -> str:
"""Load a fixture."""
file = Path(__file__).parent.absolute() / "fixtures" / filename
with file.open() as f:
return json.load(f)


MICLOUD_DEVICES_RESPONSE = load_fixture("micloud_devices_response.json")


@pytest.fixture
def cloud() -> CloudInterface:
"""Cloud interface fixture."""

return CloudInterface(username="foo", password="bar")


def test_available_locales(cloud: CloudInterface):
"""Test available locales."""
available = cloud.available_locales()
assert list(available.keys()) == ["all", "cn", "de", "i2", "ru", "sg", "us"]


def test_login_success(cloud: CloudInterface, mocker):
"""Test cloud login success."""
login = mocker.patch("micloud.MiCloud.login", return_value=True)
cloud._login()
login.assert_called()
login.reset_mock()


@pytest.mark.parametrize(
"mock_params",
[{"side_effect": MiCloudAccessDenied("msg")}, {"return_value": False}],
)
def test_login(cloud: CloudInterface, mocker, mock_params):
"""Test cloud login failures."""
mocker.patch("micloud.MiCloud.login", **mock_params)
with pytest.raises(CloudException):
cloud._login()


def test_single_login_for_all_locales(cloud: CloudInterface, mocker):
"""Test that login gets called only once."""
login = mocker.patch("micloud.MiCloud.login", return_value=True)
mocker.patch("micloud.MiCloud.get_devices", return_value=MICLOUD_DEVICES_RESPONSE)
cloud.get_devices()
login.assert_called_once()


@pytest.mark.parametrize("locale", CloudInterface.available_locales())
def test_get_devices(cloud: CloudInterface, locale, mocker):
"""Test cloud get devices."""
login = mocker.patch("micloud.MiCloud.login", return_value=True)
mocker.patch("micloud.MiCloud.get_devices", return_value=MICLOUD_DEVICES_RESPONSE)

devices = cloud.get_devices(locale)

multiplier = len(CloudInterface.available_locales()) - 1 if locale == "all" else 1
assert len(devices) == 3 * multiplier

main_devs = [dev for dev in devices.values() if not dev.is_child]
assert len(main_devs) == 2 * multiplier

dev = list(devices.values())[0]

if locale != "all":
assert dev.locale == locale

login.assert_called_once()


def test_cloud_device_info(cloud: CloudInterface, mocker):
"""Test cloud device info."""
mocker.patch("micloud.MiCloud.login", return_value=True)
mocker.patch("micloud.MiCloud.get_devices", return_value=MICLOUD_DEVICES_RESPONSE)

devices = cloud.get_devices("de")
dev = list(devices.values())[0]

assert dev.raw_data == MICLOUD_DEVICES_RESPONSE[0]
assert dev.name == "device 1"
assert dev.mac == "xx:xx:xx:xx:xx:xx"
assert dev.model == "some.model.v2"
assert dev.is_child is False
assert dev.parent_id == ""
assert dev.parent_model == ""
assert dev.is_online is False
assert dev.did == "1234"
assert dev.ssid == "ssid"
assert dev.bssid == "xx:xx:xx:xx:xx:xx"
assert dev.description == "description"
assert dev.locale == "de"
assert dev.rssi == -55

0 comments on commit 33d424c

Please sign in to comment.