From 6373db8aa1d57f94f6aa02b6c9c43db622bb062e Mon Sep 17 00:00:00 2001 From: James Kirsop Date: Tue, 25 Feb 2020 12:48:54 +1100 Subject: [PATCH] Changes to prototype this for testing with real data --- hc/api/tests/test_update_check.py | 5 ----- hc/api/urls.py | 1 + hc/api/views.py | 21 +++++++++++++++++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/hc/api/tests/test_update_check.py b/hc/api/tests/test_update_check.py index a46eec39..7c9225b6 100644 --- a/hc/api/tests/test_update_check.py +++ b/hc/api/tests/test_update_check.py @@ -72,11 +72,6 @@ class UpdateCheckTestCase(BaseTestCase): check = Check.objects.get() self.assertEqual(check.channel_set.count(), 0) - def test_it_requires_post(self): - url = "/api/v1/checks/%s" % self.check.code - r = self.client.get(url, HTTP_X_API_KEY="X" * 32) - self.assertEqual(r.status_code, 405) - def test_it_handles_invalid_uuid(self): r = self.post("not-an-uuid", {"api_key": "X" * 32}) self.assertEqual(r.status_code, 404) diff --git a/hc/api/urls.py b/hc/api/urls.py index 5ff1b5f3..f4e5a3ef 100644 --- a/hc/api/urls.py +++ b/hc/api/urls.py @@ -22,6 +22,7 @@ urlpatterns = [ path("ping//fail", views.ping, {"action": "fail"}, name="hc-fail"), path("ping//start", views.ping, {"action": "start"}, name="hc-start"), path("api/v1/checks/", views.checks), + path("api/v1/checks/", views.single, name="hc-api-single"), path("api/v1/checks/", views.update, name="hc-api-update"), path("api/v1/checks//pause", views.pause, name="hc-api-pause"), path("api/v1/notifications//bounce", views.bounce, name="hc-api-bounce"), diff --git a/hc/api/views.py b/hc/api/views.py index bc675d57..a8ec08f1 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -19,6 +19,7 @@ from hc.api import schemas from hc.api.decorators import authorize, authorize_read, cors, validate_json from hc.api.models import Check, Notification, Channel from hc.lib.badges import check_signature, get_badge_svg +from hc.lib.jsonschema import ValidationError, validate class BadChannelException(Exception): @@ -178,18 +179,19 @@ def channels(request): @csrf_exempt -@cors("POST", "DELETE") -@validate_json(schemas.check) +@cors("POST", "DELETE", "GET") +@validate_json() @authorize -def update(request, code): +def single(request, code): check = get_object_or_404(Check, code=code) if check.project != request.project: return HttpResponseForbidden() if request.method == "POST": try: + validate(request.json, schemas.check) _update(check, request.json) - except BadChannelException as e: + except (BadChannelException,ValidationError) as e: return JsonResponse({"error": str(e)}, status=400) return JsonResponse(check.to_dict()) @@ -199,10 +201,21 @@ def update(request, code): check.delete() return JsonResponse(response) + elif request.method == "GET": + return JsonResponse(check.to_dict()) + # Otherwise, method not allowed return HttpResponse(status=405) +@csrf_exempt +@cors("POST", "DELETE") +@validate_json(schemas.check) +@authorize +def update(request, code): + single(request, code) + + @cors("POST") @csrf_exempt @validate_json()