Improve Supervisor API handling (#89379)

This commit is contained in:
Franck Nijhof 2023-03-08 15:21:11 +01:00
parent 7f8a9697f0
commit b65180d20a
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
10 changed files with 620 additions and 212 deletions

View file

@ -21,7 +21,7 @@ from tests.test_util.aiohttp import AiohttpClientMocker
],
)
async def test_ingress_request_get(
hassio_client, build_type, aioclient_mock: AiohttpClientMocker
hassio_noauth_client, build_type, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test no auth needed for ."""
aioclient_mock.get(
@ -29,7 +29,7 @@ async def test_ingress_request_get(
text="test",
)
resp = await hassio_client.get(
resp = await hassio_noauth_client.get(
f"/api/hassio_ingress/{build_type[0]}/{build_type[1]}",
headers={"X-Test-Header": "beer"},
)
@ -41,7 +41,8 @@ async def test_ingress_request_get(
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456"
assert X_AUTH_TOKEN not in aioclient_mock.mock_calls[-1][3]
assert aioclient_mock.mock_calls[-1][3]["X-Hass-Source"] == "core.ingress"
assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}"
@ -63,7 +64,7 @@ async def test_ingress_request_get(
],
)
async def test_ingress_request_post(
hassio_client, build_type, aioclient_mock: AiohttpClientMocker
hassio_noauth_client, build_type, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test no auth needed for ."""
aioclient_mock.post(
@ -71,7 +72,7 @@ async def test_ingress_request_post(
text="test",
)
resp = await hassio_client.post(
resp = await hassio_noauth_client.post(
f"/api/hassio_ingress/{build_type[0]}/{build_type[1]}",
headers={"X-Test-Header": "beer"},
)
@ -83,7 +84,8 @@ async def test_ingress_request_post(
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456"
assert X_AUTH_TOKEN not in aioclient_mock.mock_calls[-1][3]
assert aioclient_mock.mock_calls[-1][3]["X-Hass-Source"] == "core.ingress"
assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}"
@ -105,7 +107,7 @@ async def test_ingress_request_post(
],
)
async def test_ingress_request_put(
hassio_client, build_type, aioclient_mock: AiohttpClientMocker
hassio_noauth_client, build_type, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test no auth needed for ."""
aioclient_mock.put(
@ -113,7 +115,7 @@ async def test_ingress_request_put(
text="test",
)
resp = await hassio_client.put(
resp = await hassio_noauth_client.put(
f"/api/hassio_ingress/{build_type[0]}/{build_type[1]}",
headers={"X-Test-Header": "beer"},
)
@ -125,7 +127,8 @@ async def test_ingress_request_put(
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456"
assert X_AUTH_TOKEN not in aioclient_mock.mock_calls[-1][3]
assert aioclient_mock.mock_calls[-1][3]["X-Hass-Source"] == "core.ingress"
assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}"
@ -147,7 +150,7 @@ async def test_ingress_request_put(
],
)
async def test_ingress_request_delete(
hassio_client, build_type, aioclient_mock: AiohttpClientMocker
hassio_noauth_client, build_type, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test no auth needed for ."""
aioclient_mock.delete(
@ -155,7 +158,7 @@ async def test_ingress_request_delete(
text="test",
)
resp = await hassio_client.delete(
resp = await hassio_noauth_client.delete(
f"/api/hassio_ingress/{build_type[0]}/{build_type[1]}",
headers={"X-Test-Header": "beer"},
)
@ -167,7 +170,8 @@ async def test_ingress_request_delete(
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456"
assert X_AUTH_TOKEN not in aioclient_mock.mock_calls[-1][3]
assert aioclient_mock.mock_calls[-1][3]["X-Hass-Source"] == "core.ingress"
assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}"
@ -189,7 +193,7 @@ async def test_ingress_request_delete(
],
)
async def test_ingress_request_patch(
hassio_client, build_type, aioclient_mock: AiohttpClientMocker
hassio_noauth_client, build_type, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test no auth needed for ."""
aioclient_mock.patch(
@ -197,7 +201,7 @@ async def test_ingress_request_patch(
text="test",
)
resp = await hassio_client.patch(
resp = await hassio_noauth_client.patch(
f"/api/hassio_ingress/{build_type[0]}/{build_type[1]}",
headers={"X-Test-Header": "beer"},
)
@ -209,7 +213,8 @@ async def test_ingress_request_patch(
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456"
assert X_AUTH_TOKEN not in aioclient_mock.mock_calls[-1][3]
assert aioclient_mock.mock_calls[-1][3]["X-Hass-Source"] == "core.ingress"
assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}"
@ -231,7 +236,7 @@ async def test_ingress_request_patch(
],
)
async def test_ingress_request_options(
hassio_client, build_type, aioclient_mock: AiohttpClientMocker
hassio_noauth_client, build_type, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test no auth needed for ."""
aioclient_mock.options(
@ -239,7 +244,7 @@ async def test_ingress_request_options(
text="test",
)
resp = await hassio_client.options(
resp = await hassio_noauth_client.options(
f"/api/hassio_ingress/{build_type[0]}/{build_type[1]}",
headers={"X-Test-Header": "beer"},
)
@ -251,7 +256,8 @@ async def test_ingress_request_options(
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456"
assert X_AUTH_TOKEN not in aioclient_mock.mock_calls[-1][3]
assert aioclient_mock.mock_calls[-1][3]["X-Hass-Source"] == "core.ingress"
assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}"
@ -273,20 +279,21 @@ async def test_ingress_request_options(
],
)
async def test_ingress_websocket(
hassio_client, build_type, aioclient_mock: AiohttpClientMocker
hassio_noauth_client, build_type, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test no auth needed for ."""
aioclient_mock.get(f"http://127.0.0.1/ingress/{build_type[0]}/{build_type[1]}")
# Ignore error because we can setup a full IO infrastructure
await hassio_client.ws_connect(
await hassio_noauth_client.ws_connect(
f"/api/hassio_ingress/{build_type[0]}/{build_type[1]}",
headers={"X-Test-Header": "beer"},
)
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456"
assert X_AUTH_TOKEN not in aioclient_mock.mock_calls[-1][3]
assert aioclient_mock.mock_calls[-1][3]["X-Hass-Source"] == "core.ingress"
assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}"
@ -298,7 +305,9 @@ async def test_ingress_websocket(
async def test_ingress_missing_peername(
hassio_client, aioclient_mock: AiohttpClientMocker, caplog: pytest.LogCaptureFixture
hassio_noauth_client,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test hadnling of missing peername."""
aioclient_mock.get(
@ -314,7 +323,7 @@ async def test_ingress_missing_peername(
return_value=MagicMock(),
) as transport_mock:
transport_mock.get_extra_info = get_extra_info
resp = await hassio_client.get(
resp = await hassio_noauth_client.get(
"/api/hassio_ingress/lorem/ipsum",
headers={"X-Test-Header": "beer"},
)
@ -323,3 +332,19 @@ async def test_ingress_missing_peername(
# Check we got right response
assert resp.status == HTTPStatus.BAD_REQUEST
async def test_forwarding_paths_as_requested(
hassio_noauth_client, aioclient_mock
) -> None:
"""Test incomnig URLs with double encoding go out as dobule encoded."""
# This double encoded string should be forwarded double-encoded too.
aioclient_mock.get(
"http://127.0.0.1/ingress/mock-token/hello/%252e./world",
text="test",
)
resp = await hassio_noauth_client.get(
"/api/hassio_ingress/mock-token/hello/%252e./world",
)
assert await resp.text() == "test"