Clean up Withings webhook (#102038)
parent
23e5bc20c2
commit
05ee28cae5
|
@ -10,7 +10,7 @@ import contextlib
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from aiohttp.hdrs import METH_HEAD, METH_POST
|
from aiohttp.hdrs import METH_POST
|
||||||
from aiohttp.web import Request, Response
|
from aiohttp.web import Request, Response
|
||||||
from aiowithings import NotificationCategory, WithingsClient
|
from aiowithings import NotificationCategory, WithingsClient
|
||||||
from aiowithings.util import to_enum
|
from aiowithings.util import to_enum
|
||||||
|
@ -198,6 +198,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
webhook_name,
|
webhook_name,
|
||||||
entry.data[CONF_WEBHOOK_ID],
|
entry.data[CONF_WEBHOOK_ID],
|
||||||
get_webhook_handler(coordinators),
|
get_webhook_handler(coordinators),
|
||||||
|
allowed_methods=[METH_POST],
|
||||||
)
|
)
|
||||||
|
|
||||||
await async_subscribe_webhooks(client, webhook_url)
|
await async_subscribe_webhooks(client, webhook_url)
|
||||||
|
@ -325,14 +326,6 @@ def get_webhook_handler(
|
||||||
async def async_webhook_handler(
|
async def async_webhook_handler(
|
||||||
hass: HomeAssistant, webhook_id: str, request: Request
|
hass: HomeAssistant, webhook_id: str, request: Request
|
||||||
) -> Response | None:
|
) -> Response | None:
|
||||||
# Handle http head calls to the path.
|
|
||||||
# When creating a notify subscription, Withings will check that the endpoint is running by sending a HEAD request.
|
|
||||||
if request.method == METH_HEAD:
|
|
||||||
return Response()
|
|
||||||
|
|
||||||
if request.method != METH_POST:
|
|
||||||
return json_message_response("Invalid method", message_code=2)
|
|
||||||
|
|
||||||
# Handle http post calls to the path.
|
# Handle http post calls to the path.
|
||||||
if not request.body_exists:
|
if not request.body_exists:
|
||||||
return json_message_response("No request body", message_code=12)
|
return json_message_response("No request body", message_code=12)
|
||||||
|
|
|
@ -4,6 +4,7 @@ from typing import Any
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
from aiohttp.hdrs import METH_HEAD
|
||||||
from aiowithings import (
|
from aiowithings import (
|
||||||
NotificationCategory,
|
NotificationCategory,
|
||||||
WithingsAuthenticationFailedError,
|
WithingsAuthenticationFailedError,
|
||||||
|
@ -173,27 +174,19 @@ async def test_webhook_subscription_polling_config(
|
||||||
assert withings.list_notification_configurations.call_count == 0
|
assert withings.list_notification_configurations.call_count == 0
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
async def test_head_request(
|
||||||
"method",
|
|
||||||
[
|
|
||||||
"PUT",
|
|
||||||
"HEAD",
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def test_requests(
|
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
withings: AsyncMock,
|
withings: AsyncMock,
|
||||||
webhook_config_entry: MockConfigEntry,
|
webhook_config_entry: MockConfigEntry,
|
||||||
hass_client_no_auth: ClientSessionGenerator,
|
hass_client_no_auth: ClientSessionGenerator,
|
||||||
method: str,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle request methods Withings sends."""
|
"""Test we handle head requests Withings sends."""
|
||||||
await setup_integration(hass, webhook_config_entry)
|
await setup_integration(hass, webhook_config_entry)
|
||||||
client = await hass_client_no_auth()
|
client = await hass_client_no_auth()
|
||||||
webhook_url = async_generate_url(hass, WEBHOOK_ID)
|
webhook_url = async_generate_url(hass, WEBHOOK_ID)
|
||||||
|
|
||||||
response = await client.request(
|
response = await client.request(
|
||||||
method=method,
|
method=METH_HEAD,
|
||||||
path=urlparse(webhook_url).path,
|
path=urlparse(webhook_url).path,
|
||||||
)
|
)
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
|
|
Loading…
Reference in New Issue