|
1 | 1 | """Tests for store supervisor client.""" |
2 | 2 |
|
| 3 | +from json import loads |
| 4 | + |
3 | 5 | from aioresponses import aioresponses |
4 | 6 | import pytest |
5 | 7 | from yarl import URL |
6 | 8 |
|
7 | 9 | from aiohasupervisor import SupervisorClient |
| 10 | +from aiohasupervisor.exceptions import ( |
| 11 | + AddonNotSupportedArchitectureError, |
| 12 | + AddonNotSupportedHomeAssistantVersionError, |
| 13 | + AddonNotSupportedMachineTypeError, |
| 14 | + SupervisorBadRequestError, |
| 15 | + SupervisorError, |
| 16 | +) |
8 | 17 | from aiohasupervisor.models import StoreAddonUpdate, StoreAddRepository |
9 | 18 | from aiohasupervisor.models.addons import StoreAddonInstall |
10 | 19 |
|
@@ -169,6 +178,92 @@ async def test_store_addon_update( |
169 | 178 | ) |
170 | 179 |
|
171 | 180 |
|
| 181 | +async def test_store_addon_availability( |
| 182 | + responses: aioresponses, supervisor_client: SupervisorClient |
| 183 | +) -> None: |
| 184 | + """Test store addon availability API.""" |
| 185 | + responses.get( |
| 186 | + f"{SUPERVISOR_URL}/store/addons/core_mosquitto/availability", status=200 |
| 187 | + ) |
| 188 | + |
| 189 | + assert (await supervisor_client.store.addon_availability("core_mosquitto")) is None |
| 190 | + |
| 191 | + |
| 192 | +@pytest.mark.parametrize( |
| 193 | + ("error_fixture", "error_key", "exc_type"), |
| 194 | + [ |
| 195 | + ( |
| 196 | + "store_addon_availability_error_architecture.json", |
| 197 | + "addon_not_supported_architecture_error", |
| 198 | + AddonNotSupportedArchitectureError, |
| 199 | + ), |
| 200 | + ( |
| 201 | + "store_addon_availability_error_machine.json", |
| 202 | + "addon_not_supported_machine_type_error", |
| 203 | + AddonNotSupportedMachineTypeError, |
| 204 | + ), |
| 205 | + ( |
| 206 | + "store_addon_availability_error_home_assistant.json", |
| 207 | + "addon_not_supported_home_assistant_version_error", |
| 208 | + AddonNotSupportedHomeAssistantVersionError, |
| 209 | + ), |
| 210 | + ( |
| 211 | + "store_addon_availability_error_other.json", |
| 212 | + None, |
| 213 | + SupervisorBadRequestError, |
| 214 | + ), |
| 215 | + ], |
| 216 | +) |
| 217 | +async def test_store_addon_availability_error( |
| 218 | + responses: aioresponses, |
| 219 | + supervisor_client: SupervisorClient, |
| 220 | + error_fixture: str, |
| 221 | + error_key: str | None, |
| 222 | + exc_type: type[SupervisorError], |
| 223 | +) -> None: |
| 224 | + """Test store addon availability errors.""" |
| 225 | + error_body = load_fixture(error_fixture) |
| 226 | + error_data = loads(error_body) |
| 227 | + |
| 228 | + def check_availability_error(err: SupervisorError) -> bool: |
| 229 | + assert err.error_key == error_key |
| 230 | + assert err.extra_fields == error_data["extra_fields"] |
| 231 | + return True |
| 232 | + |
| 233 | + # Availability API |
| 234 | + responses.get( |
| 235 | + f"{SUPERVISOR_URL}/store/addons/core_mosquitto/availability", |
| 236 | + status=400, |
| 237 | + body=error_body, |
| 238 | + ) |
| 239 | + with pytest.raises( |
| 240 | + exc_type, match=error_data["message"], check=check_availability_error |
| 241 | + ): |
| 242 | + await supervisor_client.store.addon_availability("core_mosquitto") |
| 243 | + |
| 244 | + # Install API |
| 245 | + responses.post( |
| 246 | + f"{SUPERVISOR_URL}/store/addons/core_mosquitto/install", |
| 247 | + status=400, |
| 248 | + body=error_body, |
| 249 | + ) |
| 250 | + with pytest.raises( |
| 251 | + exc_type, match=error_data["message"], check=check_availability_error |
| 252 | + ): |
| 253 | + await supervisor_client.store.install_addon("core_mosquitto") |
| 254 | + |
| 255 | + # Update API |
| 256 | + responses.post( |
| 257 | + f"{SUPERVISOR_URL}/store/addons/core_mosquitto/update", |
| 258 | + status=400, |
| 259 | + body=error_body, |
| 260 | + ) |
| 261 | + with pytest.raises( |
| 262 | + exc_type, match=error_data["message"], check=check_availability_error |
| 263 | + ): |
| 264 | + await supervisor_client.store.update_addon("core_mosquitto") |
| 265 | + |
| 266 | + |
172 | 267 | async def test_store_reload( |
173 | 268 | responses: aioresponses, supervisor_client: SupervisorClient |
174 | 269 | ) -> None: |
|
0 commit comments