mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Translate read errors in google_photos (#191900)
This commit is contained in:
@@ -19,6 +19,7 @@ from homeassistant.core import (
|
||||
)
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_validation as cv, service
|
||||
from homeassistant.helpers.os_error import os_read_error
|
||||
|
||||
from .const import DOMAIN, UPLOAD_SCOPE
|
||||
from .coordinator import GooglePhotosConfigEntry
|
||||
@@ -62,19 +63,17 @@ def _read_file_contents(
|
||||
translation_placeholders={"filename": filename},
|
||||
)
|
||||
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:
|
||||
try:
|
||||
size = filename_path.stat().st_size
|
||||
except OSError as err:
|
||||
raise os_read_error(err, filename) from err
|
||||
if 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(size),
|
||||
"limit": str(CONTENT_SIZE_LIMIT),
|
||||
},
|
||||
)
|
||||
@@ -85,7 +84,11 @@ def _read_file_contents(
|
||||
translation_key="filename_is_not_image",
|
||||
translation_placeholders={"filename": filename},
|
||||
)
|
||||
results.append((mime_type, filename_path.read_bytes()))
|
||||
try:
|
||||
content = filename_path.read_bytes()
|
||||
except OSError as err:
|
||||
raise os_read_error(err, filename) from err
|
||||
results.append((mime_type, content))
|
||||
return results
|
||||
|
||||
|
||||
|
||||
@@ -45,9 +45,6 @@
|
||||
"file_too_large": {
|
||||
"message": "`{filename}` is too large ({size} > {limit})"
|
||||
},
|
||||
"filename_does_not_exist": {
|
||||
"message": "`{filename}` does not exist"
|
||||
},
|
||||
"filename_is_not_image": {
|
||||
"message": "`{filename}` is not an image"
|
||||
},
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
from collections.abc import Generator
|
||||
from dataclasses import dataclass
|
||||
import errno
|
||||
import os
|
||||
import re
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
@@ -35,7 +37,6 @@ class MockUploadFile:
|
||||
"""Dataclass used to configure the test with a fake file behavior."""
|
||||
|
||||
content: bytes = b"image bytes"
|
||||
exists: bool = True
|
||||
is_allowed_path: bool = True
|
||||
size: int | None = None
|
||||
|
||||
@@ -56,10 +57,6 @@ def mock_upload_file(
|
||||
"homeassistant.components.google_photos.services.Path.read_bytes",
|
||||
return_value=upload_file.content,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.google_photos.services.Path.exists",
|
||||
return_value=upload_file.exists,
|
||||
),
|
||||
patch.object(
|
||||
hass.config, "is_allowed_path", return_value=upload_file.is_allowed_path
|
||||
),
|
||||
@@ -208,14 +205,32 @@ async def test_path_is_not_allowed(
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("method", ["stat", "read_bytes"])
|
||||
@pytest.mark.parametrize(
|
||||
("error_no", "translation_key"),
|
||||
[
|
||||
pytest.param(errno.ENOENT, "os_read_not_found", id="not_found"),
|
||||
pytest.param(errno.EACCES, "os_read_permission_denied", id="permission"),
|
||||
pytest.param(errno.EISDIR, "os_read_is_directory", id="is_directory"),
|
||||
pytest.param(errno.EIO, "os_read_error", id="fallback"),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("setup_integration")
|
||||
@pytest.mark.parametrize("upload_file", [MockUploadFile(exists=False)])
|
||||
async def test_filename_does_not_exist(
|
||||
async def test_file_read_error(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
method: str,
|
||||
error_no: int,
|
||||
translation_key: str,
|
||||
) -> None:
|
||||
"""Test upload service call with a filename path that does not exist."""
|
||||
with pytest.raises(HomeAssistantError, match="does not exist"):
|
||||
"""Test that a file read error raises a translated error."""
|
||||
with (
|
||||
patch(
|
||||
f"homeassistant.components.google_photos.services.Path.{method}",
|
||||
side_effect=OSError(error_no, os.strerror(error_no)),
|
||||
),
|
||||
pytest.raises(HomeAssistantError) as exc_info,
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
GooglePhotosService.UPLOAD,
|
||||
@@ -228,6 +243,10 @@ async def test_filename_does_not_exist(
|
||||
return_response=True,
|
||||
)
|
||||
|
||||
assert exc_info.value.translation_domain == "homeassistant"
|
||||
assert exc_info.value.translation_key == translation_key
|
||||
assert exc_info.value.translation_placeholders == {"path": TEST_FILENAME}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("setup_integration")
|
||||
async def test_upload_service_upload_content_failure(
|
||||
|
||||
Reference in New Issue
Block a user