From 5858570d0cfe71107f1fac14e081116b64b739f2 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Mon, 14 Feb 2022 17:19:03 +0100
Subject: [PATCH 1/2] tests: change post/patch to pass json to event view
 (#60351)

---
 tests/api/test_event.py | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/tests/api/test_event.py b/tests/api/test_event.py
index 319f182..e05394e 100644
--- a/tests/api/test_event.py
+++ b/tests/api/test_event.py
@@ -201,27 +201,27 @@ def test_add_event(app, user):
         'places': ['This field is required.'],
     }
 
     # add with errors in datetime parts
     params = {
         'start_datetime': '2021-11-15 minuit',
         'places': 10,
     }
-    resp = app.post(api_url, params=params, status=400)
+    resp = app.post_json(api_url, params=params, status=400)
     assert resp.json['err']
     assert resp.json['err_desc'] == 'invalid payload'
     assert 'Datetime has wrong format' in resp.json['errors']['start_datetime'][0]
 
     # add an event
     params = {
         'start_datetime': '2021-11-15 15:38',
         'places': 10,
     }
-    resp = app.post(api_url, params=params)
+    resp = app.post_json(api_url, params=params)
     assert not resp.json['err']
     assert resp.json['data']['id'] == 'foo-bar-event'
     assert {'api', 'disabled', 'places'}.issubset(resp.json['data'].keys())
     assert {'recurrence_days', 'recurrence_week_interval', 'recurrence_end_date'}.isdisjoint(
         resp.json['data'].keys()
     )
     event = Event.objects.filter(agenda=agenda).get(slug='foo-bar-event')
     assert str(event.start_datetime) == '2021-11-15 14:38:00+00:00'
@@ -236,17 +236,17 @@ def test_add_event(app, user):
         'publication_datetime': '2021-09-20 10:00',
         'places': 11,
         'waiting_list_places': 3,
         'label': 'FOO camp',
         'description': 'An event',
         'pricing': 'free',
         'url': 'http://example.org/foo/bar/?',
     }
-    resp = app.post(api_url, params=params)
+    resp = app.post_json(api_url, params=params)
     assert not resp.json['err']
     assert resp.json['data']['id'] == 'foo-camp'
     assert {'api', 'disabled', 'places'}.issubset(resp.json['data'].keys())
     assert {'recurrence_days', 'recurrence_week_interval', 'recurrence_end_date'}.isdisjoint(
         resp.json['data'].keys()
     )
     event = Event.objects.filter(agenda=agenda).get(slug='foo-camp')
     assert event.duration == 42
@@ -259,40 +259,40 @@ def test_add_event(app, user):
     assert str(event.publication_datetime.tzinfo) == 'UTC'
 
     # add with errors in recurrence_days list
     params = {
         'start_datetime': '2021-11-15 15:38',
         'places': 10,
         'recurrence_days': 'oups',
     }
-    resp = app.post(api_url, params=params, status=400)
+    resp = app.post_json(api_url, params=params, status=400)
     assert resp.json['err']
     assert resp.json['err_desc'] == 'invalid payload'
     assert resp.json['errors']['recurrence_days']['0'][0] == 'A valid integer is required.'
     params = {
         'start_datetime': '2021-11-15 15:38',
         'places': 10,
         'recurrence_days': '7',
     }
-    resp = app.post(api_url, params=params, status=400)
+    resp = app.post_json(api_url, params=params, status=400)
     assert resp.json['err']
     assert resp.json['err_desc'] == 'invalid payload'
     assert resp.json['errors']['recurrence_days']['0'][0] == 'Ensure this value is less than or equal to 6.'
 
     # add a recurrent event
     params = {
         'start_datetime': '2021-11-15 15:38',
         'places': 12,
         'recurrence_days': '0,3,5',
         'recurrence_week_interval': '2',
         'description': 'A recurrent event',
     }
     assert Event.objects.filter(agenda=agenda).count() == 2
-    resp = app.post(api_url, params=params)
+    resp = app.post_json(api_url, params=params)
     assert Event.objects.filter(agenda=agenda).count() == 3
     assert not resp.json['err']
     assert resp.json['data']['id'] == 'foo-bar-event-1'
     assert {'api', 'disabled', 'places'}.isdisjoint(resp.json['data'].keys())
     assert {'recurrence_days', 'recurrence_week_interval', 'recurrence_end_date'}.issubset(
         resp.json['data'].keys()
     )
     event = Event.objects.filter(agenda=agenda).get(slug='foo-bar-event-1')
@@ -305,17 +305,17 @@ def test_add_event(app, user):
     params = {
         'start_datetime': '2021-11-15 15:38',
         'places': 13,
         'recurrence_days': '0,3,5',  # Monday, Tuesday, Saturday
         'recurrence_week_interval': '2',
         'recurrence_end_date': '2021-12-27',
         'description': 'A recurrent event having recurrences',
     }
-    resp = app.post(api_url, params=params)
+    resp = app.post_json(api_url, params=params)
     assert not resp.json['err']
     assert resp.json['data']['id'] == 'foo-bar-event-2'
     assert {'api', 'disabled', 'places'}.isdisjoint(resp.json['data'].keys())
     assert {'recurrence_days', 'recurrence_week_interval', 'recurrence_end_date'}.issubset(
         resp.json['data'].keys()
     )
     event = Event.objects.filter(agenda=agenda).get(slug='foo-bar-event-2')
     assert Event.objects.filter(agenda=agenda).count() == 13
@@ -382,17 +382,17 @@ def test_update_event(app, user):
     event = Event.objects.create(agenda=agenda, start_datetime=now(), places=10, waiting_list_places=5)
     api_url = '/api/agenda/%s/event/%s/' % (agenda.slug, event.slug)
 
     # update with errors in datetime parts
     params = {
         'start_datetime': '2021-11-15 minuit',
         'recurrence_days': '7, 8',
     }
-    resp = app.patch(api_url, params=params, status=400)
+    resp = app.patch_json(api_url, params=params, status=400)
     assert resp.json['err']
     assert resp.json['err_desc'] == 'invalid payload'
     assert 'Datetime has wrong format' in resp.json['errors']['start_datetime'][0]
     assert resp.json['errors']['recurrence_days']['0'][0] == 'Ensure this value is less than or equal to 6.'
 
     # update with almost all optional managed fields
     params = {
         'start_datetime': '2021-11-15 15:38',
@@ -400,17 +400,17 @@ def test_update_event(app, user):
         'publication_datetime': '2021-09-20 12:00',
         'places': 8,
         'waiting_list_places': 3,
         'label': 'FOO camp',
         'description': 'An event',
         'pricing': 'free',
         'url': 'http://example.org/foo/bar/?',
     }
-    resp = app.patch(api_url, params=params)
+    resp = app.patch_json(api_url, params=params)
     assert not resp.json['err']
     event = Event.objects.filter(agenda=agenda).get(slug=event.slug)
     assert event.duration == 42
     assert event.places == 8
     assert event.waiting_list_places == 3
     assert event.label == 'FOO camp'
     assert event.description == 'An event'
     assert event.pricing == 'free'
@@ -419,17 +419,17 @@ def test_update_event(app, user):
     assert str(event.publication_datetime.tzinfo) == 'UTC'
 
     # update event as a recurring event
     params = {
         'recurrence_days': '0,3,5',
         'recurrence_week_interval': 2,
         'recurrence_end_date': '2021-12-27',
     }
-    resp = app.patch(api_url, params=params)
+    resp = app.patch_json(api_url, params=params)
     assert not resp.json['err']
     event = Event.objects.filter(agenda=agenda).get(slug=event.slug)
     assert event.recurrences.count() == 9
     assert [x.places for x in event.recurrences.all()] == [8] * 9
 
     recurrence_slug = event.recurrences.first().slug
     assert recurrence_slug == 'foo-bar-event--2021-11-15-1538'
     recurrence_url = '/api/agenda/%s/event/%s/' % (agenda.slug, 'foo-bar-event:2021-11-15-1538')
@@ -440,89 +440,89 @@ def test_update_event(app, user):
         'duration': 43,
         'places': 9,
         'waiting_list_places': 4,
         'label': 'BAR camp',
         'description': 'An occurence of an event recurrence',
         'pricing': '5€',
         'url': 'http://example.org/bar/bar/',
     }
-    resp = app.patch(recurrence_url, params=params)
+    resp = app.patch_json(recurrence_url, params=params)
     assert not resp.json['err']
     recurrence = Event.objects.filter(agenda=agenda).get(slug=recurrence_slug)
     assert recurrence.duration == 43
     assert recurrence.places == 9
     assert recurrence.waiting_list_places == 4
     assert recurrence.label == 'BAR camp'
     assert recurrence.description == 'An occurence of an event recurrence'
     assert recurrence.pricing == '5€'
     assert recurrence.url == 'http://example.org/bar/bar/'
 
     # try to update protected fields of one of the event recurrencies
     params = {
         'publication_datetime': '2021-11-15 12:00',
     }
-    resp = app.patch(recurrence_url, params=params, status=400)
+    resp = app.patch_json(recurrence_url, params=params, status=400)
     assert resp.json['err']
     assert 'cannot be modified' in resp.json['err_desc']
 
     #  update protected fields of one of the event recurrencies providing same value
     assert 'cannot be modified' in resp.json['err_desc']
     params = {
         'recurrence_week_interval': 1,
     }
-    resp = app.patch(recurrence_url, params=params)
+    resp = app.patch_json(recurrence_url, params=params)
     assert not resp.json['err']
 
     booking = Booking.objects.create(event=event.recurrences.all()[2])
 
     # update unprotected fields
     params = {
         'places': 7,
     }
-    resp = app.patch(api_url, params=params)
+    resp = app.patch_json(api_url, params=params)
     assert not resp.json['err']
     event = Event.objects.filter(agenda=agenda).get(slug=event.slug)
     assert [x.places for x in event.recurrences.all()] == [7] * 9
 
     # try to update recurring event protected fields
     params = {
         'recurrence_days': '1,2',
     }
-    resp = app.patch(api_url, params=params, status=400)
+    resp = app.patch_json(api_url, params=params, status=400)
     assert resp.json['err']
     assert 'cannot be modified' in resp.json['err_desc']
 
     assert booking.event.start_datetime.date() == datetime.date(2021, 11, 20)
 
     # try to reduce recurrence end date before booked event
     params = {
         'recurrence_end_date': '2021-11-20',
     }
-    resp = app.patch(api_url, params=params, status=400)
+    resp = app.patch_json(api_url, params=params, status=400)
     assert resp.json['err']
     assert 'bookings exist after this date.' in resp.json['err_desc']
 
     # reduce recurrence end date after booked event
     params = {
         'recurrence_end_date': '2021-11-21',
     }
-    resp = app.patch(api_url, params=params)
+    resp = app.patch_json(api_url, params=params)
     assert not resp.json['err']
     event = Event.objects.filter(agenda=agenda).get(slug=event.slug)
     assert event.recurrences.count() == 3
 
     booking.cancel()
 
     # update no more protected fields
     params = {
         'recurrence_days': '1,2,3,4,5',
         'recurrence_week_interval': 1,
     }
-    resp = app.patch(api_url, params=params)
+    resp = app.patch_json(api_url, params=params)
     assert not resp.json['err']
     event = Event.objects.filter(agenda=agenda).get(slug=event.slug)
     assert event.recurrences.count() == 5
 
 
 @pytest.mark.freeze_time('2021-11-01 10:00')
 def test_delete_event(app, user):
     agenda = Agenda.objects.create(label='Foo bar', kind='events')
-- 
2.30.2

