Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
davet2001 committed Dec 24, 2024
1 parent 3d59860 commit a6c0bb5
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion tests/components/generic/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,35 @@ async def test_options_only_stream(
assert result3["data"][CONF_CONTENT_TYPE] == "image/jpeg"


async def test_options_still_and_stream_not_provided(
hass: HomeAssistant,
) -> None:
"""Test we show a suitable error if neither still or stream URL are provided."""
data = TESTDATA.copy()

mock_entry = MockConfigEntry(
title="Test Camera",
domain=DOMAIN,
data={},
options=data,
)
mock_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_entry.entry_id)

result = await hass.config_entries.options.async_init(mock_entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"

data.pop(CONF_STILL_IMAGE_URL)
data.pop(CONF_STREAM_SOURCE)
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=data,
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "no_still_image_or_stream_url"}


@respx.mock
@pytest.mark.usefixtures("fakeimg_png")
async def test_form_options_stream_worker_error(
Expand Down Expand Up @@ -1012,10 +1041,15 @@ async def test_migrate_existing_ids(
@respx.mock
@pytest.mark.usefixtures("fakeimg_png")
async def test_use_wallclock_as_timestamps_option(
hass: HomeAssistant, mock_create_stream: _patch[MagicMock]
hass: HomeAssistant,
mock_create_stream: _patch[MagicMock],
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
fakeimgbytes_png: bytes,
) -> None:
"""Test the use_wallclock_as_timestamps option flow."""

respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_png)
mock_entry = MockConfigEntry(
title="Test Camera",
domain=DOMAIN,
Expand All @@ -1041,6 +1075,25 @@ async def test_use_wallclock_as_timestamps_option(
user_input={CONF_USE_WALLCLOCK_AS_TIMESTAMPS: True, **TESTDATA},
)
assert result2["type"] is FlowResultType.FORM

ws_client = await hass_ws_client()
flow_id = result2["flow_id"]
await ws_client.send_json_auto_id(
{
"type": "generic_camera/start_preview",
"flow_id": flow_id,
"flow_type": "options_flow",
},
)
json = await ws_client.receive_json()

client = await hass_client()
still_preview_url = json["event"]["attributes"]["still_url"]
# Check the preview image works.
resp = await client.get(still_preview_url)
assert resp.status == HTTPStatus.OK
assert await resp.read() == fakeimgbytes_png

# Test what happens if user rejects the preview
result3 = await hass.config_entries.options.async_configure(
result2["flow_id"], user_input={CONF_CONFIRMED_OK: False}
Expand Down

0 comments on commit a6c0bb5

Please sign in to comment.