browsertrix/backend/test/test_webhooks.py
Tessa Walsh 147bfd9d44
Add event webhook notifications system to backend (#1061)
Initial set of backend API for event webhook notifications for the following events:
* Crawl started (including boolean indicating if crawl was scheduled)
* Crawl finished
* Upload finished
* Archived item added to collection
* Archived item removed from collection

Configuration of URLs is done via /api/orgs/<oid>/event-webhook-urls. If a URL is configured for a given event, a webhook notification is added to the database and then attempted to be sent (up to a total of 5 tries per overall attempt, with an increasing backoff between, implemented via use of the backoff library, which supports async).

webhook status available via /api/orgs/<oid>/webhooks

(Additional testing + potential fastapi integration left in separate follow-ups
Fixes #1041
2023-08-31 19:52:37 -07:00

110 lines
3.0 KiB
Python

import time
import requests
from .conftest import API_PREFIX
_webhook_event_id = None
def test_list_webhook_events(admin_auth_headers, default_org_id):
# Verify that webhook URLs have been set in previous tests
r = requests.get(
f"{API_PREFIX}/orgs/{default_org_id}",
headers=admin_auth_headers,
)
assert r.status_code == 200
data = r.json()
urls = data["webhookUrls"]
assert urls["crawlStarted"]
assert urls["crawlFinished"]
assert urls["uploadFinished"]
assert urls["addedToCollection"]
assert urls["removedFromCollection"]
# Verify list endpoint works as expected
r = requests.get(
f"{API_PREFIX}/orgs/{default_org_id}/webhooks",
headers=admin_auth_headers,
)
assert r.status_code == 200
data = r.json()
assert data["total"] > 0
for item in data["items"]:
assert item["id"]
assert item["event"]
assert item["oid"]
assert item["body"]
assert item["success"] is False
assert item["attempts"] == 1
assert item["created"]
assert item["lastAttempted"]
global _webhook_event_id
_webhook_event_id = data["items"][0]["id"]
assert _webhook_event_id
def test_get_webhook_event(admin_auth_headers, default_org_id):
r = requests.get(
f"{API_PREFIX}/orgs/{default_org_id}/webhooks/{_webhook_event_id}",
headers=admin_auth_headers,
)
assert r.status_code == 200
item = r.json()
assert item["id"]
assert item["oid"]
assert item["success"] is False
assert item["attempts"] == 1
assert item["created"]
assert item["lastAttempted"]
body = item["body"]
assert body
event = item["event"]
assert event
if event in ("crawlFinished", "uploadFinished"):
assert len(body["downloadUrls"]) >= 1
assert body["itemId"]
elif event in ("crawlStarted"):
assert len(body["downloadUrls"]) == 0
assert body["itemId"]
elif event in ("addedToCollection", "removedFromCollection"):
assert len(body["downloadUrls"]) == 1
assert body["collectionId"]
assert len(body["itemIds"]) >= 1
def test_retry_webhook_event(admin_auth_headers, default_org_id):
# Expect to fail because we haven't set up URLs that accept webhooks
r = requests.get(
f"{API_PREFIX}/orgs/{default_org_id}/webhooks/{_webhook_event_id}/retry",
headers=admin_auth_headers,
)
assert r.status_code == 200
assert r.json()["success"]
# Give it some time to run
time.sleep(90)
# Verify attempts have been increased
r = requests.get(
f"{API_PREFIX}/orgs/{default_org_id}/webhooks/{_webhook_event_id}",
headers=admin_auth_headers,
)
assert r.status_code == 200
item = r.json()
assert item["id"]
assert item["event"]
assert item["oid"]
assert item["body"]
assert item["success"] is False
assert item["attempts"] == 2
assert item["created"]
assert item["lastAttempted"]