|
3 | 3 | import pytest |
4 | 4 | import requests |
5 | 5 |
|
| 6 | +from switchbot_client import ControlCommand |
6 | 7 | from switchbot_client.api import SwitchBotAPIClient |
7 | 8 | from switchbot_client.constants import AppConstants |
8 | 9 | from switchbot_client.devices import SwitchBotPhysicalDevice, SwitchBotRemoteDevice |
@@ -33,10 +34,15 @@ def test_init_by_config_file(patch_path_exists, mocker): |
33 | 34 |
|
34 | 35 | @patch("os.path.exists") |
35 | 36 | def test_init_by_another_config_file(patch_path_exists, mocker): |
36 | | - m = mocker.patch("builtins.open", mocker.mock_open(read_data="token: foo")) |
| 37 | + m = mocker.patch( |
| 38 | + "builtins.open", |
| 39 | + mocker.mock_open(read_data="token: foo\napi_host_domain: https://new-api.example.com"), |
| 40 | + ) |
37 | 41 | patch_path_exists.return_value = True |
38 | 42 | sut = SwitchBotAPIClient(config_file_path="~/.config/switch-bot-client/another-config.yml") |
39 | 43 | m.assert_called_with("~/.config/switch-bot-client/another-config.yml", encoding="utf-8") |
| 44 | + assert sut.token == "foo" |
| 45 | + assert sut.api_host_domain == "https://new-api.example.com" |
40 | 46 |
|
41 | 47 |
|
42 | 48 | @patch("os.path.exists") |
@@ -155,3 +161,158 @@ def mock_get(*args, **kwargs): |
155 | 161 | assert sut.status_code == expected.get("statusCode") |
156 | 162 | assert sut.message == expected.get("message") |
157 | 163 | assert sut.body == expected.get("body") |
| 164 | + |
| 165 | + |
| 166 | +def test_devices_status_wrong_device_error(monkeypatch): |
| 167 | + expected = { |
| 168 | + "statusCode": 190, |
| 169 | + "message": "error message", |
| 170 | + "body": {}, |
| 171 | + } |
| 172 | + |
| 173 | + class MockResponse: |
| 174 | + @staticmethod |
| 175 | + def json(): |
| 176 | + return expected |
| 177 | + |
| 178 | + def mock_get(*args, **kwargs): |
| 179 | + assert kwargs["headers"]["user-agent"] == f"switchbot-client/{AppConstants.VERSION}" |
| 180 | + return MockResponse() |
| 181 | + |
| 182 | + monkeypatch.setattr(requests, "get", mock_get) |
| 183 | + client = SwitchBotAPIClient("token") |
| 184 | + |
| 185 | + with pytest.raises(RuntimeError) as e: |
| 186 | + sut = client.devices_status("device_foo") |
| 187 | + assert "Wrong device ID or trying to get infrared virtual device status" in str(e.value) |
| 188 | + |
| 189 | + |
| 190 | +def test_devices_status_broken_response(monkeypatch): |
| 191 | + expected = { |
| 192 | + "someInvalidResponse": "panic", |
| 193 | + } |
| 194 | + |
| 195 | + class MockResponse: |
| 196 | + def __init__(self): |
| 197 | + self.text = """{ |
| 198 | + "someInvalidResponse": "panic", |
| 199 | + }""" |
| 200 | + |
| 201 | + @staticmethod |
| 202 | + def json(): |
| 203 | + return expected |
| 204 | + |
| 205 | + def mock_get(*args, **kwargs): |
| 206 | + assert kwargs["headers"]["user-agent"] == f"switchbot-client/{AppConstants.VERSION}" |
| 207 | + return MockResponse() |
| 208 | + |
| 209 | + monkeypatch.setattr(requests, "get", mock_get) |
| 210 | + client = SwitchBotAPIClient("token") |
| 211 | + |
| 212 | + with pytest.raises(RuntimeError) as e: |
| 213 | + sut = client.devices_status("device_foo") |
| 214 | + assert "format error" in str(e.value) |
| 215 | + |
| 216 | + |
| 217 | +def test_devices_status_unauthorized(monkeypatch): |
| 218 | + expected = { |
| 219 | + "message": "Unauthorized", |
| 220 | + } |
| 221 | + |
| 222 | + class MockResponse: |
| 223 | + @staticmethod |
| 224 | + def json(): |
| 225 | + return expected |
| 226 | + |
| 227 | + def mock_get(*args, **kwargs): |
| 228 | + assert kwargs["headers"]["user-agent"] == f"switchbot-client/{AppConstants.VERSION}" |
| 229 | + return MockResponse() |
| 230 | + |
| 231 | + monkeypatch.setattr(requests, "get", mock_get) |
| 232 | + client = SwitchBotAPIClient("token") |
| 233 | + |
| 234 | + with pytest.raises(RuntimeError) as e: |
| 235 | + sut = client.devices_status("device_foo") |
| 236 | + assert "Http 401 Error. User permission is denied due to invalid token." in str(e.value) |
| 237 | + |
| 238 | + |
| 239 | +def test_devices_commands(monkeypatch): |
| 240 | + expected = { |
| 241 | + "statusCode": 100, |
| 242 | + "message": "success", |
| 243 | + "body": {}, |
| 244 | + } |
| 245 | + |
| 246 | + class MockResponse: |
| 247 | + @staticmethod |
| 248 | + def json(): |
| 249 | + return expected |
| 250 | + |
| 251 | + def mock_post(*args, **kwargs): |
| 252 | + assert kwargs["headers"]["user-agent"] == f"switchbot-client/{AppConstants.VERSION}" |
| 253 | + return MockResponse() |
| 254 | + |
| 255 | + monkeypatch.setattr(requests, "post", mock_post) |
| 256 | + client = SwitchBotAPIClient("token") |
| 257 | + sut = client.devices_commands( |
| 258 | + "device_foo", |
| 259 | + ControlCommand.ColorBulb.SET_BRIGHTNESS, |
| 260 | + parameter="50", |
| 261 | + command_type="command", |
| 262 | + ) |
| 263 | + assert sut.status_code == expected.get("statusCode") |
| 264 | + assert sut.message == expected.get("message") |
| 265 | + assert sut.body == expected.get("body") |
| 266 | + |
| 267 | + |
| 268 | +def test_scenes(monkeypatch): |
| 269 | + expected = { |
| 270 | + "statusCode": 100, |
| 271 | + "message": "success", |
| 272 | + "body": [ |
| 273 | + {"sceneId": "S1", "sceneName": "Scene 1"}, |
| 274 | + {"sceneId": "S2", "sceneName": "Scene 2"}, |
| 275 | + ], |
| 276 | + } |
| 277 | + |
| 278 | + class MockResponse: |
| 279 | + @staticmethod |
| 280 | + def json(): |
| 281 | + return expected |
| 282 | + |
| 283 | + def mock_get(*args, **kwargs): |
| 284 | + assert kwargs["headers"]["user-agent"] == f"switchbot-client/{AppConstants.VERSION}" |
| 285 | + return MockResponse() |
| 286 | + |
| 287 | + monkeypatch.setattr(requests, "get", mock_get) |
| 288 | + client = SwitchBotAPIClient("token") |
| 289 | + sut = client.scenes() |
| 290 | + assert sut.status_code == expected.get("statusCode") |
| 291 | + assert sut.message == expected.get("message") |
| 292 | + assert sut.body == expected.get("body") |
| 293 | + |
| 294 | + |
| 295 | +def test_scenes_execute(monkeypatch): |
| 296 | + expected = { |
| 297 | + "statusCode": 100, |
| 298 | + "message": "success", |
| 299 | + "body": {}, |
| 300 | + } |
| 301 | + |
| 302 | + class MockResponse: |
| 303 | + @staticmethod |
| 304 | + def json(): |
| 305 | + return expected |
| 306 | + |
| 307 | + def mock_post(*args, **kwargs): |
| 308 | + assert kwargs["headers"]["user-agent"] == f"switchbot-client/{AppConstants.VERSION}" |
| 309 | + return MockResponse() |
| 310 | + |
| 311 | + monkeypatch.setattr(requests, "post", mock_post) |
| 312 | + client = SwitchBotAPIClient("token") |
| 313 | + sut = client.scenes_execute( |
| 314 | + "scene_foo", |
| 315 | + ) |
| 316 | + assert sut.status_code == expected.get("statusCode") |
| 317 | + assert sut.message == expected.get("message") |
| 318 | + assert sut.body == expected.get("body") |
0 commit comments