From 424d9fb6023e36f32f2c56c0cbebc52392fecdf5 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Fri, 18 Oct 2024 10:55:33 +0200 Subject: [PATCH] :ok_hand: [#147] PR feedback --- .../api/serializers/klantcontacten.py | 2 +- .../klantinteracties/api/tests/factories.py | 59 +++++++ .../api/tests/test_klantcontacten.py | 144 ++++++++---------- .../components/klantinteracties/api/urls.py | 4 +- .../api/viewsets/klantcontacten.py | 8 +- 5 files changed, 132 insertions(+), 85 deletions(-) create mode 100644 src/openklant/components/klantinteracties/api/tests/factories.py diff --git a/src/openklant/components/klantinteracties/api/serializers/klantcontacten.py b/src/openklant/components/klantinteracties/api/serializers/klantcontacten.py index 1228bb88..7145c211 100644 --- a/src/openklant/components/klantinteracties/api/serializers/klantcontacten.py +++ b/src/openklant/components/klantinteracties/api/serializers/klantcontacten.py @@ -553,7 +553,7 @@ def __init__(self, *args, **kwargs): self.fields["klantcontact"].read_only = True -class KlantContactBetrokkeneOnderwerpObjectSerializer(serializers.Serializer): +class KlantContactConvenienceSerializer(serializers.Serializer): klantcontact = KlantcontactSerializer() betrokkene = BetrokkeneConvenienceSerializer(required=False) onderwerpobject = OnderwerpobjectConvenienceSerializer(required=False) diff --git a/src/openklant/components/klantinteracties/api/tests/factories.py b/src/openklant/components/klantinteracties/api/tests/factories.py new file mode 100644 index 00000000..3b1baf17 --- /dev/null +++ b/src/openklant/components/klantinteracties/api/tests/factories.py @@ -0,0 +1,59 @@ +import factory + + +class KlantContactDataFactory(factory.DictFactory): + nummer = "7948723947" + kanaal = "changed" + onderwerp = "changed" + inhoud = "changed" + indicatieContactGelukt = False + taal = "de" + vertrouwelijk = False + plaatsgevondenOp = "2020-08-24T14:15:22Z" + + +class BezoekAdresDataFactory(factory.DictFactory): + nummeraanduidingId = "4a282b5c-16d7-401d-9737-28e98c865ab2" + adresregel1 = "adres1" + adresregel2 = "adres2" + adresregel3 = "adres3" + land = "6030" + + +class CorrespondentieAdresDataFactory(factory.DictFactory): + nummeraanduidingId = "c06918d9-899b-4d98-a10d-08436ebc6c20" + adresregel1 = "adres1" + adresregel2 = "adres2" + adresregel3 = "adres3" + land = "6030" + + +class ContactNaamDataFactory(factory.DictFactory): + voorletters = "P" + voornaam = "Phil" + voorvoegselAchternaam = "" + achternaam = "Bozeman" + + +class BetrokkeneDataFactory(factory.DictFactory): + wasPartij = None + bezoekadres = factory.SubFactory(BezoekAdresDataFactory) + correspondentieadres = factory.SubFactory(CorrespondentieAdresDataFactory) + contactnaam = factory.SubFactory(ContactNaamDataFactory) + rol = "vertegenwoordiger" + organisatienaam = "Whitechapel" + initiator = True + + +class OnderwerpObjectIdentificatorDataFactory(factory.DictFactory): + codeObjecttype = "codeObjecttype" + codeSoortObjectId = "codeSoortObjectId" + objectId = "objectId" + codeRegister = "codeRegister" + + +class OnderwerpObjectDataFactory(factory.DictFactory): + wasKlantcontact = None + onderwerpobjectidentificator = factory.SubFactory( + OnderwerpObjectIdentificatorDataFactory + ) diff --git a/src/openklant/components/klantinteracties/api/tests/test_klantcontacten.py b/src/openklant/components/klantinteracties/api/tests/test_klantcontacten.py index 6e2f5a8a..8192ac94 100644 --- a/src/openklant/components/klantinteracties/api/tests/test_klantcontacten.py +++ b/src/openklant/components/klantinteracties/api/tests/test_klantcontacten.py @@ -24,6 +24,12 @@ ) from openklant.components.token.tests.api_testcase import APITestCase +from .factories import ( + BetrokkeneDataFactory, + KlantContactDataFactory, + OnderwerpObjectDataFactory, +) + class KlantContactTests(APITestCase): def test_list_klantcontact(self): @@ -1479,59 +1485,14 @@ class ConvenienceEndpointTests(APITestCase): maxDiff = None url = reverse("klantinteracties:klantcontact-convenience-list") - def setUp(self): - super().setUp() - - self.post_data = { - "klantcontact": { - "nummer": "7948723947", - "kanaal": "changed", - "onderwerp": "changed", - "inhoud": "changed", - "indicatieContactGelukt": False, - "taal": "de", - "vertrouwelijk": False, - "plaatsgevondenOp": "2020-08-24T14:15:22Z", - }, - "betrokkene": { - "wasPartij": None, - "bezoekadres": { - "nummeraanduidingId": "4a282b5c-16d7-401d-9737-28e98c865ab2", - "adresregel1": "adres1", - "adresregel2": "adres2", - "adresregel3": "adres3", - "land": "6030", - }, - "correspondentieadres": { - "nummeraanduidingId": "c06918d9-899b-4d98-a10d-08436ebc6c20", - "adresregel1": "adres1", - "adresregel2": "adres2", - "adresregel3": "adres3", - "land": "6030", - }, - "contactnaam": { - "voorletters": "P", - "voornaam": "Phil", - "voorvoegselAchternaam": "", - "achternaam": "Bozeman", - }, - "rol": "vertegenwoordiger", - "organisatienaam": "Whitechapel", - "initiator": True, - }, - "onderwerpobject": { - "wasKlantcontact": None, - "onderwerpobjectidentificator": { - "codeObjecttype": "codeObjecttype", - "codeSoortObjectId": "codeSoortObjectId", - "objectId": "objectId", - "codeRegister": "codeRegister", - }, - }, + def test_create_success(self): + post_data = { + "klantcontact": KlantContactDataFactory.create(), + "betrokkene": BetrokkeneDataFactory.create(), + "onderwerpobject": OnderwerpObjectDataFactory.create(), } - def test_create_success(self): - response = self.client.post(self.url, self.post_data) + response = self.client.post(self.url, post_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) @@ -1653,9 +1614,13 @@ def test_create_klantcontact_validation_error(self): """ If there are validation errors in the Klantcontact, no resources should be created """ - self.post_data["klantcontact"]["taal"] = "incorrect" + post_data = { + "klantcontact": KlantContactDataFactory.create(taal="incorrect"), + "betrokkene": BetrokkeneDataFactory.create(), + "onderwerpobject": OnderwerpObjectDataFactory.create(), + } - response = self.client.post(self.url, self.post_data) + response = self.client.post(self.url, post_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) @@ -1681,9 +1646,13 @@ def test_create_betrokkene_validation_error(self): """ If there are validation errors in the Betrokkene, no resources should be created """ - self.post_data["betrokkene"]["wasPartij"] = "incorrect" + post_data = { + "klantcontact": KlantContactDataFactory.create(), + "betrokkene": BetrokkeneDataFactory.create(wasPartij="incorrect"), + "onderwerpobject": OnderwerpObjectDataFactory.create(), + } - response = self.client.post(self.url, self.post_data) + response = self.client.post(self.url, post_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) @@ -1706,10 +1675,14 @@ def test_create_betrokkene_validation_error(self): self.assertFalse(Onderwerpobject.objects.exists()) def test_create_use_read_only_betrokkene_attributes(self): - # This field should be ignored - self.post_data["betrokkene"]["hadKlantcontact"] = "foobar" + post_data = { + "klantcontact": KlantContactDataFactory.create(), + # `hadKlantcontact` field should be ignored + "betrokkene": BetrokkeneDataFactory.create(hadKlantcontact="foobar"), + "onderwerpobject": OnderwerpObjectDataFactory.create(), + } - response = self.client.post(self.url, self.post_data) + response = self.client.post(self.url, post_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) @@ -1763,11 +1736,16 @@ def test_create_use_read_only_betrokkene_attributes(self): def test_create_betrokkene_was_partij_and_partij(self): partij = PartijFactory.create(voorkeurs_digitaal_adres=None) - # This field should be ignored - self.post_data["betrokkene"]["partij"] = "foobar" - self.post_data["betrokkene"]["wasPartij"] = {"uuid": str(partij.uuid)} + post_data = { + "klantcontact": KlantContactDataFactory.create(), + # `partij` should be ignored + "betrokkene": BetrokkeneDataFactory.create( + partij="foobar", wasPartij={"uuid": str(partij.uuid)} + ), + "onderwerpobject": OnderwerpObjectDataFactory.create(), + } - response = self.client.post(self.url, self.post_data) + response = self.client.post(self.url, post_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) @@ -1824,11 +1802,15 @@ def test_create_onderwerpobject_validation_error(self): """ If there are validation errors in the Betrokkene, no resources should be created """ - self.post_data["onderwerpobject"]["onderwerpobjectidentificator"][ - "objectId" - ] = True + post_data = { + "klantcontact": KlantContactDataFactory.create(), + "betrokkene": BetrokkeneDataFactory.create(), + "onderwerpobject": OnderwerpObjectDataFactory.create( + onderwerpobjectidentificator__objectId=True + ), + } - response = self.client.post(self.url, self.post_data) + response = self.client.post(self.url, post_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) @@ -1849,10 +1831,14 @@ def test_create_onderwerpobject_validation_error(self): self.assertFalse(Onderwerpobject.objects.exists()) def test_create_use_read_only_onderwerpobject_attributes(self): - # This field should be ignored - self.post_data["onderwerpobject"]["klantcontact"] = "foobar" + post_data = { + "klantcontact": KlantContactDataFactory.create(), + "betrokkene": BetrokkeneDataFactory.create(), + # `klantcontact` field should be ignored + "onderwerpobject": OnderwerpObjectDataFactory.create(klantcontact="foobar"), + } - response = self.client.post(self.url, self.post_data) + response = self.client.post(self.url, post_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) @@ -1911,13 +1897,15 @@ def test_create_onderwerpobject_with_was_klantcontact(self): kwargs={"uuid": str(existing_klantcontact.uuid)}, ) existing_klantcontact_url = f"http://testserver{existing_klantcontact_url}" - - # This field should be ignored - self.post_data["onderwerpobject"]["wasKlantcontact"] = { - "uuid": str(existing_klantcontact.uuid) + post_data = { + "klantcontact": KlantContactDataFactory.create(), + "betrokkene": BetrokkeneDataFactory.create(), + "onderwerpobject": OnderwerpObjectDataFactory.create( + wasKlantcontact={"uuid": str(existing_klantcontact.uuid)} + ), } - response = self.client.post(self.url, self.post_data) + response = self.client.post(self.url, post_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) @@ -1978,9 +1966,11 @@ def test_create_onderwerpobject_with_was_klantcontact(self): self.assertEqual(onderwerpobject.was_klantcontact, existing_klantcontact) def test_create_without_betrokkene_and_onderwerpobject(self): - del self.post_data["betrokkene"] - del self.post_data["onderwerpobject"] - response = self.client.post(self.url, self.post_data) + post_data = { + "klantcontact": KlantContactDataFactory.create(), + } + + response = self.client.post(self.url, post_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) diff --git a/src/openklant/components/klantinteracties/api/urls.py b/src/openklant/components/klantinteracties/api/urls.py index 1b88bf8f..78a49d38 100644 --- a/src/openklant/components/klantinteracties/api/urls.py +++ b/src/openklant/components/klantinteracties/api/urls.py @@ -18,7 +18,7 @@ ActorKlantcontactViewSet, BetrokkeneViewSet, BijlageViewSet, - KlantContactBetrokkeneOnderwerpObjectViewSet, + KlantContactConvenienceViewSet, KlantcontactViewSet, OnderwerpobjectViewSet, ) @@ -51,7 +51,7 @@ router.register("bijlagen", BijlageViewSet) router.register( "klantcontact-convenience", - KlantContactBetrokkeneOnderwerpObjectViewSet, + KlantContactConvenienceViewSet, basename="klantcontact-convenience", ) diff --git a/src/openklant/components/klantinteracties/api/viewsets/klantcontacten.py b/src/openklant/components/klantinteracties/api/viewsets/klantcontacten.py index 92524aca..b6fb194b 100644 --- a/src/openklant/components/klantinteracties/api/viewsets/klantcontacten.py +++ b/src/openklant/components/klantinteracties/api/viewsets/klantcontacten.py @@ -13,7 +13,7 @@ ActorKlantcontactSerializer, BetrokkeneSerializer, BijlageSerializer, - KlantContactBetrokkeneOnderwerpObjectSerializer, + KlantContactConvenienceSerializer, KlantcontactSerializer, OnderwerpobjectSerializer, ) @@ -272,14 +272,12 @@ class ActorKlantcontactViewSet(viewsets.ModelViewSet): description="Maak een KlantContact, Betrokkene en een OnderwerpObject aan.", ), ) -class KlantContactBetrokkeneOnderwerpObjectViewSet( - mixins.CreateModelMixin, viewsets.GenericViewSet -): +class KlantContactConvenienceViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet): """ Convenience endpoint om in één request een Betrokkene, KlantContact en een OnderwerpObject aan te maken. De aangemaakte objecten worden automatisch aan elkaar gekoppeld. """ - serializer_class = KlantContactBetrokkeneOnderwerpObjectSerializer + serializer_class = KlantContactConvenienceSerializer authentication_classes = (TokenAuthentication,) permission_classes = (TokenPermissions,)