mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Improve error reporting in onboarding backup API (#138203)
This commit is contained in:
parent
77486b9306
commit
31ea2e1714
@ -422,6 +422,11 @@ class RestoreBackupView(BackupOnboardingView):
|
|||||||
return self.json(
|
return self.json(
|
||||||
{"code": "incorrect_password"}, status_code=HTTPStatus.BAD_REQUEST
|
{"code": "incorrect_password"}, status_code=HTTPStatus.BAD_REQUEST
|
||||||
)
|
)
|
||||||
|
except HomeAssistantError as err:
|
||||||
|
return self.json(
|
||||||
|
{"code": "restore_failed", "message": str(err)},
|
||||||
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
|
)
|
||||||
return web.Response(status=HTTPStatus.OK)
|
return web.Response(status=HTTPStatus.OK)
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ from syrupy import SnapshotAssertion
|
|||||||
from homeassistant.components import backup, onboarding
|
from homeassistant.components import backup, onboarding
|
||||||
from homeassistant.components.onboarding import const, views
|
from homeassistant.components.onboarding import const, views
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import area_registry as ar
|
from homeassistant.helpers import area_registry as ar
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
@ -978,6 +979,14 @@ async def test_onboarding_backup_restore(
|
|||||||
{"code": "incorrect_password"},
|
{"code": "incorrect_password"},
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
|
# Home Assistant error
|
||||||
|
(
|
||||||
|
{"backup_id": "abc123", "agent_id": "backup.local"},
|
||||||
|
HomeAssistantError("Boom!"),
|
||||||
|
400,
|
||||||
|
{"code": "restore_failed", "message": "Boom!"},
|
||||||
|
1,
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_onboarding_backup_restore_error(
|
async def test_onboarding_backup_restore_error(
|
||||||
@ -1010,6 +1019,49 @@ async def test_onboarding_backup_restore_error(
|
|||||||
assert len(mock_restore.mock_calls) == restore_calls
|
assert len(mock_restore.mock_calls) == restore_calls
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("params", "restore_error", "expected_status", "expected_message", "restore_calls"),
|
||||||
|
[
|
||||||
|
# Unexpected error
|
||||||
|
(
|
||||||
|
{"backup_id": "abc123", "agent_id": "backup.local"},
|
||||||
|
Exception("Boom!"),
|
||||||
|
500,
|
||||||
|
"500 Internal Server Error",
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_onboarding_backup_restore_unexpected_error(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
hass_storage: dict[str, Any],
|
||||||
|
hass_client: ClientSessionGenerator,
|
||||||
|
params: dict[str, Any],
|
||||||
|
restore_error: Exception | None,
|
||||||
|
expected_status: int,
|
||||||
|
expected_message: str,
|
||||||
|
restore_calls: int,
|
||||||
|
) -> None:
|
||||||
|
"""Test returning installation type during onboarding."""
|
||||||
|
mock_storage(hass_storage, {"done": []})
|
||||||
|
|
||||||
|
assert await async_setup_component(hass, "onboarding", {})
|
||||||
|
assert await async_setup_component(hass, "backup", {})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
client = await hass_client()
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.backup.manager.BackupManager.async_restore_backup",
|
||||||
|
side_effect=restore_error,
|
||||||
|
) as mock_restore:
|
||||||
|
resp = await client.post("/api/onboarding/backup/restore", json=params)
|
||||||
|
|
||||||
|
assert resp.status == expected_status
|
||||||
|
assert (await resp.content.read()).decode().startswith(expected_message)
|
||||||
|
assert len(mock_restore.mock_calls) == restore_calls
|
||||||
|
|
||||||
|
|
||||||
async def test_onboarding_backup_upload(
|
async def test_onboarding_backup_upload(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_storage: dict[str, Any],
|
hass_storage: dict[str, Any],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user