|
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 |
8 | 10 | from aiohasupervisor.exceptions import ( |
9 | 11 | AddonNotSupportedArchitectureError, |
10 | | - AddonNotSupportedError, |
11 | 12 | AddonNotSupportedHomeAssistantVersionError, |
12 | 13 | AddonNotSupportedMachineTypeError, |
| 14 | + SupervisorBadRequestError, |
| 15 | + SupervisorError, |
13 | 16 | ) |
14 | 17 | from aiohasupervisor.models import StoreAddonUpdate, StoreAddRepository |
15 | 18 | from aiohasupervisor.models.addons import StoreAddonInstall |
@@ -187,46 +190,80 @@ async def test_store_addon_availability( |
187 | 190 |
|
188 | 191 |
|
189 | 192 | @pytest.mark.parametrize( |
190 | | - ("error_msg", "exc_type"), |
| 193 | + ("error_fixture", "error_key", "exc_type"), |
191 | 194 | [ |
192 | 195 | ( |
193 | | - "Add-on core_mosquitto not supported on this platform, " |
194 | | - "supported architectures: i386", |
| 196 | + "store_addon_availability_error_architecture.json", |
| 197 | + "addon_not_supported_architecture_error", |
195 | 198 | AddonNotSupportedArchitectureError, |
196 | 199 | ), |
197 | 200 | ( |
198 | | - "Add-on core_mosquitto not supported on this machine, " |
199 | | - "supported machine types: odroid-n2", |
| 201 | + "store_addon_availability_error_machine.json", |
| 202 | + "addon_not_supported_machine_type_error", |
200 | 203 | AddonNotSupportedMachineTypeError, |
201 | 204 | ), |
202 | 205 | ( |
203 | | - "Add-on core_mosquitto not supported on this system, " |
204 | | - "requires Home Assistant version 2023.1.1 or greater", |
| 206 | + "store_addon_availability_error_home_assistant.json", |
| 207 | + "addon_not_supported_home_assistant_version_error", |
205 | 208 | AddonNotSupportedHomeAssistantVersionError, |
206 | 209 | ), |
207 | 210 | ( |
208 | | - "Add-on core_mosquitto not supported on this system, " |
209 | | - "requires <something new> to be <something else>", |
210 | | - AddonNotSupportedError, |
| 211 | + "store_addon_availability_error_other.json", |
| 212 | + None, |
| 213 | + SupervisorBadRequestError, |
211 | 214 | ), |
212 | 215 | ], |
213 | 216 | ) |
214 | 217 | async def test_store_addon_availability_error( |
215 | 218 | responses: aioresponses, |
216 | 219 | supervisor_client: SupervisorClient, |
217 | | - error_msg: str, |
218 | | - exc_type: type[AddonNotSupportedError], |
| 220 | + error_fixture: str, |
| 221 | + error_key: str | None, |
| 222 | + exc_type: type[SupervisorError], |
219 | 223 | ) -> None: |
220 | | - """Test store addon availability API error.""" |
| 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.message_template == error_data["message_template"] |
| 231 | + assert err.extra_fields == error_data["extra_fields"] |
| 232 | + return True |
| 233 | + |
| 234 | + # Availability API |
221 | 235 | responses.get( |
222 | 236 | f"{SUPERVISOR_URL}/store/addons/core_mosquitto/availability", |
223 | 237 | status=400, |
224 | | - body=f'{{"result": "error", "message": "{error_msg}"}}', |
| 238 | + body=error_body, |
225 | 239 | ) |
226 | | - |
227 | | - with pytest.raises(exc_type): |
| 240 | + with pytest.raises( |
| 241 | + exc_type, match=error_data["message"], check=check_availability_error |
| 242 | + ): |
228 | 243 | await supervisor_client.store.addon_availability("core_mosquitto") |
229 | 244 |
|
| 245 | + # Install API |
| 246 | + responses.post( |
| 247 | + f"{SUPERVISOR_URL}/store/addons/core_mosquitto/install", |
| 248 | + status=400, |
| 249 | + body=error_body, |
| 250 | + ) |
| 251 | + with pytest.raises( |
| 252 | + exc_type, match=error_data["message"], check=check_availability_error |
| 253 | + ): |
| 254 | + await supervisor_client.store.install_addon("core_mosquitto") |
| 255 | + |
| 256 | + # Update API |
| 257 | + responses.post( |
| 258 | + f"{SUPERVISOR_URL}/store/addons/core_mosquitto/update", |
| 259 | + status=400, |
| 260 | + body=error_body, |
| 261 | + ) |
| 262 | + with pytest.raises( |
| 263 | + exc_type, match=error_data["message"], check=check_availability_error |
| 264 | + ): |
| 265 | + await supervisor_client.store.update_addon("core_mosquitto") |
| 266 | + |
230 | 267 |
|
231 | 268 | async def test_store_reload( |
232 | 269 | responses: aioresponses, supervisor_client: SupervisorClient |
|
0 commit comments