File: test_cloud_api.py

package info (click to toggle)
python-intellifire4py 4.1.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 620 kB
  • sloc: python: 2,585; sh: 13; makefile: 5
file content (284 lines) | stat: -rw-r--r-- 9,913 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""Test cloud functions."""

import pytest

# from httpx import Cookies

from intellifire4py import UnifiedFireplace
from intellifire4py.cloud_interface import IntelliFireCloudInterface
from intellifire4py.const import IntelliFireApiMode
from intellifire4py.exceptions import LoginError


@pytest.mark.asyncio
async def test_cloud_login(  # type: ignore
    mock_cloud_login_flow_no_local,
    user_id,
    api_key,
):
    """Test cloud login."""
    username = "user"
    password = "pass"  # noqa: S105

    async with IntelliFireCloudInterface() as cloud_interface:
        await cloud_interface.login_with_credentials(
            username=username, password=password
        )
        user_data = cloud_interface.user_data
        fireplaces = await UnifiedFireplace.build_fireplaces_from_user_data(
            user_data,
            desired_control_mode=IntelliFireApiMode.NONE,
            desired_read_mode=IntelliFireApiMode.NONE,
        )
        fireplace = fireplaces[0]
        await fireplace.perform_cloud_poll()
        assert fireplace.serial == "XXXXXE834CE109D849CBB15CDDBAFF381"
        assert fireplace.data.brand == "H&G"
        assert fireplace.data.name == "Living Room"
        assert fireplace.api_key == api_key
        assert fireplace.data.is_on is False
        assert fireplace.data.pilot_on is True

        assert cloud_interface.user_data.user_id == user_id


@pytest.mark.asyncio
@pytest.mark.asyncio
async def test_control(  # type: ignore
    mock_login_for_control_testing,
    user_id: str,
    api_key: str,
) -> None:  # Build cookies
    """Test control."""
    username = "user"
    password = "pass"  # noqa: S105
    async with IntelliFireCloudInterface() as cloud_interface:
        await cloud_interface.login_with_credentials(
            username=username, password=password
        )
        user_data = cloud_interface.user_data
        fireplaces = await UnifiedFireplace.build_fireplaces_from_user_data(
            user_data,
            desired_control_mode=IntelliFireApiMode.CLOUD,
            desired_read_mode=IntelliFireApiMode.CLOUD,
        )
        fp = fireplaces[0]

        assert fp.data.serial == "unset"
        assert fp.data.brand == "H&G"
        assert fp.data.battery == 0
        assert fp.data.connection_quality == 987690
        await fp.read_api.start_background_polling()

        assert fp.read_api.is_polling_in_background is True

        await fp.control_api.pilot_on()
        assert fp.data.pilot_on is True
        await fp.control_api.flame_on()
        assert fp.data.is_on is True
        await fp.control_api.flame_off()
        await fp.control_api.soft_reset()
        await fp.control_api.stop_sleep_timer()
        await fp.control_api.set_thermostat_c(10)
        await fp.control_api.set_thermostat_f(70)
        await fp.control_api.turn_on_thermostat()
        await fp.control_api.turn_off_thermostat()
        await fp.control_api.fan_off()
        await fp.control_api.beep()
        assert fp.data.is_on is False
        assert fp.read_api.is_polling_in_background is True
        assert fp.read_api.data.name == "Living Room"
        await fp.read_api.stop_background_polling()


@pytest.mark.asyncio
async def test_incorrect_login_credentials(mock_login_bad_credentials) -> None:  # type: ignore
    """Test login with incorrect credentials."""
    username = "incorrect_user"
    password = "incorrect_password"  # noqa S105)

    async with IntelliFireCloudInterface() as cloud_interface:
        with pytest.raises(LoginError):
            await cloud_interface.login_with_credentials(
                username=username, password=password
            )


@pytest.mark.asyncio
async def test_bad_command_param(mock_cloud_login_flow_no_local) -> None:  # type:ignore
    """Test bad command parameter handling."""
    username = "user"
    password = "pass"  # noqa: S105

    async with IntelliFireCloudInterface() as cloud_interface:
        await cloud_interface.login_with_credentials(
            username=username, password=password
        )
        user_data = cloud_interface.user_data
        fireplaces = await UnifiedFireplace.build_fireplaces_from_user_data(
            user_data,
            desired_control_mode=IntelliFireApiMode.CLOUD,
            desired_read_mode=IntelliFireApiMode.CLOUD,
        )
        fireplace = fireplaces[0]  # type: ignore #noqa: F841


#
# @pytest.mark.asyncio
# @pytest.mark.parametrize(
#     "status_code, expected_error",
#     [
#         (204, None),  # Success
#         (403, "Not authorized"),
#         (404, "Fireplace not found (bad serial number)"),
#         (422, "Invalid Parameter (invalid command id or command value)"),
#         (69, "Unexpected return code"),
#     ],
# )
# async def test_sending(
#     httpx_mock: HTTPXMock,
#     status_code: int,
#     expected_error: str,
#     enum_fireplaces_json: str,
#     enum_locations_json: str,
#     cloud_poll_json: str,
#     cookies: list[tuple[str, str]],
#     user_id: str,
#     api_key: str,
# ) -> None:
#     """Test some sending."""
#     username = "user"
#     password = "pass"  # noqa: S105
#     httpx_mock.add_response(
#         method="POST",
#         url="https://iftapi.net/a/login",
#         status_code=204,
#         headers=cookies,
#     )
#     httpx_mock.add_response(
#         method="GET",
#         url="https://iftapi.net/a/enumlocations",
#         text=enum_locations_json,
#     )
#
#     httpx_mock.add_response(
#         method="GET",
#         url="https://iftapi.net/a/enumfireplaces?location_id=11118333339267293392",
#         text=enum_fireplaces_json,
#     )
#     httpx_mock.add_response(
#         method="GET",
#         url="https://iftapi.net/a/XXXXXE834CE109D849CBB15CDDBAFF381//apppoll",
#         text=cloud_poll_json,
#     )
#
#     cloud_api = IntelliFireCloudInterface()
#     await cloud_api.login_with_credentials(username=username, password=password)
#     # Fake logged in state
#
#     # cloud_api = IntelliFireAPICloud()
#     # await cloud_api.beep()  # Do nothing
#
#     # ALl this should be mock/patched

#
# @pytest.mark.asyncio
# @pytest.mark.parametrize(
#     "status_code, expected_error",
#     [
#         (204, None),  # Success
#         (403, "Not authorized"),
#         (404, "Fireplace not found (bad serial number)"),
#         (422, "Invalid Parameter (invalid command id or command value)"),
#         (69, "Unexpected return code"),
#     ],
# )
# async def test_sending(
#     status_code: int,
#     expected_error: str,
#     enum_fireplaces_json: str,
#     enum_locations_json: str,
#     cloud_poll_json: str,
#     cookies: list[tuple[str, str]],
#     user_id: str,
#     api_key: str,
# ):
#     """Test some sending."""
#     username = "user"
#     password = "pass"  # noqa: S105
#
#     with aioresponses() as m:
#         # Mocking the POST login request
#         m.post("https://iftapi.net/a/login", status=204, headers=cookies)
#
#         # Mocking other GET requests
#         m.get(
#             "https://iftapi.net/a/enumlocations", status=200, body=enum_locations_json
#         )
#         m.get(
#             "https://iftapi.net/a/enumfireplaces?location_id=11118333339267293392",
#             status=200,
#             body=enum_fireplaces_json,
#         )
#         m.get(
#             "https://iftapi.net/a/XXXXXE834CE109D849CBB15CDDBAFF381//apppoll",
#             status=200,
#             body=cloud_poll_json,
#         )
#
#         async with IntelliFireCloudInterface() as cloud_interface:
#             await cloud_interface.login_with_credentials(
#                 username=username, password=password
#             )
#
#             # Get a fireplace
#             user_data = cloud_interface.user_data
#             fireplaces = await UnifiedFireplace.build_fireplaces_from_user_data(
#                 user_data,
#                 control_mode=IntelliFireApiMode.CLOUD,
#                 read_mode=IntelliFireApiMode.CLOUD,
#             )
#             fireplace = fireplaces[0]
#
#             if expected_error:
#                 with pytest.raises(expected_error):
#                     await fireplace.control_api.beep()
#
#             # Depending on what you are testing, you'll need to simulate sending a request
#             # and mock the response based on the status_code and expected_error parameters
#             # For example, if you're testing a POST request:
#             # m.post("https://iftapi.net/a/some_endpoint", status=status_code)
#
#             # Now you can call the method that sends the request and assert the expected behavior
#             # For example:
#             # if expected_error:
#             #     with pytest.raises(expected_error):
#             #         # Call the method that sends the request
#             #         pass
#             # else:
#             #     # Call the method that sends the request and assert the success case
#             #     pass
#
#
# #
# #
# # async with IntelliFireCloudInterface() as cloud_interface:
# #        await cloud_interface.login_with_credentials(
# #            username=username, password=password
# #        )
# #        user_data = cloud_interface.user_data
# #        fireplaces = await UnifiedFireplace.build_fireplaces_from_user_data(
# #            user_data,
# #            control_mode=IntelliFireApiMode.NONE,
# #            read_mode=IntelliFireApiMode.NONE,
# #        )
# #        fireplace = fireplaces[0]
# #        await fireplace.perform_cloud_poll()
# #        assert fireplace.serial == "XXXXXE834CE109D849CBB15CDDBAFF381"
# #        assert fireplace.data.brand == "H&G"
# #        assert fireplace.data.name == "Living Room"
# #        assert fireplace.api_key == api_key
# #        assert fireplace.data.is_on is False
# #        assert fireplace.data.pilot_on is True
# #
# #        assert cloud_interface.user_data.user_id == user_id