Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy committed May 28, 2022
1 parent 4b78d47 commit f926c2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
11 changes: 7 additions & 4 deletions custom_components/frigate/media_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,14 @@ async def async_browse_media(
default_frigate_instance_id=self._get_default_frigate_instance_id(),
)

if (
identifier is not None
and self._is_allowed_as_media_source(identifier.frigate_instance_id)
and isinstance(identifier, EventSearchIdentifier)
if identifier is not None and not self._is_allowed_as_media_source(
identifier.frigate_instance_id
):
raise MediaSourceError(
"Forbidden media source identifier: %s" % item.identifier
)

if isinstance(identifier, EventSearchIdentifier):
if identifier.frigate_media_type == FrigateMediaType.CLIPS:
media_kwargs = {"has_clip": True}
else:
Expand Down
35 changes: 29 additions & 6 deletions tests/test_media_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
async_get_media_source,
)
from homeassistant.components import media_source
from homeassistant.components.media_player.errors import BrowseError
from homeassistant.components.media_source import const
from homeassistant.components.media_source.error import MediaSourceError, Unresolvable
from homeassistant.components.media_source.models import PlayMedia
Expand Down Expand Up @@ -89,18 +88,27 @@ def load_json(filename: str) -> Any:
async def test_async_disabled_browse_media(hass: HomeAssistant) -> None:
"""Test disabled browse media."""

# Create the default test Frigate instance.
create_mock_frigate_config_entry(
config_entry = create_mock_frigate_config_entry(
hass,
options={CONF_MEDIA_BROWSER_ENABLE: False},
)
await setup_mock_frigate_config_entry(hass, config_entry)

# Test on an empty identifier (won't raise an exception, but won't return
# any children).
result = await media_source.async_browse_media(
hass,
f"{const.URI_SCHEME}{DOMAIN}",
)
assert not result.children

with pytest.raises(BrowseError):
await async_get_media_source(hass)
# Test on an forbidden identifier. Will raise.
with pytest.raises(MediaSourceError) as exc:
await media_source.async_browse_media(
hass,
f"{const.URI_SCHEME}{DOMAIN}",
f"{const.URI_SCHEME}{DOMAIN}/{TEST_FRIGATE_INSTANCE_ID}/event/clips/camera/CLIP-FOO",
)
assert "Forbidden media source identifier" in str(exc.value)


async def test_async_browse_media_root(hass: HomeAssistant) -> None:
Expand Down Expand Up @@ -1195,6 +1203,21 @@ async def test_get_client_non_existent(hass: HomeAssistant) -> None:
f"{const.URI_SCHEME}{DOMAIN}/NOT_A_REAL_CONFIG_ENTRY_ID/event-search/clips",
)

# For code coverage and completeness check that _get_client(...) will raise
# on an invalid instance_id since it is used inline in many places. There's
# no public way to trigger this since there'll always be an earlier call to
# _is_allowed_as_media_source will always have caught this issue upstream.
source = await async_get_media_source(hass)
with pytest.raises(MediaSourceError):
# pylint: disable=protected-access
source._get_client(
Identifier.from_str(
"NOT_A_REAL_CONFIG_ENTRY_ID/event-search"
"/clips/.this_month.2021-06-04.front_door.person"
"/1622764800/1622851200/front_door/person/zone"
)
)


async def test_backwards_compatability_identifier_without_frigate_instance_id(
hass: HomeAssistant,
Expand Down

0 comments on commit f926c2a

Please sign in to comment.