Browse Source

Changes to prototype this for testing with real data

pull/344/head
James Kirsop 5 years ago
parent
commit
6373db8aa1
3 changed files with 18 additions and 9 deletions
  1. +0
    -5
      hc/api/tests/test_update_check.py
  2. +1
    -0
      hc/api/urls.py
  3. +17
    -4
      hc/api/views.py

+ 0
- 5
hc/api/tests/test_update_check.py View File

@ -72,11 +72,6 @@ class UpdateCheckTestCase(BaseTestCase):
check = Check.objects.get() check = Check.objects.get()
self.assertEqual(check.channel_set.count(), 0) 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): def test_it_handles_invalid_uuid(self):
r = self.post("not-an-uuid", {"api_key": "X" * 32}) r = self.post("not-an-uuid", {"api_key": "X" * 32})
self.assertEqual(r.status_code, 404) self.assertEqual(r.status_code, 404)


+ 1
- 0
hc/api/urls.py View File

@ -22,6 +22,7 @@ urlpatterns = [
path("ping/<uuid:code>/fail", views.ping, {"action": "fail"}, name="hc-fail"), path("ping/<uuid:code>/fail", views.ping, {"action": "fail"}, name="hc-fail"),
path("ping/<uuid:code>/start", views.ping, {"action": "start"}, name="hc-start"), path("ping/<uuid:code>/start", views.ping, {"action": "start"}, name="hc-start"),
path("api/v1/checks/", views.checks), path("api/v1/checks/", views.checks),
path("api/v1/checks/<uuid:code>", views.single, name="hc-api-single"),
path("api/v1/checks/<uuid:code>", views.update, name="hc-api-update"), path("api/v1/checks/<uuid:code>", views.update, name="hc-api-update"),
path("api/v1/checks/<uuid:code>/pause", views.pause, name="hc-api-pause"), path("api/v1/checks/<uuid:code>/pause", views.pause, name="hc-api-pause"),
path("api/v1/notifications/<uuid:code>/bounce", views.bounce, name="hc-api-bounce"), path("api/v1/notifications/<uuid:code>/bounce", views.bounce, name="hc-api-bounce"),


+ 17
- 4
hc/api/views.py View File

@ -19,6 +19,7 @@ from hc.api import schemas
from hc.api.decorators import authorize, authorize_read, cors, validate_json from hc.api.decorators import authorize, authorize_read, cors, validate_json
from hc.api.models import Check, Notification, Channel from hc.api.models import Check, Notification, Channel
from hc.lib.badges import check_signature, get_badge_svg from hc.lib.badges import check_signature, get_badge_svg
from hc.lib.jsonschema import ValidationError, validate
class BadChannelException(Exception): class BadChannelException(Exception):
@ -178,18 +179,19 @@ def channels(request):
@csrf_exempt @csrf_exempt
@cors("POST", "DELETE")
@validate_json(schemas.check)
@cors("POST", "DELETE", "GET")
@validate_json()
@authorize @authorize
def update(request, code):
def single(request, code):
check = get_object_or_404(Check, code=code) check = get_object_or_404(Check, code=code)
if check.project != request.project: if check.project != request.project:
return HttpResponseForbidden() return HttpResponseForbidden()
if request.method == "POST": if request.method == "POST":
try: try:
validate(request.json, schemas.check)
_update(check, request.json) _update(check, request.json)
except BadChannelException as e:
except (BadChannelException,ValidationError) as e:
return JsonResponse({"error": str(e)}, status=400) return JsonResponse({"error": str(e)}, status=400)
return JsonResponse(check.to_dict()) return JsonResponse(check.to_dict())
@ -199,10 +201,21 @@ def update(request, code):
check.delete() check.delete()
return JsonResponse(response) return JsonResponse(response)
elif request.method == "GET":
return JsonResponse(check.to_dict())
# Otherwise, method not allowed # Otherwise, method not allowed
return HttpResponse(status=405) return HttpResponse(status=405)
@csrf_exempt
@cors("POST", "DELETE")
@validate_json(schemas.check)
@authorize
def update(request, code):
single(request, code)
@cors("POST") @cors("POST")
@csrf_exempt @csrf_exempt
@validate_json() @validate_json()


Loading…
Cancel
Save