|
10 | 10 | from custom_components.pyscript import trigger |
11 | 11 | from custom_components.pyscript.const import DOMAIN |
12 | 12 | from custom_components.pyscript.function import Function |
| 13 | +from homeassistant.components import webhook |
13 | 14 | from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_STATE_CHANGED |
14 | 15 | from homeassistant.setup import async_setup_component |
| 16 | +from homeassistant.util.aiohttp import MockRequest |
15 | 17 |
|
16 | 18 |
|
17 | 19 | async def setup_script(hass, notify_q, now, source): |
@@ -224,3 +226,33 @@ def func6(value): |
224 | 226 | hass.states.async_set("pyscript.var1", 6 + 2 * i) |
225 | 227 | seq_num += 1 |
226 | 228 | assert literal_eval(await wait_until_done(notify_q)) == [seq_num, 6 + 2 * i] |
| 229 | + |
| 230 | + |
| 231 | +@pytest.mark.asyncio |
| 232 | +async def test_webhook_request_kwarg(hass): |
| 233 | + """The aiohttp request is passed to the user function as the `request` kwarg.""" |
| 234 | + notify_q = asyncio.Queue(0) |
| 235 | + await setup_script( |
| 236 | + hass, |
| 237 | + notify_q, |
| 238 | + [dt(2020, 7, 1, 11, 59, 59, 999999)], |
| 239 | + """ |
| 240 | +@webhook_trigger("test_req_hook") |
| 241 | +def webhook_test(payload, request): |
| 242 | + pyscript.done = [request.headers["X-My-Sig"], request.method, payload] |
| 243 | +""", |
| 244 | + ) |
| 245 | + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) |
| 246 | + await hass.async_block_till_done() |
| 247 | + |
| 248 | + request = MockRequest( |
| 249 | + content=b'{"hello": "world"}', |
| 250 | + mock_source="test", |
| 251 | + method="POST", |
| 252 | + headers={"Content-Type": "application/json", "X-My-Sig": "abc123"}, |
| 253 | + remote="127.0.0.1", |
| 254 | + ) |
| 255 | + |
| 256 | + await webhook.async_handle_webhook(hass, "test_req_hook", request) |
| 257 | + |
| 258 | + assert literal_eval(await wait_until_done(notify_q)) == ["abc123", "POST", {"hello": "world"}] |
0 commit comments