2022-02-28 23:48:12 +00:00
|
|
|
"""Tests for the Backup integration."""
|
|
|
|
from collections.abc import Awaitable, Callable
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from aiohttp import ClientWebSocketResponse
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
|
|
|
from .common import TEST_BACKUP, setup_backup_integration
|
|
|
|
|
|
|
|
|
|
|
|
async def test_info(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_ws_client: Callable[[HomeAssistant], Awaitable[ClientWebSocketResponse]],
|
|
|
|
) -> None:
|
|
|
|
"""Test getting backup info."""
|
|
|
|
await setup_backup_integration(hass)
|
|
|
|
|
|
|
|
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},
|
|
|
|
):
|
|
|
|
await client.send_json({"id": 1, "type": "backup/info"})
|
|
|
|
msg = await client.receive_json()
|
2022-02-28 23:48:12 +00:00
|
|
|
|
|
|
|
assert msg["id"] == 1
|
|
|
|
assert msg["success"]
|
2022-03-01 20:37:51 +00:00
|
|
|
assert msg["result"] == {"backing_up": False, "backups": [TEST_BACKUP.as_dict()]}
|
2022-02-28 23:48:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_remove(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_ws_client: Callable[[HomeAssistant], Awaitable[ClientWebSocketResponse]],
|
|
|
|
caplog: pytest.LogCaptureFixture,
|
|
|
|
) -> None:
|
|
|
|
"""Test removing a backup file."""
|
|
|
|
await setup_backup_integration(hass)
|
|
|
|
|
|
|
|
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
|
|
|
):
|
|
|
|
await client.send_json({"id": 1, "type": "backup/remove", "slug": "abc123"})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
|
|
|
assert msg["id"] == 1
|
|
|
|
assert msg["success"]
|
|
|
|
|
|
|
|
|
|
|
|
async def test_generate(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_ws_client: Callable[[HomeAssistant], Awaitable[ClientWebSocketResponse]],
|
|
|
|
) -> None:
|
|
|
|
"""Test removing a backup file."""
|
|
|
|
await setup_backup_integration(hass)
|
|
|
|
|
|
|
|
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,
|
|
|
|
):
|
|
|
|
await client.send_json({"id": 1, "type": "backup/generate"})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
|
|
|
assert msg["id"] == 1
|
|
|
|
assert msg["success"]
|
|
|
|
assert msg["result"] == TEST_BACKUP.as_dict()
|