Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions custom_components/pyscript/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ async def webhook_handler(cls, hass, webhook_id, request):
func_args = {
"trigger_type": "webhook",
"webhook_id": webhook_id,
"request": request,
}

if "json" in request.headers.get(hdrs.CONTENT_TYPE, ""):
Expand Down
28 changes: 27 additions & 1 deletion tests/test_decorator_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from ast import literal_eval
import asyncio
from datetime import datetime as dt
from unittest.mock import mock_open, patch
from unittest.mock import AsyncMock, MagicMock, mock_open, patch

import pytest

from custom_components.pyscript import trigger
from custom_components.pyscript.const import DOMAIN
from custom_components.pyscript.decorators.webhook import WebhookTriggerDecorator
from custom_components.pyscript.function import Function
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_STATE_CHANGED
from homeassistant.setup import async_setup_component
Expand Down Expand Up @@ -212,7 +213,32 @@
in caplog.text
)

@pytest.mark.asyncio
async def test_webhook_request_kwarg(hass):
"""The aiohttp request is passed to the user function as the `request` kwarg."""
notify_q = asyncio.Queue(0)
await setup_script(
hass,
notify_q,
[dt(2020, 7, 1, 11, 59, 59, 999999)],
"""
@webhook_trigger("test_req_hook")
def webhook_test(payload, request):
pyscript.done = [request.headers["X-My-Sig"], request.method, payload]
""",
)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()

request = MagicMock()
request.headers = {"Content-Type": "application/json", "X-My-Sig": "abc123"}
request.method = "POST"
request.json = AsyncMock(return_value={"hello": "world"})

await WebhookTriggerDecorator._handler(hass, "test_req_hook", request)

assert literal_eval(await wait_until_done(notify_q)) == ["abc123", "POST", {"hello": "world"}]

Check failure on line 241 in tests/test_decorator_errors.py

View workflow job for this annotation

GitHub Actions / Check style formatting

ruff (W293)

tests/test_decorator_errors.py:241:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
@pytest.mark.asyncio
async def test_trigger_expression_errors(hass, caplog, monkeypatch):
"""Legacy trigger expression errors should not stop trigger loops."""
Expand Down
Loading