Skip to content

Use setup_backup_integration test helper in backup tests #138362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions tests/components/backup/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from tests.common import MockPlatform, mock_platform
from tests.common import mock_platform

LOCAL_AGENT_ID = f"{DOMAIN}.local"

Expand Down Expand Up @@ -138,15 +138,15 @@ async def setup_backup_integration(
raise ValueError(f"Invalid agent_id: {agent}")
name = agent.partition(".")[2]
remote_agents_dict[agent] = mock_backup_agent(name, backups.get(agent))
platform = Mock(
async_get_backup_agents=AsyncMock(
return_value=list(remote_agents_dict.values())
),
spec_set=BackupAgentPlatformProtocol,
)

mock_platform(hass, f"{TEST_DOMAIN}.backup", platform or MockPlatform())
assert await async_setup_component(hass, TEST_DOMAIN, {})
if remote_agents:
platform = Mock(
async_get_backup_agents=AsyncMock(
return_value=list(remote_agents_dict.values())
),
spec_set=BackupAgentPlatformProtocol,
)
await setup_backup_platform(hass, domain=TEST_DOMAIN, platform=platform)

assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()

Expand Down
40 changes: 20 additions & 20 deletions tests/components/backup/snapshots/test_websocket.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
'result': dict({
'agents': list([
dict({
'agent_id': 'backup.local',
'name': 'local',
'agent_id': 'test.remote',
'name': 'remote',
}),
dict({
'agent_id': 'test.test',
'name': 'test',
'agent_id': 'backup.local',
'name': 'local',
}),
]),
}),
Expand Down Expand Up @@ -3457,7 +3457,7 @@
'id': 1,
'result': dict({
'agent_errors': dict({
'domain.test': 'The backup agent is unreachable.',
'test.remote': 'The backup agent is unreachable.',
}),
}),
'success': True,
Expand All @@ -3480,7 +3480,7 @@
}),
]),
'agents': dict({
'domain.test': dict({
'test.remote': dict({
'protected': False,
'size': 0,
}),
Expand Down Expand Up @@ -3520,7 +3520,7 @@
'id': 1,
'result': dict({
'agent_errors': dict({
'domain.test': 'The backup agent is unreachable.',
'test.remote': 'The backup agent is unreachable.',
}),
}),
'success': True,
Expand All @@ -3543,7 +3543,7 @@
}),
]),
'agents': dict({
'domain.test': dict({
'test.remote': dict({
'protected': False,
'size': 0,
}),
Expand Down Expand Up @@ -3606,7 +3606,7 @@
}),
]),
'agents': dict({
'domain.test': dict({
'test.remote': dict({
'protected': False,
'size': 0,
}),
Expand Down Expand Up @@ -3668,7 +3668,7 @@
}),
]),
'agents': dict({
'domain.test': dict({
'test.remote': dict({
'protected': False,
'size': 0,
}),
Expand Down Expand Up @@ -3708,7 +3708,7 @@
'id': 1,
'result': dict({
'agent_errors': dict({
'domain.test': 'Boom!',
'test.remote': 'Boom!',
}),
}),
'success': True,
Expand All @@ -3731,7 +3731,7 @@
}),
]),
'agents': dict({
'domain.test': dict({
'test.remote': dict({
'protected': False,
'size': 0,
}),
Expand Down Expand Up @@ -3771,7 +3771,7 @@
'id': 1,
'result': dict({
'agent_errors': dict({
'domain.test': 'Boom!',
'test.remote': 'Boom!',
}),
}),
'success': True,
Expand All @@ -3794,7 +3794,7 @@
}),
]),
'agents': dict({
'domain.test': dict({
'test.remote': dict({
'protected': False,
'size': 0,
}),
Expand Down Expand Up @@ -3992,7 +3992,7 @@
'id': 1,
'result': dict({
'agent_errors': dict({
'domain.test': 'The backup agent is unreachable.',
'test.remote': 'The backup agent is unreachable.',
}),
'backup': dict({
'addons': list([
Expand Down Expand Up @@ -4036,7 +4036,7 @@
'id': 1,
'result': dict({
'agent_errors': dict({
'domain.test': 'Oops',
'test.remote': 'Oops',
}),
'backup': dict({
'addons': list([
Expand Down Expand Up @@ -4080,7 +4080,7 @@
'id': 1,
'result': dict({
'agent_errors': dict({
'domain.test': 'Boom!',
'test.remote': 'Boom!',
}),
'backup': dict({
'addons': list([
Expand Down Expand Up @@ -4584,7 +4584,7 @@
'id': 1,
'result': dict({
'agent_errors': dict({
'domain.test': 'The backup agent is unreachable.',
'test.remote': 'The backup agent is unreachable.',
}),
'backups': list([
dict({
Expand Down Expand Up @@ -4636,7 +4636,7 @@
'id': 1,
'result': dict({
'agent_errors': dict({
'domain.test': 'Oops',
'test.remote': 'Oops',
}),
'backups': list([
dict({
Expand Down Expand Up @@ -4688,7 +4688,7 @@
'id': 1,
'result': dict({
'agent_errors': dict({
'domain.test': 'Boom!',
'test.remote': 'Boom!',
}),
'backups': list([
dict({
Expand Down
73 changes: 24 additions & 49 deletions tests/components/backup/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@
BackupNotFound,
Folder,
)
from homeassistant.components.backup.const import DATA_MANAGER, DOMAIN
from homeassistant.components.backup.const import DOMAIN
from homeassistant.core import HomeAssistant

from .common import (
TEST_BACKUP_ABC123,
aiter_from_iter,
mock_backup_agent,
setup_backup_integration,
)
from .common import TEST_BACKUP_ABC123, aiter_from_iter, setup_backup_integration

from tests.common import MockUser, get_fixture_path
from tests.typing import ClientSessionGenerator

PROTECTED_BACKUP = AgentBackup(
addons=[AddonInfo(name="Test", slug="test", version="1.0.0")],
backup_id="c0cb53bd",
database_included=True,
date="1970-01-01T00:00:00Z",
extra_metadata={},
folders=[Folder.MEDIA, Folder.SHARE],
homeassistant_included=True,
homeassistant_version="2024.12.0",
name="Test",
protected=True,
size=13,
)


async def test_downloading_local_backup(
hass: HomeAssistant,
Expand Down Expand Up @@ -115,32 +124,15 @@ async def test_downloading_remote_encrypted_backup(
) -> None:
"""Test downloading a local backup file."""
backup_path = get_fixture_path("test_backups/c0cb53bd.tar", DOMAIN)
await setup_backup_integration(hass)
mock_agent = mock_backup_agent(
"test",
[
AgentBackup(
addons=[AddonInfo(name="Test", slug="test", version="1.0.0")],
backup_id="c0cb53bd",
database_included=True,
date="1970-01-01T00:00:00Z",
extra_metadata={},
folders=[Folder.MEDIA, Folder.SHARE],
homeassistant_included=True,
homeassistant_version="2024.12.0",
name="Test",
protected=True,
size=13,
)
],
mock_agents = await setup_backup_integration(
hass, remote_agents=["test.test"], backups={"test.test": [PROTECTED_BACKUP]}
)
hass.data[DATA_MANAGER].backup_agents["domain.test"] = mock_agent

async def download_backup(backup_id: str, **kwargs: Any) -> AsyncIterator[bytes]:
return aiter_from_iter((backup_path.read_bytes(),))

mock_agent.async_download_backup.side_effect = download_backup
await _test_downloading_encrypted_backup(hass_client, "domain.test")
mock_agents["test.test"].async_download_backup.side_effect = download_backup
await _test_downloading_encrypted_backup(hass_client, "test.test")


@pytest.mark.parametrize(
Expand All @@ -157,31 +149,14 @@ async def test_downloading_remote_encrypted_backup_with_error(
status: int,
) -> None:
"""Test downloading a local backup file."""
await setup_backup_integration(hass)
mock_agent = mock_backup_agent(
"test",
[
AgentBackup(
addons=[AddonInfo(name="Test", slug="test", version="1.0.0")],
backup_id="abc123",
database_included=True,
date="1970-01-01T00:00:00Z",
extra_metadata={},
folders=[Folder.MEDIA, Folder.SHARE],
homeassistant_included=True,
homeassistant_version="2024.12.0",
name="Test",
protected=True,
size=13,
)
],
mock_agents = await setup_backup_integration(
hass, remote_agents=["test.test"], backups={"test.test": [PROTECTED_BACKUP]}
)
hass.data[DATA_MANAGER].backup_agents["domain.test"] = mock_agent

mock_agent.async_download_backup.side_effect = error
mock_agents["test.test"].async_download_backup.side_effect = error
client = await hass_client()
resp = await client.get(
"/api/backup/download/abc123?agent_id=domain.test&password=blah"
f"/api/backup/download/{PROTECTED_BACKUP.backup_id}?agent_id=test.test&password=blah"
)
assert resp.status == status

Expand Down
Loading