2022-02-28 23:48:12 +00:00
|
|
|
"""Tests for the Backup integration."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
2024-02-27 16:56:07 +00:00
|
|
|
from syrupy import SnapshotAssertion
|
2022-02-28 23:48:12 +00:00
|
|
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
2024-02-27 16:56:07 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2022-02-28 23:48:12 +00:00
|
|
|
|
|
|
|
from .common import TEST_BACKUP, setup_backup_integration
|
|
|
|
|
2023-03-14 15:31:40 +00:00
|
|
|
from tests.typing import WebSocketGenerator
|
|
|
|
|
2022-02-28 23:48:12 +00:00
|
|
|
|
2024-02-27 16:56:07 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def sync_access_token_proxy(
|
|
|
|
access_token_fixture_name: str,
|
|
|
|
request: pytest.FixtureRequest,
|
|
|
|
) -> str:
|
|
|
|
"""Non-async proxy for the *_access_token fixture.
|
|
|
|
|
|
|
|
Workaround for https://github.com/pytest-dev/pytest-asyncio/issues/112
|
|
|
|
"""
|
|
|
|
return request.getfixturevalue(access_token_fixture_name)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"with_hassio",
|
|
|
|
(
|
|
|
|
pytest.param(True, id="with_hassio"),
|
|
|
|
pytest.param(False, id="without_hassio"),
|
|
|
|
),
|
|
|
|
)
|
2022-02-28 23:48:12 +00:00
|
|
|
async def test_info(
|
|
|
|
hass: HomeAssistant,
|
2023-03-14 15:31:40 +00:00
|
|
|
hass_ws_client: WebSocketGenerator,
|
2024-02-27 16:56:07 +00:00
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
with_hassio: bool,
|
2022-02-28 23:48:12 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test getting backup info."""
|
2024-02-27 16:56:07 +00:00
|
|
|
await setup_backup_integration(hass, with_hassio=with_hassio)
|
2022-02-28 23:48:12 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2022-03-01 20:37:51 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.backup.websocket.BackupManager.get_backups",
|
|
|
|
return_value={TEST_BACKUP.slug: TEST_BACKUP},
|
|
|
|
):
|
2024-02-27 16:56:07 +00:00
|
|
|
await client.send_json_auto_id({"type": "backup/info"})
|
|
|
|
assert snapshot == await client.receive_json()
|
2022-02-28 23:48:12 +00:00
|
|
|
|
|
|
|
|
2024-02-27 16:56:07 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"with_hassio",
|
|
|
|
(
|
|
|
|
pytest.param(True, id="with_hassio"),
|
|
|
|
pytest.param(False, id="without_hassio"),
|
|
|
|
),
|
|
|
|
)
|
2022-02-28 23:48:12 +00:00
|
|
|
async def test_remove(
|
|
|
|
hass: HomeAssistant,
|
2023-03-14 15:31:40 +00:00
|
|
|
hass_ws_client: WebSocketGenerator,
|
2024-02-27 16:56:07 +00:00
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
with_hassio: bool,
|
2022-02-28 23:48:12 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test removing a backup file."""
|
2024-02-27 16:56:07 +00:00
|
|
|
await setup_backup_integration(hass, with_hassio=with_hassio)
|
2022-02-28 23:48:12 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
with patch(
|
2022-03-01 14:16:18 +00:00
|
|
|
"homeassistant.components.backup.websocket.BackupManager.remove_backup",
|
2022-02-28 23:48:12 +00:00
|
|
|
):
|
2024-02-27 16:56:07 +00:00
|
|
|
await client.send_json_auto_id({"type": "backup/remove", "slug": "abc123"})
|
|
|
|
assert snapshot == await client.receive_json()
|
2022-02-28 23:48:12 +00:00
|
|
|
|
|
|
|
|
2024-02-27 16:56:07 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"with_hassio",
|
|
|
|
(
|
|
|
|
pytest.param(True, id="with_hassio"),
|
|
|
|
pytest.param(False, id="without_hassio"),
|
|
|
|
),
|
|
|
|
)
|
2022-02-28 23:48:12 +00:00
|
|
|
async def test_generate(
|
|
|
|
hass: HomeAssistant,
|
2023-03-14 15:31:40 +00:00
|
|
|
hass_ws_client: WebSocketGenerator,
|
2024-02-27 16:56:07 +00:00
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
with_hassio: bool,
|
2022-02-28 23:48:12 +00:00
|
|
|
) -> None:
|
2024-02-27 16:56:07 +00:00
|
|
|
"""Test generating a backup."""
|
|
|
|
await setup_backup_integration(hass, with_hassio=with_hassio)
|
2022-02-28 23:48:12 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.backup.websocket.BackupManager.generate_backup",
|
|
|
|
return_value=TEST_BACKUP,
|
|
|
|
):
|
2024-02-27 16:56:07 +00:00
|
|
|
await client.send_json_auto_id({"type": "backup/generate"})
|
|
|
|
assert snapshot == await client.receive_json()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"access_token_fixture_name",
|
|
|
|
["hass_access_token", "hass_supervisor_access_token"],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("with_hassio"),
|
|
|
|
(
|
|
|
|
pytest.param(True, id="with_hassio"),
|
|
|
|
pytest.param(False, id="without_hassio"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
async def test_backup_end(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_ws_client: WebSocketGenerator,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
request: pytest.FixtureRequest,
|
|
|
|
sync_access_token_proxy: str,
|
|
|
|
*,
|
|
|
|
access_token_fixture_name: str,
|
|
|
|
with_hassio: bool,
|
|
|
|
) -> None:
|
|
|
|
"""Test handling of post backup actions from a WS command."""
|
|
|
|
await setup_backup_integration(hass, with_hassio=with_hassio)
|
|
|
|
|
|
|
|
client = await hass_ws_client(hass, sync_access_token_proxy)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.backup.websocket.BackupManager.post_backup_actions",
|
|
|
|
):
|
|
|
|
await client.send_json_auto_id({"type": "backup/end"})
|
|
|
|
assert snapshot == await client.receive_json()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"access_token_fixture_name",
|
|
|
|
["hass_access_token", "hass_supervisor_access_token"],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("with_hassio"),
|
|
|
|
(
|
|
|
|
pytest.param(True, id="with_hassio"),
|
|
|
|
pytest.param(False, id="without_hassio"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
async def test_backup_start(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_ws_client: WebSocketGenerator,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
sync_access_token_proxy: str,
|
|
|
|
*,
|
|
|
|
access_token_fixture_name: str,
|
|
|
|
with_hassio: bool,
|
|
|
|
) -> None:
|
|
|
|
"""Test handling of pre backup actions from a WS command."""
|
|
|
|
await setup_backup_integration(hass, with_hassio=with_hassio)
|
|
|
|
|
|
|
|
client = await hass_ws_client(hass, sync_access_token_proxy)
|
|
|
|
await hass.async_block_till_done()
|
2022-02-28 23:48:12 +00:00
|
|
|
|
2024-02-27 16:56:07 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.backup.websocket.BackupManager.pre_backup_actions",
|
|
|
|
):
|
|
|
|
await client.send_json_auto_id({"type": "backup/start"})
|
|
|
|
assert snapshot == await client.receive_json()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"exception",
|
|
|
|
(
|
|
|
|
TimeoutError(),
|
|
|
|
HomeAssistantError("Boom"),
|
|
|
|
Exception("Boom"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
async def test_backup_end_excepion(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_ws_client: WebSocketGenerator,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
hass_supervisor_access_token: str,
|
|
|
|
exception: Exception,
|
|
|
|
) -> None:
|
|
|
|
"""Test exception handling while running post backup actions from a WS command."""
|
|
|
|
await setup_backup_integration(hass, with_hassio=True)
|
|
|
|
|
|
|
|
client = await hass_ws_client(hass, hass_supervisor_access_token)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.backup.websocket.BackupManager.post_backup_actions",
|
|
|
|
side_effect=exception,
|
|
|
|
):
|
|
|
|
await client.send_json_auto_id({"type": "backup/end"})
|
|
|
|
assert snapshot == await client.receive_json()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"exception",
|
|
|
|
(
|
|
|
|
TimeoutError(),
|
|
|
|
HomeAssistantError("Boom"),
|
|
|
|
Exception("Boom"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
async def test_backup_start_excepion(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_ws_client: WebSocketGenerator,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
hass_supervisor_access_token: str,
|
|
|
|
exception: Exception,
|
|
|
|
) -> None:
|
|
|
|
"""Test exception handling while running pre backup actions from a WS command."""
|
|
|
|
await setup_backup_integration(hass, with_hassio=True)
|
|
|
|
|
|
|
|
client = await hass_ws_client(hass, hass_supervisor_access_token)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.backup.websocket.BackupManager.pre_backup_actions",
|
|
|
|
side_effect=exception,
|
|
|
|
):
|
|
|
|
await client.send_json_auto_id({"type": "backup/start"})
|
|
|
|
assert snapshot == await client.receive_json()
|