Skip to content
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
25 changes: 16 additions & 9 deletions homeassistant/components/onedrive/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,35 @@ def _read_file_contents(
hass: HomeAssistant, filenames: list[str]
) -> list[tuple[str, bytes]]:
"""Return the mime types and file contents for each file."""
results = []
missing: list[str] = []
for filename in filenames:
if not hass.config.is_allowed_path(filename):
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="no_access_to_path",
translation_placeholders={"filename": filename},
)
if not Path(filename).exists():
missing.append(filename)
if missing:
raise HomeAssistantError(
translation_domain=DOMAIN,
Comment thread
leodrivera marked this conversation as resolved.
translation_key="filenames_do_not_exist",
translation_placeholders={
"filenames": ", ".join(f"`{f}`" for f in missing)
},
)
results = []
for filename in filenames:
filename_path = Path(filename)
if not filename_path.exists():
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="filename_does_not_exist",
translation_placeholders={"filename": filename},
)
if filename_path.stat().st_size > CONTENT_SIZE_LIMIT:
file_size = filename_path.stat().st_size
if file_size > CONTENT_SIZE_LIMIT:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="file_too_large",
translation_placeholders={
"filename": filename,
"size": str(filename_path.stat().st_size),
"size": str(file_size),
"limit": str(CONTENT_SIZE_LIMIT),
},
)
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/onedrive/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
"file_too_large": {
"message": "`{filename}` is too large ({size} > {limit})"
},
"filename_does_not_exist": {
"message": "`{filename}` does not exist"
"filenames_do_not_exist": {
"message": "The following files do not exist: {filenames}"
},
"no_access_to_path": {
"message": "Cannot read {filename}, no access to path; `allowlist_external_dirs` may need to be adjusted in `configuration.yaml`"
Expand Down
29 changes: 28 additions & 1 deletion tests/components/onedrive/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async def test_filename_does_not_exist(
) -> None:
"""Test upload service call with a filename path that does not exist."""
await setup_integration(hass, mock_config_entry)
with pytest.raises(HomeAssistantError, match="does not exist"):
with pytest.raises(HomeAssistantError) as exc_info:
await hass.services.async_call(
DOMAIN,
UPLOAD_SERVICE,
Expand All @@ -206,6 +206,33 @@ async def test_filename_does_not_exist(
blocking=True,
return_response=True,
)
assert exc_info.value.translation_key == "filenames_do_not_exist"
assert TEST_FILENAME in exc_info.value.translation_placeholders["filenames"]


@pytest.mark.parametrize("upload_file", [MockUploadFile(exists=False)])
async def test_multiple_filenames_do_not_exist(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test upload service reports all missing files, not just the first one."""
await setup_integration(hass, mock_config_entry)
second_filename = "other_snapshot.jpg"
with pytest.raises(HomeAssistantError) as exc_info:
await hass.services.async_call(
DOMAIN,
UPLOAD_SERVICE,
{
CONF_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
CONF_FILENAME: [TEST_FILENAME, second_filename],
CONF_DESTINATION_FOLDER: DESINATION_FOLDER,
},
blocking=True,
return_response=True,
)
assert exc_info.value.translation_key == "filenames_do_not_exist"
assert TEST_FILENAME in exc_info.value.translation_placeholders["filenames"]
assert second_filename in exc_info.value.translation_placeholders["filenames"]


async def test_upload_service_fails_upload(
Expand Down
Loading