From 2d2052f91c159e00bde7755c6938a22bd3ce4281 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 25 May 2023 14:51:45 +0200 Subject: [PATCH] api: add agenda patch endpoint (#77852) --- chrono/api/views.py | 14 ++++++++++++++ tests/api/test_agenda.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/chrono/api/views.py b/chrono/api/views.py index 585a273b..78b0ee23 100644 --- a/chrono/api/views.py +++ b/chrono/api/views.py @@ -823,6 +823,7 @@ agendas = Agendas.as_view() class AgendaAPI(APIView): permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + serializer_class = serializers.AgendaSerializer def get(self, request, agenda_identifier): agenda = get_object_or_404(Agenda, slug=agenda_identifier) @@ -841,6 +842,19 @@ class AgendaAPI(APIView): agenda.delete() return Response({'err': 0}) + def patch(self, request, agenda_identifier): + agenda = get_object_or_404(Agenda, slug=agenda_identifier) + serializer = self.serializer_class(agenda, data=request.data, partial=True) + + if not serializer.is_valid(): + raise APIErrorBadRequest(N_('invalid payload'), errors=serializer.errors) + + if 'kind' in serializer.validated_data and serializer.validated_data['kind'] != agenda.kind: + raise APIErrorBadRequest(N_('it is not possible to change kind value')) + + serializer.save() + return self.get(request, agenda_identifier) + agenda = AgendaAPI.as_view() diff --git a/tests/api/test_agenda.py b/tests/api/test_agenda.py index d2c1609b..39b1f102 100644 --- a/tests/api/test_agenda.py +++ b/tests/api/test_agenda.py @@ -626,3 +626,35 @@ def test_add_agenda(app, user, settings): resp = app.get('/api/agendas/datetimes/?agendas=%s' % agenda.slug) assert 'data' in resp.json + + +@pytest.mark.freeze_time('2021-07-09T08:00:00.0+02:00') +def test_patch_agenda(app, user): + Category.objects.create(label='Category A') + agenda = Agenda.objects.create(label='Foo bar', kind='events') + + app.authorization = ('Basic', ('john.doe', 'password')) + + resp = app.patch_json('/api/agenda/%s/' % agenda.slug) + assert resp.json['data']['id'] == 'foo-bar' + assert resp.json['data']['text'] == 'Foo bar' + assert resp.json['data']['kind'] == 'events' + assert resp.json['data']['category'] is None + + resp = app.patch_json('/api/agenda/%s/' % agenda.slug, params={'label': 'Test', 'kind': 'events'}) + assert resp.json['data']['id'] == 'foo-bar' + assert resp.json['data']['text'] == 'Test' + assert resp.json['data']['kind'] == 'events' + + resp = app.patch_json('/api/agenda/%s/' % agenda.slug, params={'category': 'category-a'}) + assert resp.json['data']['id'] == 'foo-bar' + assert resp.json['data']['category'] == 'category-a' + + # changing kind is forbidden + resp = app.patch_json('/api/agenda/%s/' % agenda.slug, params={'kind': 'meetings'}, status=400) + assert resp.json['err'] == 1 + assert resp.json['err_desc'] == 'it is not possible to change kind value' + + # unkwown category + resp = app.patch_json('/api/agenda/%s/' % agenda.slug, params={'category': 'xxx'}, status=400) + assert resp.json['err'] == 1