Clean up Withings webhook (#102038)

pull/101761/head^2
Joost Lekkerkerker 2023-10-15 18:00:18 +02:00 committed by GitHub
parent 23e5bc20c2
commit 05ee28cae5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 20 deletions

View File

@ -10,7 +10,7 @@ import contextlib
from datetime import timedelta
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 aiowithings import NotificationCategory, WithingsClient
from aiowithings.util import to_enum
@ -198,6 +198,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
webhook_name,
entry.data[CONF_WEBHOOK_ID],
get_webhook_handler(coordinators),
allowed_methods=[METH_POST],
)
await async_subscribe_webhooks(client, webhook_url)
@ -325,14 +326,6 @@ def get_webhook_handler(
async def async_webhook_handler(
hass: HomeAssistant, webhook_id: str, request: Request
) -> 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.
if not request.body_exists:
return json_message_response("No request body", message_code=12)

View File

@ -4,6 +4,7 @@ from typing import Any
from unittest.mock import AsyncMock, MagicMock, patch
from urllib.parse import urlparse
from aiohttp.hdrs import METH_HEAD
from aiowithings import (
NotificationCategory,
WithingsAuthenticationFailedError,
@ -173,27 +174,19 @@ async def test_webhook_subscription_polling_config(
assert withings.list_notification_configurations.call_count == 0
@pytest.mark.parametrize(
"method",
[
"PUT",
"HEAD",
],
)
async def test_requests(
async def test_head_request(
hass: HomeAssistant,
withings: AsyncMock,
webhook_config_entry: MockConfigEntry,
hass_client_no_auth: ClientSessionGenerator,
method: str,
) -> None:
"""Test we handle request methods Withings sends."""
"""Test we handle head requests Withings sends."""
await setup_integration(hass, webhook_config_entry)
client = await hass_client_no_auth()
webhook_url = async_generate_url(hass, WEBHOOK_ID)
response = await client.request(
method=method,
method=METH_HEAD,
path=urlparse(webhook_url).path,
)
assert response.status == 200