diff --git a/requirements.txt b/requirements.txt index 22770025..521cfe9f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ pytest +pytest-asyncio pytest-cov ruff aiohttp diff --git a/tests/cloud_test.py b/tests/cloud_test.py new file mode 100644 index 00000000..1cdcca10 --- /dev/null +++ b/tests/cloud_test.py @@ -0,0 +1,89 @@ +"""Test cloud""" + +import os +from unittest import IsolatedAsyncioTestCase +from unittest.mock import AsyncMock, Mock + +from midealocal.cloud import MSmartHomeCloud, MeijuCloud, MideaAirCloud, get_midea_cloud + + +class CloudTest(IsolatedAsyncioTestCase): + """Cloud test case.""" + + responses: dict[str, bytes] = {} + + def setUp(self) -> None: + """Set tests up.""" + for file in os.listdir(os.path.join(os.path.dirname(__file__), "responses")): + filename = os.path.basename(file) + with open( + file=os.path.join(os.path.dirname(__file__), "responses", file), + encoding="utf-8", + ) as f: + self.responses[filename] = bytes(f.read(), encoding="utf-8") + + def test_get_midea_cloud(self) -> None: + """Test get midea cloud""" + assert isinstance(get_midea_cloud("美的美居", None, "", ""), MeijuCloud) + assert isinstance(get_midea_cloud("MSmartHome", None, "", ""), MSmartHomeCloud) + assert isinstance(get_midea_cloud("Midea Air", None, "", ""), MideaAirCloud) + assert isinstance(get_midea_cloud("NetHome Plus", None, "", ""), MideaAirCloud) + assert isinstance(get_midea_cloud("Ariston Clima", None, "", ""), MideaAirCloud) + + async def test_meijucloud_login_success(self) -> None: + """Test MeijuCloud login""" + session = Mock() + response = Mock() + response.read = AsyncMock( + side_effect=[ + self.responses["meijucloud_login_id.json"], + self.responses["meijucloud_login.json"], + ] + ) + session.request = AsyncMock(return_value=response) + cloud = get_midea_cloud( + "美的美居", session=session, account="account", password="password" + ) + assert await cloud.login() + + async def test_meijucloud_login_invalid_user(self) -> None: + """Test MeijuCloud login invalid user""" + session = Mock() + response = Mock() + response.read = AsyncMock(return_value=self.responses["invalid_response.json"]) + session.request = AsyncMock(return_value=response) + cloud = get_midea_cloud( + "美的美居", session=session, account="account", password="password" + ) + assert not await cloud.login() + + async def test_meijucloud_get_keys(self) -> None: + """Test MeijuCloud get_keys""" + session = Mock() + response = Mock() + response.read = AsyncMock( + return_value=self.responses["meijucloud_get_keys.json"] + ) + session.request = AsyncMock(return_value=response) + cloud = get_midea_cloud( + "美的美居", session=session, account="account", password="password" + ) + keys: dict = await cloud.get_keys(100) + assert keys.get(1).get("token") == "returnedappliancetoken" + assert keys.get(1).get("key") == "returnedappliancekey" + + async def test_meijucloud_list_home(self) -> None: + """Test MeijuCloud list_home""" + session = Mock() + response = Mock() + response.read = AsyncMock( + return_value=self.responses["meijucloud_list_home.json"] + ) + session.request = AsyncMock(return_value=response) + cloud = get_midea_cloud( + "美的美居", session=session, account="account", password="password" + ) + homes = await cloud.list_home() + assert len(homes.keys()) == 2 + assert homes.get(1) == "Home 1" + assert homes.get(2) == "Home 2" diff --git a/tests/responses/invalid_response.json b/tests/responses/invalid_response.json new file mode 100644 index 00000000..5b36f492 --- /dev/null +++ b/tests/responses/invalid_response.json @@ -0,0 +1,3 @@ +{ + "code":10 +} \ No newline at end of file diff --git a/tests/responses/meijucloud_get_keys.json b/tests/responses/meijucloud_get_keys.json new file mode 100644 index 00000000..b5183639 --- /dev/null +++ b/tests/responses/meijucloud_get_keys.json @@ -0,0 +1,10 @@ +{ + "code":0, + "data":{ + "tokenlist":[{ + "udpId": "dec4da86e0aeefadde14a4e553680b9b", + "token": "returnedappliancetoken", + "key": "returnedappliancekey" + }] + } +} \ No newline at end of file diff --git a/tests/responses/meijucloud_list_home.json b/tests/responses/meijucloud_list_home.json new file mode 100644 index 00000000..266eefb2 --- /dev/null +++ b/tests/responses/meijucloud_list_home.json @@ -0,0 +1,12 @@ +{ + "code":0, + "data":{ + "homeList":[{ + "homegroupId": "1", + "name": "Home 1" + },{ + "homegroupId": "2", + "name": "Home 2" + }] + } +} \ No newline at end of file diff --git a/tests/responses/meijucloud_login.json b/tests/responses/meijucloud_login.json new file mode 100644 index 00000000..6d7e480e --- /dev/null +++ b/tests/responses/meijucloud_login.json @@ -0,0 +1,9 @@ +{ + "code":0, + "data":{ + "mdata":{ + "accessToken": "returnedAccessToken" + }, + "key": "9d52c159dcdd32bac5109cf54080fca7" + } +} \ No newline at end of file diff --git a/tests/responses/meijucloud_login_id.json b/tests/responses/meijucloud_login_id.json new file mode 100644 index 00000000..0dbfe10c --- /dev/null +++ b/tests/responses/meijucloud_login_id.json @@ -0,0 +1,6 @@ +{ + "code":0, + "data":{ + "loginId": "returnedLoginId" + } +} \ No newline at end of file