From 1d52cd725905f48a42b0347d59f97680ce4b4fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 19 Oct 2017 13:21:06 +0200 Subject: [PATCH] api: add dedicated error handling for invalid place count value (#19552) --- chrono/api/views.py | 8 +++++++- tests/test_api.py | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/chrono/api/views.py b/chrono/api/views.py index 430e6af..f4144a2 100644 --- a/chrono/api/views.py +++ b/chrono/api/views.py @@ -312,7 +312,13 @@ class Fillslot(GenericAPIView): raise Http404() if 'count' in request.GET: - places_count = int(request.GET['count']) + try: + places_count = int(request.GET['count']) + except ValueError: + return Response({ + 'err': 1, + 'reason': 'invalid value for count (%r)' % request.GET['count'], + }) else: places_count = 1 diff --git a/tests/test_api.py b/tests/test_api.py index cf086ff..fb2f546 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -616,6 +616,10 @@ def test_multiple_booking_api(app, some_data, user): event_fillslot_url = [x for x in resp_datetimes.json['data'] if x['id'] == event.id][0]['api']['fillslot_url'] app.authorization = ('Basic', ('john.doe', 'password')) + resp = app.post('/api/agenda/%s/fillslot/%s/?count=NaN' % (agenda.slug, event.id)) + assert resp.json['err'] == 1 + assert resp.json['reason'] == "invalid value for count (u'NaN')" + resp = app.post('/api/agenda/%s/fillslot/%s/?count=3' % (agenda.slug, event.id)) Booking.objects.get(id=resp.json['booking_id']) assert resp.json['datetime'] == localtime(event.start_datetime).isoformat() -- 2.15.0.rc1