From 09c24ac6f3c0ee4e081021d0381f360cc1f80ffd Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Thu, 28 May 2020 16:37:29 +0200
Subject: [PATCH 2/2] api: return end_datetime on booking events (#37352)

---
 chrono/api/views.py | 2 ++
 tests/test_api.py   | 7 ++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/chrono/api/views.py b/chrono/api/views.py
index 73b5885..4acf16f 100644
--- a/chrono/api/views.py
+++ b/chrono/api/views.py
@@ -895,22 +895,24 @@ class Fillslots(APIView):
             response['desk'] = {'label': available_desk.label, 'slug': available_desk.slug}
         if to_cancel_booking:
             response['cancelled_booking_id'] = cancelled_booking_id
         if agenda.kind == 'events' and not multiple_booking:
             event = events[0]
             # event.full is not up to date, it might have been changed by previous new_booking.save().
             event.refresh_from_db()
             response['places'] = get_event_places(event)
+            response['end_datetime'] = format_response_datetime(event.end_datetime)
         if agenda.kind == 'events' and multiple_booking:
             response['events'] = [
                 {
                     'slug': x.slug,
                     'text': str(x),
                     'datetime': format_response_datetime(x.start_datetime),
+                    'end_datetime': format_response_datetime(x.end_datetime),
                     'description': x.description,
                 }
                 for x in events
             ]
 
         return Response(response)
 
 
diff --git a/tests/test_api.py b/tests/test_api.py
index 465ac92..c42965a 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -67,17 +67,18 @@ def mock_now(request, monkeypatch):
 
 @pytest.fixture
 def some_data(time_zone, mock_now):
     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)
     for i in range(3):
-        event = Event(start_datetime=first_date + datetime.timedelta(days=i), places=20, agenda=agenda)
+        event = Event(start_datetime=first_date + datetime.timedelta(days=i), duration=i * 45,
+                      places=20, agenda=agenda)
         event.save()
 
     agenda2 = Agenda(label=u'Foo bar 2')
     agenda2.save()
     first_date = localtime(now()).replace(hour=20, minute=0, second=0, microsecond=0)
     first_date += datetime.timedelta(days=1)
     for i in range(2):
         event = Event(start_datetime=first_date + datetime.timedelta(days=i), places=20, agenda=agenda2)
@@ -444,16 +445,17 @@ def test_booking_api(app, some_data, user):
             agenda.slug,
             event.slug or event.id,
         )
 
     app.authorization = ('Basic', ('john.doe', 'password'))
     resp = app.post('/api/agenda/%s/fillslot/%s/' % (agenda.slug, event.id))
     Booking.objects.get(id=resp.json['booking_id'])
     assert resp.json['datetime'] == localtime(event.start_datetime).strftime('%Y-%m-%d %H:%M:%S')
+    assert resp.json['end_datetime'] == localtime(event.end_datetime).strftime('%Y-%m-%d %H:%M:%S')
     assert 'accept_url' not in resp.json['api']
     assert 'suspend_url' in resp.json['api']
     assert 'cancel_url' in resp.json['api']
     assert 'ics_url' in resp.json['api']
     assert urlparse.urlparse(resp.json['api']['suspend_url']).netloc
     assert urlparse.urlparse(resp.json['api']['cancel_url']).netloc
     assert urlparse.urlparse(resp.json['api']['ics_url']).netloc
     assert Booking.objects.count() == 1
@@ -758,16 +760,19 @@ def test_booking_api_fillslots_slots_string_param(app, some_data, user):
     assert resp.json['err_class'] == 'invalid payload'
     assert resp.json['err_desc'] == 'invalid payload'
 
     slots_string_param = ','.join([str(e) for e in events_ids])
     resp = app.post_json('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': slots_string_param})
     primary_booking_id = resp.json['booking_id']
     Booking.objects.get(id=primary_booking_id)
     assert Booking.objects.count() == 3
+    for idx, id in enumerate(events_ids):
+        event = Event.objects.get(id=id)
+        assert resp.json['events'][idx]['end_datetime'] == localtime(event.end_datetime).strftime('%Y-%m-%d %H:%M:%S')
 
 
 def test_booking_api_meeting(app, meetings_agenda, user):
     agenda_id = meetings_agenda.slug
     meeting_type = MeetingType.objects.get(agenda=meetings_agenda)
     resp = app.get('/api/agenda/meetings/%s/datetimes/' % meeting_type.id)
     event_id = resp.json['data'][2]['id']
     assert urlparse.urlparse(
-- 
2.26.2

