Skip to content

Commit

Permalink
Add helper functions for repair tests (home-assistant#125886)
Browse files Browse the repository at this point in the history
* Expose repairs constants and function for other components

* Reorder

* Use helper methods

* Adjust core_files

* Improve

* Update test_migrate.py
  • Loading branch information
epenet authored Sep 14, 2024
1 parent 3eed5de commit 6d212ea
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 360 deletions.
2 changes: 2 additions & 0 deletions .core_files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ tests: &tests
- tests/*.py
- tests/auth/**
- tests/backports/**
- tests/components/diagnostics/**
- tests/components/history/**
- tests/components/logbook/**
- tests/components/recorder/**
- tests/components/repairs/**
- tests/components/sensor/**
- tests/hassfest/**
- tests/helpers/**
Expand Down
24 changes: 7 additions & 17 deletions tests/components/doorbird/test_repairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

from __future__ import annotations

from http import HTTPStatus

from homeassistant.components.doorbird.const import DOMAIN
from homeassistant.components.repairs.issue_handler import (
async_process_repairs_platforms,
)
from homeassistant.components.repairs.websocket_api import (
RepairsFlowIndexView,
RepairsFlowResourceView,
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
Expand All @@ -20,6 +11,11 @@
from . import mock_not_found_exception
from .conftest import DoorbirdMockerType

from tests.components.repairs import (
async_process_repairs_platforms,
process_repair_fix_flow,
start_repair_fix_flow,
)
from tests.typing import ClientSessionGenerator


Expand All @@ -43,19 +39,13 @@ async def test_change_schedule_fails(
await async_process_repairs_platforms(hass)
client = await hass_client()

url = RepairsFlowIndexView.url
resp = await client.post(url, json={"handler": DOMAIN, "issue_id": issue_id})
assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await start_repair_fix_flow(client, DOMAIN, issue_id)

flow_id = data["flow_id"]
placeholders = data["description_placeholders"]
assert "404" in placeholders["error"]
assert data["step_id"] == "confirm"

url = RepairsFlowResourceView.url.format(flow_id=flow_id)
resp = await client.post(url)
assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await process_repair_fix_flow(client, flow_id)

assert data["type"] == "create_entry"
25 changes: 8 additions & 17 deletions tests/components/ecobee/test_repairs.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
"""Test repairs for Ecobee integration."""

from http import HTTPStatus
from unittest.mock import MagicMock

from homeassistant.components.ecobee import DOMAIN
from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN
from homeassistant.components.repairs.issue_handler import (
async_process_repairs_platforms,
)
from homeassistant.components.repairs.websocket_api import (
RepairsFlowIndexView,
RepairsFlowResourceView,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir

from .common import setup_platform

from tests.components.repairs import (
async_process_repairs_platforms,
process_repair_fix_flow,
start_repair_fix_flow,
)
from tests.typing import ClientSessionGenerator

THERMOSTAT_ID = 0
Expand Down Expand Up @@ -53,20 +50,14 @@ async def test_ecobee_notify_repair_flow(
)
assert len(issue_registry.issues) == 1

url = RepairsFlowIndexView.url
resp = await http_client.post(
url, json={"handler": "notify", "issue_id": f"migrate_notify_{DOMAIN}_{DOMAIN}"}
data = await start_repair_fix_flow(
http_client, "notify", f"migrate_notify_{DOMAIN}_{DOMAIN}"
)
assert resp.status == HTTPStatus.OK
data = await resp.json()

flow_id = data["flow_id"]
assert data["step_id"] == "confirm"

url = RepairsFlowResourceView.url.format(flow_id=flow_id)
resp = await http_client.post(url)
assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await process_repair_fix_flow(http_client, flow_id)
assert data["type"] == "create_entry"
# Test confirm step in repair flow
await hass.async_block_till_done()
Expand Down
57 changes: 15 additions & 42 deletions tests/components/homeassistant/test_repairs.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
"""Test the Homeassistant repairs module."""

from http import HTTPStatus

from homeassistant.components.repairs import DOMAIN as REPAIRS_DOMAIN
from homeassistant.components.repairs.issue_handler import (
async_process_repairs_platforms,
)
from homeassistant.components.repairs.websocket_api import (
RepairsFlowIndexView,
RepairsFlowResourceView,
)
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
from homeassistant.setup import async_setup_component

from tests.common import MockConfigEntry
from tests.components.repairs import (
async_process_repairs_platforms,
process_repair_fix_flow,
start_repair_fix_flow,
)
from tests.typing import ClientSessionGenerator, WebSocketGenerator


Expand Down Expand Up @@ -48,32 +44,20 @@ async def test_integration_not_found_confirm_step(
assert issue["issue_id"] == issue_id
assert issue["translation_placeholders"] == {"domain": "test1"}

url = RepairsFlowIndexView.url
resp = await http_client.post(
url, json={"handler": HOMEASSISTANT_DOMAIN, "issue_id": issue_id}
)
assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await start_repair_fix_flow(http_client, HOMEASSISTANT_DOMAIN, issue_id)

flow_id = data["flow_id"]
assert data["step_id"] == "init"
assert data["description_placeholders"] == {"domain": "test1"}

url = RepairsFlowResourceView.url.format(flow_id=flow_id)

# Show menu
resp = await http_client.post(url)

assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await process_repair_fix_flow(http_client, flow_id)

assert data["type"] == "menu"

# Apply fix
resp = await http_client.post(url, json={"next_step_id": "confirm"})

assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await process_repair_fix_flow(
http_client, flow_id, json={"next_step_id": "confirm"}
)

assert data["type"] == "create_entry"

Expand Down Expand Up @@ -118,32 +102,21 @@ async def test_integration_not_found_ignore_step(
assert issue["issue_id"] == issue_id
assert issue["translation_placeholders"] == {"domain": "test1"}

url = RepairsFlowIndexView.url
resp = await http_client.post(
url, json={"handler": HOMEASSISTANT_DOMAIN, "issue_id": issue_id}
)
assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await start_repair_fix_flow(http_client, HOMEASSISTANT_DOMAIN, issue_id)

flow_id = data["flow_id"]
assert data["step_id"] == "init"
assert data["description_placeholders"] == {"domain": "test1"}

url = RepairsFlowResourceView.url.format(flow_id=flow_id)

# Show menu
resp = await http_client.post(url)

assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await process_repair_fix_flow(http_client, flow_id)

assert data["type"] == "menu"

# Apply fix
resp = await http_client.post(url, json={"next_step_id": "ignore"})

assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await process_repair_fix_flow(
http_client, flow_id, json={"next_step_id": "ignore"}
)

assert data["type"] == "abort"
assert data["reason"] == "issue_ignored"
Expand Down
20 changes: 4 additions & 16 deletions tests/components/knx/test_repairs.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
"""Test repairs for KNX integration."""

from http import HTTPStatus

from homeassistant.components.knx.const import DOMAIN, KNX_ADDRESS
from homeassistant.components.knx.schema import NotifySchema
from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN
from homeassistant.components.repairs.websocket_api import (
RepairsFlowIndexView,
RepairsFlowResourceView,
)
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
import homeassistant.helpers.issue_registry as ir

from .conftest import KNXTestKit

from tests.components.repairs import process_repair_fix_flow, start_repair_fix_flow
from tests.typing import ClientSessionGenerator


Expand Down Expand Up @@ -59,21 +54,14 @@ async def test_knx_notify_service_issue(
)

# Test confirm step in repair flow
resp = await http_client.post(
RepairsFlowIndexView.url,
json={"handler": "notify", "issue_id": f"migrate_notify_{DOMAIN}_notify"},
data = await start_repair_fix_flow(
http_client, "notify", f"migrate_notify_{DOMAIN}_notify"
)
assert resp.status == HTTPStatus.OK
data = await resp.json()

flow_id = data["flow_id"]
assert data["step_id"] == "confirm"

resp = await http_client.post(
RepairsFlowResourceView.url.format(flow_id=flow_id),
)
assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await process_repair_fix_flow(http_client, flow_id)
assert data["type"] == "create_entry"

# Assert the issue is no longer present
Expand Down
25 changes: 7 additions & 18 deletions tests/components/notify/test_repairs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Test repairs for notify entity component."""

from http import HTTPStatus
from unittest.mock import AsyncMock

import pytest
Expand All @@ -9,18 +8,16 @@
DOMAIN as NOTIFY_DOMAIN,
migrate_notify_issue,
)
from homeassistant.components.repairs.issue_handler import (
async_process_repairs_platforms,
)
from homeassistant.components.repairs.websocket_api import (
RepairsFlowIndexView,
RepairsFlowResourceView,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component

from tests.common import MockConfigEntry, MockModule, mock_integration
from tests.components.repairs import (
async_process_repairs_platforms,
process_repair_fix_flow,
start_repair_fix_flow,
)
from tests.typing import ClientSessionGenerator

THERMOSTAT_ID = 0
Expand Down Expand Up @@ -66,20 +63,12 @@ async def test_notify_migration_repair_flow(
)
assert len(issue_registry.issues) == 1

url = RepairsFlowIndexView.url
resp = await http_client.post(
url, json={"handler": NOTIFY_DOMAIN, "issue_id": translation_key}
)
assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await start_repair_fix_flow(http_client, NOTIFY_DOMAIN, translation_key)

flow_id = data["flow_id"]
assert data["step_id"] == "confirm"

url = RepairsFlowResourceView.url.format(flow_id=flow_id)
resp = await http_client.post(url)
assert resp.status == HTTPStatus.OK
data = await resp.json()
data = await process_repair_fix_flow(http_client, flow_id)
assert data["type"] == "create_entry"
# Test confirm step in repair flow
await hass.async_block_till_done()
Expand Down
32 changes: 32 additions & 0 deletions tests/components/repairs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
"""Tests for the repairs integration."""

from http import HTTPStatus
from typing import Any

from aiohttp.test_utils import TestClient

from homeassistant.components.repairs.issue_handler import ( # noqa: F401
async_process_repairs_platforms,
)
from homeassistant.components.repairs.websocket_api import (
RepairsFlowIndexView,
RepairsFlowResourceView,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

Expand Down Expand Up @@ -27,3 +39,23 @@ async def get_repairs(
assert msg["result"]

return msg["result"]["issues"]


async def start_repair_fix_flow(
client: TestClient, handler: str, issue_id: int
) -> dict[str, Any]:
"""Start a flow from an issue."""
url = RepairsFlowIndexView.url
resp = await client.post(url, json={"handler": handler, "issue_id": issue_id})
assert resp.status == HTTPStatus.OK
return await resp.json()


async def process_repair_fix_flow(
client: TestClient, flow_id: int, json: dict[str, Any] | None = None
) -> dict[str, Any]:
"""Return the repairs list of issues."""
url = RepairsFlowResourceView.url.format(flow_id=flow_id)
resp = await client.post(url, json=json)
assert resp.status == HTTPStatus.OK
return await resp.json()
Loading

0 comments on commit 6d212ea

Please sign in to comment.