|
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 |
|
@@ -151,46 +154,80 @@ async def test_store_addon_availability( |
151 | 154 |
|
152 | 155 |
|
153 | 156 | @pytest.mark.parametrize( |
154 | | - ("error_msg", "exc_type"), |
| 157 | + ("error_fixture", "error_key", "exc_type"), |
155 | 158 | [ |
156 | 159 | ( |
157 | | - "Add-on core_mosquitto not supported on this platform, " |
158 | | - "supported architectures: i386", |
| 160 | + "store_addon_availability_error_architecture.json", |
| 161 | + "addon_not_supported_architecture_error", |
159 | 162 | AddonNotSupportedArchitectureError, |
160 | 163 | ), |
161 | 164 | ( |
162 | | - "Add-on core_mosquitto not supported on this machine, " |
163 | | - "supported machine types: odroid-n2", |
| 165 | + "store_addon_availability_error_machine.json", |
| 166 | + "addon_not_supported_machine_type_error", |
164 | 167 | AddonNotSupportedMachineTypeError, |
165 | 168 | ), |
166 | 169 | ( |
167 | | - "Add-on core_mosquitto not supported on this system, " |
168 | | - "requires Home Assistant version 2023.1.1 or greater", |
| 170 | + "store_addon_availability_error_home_assistant.json", |
| 171 | + "addon_not_supported_home_assistant_version_error", |
169 | 172 | AddonNotSupportedHomeAssistantVersionError, |
170 | 173 | ), |
171 | 174 | ( |
172 | | - "Add-on core_mosquitto not supported on this system, " |
173 | | - "requires <something new> to be <something else>", |
174 | | - AddonNotSupportedError, |
| 175 | + "store_addon_availability_error_other.json", |
| 176 | + None, |
| 177 | + SupervisorBadRequestError, |
175 | 178 | ), |
176 | 179 | ], |
177 | 180 | ) |
178 | 181 | async def test_store_addon_availability_error( |
179 | 182 | responses: aioresponses, |
180 | 183 | supervisor_client: SupervisorClient, |
181 | | - error_msg: str, |
182 | | - exc_type: type[AddonNotSupportedError], |
| 184 | + error_fixture: str, |
| 185 | + error_key: str | None, |
| 186 | + exc_type: type[SupervisorError], |
183 | 187 | ) -> None: |
184 | | - """Test store addon availability API error.""" |
| 188 | + """Test store addon availability errors.""" |
| 189 | + error_body = load_fixture(error_fixture) |
| 190 | + error_data = loads(error_body) |
| 191 | + |
| 192 | + def check_availability_error(err: SupervisorError) -> bool: |
| 193 | + assert err.error_key == error_key |
| 194 | + assert err.message_template == error_data["message_template"] |
| 195 | + assert err.extra_fields == error_data["extra_fields"] |
| 196 | + return True |
| 197 | + |
| 198 | + # Availability API |
185 | 199 | responses.get( |
186 | 200 | f"{SUPERVISOR_URL}/store/addons/core_mosquitto/availability", |
187 | 201 | status=400, |
188 | | - body=f'{{"result": "error", "message": "{error_msg}"}}', |
| 202 | + body=error_body, |
189 | 203 | ) |
190 | | - |
191 | | - with pytest.raises(exc_type): |
| 204 | + with pytest.raises( |
| 205 | + exc_type, match=error_data["message"], check=check_availability_error |
| 206 | + ): |
192 | 207 | await supervisor_client.store.addon_availability("core_mosquitto") |
193 | 208 |
|
| 209 | + # Install API |
| 210 | + responses.post( |
| 211 | + f"{SUPERVISOR_URL}/store/addons/core_mosquitto/install", |
| 212 | + status=400, |
| 213 | + body=error_body, |
| 214 | + ) |
| 215 | + with pytest.raises( |
| 216 | + exc_type, match=error_data["message"], check=check_availability_error |
| 217 | + ): |
| 218 | + await supervisor_client.store.install_addon("core_mosquitto") |
| 219 | + |
| 220 | + # Update API |
| 221 | + responses.post( |
| 222 | + f"{SUPERVISOR_URL}/store/addons/core_mosquitto/update", |
| 223 | + status=400, |
| 224 | + body=error_body, |
| 225 | + ) |
| 226 | + with pytest.raises( |
| 227 | + exc_type, match=error_data["message"], check=check_availability_error |
| 228 | + ): |
| 229 | + await supervisor_client.store.update_addon("core_mosquitto") |
| 230 | + |
194 | 231 |
|
195 | 232 | async def test_store_reload( |
196 | 233 | responses: aioresponses, supervisor_client: SupervisorClient |
|
0 commit comments