Increase file upload limit to 100 MB (#77117)

* Increase file upload limit to 100 MB

* Remove comment

* Add test and fix chunk processing

* Add test for wrong field

* Add review suggestions

* Use nonlocal and remove unneeded executor task

* Use Janus to process chunk uploading

* Address review comments

* Address review comments #2

* Improve tests

* Fix discovery test

* Fix tests
This commit is contained in:
Marvin Wichmann
2022-11-30 02:46:34 +01:00
committed by GitHub
parent a3ec9529ec
commit 1908feab79
8 changed files with 107 additions and 16 deletions

View File

@@ -64,3 +64,49 @@ async def test_removed_on_stop(hass: HomeAssistant, hass_client, uploaded_file_d
# Test it's removed
assert not uploaded_file_dir.exists()
async def test_upload_large_file(hass: HomeAssistant, hass_client, large_file_io):
"""Test uploading large file."""
assert await async_setup_component(hass, "file_upload", {})
client = await hass_client()
with patch(
# Patch temp dir name to avoid tests fail running in parallel
"homeassistant.components.file_upload.TEMP_DIR_NAME",
file_upload.TEMP_DIR_NAME + f"-{getrandbits(10):03x}",
), patch(
# Patch one megabyte to 8 bytes to prevent having to use big files in tests
"homeassistant.components.file_upload.ONE_MEGABYTE",
8,
):
res = await client.post("/api/file_upload", data={"file": large_file_io})
assert res.status == 200
response = await res.json()
file_dir = hass.data[file_upload.DOMAIN].file_dir(response["file_id"])
assert file_dir.is_dir()
large_file_io.seek(0)
with file_upload.process_uploaded_file(hass, file_dir.name) as file_path:
assert file_path.is_file()
assert file_path.parent == file_dir
assert file_path.read_bytes() == large_file_io.read().encode("utf-8")
async def test_upload_with_wrong_key_fails(
hass: HomeAssistant, hass_client, large_file_io
):
"""Test uploading fails."""
assert await async_setup_component(hass, "file_upload", {})
client = await hass_client()
with patch(
# Patch temp dir name to avoid tests fail running in parallel
"homeassistant.components.file_upload.TEMP_DIR_NAME",
file_upload.TEMP_DIR_NAME + f"-{getrandbits(10):03x}",
):
res = await client.post("/api/file_upload", data={"wrong_key": large_file_io})
assert res.status == 400