Beispiel #1
0
async def test_hmac_signature() -> None:
    webhook = Webhook(url="http://localhost:8080/webhook", events="after:measure", secret="testing")
    config = WebhooksConfiguration(__root__=[webhook])
    connector = WebhooksConnector(config=config)
    await connector.startup()

    info = {}
    def match_and_mock(request):
        if request.method != "POST":
            return None

        if "x-servo-signature" in request.headers:
            signature = request.headers["x-servo-signature"]
            body = request.read()
            info.update(dict(signature=signature, body=body))

        return httpx.Response(204)

    webhook_request = respx.route().mock(side_effect=match_and_mock)
    await connector.dispatch_event("measure")
    assert webhook_request.called

    expected_signature = info["signature"]
    signature = str(hmac.new("testing".encode(), info["body"], hashlib.sha1).hexdigest())
    assert signature == expected_signature
Beispiel #2
0
async def test_webhook() -> None:
    webhook = Webhook(url="http://localhost:8080/webhook", events="before:measure", secret="testing")
    config = WebhooksConfiguration(__root__=[webhook])
    connector = WebhooksConnector(config=config)
    await connector.startup()

    request = respx.post("http://localhost:8080/webhook").mock(return_value=httpx.Response(204))
    await connector.dispatch_event("measure")
    assert request.called
Beispiel #3
0
async def test_after_metrics_webhook() -> None:
    webhook = Webhook(url="http://localhost:8080/webhook", events=["after:metrics"], secret="w00t")
    config = WebhooksConfiguration(__root__=[webhook])
    connector = WebhooksConnector(config=config)
    await connector.startup()

    request = respx.post("http://localhost:8080/webhook").respond(204)
    provider = WebhookEventConnector(config=BaseConfiguration())
    provider.__connectors__.append(connector)
    results = await provider.dispatch_event("metrics")

    assert request.called
Beispiel #4
0
async def test_channel_webhooks(
    fakeapi_url: str,
    fastapi_app: fastapi.FastAPI
) -> None:
    publisher = PublisherConnector(config={})

    webhook = Webhook(url=fakeapi_url, channels=["the_news"], secret="testing")
    config = WebhooksConfiguration(__root__=[webhook])
    connector = WebhooksConnector(config=config, pubsub_exchange=publisher.pubsub_exchange)

    publisher.pubsub_exchange.start()

    await publisher.startup()
    await connector.startup()

    await asyncio.sleep(3.5)

    assert publisher.count
    assert notifications
    assert publisher.count == len(notifications)
Beispiel #5
0
async def test_unresponsive_webhook_doesnt_crash() -> None:
    webhook = Webhook(url="http://localhost:8259/webhook", events=["before:measure", "after:adjust"], secret="test")
    config = WebhooksConfiguration(__root__=[webhook])
    connector = WebhooksConnector(config=config)
    await connector.startup()
    await connector.dispatch_event("adjust")
Beispiel #6
0
def test_generate():
    config = WebhooksConfiguration.generate()
    debug(config.yaml())