From 7c6528f3b8e9714aae07eef022a35fe7b4e9709e Mon Sep 17 00:00:00 2001 From: Emmanuel Cazenave Date: Mon, 16 Dec 2019 16:50:38 +0100 Subject: [PATCH] api: count correctly requested places on multiple booking (#38306) --- chrono/api/views.py | 5 ++++- tests/test_api.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/chrono/api/views.py b/chrono/api/views.py index ee844ce4..c05bb710 100644 --- a/chrono/api/views.py +++ b/chrono/api/views.py @@ -496,7 +496,10 @@ class Fillslots(APIView): if to_cancel_booking.cancellation_datetime: cancel_error = gettext_noop('cancel booking: booking already cancelled') else: - to_cancel_places_count = to_cancel_booking.secondary_booking_set.count() + 1 + to_cancel_places_count = ( + to_cancel_booking.secondary_booking_set.filter(event=to_cancel_booking.event).count() + + 1 + ) if places_count != to_cancel_places_count: cancel_error = gettext_noop('cancel booking: count is different') except Booking.DoesNotExist: diff --git a/tests/test_api.py b/tests/test_api.py index 5a3d62f4..4c2540e9 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1467,6 +1467,48 @@ def test_multiple_booking_api_fillslots(app, some_data, user): assert Event.objects.get(id=event.id).waiting_list == 2 +def test_multiple_booking_move_booking(app, user): + agenda = Agenda(label=u'Foo bar') + agenda.save() + first_date = localtime(now()).replace(hour=17, minute=0, second=0, microsecond=0) + first_date += datetime.timedelta(days=1) + events = [] + for i in range(10): + event = Event(start_datetime=first_date + datetime.timedelta(days=i), places=20, agenda=agenda) + event.save() + events.append(event) + + first_two_events = events[:2] + events_ids = [x.id for x in first_two_events] + resp_datetimes = app.get('/api/agenda/%s/datetimes/' % agenda.id) + slots = [x['id'] for x in resp_datetimes.json['data'] if x['id'] in events_ids] + + app.authorization = ('Basic', ('john.doe', 'password')) + + # get 1 place on 2 slots + resp = app.post('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': slots}) + booking = Booking.objects.get(id=resp.json['booking_id']) + assert Booking.objects.filter(primary_booking=booking).count() == 1 + for event in first_two_events: + assert Event.objects.get(id=event.id).booked_places == 1 + + # change, 1 place on 2 other slots + last_two_events = events[-2:] + events_ids = [x.id for x in last_two_events] + resp_datetimes = app.get('/api/agenda/%s/datetimes/' % agenda.id) + slots = [x['id'] for x in resp_datetimes.json['data'] if x['id'] in events_ids] + + resp = app.post( + '/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': slots, 'cancel_booking_id': booking.pk} + ) + booking = Booking.objects.get(id=resp.json['booking_id']) + assert Booking.objects.filter(primary_booking=booking).count() == 1 + for event in first_two_events: + assert Event.objects.get(id=event.id).booked_places == 0 + for event in last_two_events: + assert Event.objects.get(id=event.id).booked_places == 1 + + def test_agenda_detail_api(app, some_data): agenda = Agenda.objects.get(slug='foo-bar') resp = app.get('/api/agenda/%s/' % agenda.slug)