From a4c6760849a31c122e282eed80988a9b0ca90936 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Tue, 17 Dec 2024 12:21:55 +0100 Subject: [PATCH] :white_check_mark: [#207] Add tests for PUT and PATCH on Kanaal --- src/nrc/api/tests/test_kanaal.py | 98 +++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 14 deletions(-) diff --git a/src/nrc/api/tests/test_kanaal.py b/src/nrc/api/tests/test_kanaal.py index 58250669..196aa89a 100644 --- a/src/nrc/api/tests/test_kanaal.py +++ b/src/nrc/api/tests/test_kanaal.py @@ -2,7 +2,12 @@ from rest_framework import status from rest_framework.test import APITestCase -from vng_api_common.tests import JWTAuthMixin, get_operation_url +from vng_api_common.tests import ( + JWTAuthMixin, + get_operation_url, + get_validation_errors, + reverse, +) from nrc.datamodel.models import Kanaal from nrc.datamodel.tests.factories import KanaalFactory @@ -46,25 +51,90 @@ def test_kanaal_create_nonunique(self): response.status_code, status.HTTP_400_BAD_REQUEST, response.data ) - def test_kanaal_update_delete(self): + def test_kanaal_update(self): + kanaal = KanaalFactory.create( + naam="zaken", + documentatie_link="https://example.com/doc", + filters=["zaaktype"], + ) + kanaal_url = reverse(kanaal) + data = { + "naam": "zaken", + "documentatie_link": "https://example.com/updated", + "filters": ["zaaktype", "zaaktype.catalogus"], + } + + response = self.client.put(kanaal_url, data) + + self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) + + # check parsing to model + data = response.json() + kanaal = Kanaal.objects.get() + self.assertEqual(kanaal.naam, "zaken") + self.assertEqual(kanaal.documentatie_link, "https://example.com/updated") + self.assertEqual(kanaal.filters, ["zaaktype", "zaaktype.catalogus"]) + + def test_kanaal_partial_update(self): + kanaal = KanaalFactory.create( + naam="zaken", + documentatie_link="https://example.com/doc", + filters=["zaaktype"], + ) + kanaal_url = reverse(kanaal) + data = {"filters": ["zaaktype", "zaaktype.catalogus"]} + + response = self.client.patch(kanaal_url, data) + + self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) + + # check parsing to model + data = response.json() + kanaal = Kanaal.objects.get() + self.assertEqual(kanaal.naam, "zaken") + self.assertEqual(kanaal.documentatie_link, "https://example.com/doc") + self.assertEqual(kanaal.filters, ["zaaktype", "zaaktype.catalogus"]) + + def test_kanaal_cannot_update_naam(self): + kanaal = KanaalFactory.create( + naam="zaken", + documentatie_link="https://example.com/doc", + filters=["zaaktype"], + ) + kanaal_url = reverse(kanaal) + data = { + "naam": "modified", + "documentatie_link": "https://example.com/updated", + "filters": ["zaaktype", "zaaktype.catalogus"], + } + + response = self.client.put(kanaal_url, data) + + self.assertEqual( + response.status_code, status.HTTP_400_BAD_REQUEST, response.data + ) + + # check parsing to model + data = response.json() + + error = get_validation_errors(response, "naam") + self.assertEqual(error["code"], "wijzigen-niet-toegelaten") + + kanaal = Kanaal.objects.get() + self.assertEqual(kanaal.naam, "zaken") + self.assertEqual(kanaal.documentatie_link, "https://example.com/doc") + self.assertEqual(kanaal.filters, ["zaaktype"]) + + def test_kanaal_delete(self): """ - test /kanaal PUT, DELETE: - attempt to update and destroy kanaal via request + test /kanaal DELETE: + attempt to destroy kanaal via request check if response contents status 405 """ kanaal = Kanaal.objects.create(naam="zaken") kanaal_url = get_operation_url("kanaal_read", uuid=kanaal.uuid) - data = {"documentatie_link": "https://example.com/doc"} - - response_put = self.client.put(kanaal_url, data) - - self.assertEqual( - response_put.status_code, - status.HTTP_405_METHOD_NOT_ALLOWED, - response_put.data, - ) - response_delete = self.client.delete(kanaal_url, data) + response_delete = self.client.delete(kanaal_url) self.assertEqual( response_delete.status_code,