1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add upload capability to the backup integration (#128546)

* Add upload capability to the backup integration

* Limit context switch

* rename

* coverage for http

* Test receiving a backup file

* Update test_manager.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Joakim Sørensen
2024-11-12 15:27:53 +01:00
committed by GitHub
parent cb9cc0f801
commit ac0c75a598
4 changed files with 195 additions and 7 deletions

View File

@@ -1,8 +1,11 @@
"""Tests for the Backup integration."""
import asyncio
from io import StringIO
from unittest.mock import patch
from aiohttp import web
import pytest
from homeassistant.core import HomeAssistant
@@ -49,12 +52,12 @@ async def test_downloading_backup_not_found(
assert resp.status == 404
async def test_non_admin(
async def test_downloading_as_non_admin(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_admin_user: MockUser,
) -> None:
"""Test downloading a backup file that does not exist."""
"""Test downloading a backup file when you are not an admin."""
hass_admin_user.groups = []
await setup_backup_integration(hass)
@@ -62,3 +65,53 @@ async def test_non_admin(
resp = await client.get("/api/backup/download/abc123")
assert resp.status == 401
async def test_uploading_a_backup_file(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
) -> None:
"""Test uploading a backup file."""
await setup_backup_integration(hass)
client = await hass_client()
with patch(
"homeassistant.components.backup.manager.BackupManager.async_receive_backup",
) as async_receive_backup_mock:
resp = await client.post(
"/api/backup/upload",
data={"file": StringIO("test")},
)
assert resp.status == 201
assert async_receive_backup_mock.called
@pytest.mark.parametrize(
("error", "message"),
[
(OSError("Boom!"), "Can't write backup file Boom!"),
(asyncio.CancelledError("Boom!"), ""),
],
)
async def test_error_handling_uploading_a_backup_file(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
error: Exception,
message: str,
) -> None:
"""Test error handling when uploading a backup file."""
await setup_backup_integration(hass)
client = await hass_client()
with patch(
"homeassistant.components.backup.manager.BackupManager.async_receive_backup",
side_effect=error,
):
resp = await client.post(
"/api/backup/upload",
data={"file": StringIO("test")},
)
assert resp.status == 500
assert await resp.text() == message