Add type hints to integration tests (part 7) (#87980)

This commit is contained in:
epenet 2023-02-13 09:53:09 +01:00 committed by GitHub
parent b9beed4624
commit b68f502769
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 755 additions and 395 deletions

View file

@ -22,7 +22,8 @@ from homeassistant.loader import async_get_integration
from homeassistant.setup import async_setup_component
from homeassistant.util import dt
from tests.common import async_capture_events, async_fire_time_changed
from tests.common import MockUser, async_capture_events, async_fire_time_changed
from tests.typing import WebSocketGenerator
MOCK_THEMES = {
"happy": {"primary-color": "red", "app-header-background-color": "blue"},
@ -130,7 +131,7 @@ def mock_onboarded():
yield
async def test_frontend_and_static(mock_http_client, mock_onboarded):
async def test_frontend_and_static(mock_http_client, mock_onboarded) -> None:
"""Test if we can get the frontend."""
resp = await mock_http_client.get("")
assert resp.status == 200
@ -147,26 +148,26 @@ async def test_frontend_and_static(mock_http_client, mock_onboarded):
assert "public" in resp.headers.get("cache-control")
async def test_dont_cache_service_worker(mock_http_client):
async def test_dont_cache_service_worker(mock_http_client) -> None:
"""Test that we don't cache the service worker."""
resp = await mock_http_client.get("/service_worker.js")
assert resp.status == 200
assert "cache-control" not in resp.headers
async def test_404(mock_http_client):
async def test_404(mock_http_client) -> None:
"""Test for HTTP 404 error."""
resp = await mock_http_client.get("/not-existing")
assert resp.status == HTTPStatus.NOT_FOUND
async def test_we_cannot_POST_to_root(mock_http_client):
async def test_we_cannot_POST_to_root(mock_http_client) -> None:
"""Test that POST is not allow to root."""
resp = await mock_http_client.post("/")
assert resp.status == 405
async def test_themes_api(hass, themes_ws_client):
async def test_themes_api(hass: HomeAssistant, themes_ws_client) -> None:
"""Test that /api/themes returns correct data."""
await themes_ws_client.send_json({"id": 5, "type": "frontend/get_themes"})
msg = await themes_ws_client.receive_json()
@ -186,7 +187,12 @@ async def test_themes_api(hass, themes_ws_client):
}
async def test_themes_persist(hass, hass_storage, hass_ws_client, ignore_frontend_deps):
async def test_themes_persist(
hass: HomeAssistant,
hass_storage,
hass_ws_client: WebSocketGenerator,
ignore_frontend_deps,
) -> None:
"""Test that theme settings are restores after restart."""
hass_storage[THEMES_STORAGE_KEY] = {
"key": THEMES_STORAGE_KEY,
@ -207,7 +213,9 @@ async def test_themes_persist(hass, hass_storage, hass_ws_client, ignore_fronten
assert msg["result"]["default_dark_theme"] == "dark"
async def test_themes_save_storage(hass, hass_storage, frontend_themes):
async def test_themes_save_storage(
hass: HomeAssistant, hass_storage, frontend_themes
) -> None:
"""Test that theme settings are restores after restart."""
await hass.services.async_call(
@ -229,7 +237,7 @@ async def test_themes_save_storage(hass, hass_storage, frontend_themes):
}
async def test_themes_set_theme(hass, themes_ws_client):
async def test_themes_set_theme(hass: HomeAssistant, themes_ws_client) -> None:
"""Test frontend.set_theme service."""
await hass.services.async_call(
DOMAIN, "set_theme", {"name": "happy"}, blocking=True
@ -261,7 +269,9 @@ async def test_themes_set_theme(hass, themes_ws_client):
assert msg["result"]["default_theme"] == "default"
async def test_themes_set_theme_wrong_name(hass, themes_ws_client):
async def test_themes_set_theme_wrong_name(
hass: HomeAssistant, themes_ws_client
) -> None:
"""Test frontend.set_theme service called with wrong name."""
await hass.services.async_call(
@ -275,7 +285,7 @@ async def test_themes_set_theme_wrong_name(hass, themes_ws_client):
assert msg["result"]["default_theme"] == "default"
async def test_themes_set_dark_theme(hass, themes_ws_client):
async def test_themes_set_dark_theme(hass: HomeAssistant, themes_ws_client) -> None:
"""Test frontend.set_theme service called with dark mode."""
await hass.services.async_call(
@ -315,7 +325,9 @@ async def test_themes_set_dark_theme(hass, themes_ws_client):
assert msg["result"]["default_dark_theme"] == "light_and_dark"
async def test_themes_set_dark_theme_wrong_name(hass, frontend, themes_ws_client):
async def test_themes_set_dark_theme_wrong_name(
hass: HomeAssistant, frontend, themes_ws_client
) -> None:
"""Test frontend.set_theme service called with mode dark and wrong name."""
await hass.services.async_call(
DOMAIN, "set_theme", {"name": "wrong", "mode": "dark"}, blocking=True
@ -328,7 +340,9 @@ async def test_themes_set_dark_theme_wrong_name(hass, frontend, themes_ws_client
assert msg["result"]["default_dark_theme"] is None
async def test_themes_reload_themes(hass, frontend, themes_ws_client):
async def test_themes_reload_themes(
hass: HomeAssistant, frontend, themes_ws_client
) -> None:
"""Test frontend.reload_themes service."""
with patch(
@ -348,7 +362,7 @@ async def test_themes_reload_themes(hass, frontend, themes_ws_client):
assert msg["result"]["default_theme"] == "default"
async def test_missing_themes(hass, ws_client):
async def test_missing_themes(hass: HomeAssistant, ws_client) -> None:
"""Test that themes API works when themes are not defined."""
await ws_client.send_json({"id": 5, "type": "frontend/get_themes"})
@ -361,7 +375,9 @@ async def test_missing_themes(hass, ws_client):
assert msg["result"]["themes"] == {}
async def test_get_panels(hass, hass_ws_client, mock_http_client):
async def test_get_panels(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, mock_http_client
) -> None:
"""Test get_panels command."""
events = async_capture_events(hass, EVENT_PANELS_UPDATED)
@ -399,7 +415,9 @@ async def test_get_panels(hass, hass_ws_client, mock_http_client):
assert len(events) == 2
async def test_get_panels_non_admin(hass, ws_client, hass_admin_user):
async def test_get_panels_non_admin(
hass: HomeAssistant, ws_client, hass_admin_user: MockUser
) -> None:
"""Test get_panels command."""
hass_admin_user.groups = []
@ -421,7 +439,7 @@ async def test_get_panels_non_admin(hass, ws_client, hass_admin_user):
assert "map" not in msg["result"]
async def test_get_translations(hass, ws_client):
async def test_get_translations(hass: HomeAssistant, ws_client) -> None:
"""Test get_translations command."""
with patch(
"homeassistant.components.frontend.async_get_translations",
@ -445,7 +463,9 @@ async def test_get_translations(hass, ws_client):
assert msg["result"] == {"resources": {"lang": "nl"}}
async def test_get_translations_for_integrations(hass, ws_client):
async def test_get_translations_for_integrations(
hass: HomeAssistant, ws_client
) -> None:
"""Test get_translations for integrations command."""
with patch(
"homeassistant.components.frontend.async_get_translations",
@ -471,7 +491,9 @@ async def test_get_translations_for_integrations(hass, ws_client):
assert set(msg["result"]["resources"]["integration"]) == {"frontend", "http"}
async def test_get_translations_for_single_integration(hass, ws_client):
async def test_get_translations_for_single_integration(
hass: HomeAssistant, ws_client
) -> None:
"""Test get_translations for integration command."""
with patch(
"homeassistant.components.frontend.async_get_translations",
@ -509,7 +531,7 @@ async def test_onboarding_load(hass: HomeAssistant) -> None:
assert "onboarding" in frontend.dependencies
async def test_auth_authorize(mock_http_client):
async def test_auth_authorize(mock_http_client) -> None:
"""Test the authorize endpoint works."""
resp = await mock_http_client.get(
"/auth/authorize?response_type=code&client_id=https://localhost/&"
@ -532,7 +554,7 @@ async def test_auth_authorize(mock_http_client):
assert "public" in resp.headers.get("cache-control")
async def test_get_version(hass, ws_client):
async def test_get_version(hass: HomeAssistant, ws_client) -> None:
"""Test get_version command."""
frontend = await async_get_integration(hass, "frontend")
cur_version = next(
@ -550,7 +572,7 @@ async def test_get_version(hass, ws_client):
assert msg["result"] == {"version": cur_version}
async def test_static_paths(hass, mock_http_client):
async def test_static_paths(hass: HomeAssistant, mock_http_client) -> None:
"""Test static paths."""
resp = await mock_http_client.get(
"/.well-known/change-password", allow_redirects=False
@ -559,7 +581,9 @@ async def test_static_paths(hass, mock_http_client):
assert resp.headers["location"] == "/profile"
async def test_manifest_json(hass, frontend_themes, mock_http_client):
async def test_manifest_json(
hass: HomeAssistant, frontend_themes, mock_http_client
) -> None:
"""Test for fetching manifest.json."""
resp = await mock_http_client.get("/manifest.json")
assert resp.status == HTTPStatus.OK
@ -581,7 +605,7 @@ async def test_manifest_json(hass, frontend_themes, mock_http_client):
assert json["theme_color"] != DEFAULT_THEME_COLOR
async def test_static_path_cache(hass, mock_http_client):
async def test_static_path_cache(hass: HomeAssistant, mock_http_client) -> None:
"""Test static paths cache."""
resp = await mock_http_client.get("/lovelace/default_view", allow_redirects=False)
assert resp.status == 404