Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NickM-27 committed Mar 19, 2024
1 parent daab247 commit 9cf2f03
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
10 changes: 10 additions & 0 deletions tests/components/image/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ async def async_image(self) -> bytes | None:
return b"Test"


class MockImageNoDataEntity(image.ImageEntity):
"""Mock image entity."""

_attr_name = "Test"

async def async_image(self) -> bytes | None:
"""Return bytes of image."""
return None


class MockImageSyncEntity(image.ImageEntity):
"""Mock image entity."""

Expand Down
71 changes: 66 additions & 5 deletions tests/components/image/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .conftest import (
MockImageEntity,
MockImageEntityInvalidContentType,
MockImageNoDataEntity,
MockImageNoStateEntity,
MockImagePlatform,
MockImageSyncEntity,
Expand Down Expand Up @@ -362,6 +363,12 @@ async def _wrap_async_get_still_stream(*args, **kwargs):
async def test_snapshot_service(hass: HomeAssistant) -> None:
"""Test snapshot service."""
mopen = mock_open()
mock_integration(hass, MockModule(domain="test"))
mock_platform(hass, "test.image", MockImagePlatform([MockImageSyncEntity(hass)]))
assert await async_setup_component(
hass, image.DOMAIN, {"image": {"platform": "test"}}
)
await hass.async_block_till_done()

with patch("homeassistant.components.image.open", mopen, create=True), patch(
"homeassistant.components.image.os.makedirs",
Expand All @@ -370,7 +377,7 @@ async def test_snapshot_service(hass: HomeAssistant) -> None:
image.DOMAIN,
image.SERVICE_SNAPSHOT,
{
ATTR_ENTITY_ID: "image.demo_image",
ATTR_ENTITY_ID: "image.test",
image.ATTR_FILENAME: "/test/snapshot.jpg",
},
blocking=True,
Expand All @@ -382,18 +389,72 @@ async def test_snapshot_service(hass: HomeAssistant) -> None:
assert mock_write.mock_calls[0][1][0] == b"Test"


async def test_snapshot_service_not_allowed_path(hass: HomeAssistant) -> None:
"""Test snapshot service with a not allowed path."""
async def test_snapshot_service_no_image(hass: HomeAssistant) -> None:
"""Test snapshot service with no image."""
mopen = mock_open()
mock_integration(hass, MockModule(domain="test"))
mock_platform(hass, "test.image", MockImagePlatform([MockImageNoDataEntity(hass)]))
assert await async_setup_component(
hass, image.DOMAIN, {"image": {"platform": "test"}}
)
await hass.async_block_till_done()

with patch("homeassistant.components.image.open", mopen, create=True), patch(
"homeassistant.components.image.os.makedirs",
), pytest.raises(HomeAssistantError, match="/test/snapshot.jpg"):
), patch.object(hass.config, "is_allowed_path", return_value=True):
await hass.services.async_call(
image.DOMAIN,
image.SERVICE_SNAPSHOT,
{
ATTR_ENTITY_ID: "image.test",
image.ATTR_FILENAME: "/test/snapshot.jpg",
},
blocking=True,
)

mock_write = mopen().write

assert len(mock_write.mock_calls) == 0


async def test_snapshot_service_not_allowed_path(hass: HomeAssistant) -> None:
"""Test snapshot service with a not allowed path."""
mock_integration(hass, MockModule(domain="test"))
mock_platform(hass, "test.image", MockImagePlatform([MockURLImageEntity(hass)]))
assert await async_setup_component(
hass, image.DOMAIN, {"image": {"platform": "test"}}
)
await hass.async_block_till_done()

with pytest.raises(HomeAssistantError, match="/test/snapshot.jpg"):
await hass.services.async_call(
image.DOMAIN,
image.SERVICE_SNAPSHOT,
{
ATTR_ENTITY_ID: "image.test",
image.ATTR_FILENAME: "/test/snapshot.jpg",
},
blocking=True,
)


async def test_snapshot_service_os_error(hass: HomeAssistant) -> None:
"""Test snapshot service with os error."""
mock_integration(hass, MockModule(domain="test"))
mock_platform(hass, "test.image", MockImagePlatform([MockImageSyncEntity(hass)]))
assert await async_setup_component(
hass, image.DOMAIN, {"image": {"platform": "test"}}
)
await hass.async_block_till_done()

with patch.object(hass.config, "is_allowed_path", return_value=True), patch(
"os.makedirs", side_effect=OSError
):
await hass.services.async_call(
image.DOMAIN,
image.SERVICE_SNAPSHOT,
{
ATTR_ENTITY_ID: "image.demo_image",
ATTR_ENTITY_ID: "image.test",
image.ATTR_FILENAME: "/test/snapshot.jpg",
},
blocking=True,
Expand Down

0 comments on commit 9cf2f03

Please sign in to comment.