def test_delete_with_external_zaak(self): zaak = f"{self.base}zaken/1c8e36be-338c-4c07-ac5e-1adf55bec04a" besluit = self._create_besluit(zaak) besluit_url = f"http://testserver{reverse(besluit)}" zaakbesluit_url = besluit._zaakbesluit_url zaaktype = besluit.besluittype.zaaktypen.get() zaaktype_url = f"http://testserver{reverse(zaaktype)}" with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.zrc, self.base) m.get(zaak, json=get_zaak_response(zaak, zaaktype_url)) m.delete(zaakbesluit_url, status_code=204) response = self.client.delete(besluit_url) self.assertEqual( response.status_code, status.HTTP_204_NO_CONTENT, response.data ) self.assertEqual(Besluit.objects.count(), 0) history_delete = [ req for req in m.request_history if req.method == "DELETE" and req.url == zaakbesluit_url ] self.assertEqual(len(history_delete), 1)
def test_zaak_delete_oio_removed(self, m): mock_service_oas_get(m, APITypes.drc, self.base) document = f"{self.base}enkelvoudiginformatieobjecten/{uuid.uuid4()}" eio_response = get_eio_response( document, informatieobjecttype="http://testserver/catalogi/api/v1/iot/dummy", ) m.get(document, json=eio_response) zaak = ZaakFactory.create() zio = ZaakInformatieObjectFactory.create( zaak=zaak, informatieobject=document, _objectinformatieobject_url=f"{self.base}_objectinformatieobjecten/{uuid.uuid4()}", ) m.delete(zio._objectinformatieobject_url, status_code=204) zaak_delete_url = get_operation_url("zaak_delete", uuid=zaak.uuid) with capture_on_commit_callbacks(execute=True): response = self.client.delete(zaak_delete_url, **ZAAK_WRITE_KWARGS) self.assertEqual( response.status_code, status.HTTP_204_NO_CONTENT, response.data ) delete_call = next((req for req in m.request_history if req.method == "DELETE")) self.assertEqual(delete_call.url, zio._objectinformatieobject_url)
def test_destroy_external_besluit(self): mock_service_oas_get(self.adapter, APITypes.zrc, "https://externe.catalogus.nl/api/v1/") self.adapter.get( self.besluit, json=get_besluit_response(self.besluit, self.besluittype, self.zaak), ) self.adapter.get(self.zaak, json=get_zaak_response(self.zaak, self.zaaktype)) self.adapter.get(self.zaaktype, json=get_zaak_response(self.catalogus, self.zaaktype)) eio = EnkelvoudigInformatieObjectFactory.create() oio = ObjectInformatieObject.objects.create( informatieobject=f"http://testserver{reverse(eio)}", besluit=self.besluit, object_type="besluit", ) url = reverse(oio) self.adapter.register_uri( "GET", self.besluit, json=get_besluit_response(self.besluit, self.besluittype), ) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(ObjectInformatieObject.objects.count(), 0)
def test_besluittype_external_iotype_external_success(self): catalogus = f"{self.base}catalogussen/1c8e36be-338c-4c07-ac5e-1adf55bec04a" besluittype = f"{self.base}besluittypen/b71f72ef-198d-44d8-af64-ae1932df830a" besluit = BesluitFactory.create(besluittype=besluittype) besluit_url = f"http://openbesluit.nl{reverse(besluit)}" informatieobjecttype = f"{self.base}informatieobjecttypen/{uuid.uuid4()}" besluittype_data = get_besluittype_response(catalogus, besluittype) besluittype_data["informatieobjecttypen"] = [informatieobjecttype] with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.drc, self.base) m.get(besluittype, json=besluittype_data) m.get( informatieobjecttype, json=get_informatieobjecttype_response(catalogus, informatieobjecttype), ) m.get( self.document, json=get_eio_response( self.document, informatieobjecttype=informatieobjecttype ), ) m.post( f"{self.base}objectinformatieobjecten", json=get_oio_response(self.document, besluit_url), status_code=201, ) response = self.client.post( self.list_url, {"besluit": besluit_url, "informatieobject": self.document}, HTTP_HOST="openbesluit.nl", ) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_update_external_zaak_fail_delete_zaakbesluit(self): zaak_old = f"{self.base}zaken/{uuid.uuid4()}" besluit = self._create_besluit(zaak_old) zaaktype = besluit.besluittype.zaaktypen.get() zaaktype_url = f"http://testserver{reverse(zaaktype)}" old_zaakbesluit_url = besluit._zaakbesluit_url # update zaak in the besluit zaak_new = f"{self.base}zaken/{uuid.uuid4()}" zaakbesluit_new_data = get_zaakbesluit_response(zaak_new) besluit_url = f"http://testserver{reverse(besluit)}" with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.zrc, self.base) m.get(zaak_old, json=get_zaak_response(zaak_old, zaaktype_url)) m.get(zaak_new, json=get_zaak_response(zaak_new, zaaktype_url)) m.delete(old_zaakbesluit_url, status_code=404, text="not found") m.post(f"{zaak_new}/besluiten", json=zaakbesluit_new_data, status_code=201) response = self.client.patch(besluit_url, {"zaak": zaak_new}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data) error = get_validation_errors(response, "zaak") self.assertEqual(error["code"], "pending-relations")
def create_zaak(self, **kwargs): from openzaak.components.zaken.tests.factories import ZaakFactory from openzaak.tests.utils import mock_service_oas_get zaak = ZaakFactory.create(**kwargs) mock_service_oas_get(self.adapter, APITypes.zrc, self.base_zaak) mock_service_oas_get(self.adapter, APITypes.ztc, self.base_zaaktype) if kwargs.get("zaaktype") is not None and isinstance( kwargs.get("zaaktype"), str): zaaktype_url = kwargs.get("zaaktype") zaaktype_identificatie = zaaktype_url.split("/")[-1] else: zaaktype_url = make_absolute_uri(reverse(zaak.zaaktype)) zaaktype_identificatie = zaak.zaaktype.identificatie self.adapter.get( make_absolute_uri(reverse(zaak)), json={ "url": make_absolute_uri(reverse(zaak)), "identificatie": zaak.identificatie, "zaaktype": zaaktype_url, }, ) self.adapter.get( zaaktype_url, json={ "url": zaaktype_url, "identificatie": zaaktype_identificatie, "omschrijving": "Melding Openbare Ruimte", }, ) return zaak
def _create_zaak(self, **headers): base_zaak = "http://testserver/zaken/api/v1/" base_zaaktype = "http://testserver/catalogi/api/v1/" Service.objects.create( api_type=APITypes.zrc, api_root=base_zaak, label="external zaken", auth_type=AuthTypes.no_auth, ) Service.objects.create( api_type=APITypes.ztc, api_root=base_zaaktype, label="external zaaktypen", auth_type=AuthTypes.no_auth, ) mock_service_oas_get(self.adapter, APITypes.zrc, base_zaak) mock_service_oas_get(self.adapter, APITypes.ztc, base_zaaktype) url = reverse(Zaak) zaaktype = ZaakTypeFactory.create(concept=False) zaaktype_url = f"http://testserver{reverse(zaaktype)}" zaak_data = { "zaaktype": zaaktype_url, "vertrouwelijkheidaanduiding": VertrouwelijkheidsAanduiding.openbaar, "bronorganisatie": "517439943", "verantwoordelijkeOrganisatie": "517439943", "registratiedatum": "2018-12-24", "startdatum": "2018-12-24", "productenOfDiensten": ["https://example.com/product/123"], } response = self.client.post(url, zaak_data, **ZAAK_WRITE_KWARGS, **headers) self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Mocking calls for the CMIS adapter, for when it will create a Oio zaak = Zaak.objects.get() zaak_url = f"http://testserver{reverse(zaak)}" self.adapter.get( zaak_url, json={ "url": zaak_url, "identificatie": zaak.identificatie, "zaaktype": zaaktype_url, }, ) self.adapter.get( zaaktype_url, json={ "url": zaaktype_url, "identificatie": zaak.zaaktype.identificatie, "omschrijving": "Melding Openbare Ruimte", }, ) return response.data
def test_destroy_with_external_informatieobject_fail_send(self): oio = f"{self.base}objectinformatieobjecten/{uuid.uuid4()}" informatieobjecttype = InformatieObjectTypeFactory.create() with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.drc, self.base) m.get( self.document, json=get_eio_response( self.document, informatieobjecttype=f"http://openzaak.nl{reverse(informatieobjecttype)}", ), ) m.delete(oio, status_code=404, text="Not found") bio = BesluitInformatieObjectFactory.create( informatieobject=self.document, _objectinformatieobject_url=oio ) bio_url = reverse(bio) response = self.client.delete(bio_url, HTTP_HOST="openzaak.nl") self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) error = get_validation_errors(response, "informatieobject") self.assertEqual(error["code"], "pending-relations")
def test_create_external_besluit(self): mock_service_oas_get(self.adapter, APITypes.zrc, "https://externe.catalogus.nl/api/v1/") self.adapter.get( self.besluit, json=get_besluit_response(self.besluit, self.besluittype, self.zaak), ) self.adapter.get(self.zaak, json=get_zaak_response(self.zaak, self.zaaktype)) self.adapter.get(self.zaaktype, json=get_zaak_response(self.catalogus, self.zaaktype)) # The test eio = EnkelvoudigInformatieObjectFactory.create() eio_path = reverse(eio) eio_url = f"http://testserver{eio_path}" response = self.client.post( self.list_url, { "object": self.besluit, "informatieobject": eio_url, "objectType": "besluit", }, ) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) oio = ObjectInformatieObject.objects.get() self.assertEqual(oio.get_informatieobject_url(), eio_url) self.assertEqual(oio.object, self.besluit)
def test_create_fail_not_unique(self): mock_service_oas_get(self.adapter, APITypes.zrc, "https://externe.catalogus.nl/api/v1/") self.adapter.get( self.besluit, json=get_besluit_response(self.besluit, self.besluittype, self.zaak), ) self.adapter.get(self.zaak, json=get_zaak_response(self.zaak, self.zaaktype)) self.adapter.get(self.zaaktype, json=get_zaak_response(self.catalogus, self.zaaktype)) # The test eio = EnkelvoudigInformatieObjectFactory.create() eio_url = f"http://testserver{reverse(eio)}" ObjectInformatieObject.objects.create(informatieobject=eio_url, besluit=self.besluit, object_type="besluit") response = self.client.post( self.list_url, { "object": self.besluit, "informatieobject": eio_url, "objectType": "besluit", }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) error = get_validation_errors(response, "nonFieldErrors") self.assertEqual(error["code"], "unique")
def test_read_external_besluit(self): mock_service_oas_get(self.adapter, APITypes.zrc, "https://externe.catalogus.nl/api/v1/") self.adapter.get( self.besluit, json=get_besluit_response(self.besluit, self.besluittype, self.zaak), ) self.adapter.get(self.zaak, json=get_zaak_response(self.zaak, self.zaaktype)) self.adapter.get(self.zaaktype, json=get_zaak_response(self.catalogus, self.zaaktype)) # The test eio = EnkelvoudigInformatieObjectFactory.create() oio = ObjectInformatieObject.objects.create( informatieobject=f"http://testserver{reverse(eio)}", besluit=self.besluit, object_type="besluit", ) url = reverse(oio) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual(data["object"], self.besluit) self.assertEqual(data["informatieobject"], f"http://testserver{reverse(eio)}")
def test_destroy_with_external_informatieobject(self): oio = f"{self.base}objectinformatieobjecten/{uuid.uuid4()}" informatieobjecttype = InformatieObjectTypeFactory.create() with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.drc, self.base) m.get( self.document, json=get_eio_response( self.document, informatieobjecttype=f"http://openzaak.nl{reverse(informatieobjecttype)}", ), ) m.delete(oio, status_code=204) bio = BesluitInformatieObjectFactory.create( informatieobject=self.document, _objectinformatieobject_url=oio ) bio_url = reverse(bio) response = self.client.delete(bio_url, HTTP_HOST="openzaak.nl") self.assertEqual( response.status_code, status.HTTP_204_NO_CONTENT, response.data ) self.assertEqual(BesluitInformatieObject.objects.count(), 0) history_delete = [ req for req in m.request_history if req.method == "DELETE" and req.url == oio ] self.assertEqual(len(history_delete), 1)
def test_create_with_external_zaak_fail_create_zaakbesluit(self): besluittype = BesluitTypeFactory.create(concept=False) zaaktype = ZaakTypeFactory.create(concept=False, catalogus=besluittype.catalogus) zaaktype.besluittypen.add(besluittype) zaaktype_url = f"http://testserver{reverse(zaaktype)}" zaak = f"{self.base}zaken/1c8e36be-338c-4c07-ac5e-1adf55bec04a" url = get_operation_url("besluit_create") with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.zrc, self.base) m.get(zaak, json=get_zaak_response(zaak, zaaktype_url)) m.post(f"{zaak}/besluiten", status_code=404, text="Not Found") response = self.client.post( url, { "verantwoordelijke_organisatie": "517439943", "identificatie": "123123", "besluittype": f"http://testserver{reverse(besluittype)}", "datum": "2018-09-06", "toelichting": "Vergunning verleend.", "ingangsdatum": "2018-10-01", "vervaldatum": "2018-11-01", "vervalreden": VervalRedenen.tijdelijk, "zaak": zaak, }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data) error = get_validation_errors(response, "zaak") self.assertEqual(error["code"], "pending-relations")
def create_besluit(self, **kwargs): from openzaak.components.besluiten.tests.factories import BesluitFactory from openzaak.tests.utils import mock_service_oas_get zaak = self.create_zaak() besluit = BesluitFactory.create(zaak=zaak, **kwargs) mock_service_oas_get(self.adapter, APITypes.brc, self.base_besluit) self.adapter.get( make_absolute_uri(reverse(besluit)), json={"zaak": make_absolute_uri(reverse(zaak))}, ) return besluit
def test_retrieve_oio_no_url_mapping(self): config = CMISConfig.get_solo() UrlMapping.objects.create( long_pattern="https://externe.catalogus.nl", short_pattern="https://xcat.nl", config=config, ) Service.objects.create( api_type=APITypes.zrc, api_root="https://externe.catalogus.nl/api/v1/", label="external zaken", auth_type=AuthTypes.no_auth, ) zaak = "https://externe.catalogus.nl/api/v1/zaken/1c8e36be-338c-4c07-ac5e-1adf55bec04a" zaaktype = "https://externe.catalogus.nl/api/v1/zaaktypen/b71f72ef-198d-44d8-af64-ae1932df830a" catalogus = "https://externe.catalogus.nl/api/v1/catalogussen/5c4c492b-3548-4258-b17f-0e2e31dcfe25" mock_service_oas_get(self.adapter, APITypes.zrc, "https://externe.catalogus.nl/api/v1/") self.adapter.get(zaak, json=get_zaak_response(zaak, zaaktype)) self.adapter.get(zaaktype, json=get_zaak_response(catalogus, zaaktype)) eio1 = EnkelvoudigInformatieObjectFactory.create() oio1 = ObjectInformatieObject.objects.create( informatieobject=f"http://testserver{reverse(eio1)}", zaak=zaak, object_type="zaak", ) eio2 = EnkelvoudigInformatieObjectFactory.create() ObjectInformatieObject.objects.create( informatieobject=f"http://testserver{reverse(eio2)}", zaak=zaak, object_type="zaak", ) # Remove all available mappings UrlMapping.objects.all().delete() response = self.client.get(reverse(oio1)) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data) self.assertEqual( response.data["detail"], "CMIS-adapter could not shrink one of the URL fields.", )
def test_bio_visibility_outside_transaction(self): self.setUpTestData() document = f"{self.base}enkelvoudiginformatieobjecten/{uuid.uuid4()}" besluit = BesluitFactory.create(besluittype__concept=False) besluit_url = f"http://openzaak.nl{reverse(besluit)}" informatieobjecttype = InformatieObjectTypeFactory.create( catalogus=besluit.besluittype.catalogus, concept=False ) informatieobjecttype_url = f"http://openzaak.nl{reverse(informatieobjecttype)}" informatieobjecttype.besluittypen.add(besluit.besluittype) eio_response = get_eio_response( document, informatieobjecttype=informatieobjecttype_url ) def worker(besluit_id: int): """ Worker to run in a thread. When this worker runs, there should be one ZIO visible in the database - it must have been committed so that the remote DRC can view this record too. """ bios = BesluitInformatieObject.objects.filter(besluit__id=besluit_id) self.assertEqual(bios.count(), 1) close_old_connections() def mock_create_remote_oio(*args, **kwargs): with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: future = executor.submit(worker, besluit.id) future.result() # raises any exceptions, such as assertionerrors return {"url": f"{self.base}objectinformatieobjecten/{uuid.uuid4()}"} with patch( "openzaak.components.besluiten.api.serializers.create_remote_oio", side_effect=mock_create_remote_oio, ): with requests_mock.Mocker() as m: mock_service_oas_get( m, APITypes.drc, self.base, oas_url=settings.DRC_API_SPEC ) m.get(document, json=eio_response) response = self.client.post( self.list_url, {"besluit": besluit_url, "informatieobject": document}, ) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
def test_list_filter_by_external_besluit(self): eio = EnkelvoudigInformatieObjectFactory.create() mock_service_oas_get(self.adapter, APITypes.zrc, "https://externe.catalogus.nl/api/v1/") # Needed for the CMIS adapter zaak1 = "https://externe.catalogus.nl/api/v1/zaken/1c8e36be-338c-4c07-ac5e-1adf55bec04a" zaak2 = "https://externe.catalogus.nl/api/v1/zaken/b923543f-97aa-4a55-8c20-889b5906cf75" zaaktype1 = "https://externe.catalogus.nl/api/v1/zaaktypen/b71f72ef-198d-44d8-af64-ae1932df830a" zaaktype2 = "https://externe.catalogus.nl/api/v1/zaaktypen/5c4c492b-3548-4258-b17f-0e2e31dcfe25" catalogus1 = "https://externe.catalogus.nl/api/v1/catalogussen/5c4c492b-3548-4258-b17f-0e2e31dcfe25" catalogus2 = "https://externe.catalogus.nl/api/v1/catalogussen/a8e03e86-152d-4e8c-83fc-047645cfc585" besluit1 = "https://externe.catalogus.nl/api/v1/besluiten/1c8e36be-338c-4c07-ac5e-1adf55bec04a" besluit2 = "https://externe.catalogus.nl/api/v1/besluiten/b923543f-97aa-4a55-8c20-889b5906cf75" besluittype1 = "https://externe.catalogus.nl/api/v1/besluittypen/b71f72ef-198d-44d8-af64-ae1932df830a" besluittype2 = "https://externe.catalogus.nl/api/v1/besluittypen/3665b9be-6ac5-4075-8736-d79598e5325c" self.adapter.get(zaak1, json=get_zaak_response(zaak1, zaaktype1)) self.adapter.get(zaaktype1, json=get_zaak_response(catalogus1, zaaktype1)) self.adapter.get(zaak2, json=get_zaak_response(zaak2, zaaktype2)) self.adapter.get(zaaktype2, json=get_zaak_response(catalogus2, zaaktype2)) self.adapter.get(besluit1, json=get_besluit_response(besluit1, besluittype1, zaak1)) self.adapter.get(besluit2, json=get_besluit_response(besluit2, besluittype2, zaak2)) ObjectInformatieObject.objects.create( informatieobject=f"http://testserver{reverse(eio)}", besluit=besluit1, object_type="besluit", ) ObjectInformatieObject.objects.create( informatieobject=f"http://testserver{reverse(eio)}", besluit=besluit2, object_type="besluit", ) url = reverse(ObjectInformatieObject) response = self.client.get(url, {"object": besluit2}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]["object"], besluit2)
def test_filter_by_external_informatieobject(self): base = "https://external.documenten.nl/api/v1/" document = f"{base}enkelvoudiginformatieobjecten/{uuid.uuid4()}" Service.objects.create( api_type=APITypes.drc, api_root=base, label="external documents", auth_type=AuthTypes.no_auth, ) zio_type = ZaakTypeInformatieObjectTypeFactory.create( informatieobjecttype__concept=False, zaaktype__concept=False) zaak = ZaakFactory.create(zaaktype=zio_type.zaaktype) zaak_url = f"http://openzaak.nl{reverse(zaak)}" eio_response = get_eio_response( document, informatieobjecttype= f"http://openzaak.nl{reverse(zio_type.informatieobjecttype)}", ) with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.drc, base) m.get(document, json=eio_response) m.post( "https://external.documenten.nl/api/v1/objectinformatieobjecten", json=get_oio_response(document, zaak_url), status_code=201, ) response = self.client.post( reverse(ZaakInformatieObject), { "zaak": zaak_url, "informatieobject": document }, HTTP_HOST="openzaak.nl", ) io_url = response.data["informatieobject"] zio_list_url = reverse("zaakinformatieobject-list") response = self.client.get(zio_list_url, {"informatieobject": io_url}, HTTP_HOST="openzaak.nl") self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]["informatieobject"], io_url)
def test_update_external_zaak(self): zaak_old = f"{self.base}zaken/{uuid.uuid4()}" besluit = self._create_besluit(zaak_old) zaaktype = besluit.besluittype.zaaktypen.get() zaaktype_url = f"http://testserver{reverse(zaaktype)}" old_zaakbesluit_url = besluit._zaakbesluit_url # update zaak in the besluit zaak_new = f"{self.base}zaken/{uuid.uuid4()}" zaakbesluit_new_data = get_zaakbesluit_response(zaak_new) besluit_url = f"http://testserver{reverse(besluit)}" with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.zrc, self.base) m.get(zaak_old, json=get_zaak_response(zaak_old, zaaktype_url)) m.get(zaak_new, json=get_zaak_response(zaak_new, zaaktype_url)) m.delete(old_zaakbesluit_url, status_code=204) m.post(f"{zaak_new}/besluiten", json=zaakbesluit_new_data, status_code=201) response = self.client.patch(besluit_url, {"zaak": zaak_new}) self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) besluit.refresh_from_db() self.assertEqual(besluit._previous_zaak_url, zaak_old) self.assertEqual(besluit._zaak_url, zaak_new) self.assertEqual(besluit._zaakbesluit_url, zaakbesluit_new_data["url"]) history_post = [ req for req in m.request_history if req.method == "POST" and req.url == f"{zaak_new}/besluiten" ] self.assertEqual(len(history_post), 1) self.assertEqual(history_post[0].json(), {"besluit": besluit_url}) history_delete = [ req for req in m.request_history if req.method == "DELETE" and req.url == old_zaakbesluit_url ] self.assertEqual(len(history_delete), 1)
def setUp(self): from openzaak.tests.utils import mock_service_oas_get super().setUp() mocker = getattr(self, self.mocker_attr) mock_service_oas_get(mocker, "brc", oas_url=settings.BRC_API_SPEC) mock_service_oas_get(mocker, "drc", oas_url=settings.DRC_API_SPEC) mock_service_oas_get(mocker, "zrc", oas_url=settings.ZRC_API_SPEC) mock_service_oas_get(mocker, "ztc", oas_url=settings.ZTC_API_SPEC)
def test_create_with_external_zaak(self): zaak = f"{self.base}zaken/1c8e36be-338c-4c07-ac5e-1adf55bec04a" besluittype = BesluitTypeFactory.create(concept=False) zaaktype = ZaakTypeFactory.create( concept=False, catalogus=besluittype.catalogus ) zaaktype.besluittypen.add(besluittype) zaaktype_url = f"http://testserver{reverse(zaaktype)}" zaakbesluit_data = get_zaakbesluit_response(zaak) url = get_operation_url("besluit_create") with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.zrc, self.base) m.get(zaak, json=get_zaak_response(zaak, zaaktype_url)) m.post(f"{zaak}/besluiten", json=zaakbesluit_data, status_code=201) response = self.client.post( url, { "verantwoordelijke_organisatie": "517439943", "identificatie": "123123", "besluittype": f"http://testserver{reverse(besluittype)}", "datum": "2018-09-06", "toelichting": "Vergunning verleend.", "ingangsdatum": "2018-10-01", "vervaldatum": "2018-11-01", "vervalreden": VervalRedenen.tijdelijk, "zaak": zaak, }, ) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) besluit = Besluit.objects.get() besluit_url = f"http://testserver{reverse(besluit)}" self.assertEqual(besluit._zaakbesluit_url, zaakbesluit_data["url"]) history_post = [ req for req in m.request_history if req.method == "POST" and req.url == f"{zaak}/besluiten" ] self.assertEqual(len(history_post), 1) self.assertEqual(history_post[0].json(), {"besluit": besluit_url})
def create_besluit_without_zaak(self, **kwargs): from openzaak.components.besluiten.tests.factories import BesluitFactory from openzaak.tests.utils import mock_service_oas_get besluit = BesluitFactory.create(**kwargs) mock_service_oas_get(self.adapter, APITypes.brc, self.base_besluit) self.adapter.get( make_absolute_uri(reverse(besluit)), json={ "verantwoordelijke_organisatie": "517439943", "identificatie": "123123", "besluittype": "http://testserver/besluittype/some-random-id", "datum": "2018-09-06", "toelichting": "Vergunning verleend.", "ingangsdatum": "2018-10-01", "vervaldatum": "2018-11-01", }, ) return besluit
def test_delete_with_external_zaak_fail_delete_zaakbesluit(self): zaak = f"{self.base}zaken/1c8e36be-338c-4c07-ac5e-1adf55bec04a" besluit = self._create_besluit(zaak) besluit_url = f"http://testserver{reverse(besluit)}" zaakbesluit_url = besluit._zaakbesluit_url zaaktype = besluit.besluittype.zaaktypen.get() zaaktype_url = f"http://testserver{reverse(zaaktype)}" with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.zrc, self.base) m.get(zaak, json=get_zaak_response(zaak, zaaktype_url)) m.delete(zaakbesluit_url, status_code=404, text="Not found") response = self.client.delete(besluit_url) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data) error = get_validation_errors(response, "zaak") self.assertEqual(error["code"], "pending-relations")
def _mock_zaak(self): mock_service_oas_get(self.adapter, APITypes.zrc, self.base_zaak) mock_service_oas_get(self.adapter, APITypes.ztc, self.base_zaaktype) self.adapter.get( make_absolute_uri(reverse(self.zaak)), json={ "url": make_absolute_uri(reverse(self.zaak)), "identificatie": self.zaak.identificatie, "zaaktype": make_absolute_uri(reverse(self.zaak.zaaktype)), }, ) self.adapter.get( make_absolute_uri(reverse(self.zaak.zaaktype)), json={ "url": make_absolute_uri(reverse(self.zaak.zaaktype)), "identificatie": self.zaak.zaaktype.identificatie, "omschrijving": "Melding Openbare Ruimte", }, )
def test_update_zio_metadata(self, m): mock_service_oas_get(m, APITypes.drc, self.base, oas_url=settings.DRC_API_SPEC) document = f"{self.base}enkelvoudiginformatieobjecten/{uuid.uuid4()}" zio_type = ZaakTypeInformatieObjectTypeFactory.create( informatieobjecttype__concept=False, zaaktype__concept=False) zaak = ZaakFactory.create(zaaktype=zio_type.zaaktype) zaak_url = f"http://openzaak.nl{reverse(zaak)}" eio_response = get_eio_response( document, informatieobjecttype= f"http://testserver{reverse(zio_type.informatieobjecttype)}", ) m.get(document, json=eio_response) zio = ZaakInformatieObjectFactory.create(zaak=zaak, informatieobject=document) zio_detail_url = reverse(zio) response = self.client.put( zio_detail_url, { "zaak": zaak_url, "informatieobject": document, "titel": "updated title", "beschrijving": "same", }, ) self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.assertEqual(response.data["titel"], "updated title") self.assertEqual(response.data["beschrijving"], "same") zio.refresh_from_db() self.assertEqual(zio.titel, "updated title") self.assertEqual(zio.beschrijving, "same")
def test_correct_credentials_used(self): svc, _ = Service.objects.update_or_create( api_root="https://open-notificaties.local/api/v1/", defaults=dict( label="NRC", api_type=APITypes.nrc, client_id="some-client-id", secret="some-secret", auth_type=AuthTypes.zgw, ), ) config = NotificationsConfig.get_solo() config.api_root = svc.api_root config.save() with requests_mock.Mocker() as m: mock_service_oas_get(m, "nrc", url=svc.api_root) m.get("https://open-notificaties.local/api/v1/kanaal?naam=zaken", json=[]) m.post("https://open-notificaties.local/api/v1/kanaal", status_code=201) call_command("register_kanaal", kanaal="zaken") # check for auth in the calls for request in m.request_history[1:]: with self.subTest(method=request.method, url=request.url): self.assertIn("Authorization", request.headers) token = request.headers["Authorization"].split(" ")[1] try: jwt.decode(token, key="some-secret", algorithms=["HS256"]) except Exception as exc: self.fail( "Not a vaid JWT in Authorization header: %s" % exc)
def test_us162_voeg_besluit_toe_aan_zaak(self): self.create_zaak_besluit_services() zaak = self.create_zaak(zaaktype__concept=False) zaak_url = reverse(zaak) besluittype = BesluitTypeFactory.create(concept=False) besluittype_url = reverse(besluittype) besluittype.zaaktypen.add(zaak.zaaktype) io = EnkelvoudigInformatieObjectFactory.create( informatieobjecttype__concept=False ) io_url = reverse(io) self.adapter.get(io_url, json=serialise_eio(io, io_url)) besluittype.informatieobjecttypen.add(io.informatieobjecttype) # Mocking the besluit besluit_data = { "verantwoordelijke_organisatie": "517439943", # RSIN "identificatie": "123123", "besluittype": f"http://testserver{besluittype_url}", "zaak": f"http://testserver{zaak_url}", "datum": "2018-09-06", "toelichting": "Vergunning verleend.", "ingangsdatum": "2018-10-01", "vervaldatum": "2018-11-01", "vervalreden": VervalRedenen.tijdelijk, } mock_service_oas_get(self.adapter, APITypes.brc, self.base_besluit) matcher = re.compile("besluiten/api/v1/.+?-.+?-.+?-.+?-.+?") self.adapter.register_uri("GET", matcher, json=besluit_data) with self.subTest(part="besluit_create"): url = get_operation_url("besluit_create") response = self.client.post(url, besluit_data,) self.assertEqual( response.status_code, status.HTTP_201_CREATED, response.data ) self.assertResponseTypes( response.data, ( ("url", str), ("identificatie", str), ("verantwoordelijke_organisatie", str), ("besluittype", str), ("zaak", str), ("datum", str), ("toelichting", str), ("bestuursorgaan", str), ("ingangsdatum", str), ("vervaldatum", str), ("vervalreden", str), ("publicatiedatum", type(None)), ("verzenddatum", type(None)), ("uiterlijke_reactiedatum", type(None)), ), ) self.assertEqual(Besluit.objects.count(), 1) besluit = Besluit.objects.get() self.assertEqual(besluit.verantwoordelijke_organisatie, "517439943") self.assertEqual(besluit.besluittype, besluittype) self.assertEqual(besluit.zaak, zaak) self.assertEqual(besluit.datum, date(2018, 9, 6)) self.assertEqual(besluit.toelichting, "Vergunning verleend.") self.assertEqual(besluit.ingangsdatum, date(2018, 10, 1)) self.assertEqual(besluit.vervaldatum, date(2018, 11, 1)) self.assertEqual(besluit.vervalreden, VervalRedenen.tijdelijk) with self.subTest(part="besluitinformatieobject_create"): url = get_operation_url("besluitinformatieobject_create") response = self.client.post( url, { "besluit": f"http://testserver{reverse(besluit)}", "informatieobject": f"http://testserver{io_url}", }, ) self.assertEqual( response.status_code, status.HTTP_201_CREATED, response.data ) self.assertResponseTypes( response.data, (("url", str), ("informatieobject", str)) ) self.assertEqual(besluit.besluitinformatieobject_set.count(), 1) self.assertEqual( besluit.besluitinformatieobject_set.get().informatieobject.uuid, io.uuid, )
def test_relate_external_document(self): document = f"{self.base}enkelvoudiginformatieobjecten/{uuid.uuid4()}" zio_type = ZaakTypeInformatieObjectTypeFactory.create( informatieobjecttype__concept=False, zaaktype__concept=False) zaak = ZaakFactory.create(zaaktype=zio_type.zaaktype) zaak_url = f"http://openzaak.nl{reverse(zaak)}" eio_response = get_eio_response( document, informatieobjecttype= f"http://testserver{reverse(zio_type.informatieobjecttype)}", ) oio_response = get_oio_response(document, zaak_url) with self.subTest(section="zio-create"): with requests_mock.Mocker() as m: mock_service_oas_get(m, APITypes.drc, self.base) m.get(document, json=eio_response) m.post( "https://external.documenten.nl/api/v1/objectinformatieobjecten", json=oio_response, status_code=201, ) response = self.client.post( reverse(ZaakInformatieObject), { "zaak": zaak_url, "informatieobject": document }, ) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) posts = [req for req in m.request_history if req.method == "POST"] self.assertEqual(len(posts), 1) request = posts[0] self.assertEqual( request.url, "https://external.documenten.nl/api/v1/objectinformatieobjecten", ) self.assertEqual( request.json(), { "informatieobject": document, "object": zaak_url, "objectType": "zaak", }, ) self.assertFalse(ObjectInformatieObject.objects.exists()) zio = ZaakInformatieObject.objects.get() self.assertEqual(zio._objectinformatieobject_url, oio_response["url"]) with self.subTest(section="zio-list"): list_response = self.client.get( reverse(ZaakInformatieObject), {"zaak": zaak_url}, HTTP_HOST="openzaak.nl", ) self.assertEqual(list_response.status_code, status.HTTP_200_OK) data = list_response.json() self.assertEqual(len(data), 1) self.assertEqual(data[0]["informatieobject"], document)
def test_zio_visibility_outside_transaction(self): """ Test that the ZIO being created is visible to an external DRC. This is a regression test for #819 - because of the DB transaction wrapping, API calls requesting ZIO don't actually see the object yet, leading to validation errors. We simulate this by using threading to get a different DB connection, outside of the possible current transaction. """ document = f"{self.base}enkelvoudiginformatieobjecten/{uuid.uuid4()}" zio_type = ZaakTypeInformatieObjectTypeFactory.create( informatieobjecttype__concept=False, zaaktype__concept=False) zaak = ZaakFactory.create(zaaktype=zio_type.zaaktype) zaak_url = f"http://openzaak.nl{reverse(zaak)}" eio_response = get_eio_response( document, informatieobjecttype= f"http://testserver{reverse(zio_type.informatieobjecttype)}", ) def worker(zaak_id: int): """ Worker to run in a thread. When this worker runs, there should be one ZIO visible in the database - it must have been committed so that the remote DRC can view this record too. """ zios = ZaakInformatieObject.objects.filter(zaak__id=zaak_id) self.assertEqual(zios.count(), 1) close_old_connections() def mock_create_remote_oio(*args, **kwargs): with concurrent.futures.ThreadPoolExecutor( max_workers=1) as executor: future = executor.submit(worker, zaak.id) future.result( ) # raises any exceptions, such as assertionerrors return { "url": f"{self.base}objectinformatieobjecten/{uuid.uuid4()}" } with patch( "openzaak.components.zaken.api.serializers.zaken.create_remote_oio", side_effect=mock_create_remote_oio, ): with requests_mock.Mocker() as m: mock_service_oas_get(m, APITypes.drc, self.base, oas_url=settings.DRC_API_SPEC) m.get(document, json=eio_response) response = self.client.post( reverse(ZaakInformatieObject), { "zaak": zaak_url, "informatieobject": document }, ) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_create_bio_external_document(self): document = f"{self.base}enkelvoudiginformatieobjecten/{uuid.uuid4()}" besluit = BesluitFactory.create(besluittype__concept=False) besluit_url = f"http://openzaak.nl{reverse(besluit)}" informatieobjecttype = InformatieObjectTypeFactory.create( catalogus=besluit.besluittype.catalogus, concept=False ) informatieobjecttype_url = f"http://openzaak.nl{reverse(informatieobjecttype)}" informatieobjecttype.besluittypen.add(besluit.besluittype) eio_response = get_eio_response( document, informatieobjecttype=informatieobjecttype_url ) oio_response = get_oio_response(document, besluit_url, "besluit") with self.subTest(section="bio-create"): with requests_mock.Mocker(real_http=True) as m: mock_service_oas_get(m, APITypes.drc, self.base) m.get(document, json=eio_response) m.post( "https://external.documenten.nl/api/v1/objectinformatieobjecten", json=oio_response, status_code=201, ) response = self.client.post( self.list_url, {"besluit": besluit_url, "informatieobject": document}, ) self.assertEqual( response.status_code, status.HTTP_201_CREATED, response.data ) posts = [req for req in m.request_history if req.method == "POST"] self.assertEqual(len(posts), 1) request = posts[0] self.assertEqual( request.url, "https://external.documenten.nl/api/v1/objectinformatieobjecten", ) self.assertEqual( request.json(), { "informatieobject": document, "object": besluit_url, "objectType": "besluit", }, ) self.assertFalse(ObjectInformatieObject.objects.exists()) bio = BesluitInformatieObject.objects.get() self.assertEqual(bio._objectinformatieobject_url, oio_response["url"]) with self.subTest(section="bio-list"): list_response = self.client.get( self.list_url, {"besluit": besluit_url}, HTTP_HOST="openzaak.nl", ) self.assertEqual( list_response.status_code, status.HTTP_200_OK, response.data ) data = list_response.json() self.assertEqual(len(data), 1) self.assertEqual(data[0]["informatieobject"], document)