Skip to content

Commit 30b9831

Browse files
committed
added sanity check tests for webhook endpoint
1 parent 89a935a commit 30b9831

File tree

7 files changed

+53
-4
lines changed

7 files changed

+53
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.vimrc
2+
13
# Byte-compiled / optimized / DLL files
24
__pycache__/
35
*.py[cod]

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DEV_CMD=cd intbot && DJANGO_ENV="dev" uv run ./manage.py
2+
TEST_CMD=cd intbot && DJANGO_SETTINGS_MODULE="intbot.settings" DJANGO_ENV="test" uv run pytest --nomigrations
23

34

45
help:
@@ -18,4 +19,4 @@ migrations:
1819
$(DEV_CMD) makemigrations -n $(N)
1920

2021
test:
21-
cd intbot && DJANGO_SETTINGS_MODULE="intbot.settings" DJANGO_ENV="test" uv run pytest
22+
$(TEST_CMD) -s -vv
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from django.http import JsonResponse
1+
from django.http.response import JsonResponse
2+
23

34
def index(request):
45
return JsonResponse({"hello": "world"})

intbot/core/endpoints/webhooks.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import json
2+
from django.http.response import HttpResponseNotAllowed, JsonResponse
3+
from django.views.decorators.csrf import csrf_exempt
4+
5+
from core.models import Webhook
6+
7+
@csrf_exempt
8+
def internal_webhook_endpoint(request):
9+
if request.method == "POST":
10+
print(request.body)
11+
wh = Webhook.objects.create(
12+
source="internal",
13+
content=json.loads(request.body),
14+
)
15+
16+
return JsonResponse({"status": "created", "guid": wh.uuid})
17+
18+
return HttpResponseNotAllowed("Only POST")

intbot/core/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Webhook(models.Model):
1414

1515
# Extra information about the webhook, that might be provided in the
1616
# headers or similar
17-
meta = models.JSONField()
17+
meta = models.JSONField(default={})
1818

1919
content = models.JSONField()
2020

intbot/intbot/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from django.contrib import admin
22
from django.urls import path
33

4-
from core.views import index
4+
from core.endpoints.basic import index
5+
from core.endpoints.webhooks import internal_webhook_endpoint
56

67
urlpatterns = [
78
path('admin/', admin.site.urls),
89
path('', index),
10+
# Internal Webhooks
11+
path('webhook/internal/', internal_webhook_endpoint),
912
]

intbot/tests/test_webhooks.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
from core.models import Webhook
3+
4+
5+
@pytest.mark.django_db
6+
def test_database_sanity_check(client):
7+
webhook_body = {
8+
"event": "test1",
9+
"content": {
10+
"random": "content",
11+
},
12+
}
13+
14+
response = client.post(
15+
"/webhook/internal/",
16+
json=webhook_body,
17+
content_type="application/json",
18+
)
19+
20+
wh = Webhook.objects.get()
21+
assert response.status_code == 200
22+
assert response["Content-Type"] == "application/json"
23+
assert response.json()["status"] == "created"
24+
assert response.json()["guid"] == str(wh.uuid)

0 commit comments

Comments
 (0)