def test_list_contactmomenten(self): list_url = reverse(ContactMoment) ContactMomentFactory.create_batch(2) response = self.client.get(list_url) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual(len(data["results"]), 2)
def test_pagination_page_param(self): ContactMomentFactory.create_batch(2) url = reverse(ContactMoment) response = self.client.get(url, {"page": 1}) self.assertEqual(response.status_code, status.HTTP_200_OK) response_data = response.json() self.assertEqual(response_data["count"], 2) self.assertIsNone(response_data["previous"]) self.assertIsNone(response_data["next"])
def test_filter_voorkeurstaal(self): ContactMomentFactory.create(voorkeurstaal="nld") ContactMomentFactory.create(voorkeurstaal="eng") response = self.client.get( self.list_url, {"voorkeurstaal": "nld"}, HTTP_HOST="testserver.com", ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data["results"]), 1) self.assertEqual( response.data["results"][0]["voorkeurstaal"], "nld", )
def test_create_objectcontactmoment(self, *mocks): contactmoment = ContactMomentFactory.create() contactmoment_url = reverse(contactmoment) list_url = reverse(ObjectContactMoment) data = { "contactmoment": contactmoment_url, "objectType": ObjectTypes.zaak, "object": ZAAK, } responses = { "http://example.com/api/v1/zaakcontactmomenten": [{ "url": f"https://example.com/api/v1/zaakcontactmomenten/{uuid.uuid4()}", "contactmoment": f"http://testserver/api/v1/contactmomenten/{uuid.uuid4()}", "zaak": ZAAK, }] } with mock_client(responses): response = self.client.post(list_url, data) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) objectcontactmoment = ObjectContactMoment.objects.get() self.assertEqual(objectcontactmoment.contactmoment, contactmoment) self.assertEqual(objectcontactmoment.object_type, ObjectTypes.zaak) self.assertEqual(objectcontactmoment.object, ZAAK)
def test_read_contactmoment(self): contactmoment = ContactMomentFactory.create( registratiedatum=make_aware(datetime(2019, 1, 1)), initiatiefnemer=InitiatiefNemer.gemeente, ) detail_url = reverse(contactmoment) response = self.client.get(detail_url) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual( data, { "url": f"http://testserver{detail_url}", "vorigContactmoment": None, "volgendContactmoment": None, "bronorganisatie": contactmoment.bronorganisatie, "registratiedatum": "2019-01-01T00:00:00Z", "kanaal": contactmoment.kanaal, "voorkeurskanaal": contactmoment.voorkeurskanaal, "voorkeurstaal": contactmoment.voorkeurstaal, "tekst": contactmoment.tekst, "onderwerpLinks": [], "initiatiefnemer": InitiatiefNemer.gemeente, "medewerker": contactmoment.medewerker, "medewerkerIdentificatie": None, }, )
def test_send_notif_delete_contactmoment(self, mock_client): """ Check if notifications will be send when ContactMoment is deleted """ client = mock_client.return_value contactmoment = ContactMomentFactory.create(bronorganisatie=423182687, kanaal="telephone") contactmoment_url = get_operation_url("contactmoment_delete", uuid=contactmoment.uuid) response = self.client.delete(contactmoment_url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.data) client.create.assert_called_once_with( "notificaties", { "kanaal": "contactmomenten", "hoofdObject": f"http://testserver{contactmoment_url}", "resource": "contactmoment", "resourceUrl": f"http://testserver{contactmoment_url}", "actie": "destroy", "aanmaakdatum": "2018-09-07T00:00:00Z", "kenmerken": { "bronorganisatie": "423182687", "kanaal": "telephone" }, }, )
def test_update_contactmoment_with_medewerker(self): contactmoment = ContactMomentFactory.create() detail_url = reverse(contactmoment) data = { "medewerker": "", "medewerkerIdentificatie": { "identificatie": "12345", "achternaam": "Buurman", "voorletters": "B B", }, } response = self.client.patch(detail_url, data) self.assertEqual(response.status_code, status.HTTP_200_OK) contactmoment.refresh_from_db() self.assertEqual(contactmoment.medewerker, "") medewerker = contactmoment.medewerker_identificatie self.assertEqual(medewerker.identificatie, "12345") self.assertEqual(medewerker.achternaam, "Buurman") self.assertEqual(medewerker.voorletters, "B B")
def test_filter_bronorganisatie(self): ContactMomentFactory.create(bronorganisatie="000000000") ContactMomentFactory.create(bronorganisatie="000099998") response = self.client.get( self.list_url, {"bronorganisatie": "000000000"}, HTTP_HOST="testserver.com", ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data["results"]), 1, response.data) self.assertEqual( response.data["results"][0]["bronorganisatie"], "000000000", )
def test_create_klantcontactmoment_unique_klant_contactmoment_and_rol( self): cmc = ContactMomentFactory.create( registratiedatum=make_aware(datetime(2019, 1, 1)), initiatiefnemer=InitiatiefNemer.gemeente, ) cmc_url = reverse(cmc) KlantContactMomentFactory.create( klant="http://testserver.com/klant/1", contactmoment=cmc, rol=Rol.gesprekspartner, ) list_url = reverse(KlantContactMoment) data = { "klant": "http://testserver.com/klant/1", "contactmoment": f"http://testserver{cmc_url}", "rol": Rol.gesprekspartner, } with requests_mock.Mocker() as m: m.get("http://testserver.com/klant/1", json={}) response = self.client.post(list_url, data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) error = get_validation_errors(response, "nonFieldErrors") self.assertEqual(error["code"], "unique")
def test_filter_registratiedatum_lte(self): ContactMomentFactory.create(registratiedatum="2020-01-01T12:00:00Z") ContactMomentFactory.create(registratiedatum="2019-03-02T22:00:00Z") response = self.client.get( self.list_url, {"registratiedatum__lte": "2019-03-02T22:00:00Z"}, HTTP_HOST="testserver.com", ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data["results"]), 1) self.assertEqual( response.data["results"][0]["registratiedatum"], "2019-03-02T22:00:00Z", )
def test_filter_ordering(self): ContactMomentFactory.create(kanaal="bcd") ContactMomentFactory.create(kanaal="abc") response = self.client.get( self.list_url, {"ordering": "kanaal"}, HTTP_HOST="testserver.com", ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data["results"]), 2) self.assertEqual( response.data["results"][0]["kanaal"], "abc", )
def test_filter_initiatiefnemer(self): ContactMomentFactory.create(initiatiefnemer=InitiatiefNemer.gemeente) ContactMomentFactory.create(initiatiefnemer=InitiatiefNemer.klant) response = self.client.get( self.list_url, {"initiatiefnemer": InitiatiefNemer.gemeente}, HTTP_HOST="testserver.com", ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data["results"]), 1) self.assertEqual( response.data["results"][0]["initiatiefnemer"], InitiatiefNemer.gemeente, )
def test_read_klantcontactmoment(self): cmc = ContactMomentFactory.create( registratiedatum=make_aware(datetime(2019, 1, 1)), initiatiefnemer=InitiatiefNemer.gemeente, ) cmc_url = reverse(cmc) klantcontactmoment = KlantContactMomentFactory.create( contactmoment=cmc, rol=Rol.belanghebbende, ) detail_url = reverse(klantcontactmoment) response = self.client.get(detail_url) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual( data, { "url": f"http://testserver{detail_url}", "klant": klantcontactmoment.klant, "contactmoment": f"http://testserver{cmc_url}", "rol": Rol.belanghebbende, }, )
def test_filter_kanaal(self): ContactMomentFactory.create(kanaal="kanaal1") ContactMomentFactory.create(kanaal="kanaal2") response = self.client.get( self.list_url, {"kanaal": "kanaal1"}, HTTP_HOST="testserver.com", ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data["results"]), 1) self.assertEqual( response.data["results"][0]["kanaal"], "kanaal1", )
def test_create_klantcontactmoment(self): cmc = ContactMomentFactory.create( registratiedatum=make_aware(datetime(2019, 1, 1)), initiatiefnemer=InitiatiefNemer.gemeente, ) cmc_url = reverse(cmc) list_url = reverse(KlantContactMoment) data = { "klant": "http://testserver.com/klant/1", "contactmoment": f"http://testserver{cmc_url}", "rol": Rol.gesprekspartner, } with requests_mock.Mocker() as m: m.get("http://testserver.com/klant/1", json={}) response = self.client.post(list_url, data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) klantcontactmoment = KlantContactMoment.objects.get() self.assertEqual(klantcontactmoment.klant, "http://testserver.com/klant/1") self.assertEqual(klantcontactmoment.contactmoment, cmc) self.assertEqual(klantcontactmoment.rol, Rol.gesprekspartner)
def test_destroy_contactmoment(self): contactmoment = ContactMomentFactory.create() detail_url = reverse(contactmoment) response = self.client.delete(detail_url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(ContactMoment.objects.count(), 0)
def test_filter_medewerker(self): ContactMomentFactory.create( medewerker="http://testserver.com/medewerker/1") ContactMomentFactory.create( medewerker="http://testserver.com/medewerker/2") response = self.client.get( self.list_url, {"medewerker": "http://testserver.com/medewerker/1"}, HTTP_HOST="testserver.com", ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data["results"]), 1) self.assertEqual( response.data["results"][0]["medewerker"], "http://testserver.com/medewerker/1", )
def test_cannot_read_without_correct_scope(self): contactmoment = ContactMomentFactory.create() urls = [ reverse("contactmoment-list"), reverse(contactmoment), ] for url in urls: with self.subTest(url=url): self.assertForbidden(url, method="get")
def test_update_contactmoment(self): contactmoment = ContactMomentFactory.create() detail_url = reverse(contactmoment) response = self.client.patch(detail_url, {"kanaal": "some-kanaal"}) self.assertEqual(response.status_code, status.HTTP_200_OK) contactmoment.refresh_from_db() self.assertEqual(contactmoment.kanaal, "some-kanaal")
def test_update_contactmoment_override_vorig_contactmoment(self): vorig_cmc = ContactMomentFactory.create() contactmoment = ContactMomentFactory.create( vorig_contactmoment=vorig_cmc) new_vorig_cmc = ContactMomentFactory.create() detail_url = reverse(contactmoment) data = {"vorigContactmoment": reverse(new_vorig_cmc)} response = self.client.patch(detail_url, data) self.assertEqual(response.status_code, status.HTTP_200_OK) vorig_cmc.refresh_from_db() contactmoment.refresh_from_db() new_vorig_cmc.refresh_from_db() self.assertEqual(contactmoment.vorig_contactmoment, new_vorig_cmc) self.assertEqual(new_vorig_cmc.volgend_contactmoment, contactmoment)
def test_list_contactmomenten_filter_volgend_contactmoment(self): list_url = reverse(ContactMoment) cmc1, cmc2, cmc3 = ContactMomentFactory.create_batch(3) cmc3.vorig_contactmoment = cmc2 cmc3.save() response = self.client.get( list_url, {"volgendContactmoment": f"http://testserver.com{reverse(cmc3)}"}, HTTP_HOST="testserver.com", ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data["results"]), 1) self.assertEqual( response.data["results"][0]["volgend_contactmoment"], f"http://testserver.com{reverse(cmc3)}", )
def test_create_objectcontactmoment_fail_no_remote_relation(self, *mocks): contactmoment = ContactMomentFactory.create() contactmoment_url = reverse(contactmoment) list_url = reverse(ObjectContactMoment) data = { "contactmoment": contactmoment_url, "objectType": ObjectTypes.zaak, "object": ZAAK, } responses = {"http://example.com/api/v1/zaakcontactmomenten": []} with mock_client(responses): response = self.client.post(list_url, data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) error = get_validation_errors(response, "nonFieldErrors") self.assertEqual(error["code"], "inconsistent-relation")
def test_read_objectcontactmoment(self): contactmoment = ContactMomentFactory.create() contactmoment_url = reverse(contactmoment) objectcontactmoment = ObjectContactMomentFactory.create( contactmoment=contactmoment) detail_url = reverse(objectcontactmoment) response = self.client.get(detail_url) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual( data, { "url": f"http://testserver{detail_url}", "contactmoment": f"http://testserver{contactmoment_url}", "objectType": objectcontactmoment.object_type, "object": objectcontactmoment.object, }, )
def test_create_contactmoment_vorig_contactmoment(self): vorig_cmc = ContactMomentFactory.create() list_url = reverse(ContactMoment) data = { "bronorganisatie": "423182687", "kanaal": "telephone", "tekst": "some text", "onderwerpLinks": [], "initiatiefnemer": InitiatiefNemer.gemeente, "medewerker": "http://example.com/medewerker/1", "vorigContactmoment": reverse(vorig_cmc), } response = self.client.post(list_url, data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) contactmoment = ContactMoment.objects.last() self.assertEqual(contactmoment.kanaal, "telephone") self.assertEqual(contactmoment.tekst, "some text") self.assertEqual(contactmoment.initiatiefnemer, InitiatiefNemer.gemeente) self.assertEqual(contactmoment.medewerker, "http://example.com/medewerker/1") self.assertEqual(contactmoment.vorig_contactmoment, vorig_cmc) # Check if volgendContactmoment is set correctly response = self.client.get(reverse(vorig_cmc)) self.assertEqual( response.data["volgend_contactmoment"], f"http://testserver{reverse(contactmoment)}", ) vorig_cmc.refresh_from_db() self.assertEqual(vorig_cmc.volgend_contactmoment, contactmoment)
def test_create_klantcontactmoment_klant_url_invalid(self): cmc = ContactMomentFactory.create( registratiedatum=make_aware(datetime(2019, 1, 1)), initiatiefnemer=InitiatiefNemer.gemeente, ) cmc_url = reverse(cmc) list_url = reverse(KlantContactMoment) data = { "klant": "http://testserver.com/klant/1", "contactmoment": f"http://testserver{cmc_url}", "rol": Rol.gesprekspartner, } with requests_mock.Mocker() as m: m.get("http://testserver.com/klant/1", status_code=404) response = self.client.post(list_url, data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(KlantContactMoment.objects.count(), 0) error = get_validation_errors(response, "klant") self.assertEqual(error["code"], "bad-url")
def test_read_contactmoment_with_medewerker(self): contactmoment = ContactMomentFactory.create( interactiedatum=make_aware(datetime(2019, 1, 1)), initiatiefnemer=InitiatiefNemer.gemeente, medewerker="", ) medewerker = MedewerkerFactory.create(contactmoment=contactmoment) detail_url = reverse(contactmoment) response = self.client.get(detail_url) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual( data, { "url": f"http://testserver{detail_url}", "bronorganisatie": contactmoment.bronorganisatie, "klant": contactmoment.klant, "interactiedatum": "2019-01-01T00:00:00Z", "kanaal": contactmoment.kanaal, "voorkeurskanaal": contactmoment.voorkeurskanaal, "tekst": contactmoment.tekst, "onderwerpLinks": [], "initiatiefnemer": InitiatiefNemer.gemeente, "medewerker": "", "medewerkerIdentificatie": { "identificatie": medewerker.identificatie, "achternaam": medewerker.achternaam, "voorletters": medewerker.voorletters, "voorvoegselAchternaam": medewerker.voorvoegsel_achternaam, }, }, )