def test_retrieve_with_selected_fields(self): object = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create( object=object, start_at=date.today(), geometry="POINT (4.910649523925713 52.37240093589432)", ) url = reverse("object-detail", args=[object.uuid]) response = self.client.get(url, {"fields": "url,type,record__geometry"}) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual( data, { "url": f'http://testserver{reverse("object-detail", args=[object.uuid])}', "type": object.object_type.url, "record": { "geometry": { "type": "Point", "coordinates": [4.910649523925713, 52.37240093589432], }, }, }, )
def test_filter_object_type(self): object = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create(object=object) ObjectFactory.create(object_type=self.another_object_type) response = self.client.get(self.url, {"type": self.object_type.url}) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual(len(data), 1) self.assertEqual( data[0]["url"], f"http://testserver{reverse('object-detail', args=[object.uuid])}", )
def test_get_with_incorrect_get_headers(self): object = ObjectFactory.create(object_type=self.object_type) url = reverse("object-detail", args=[object.uuid]) response = self.client.get(url, HTTP_ACCEPT_CRS="EPSG:3857") self.assertEqual(response.status_code, status.HTTP_406_NOT_ACCEPTABLE)
def test_retrieve_with_query_fields(self): PermissionFactory.create( object_type=self.object_type, mode=PermissionModes.read_only, token_auth=self.token_auth, use_fields=True, fields={"1": ["url", "type", "record"]}, ) object = ObjectFactory.create(object_type=self.object_type) record = ObjectRecordFactory.create(object=object, version=1) url = reverse("object-detail", args=[object.uuid]) response = self.client.get(url, {"fields": "url,type,record__data__name"}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.json(), { "url": f"http://testserver{url}", "type": self.object_type.url, "record": { "data": { "name": record.data["name"] } }, }, ) self.assertNotIn("x-unauthorized-fields", response._headers)
def setUpTestData(cls): super().setUpTestData() cls.object_type = ObjectTypeFactory(service__api_root=OBJECT_TYPES_API) cls.object = ObjectFactory.create(object_type=cls.object_type) cls.record_1 = ObjectRecordFactory.create( object=cls.object, data={ "geslachtsnaam": "Poepenstaart", "voorletters": "JP", "geboortedatum": "1977-08-07", "burgerlijkestaat": "ongehuwd", }, start_at=date(1977, 8, 7), end_at=date(2001, 9, 3), registration_at=date(1977, 8, 15), ) cls.record_10 = ObjectRecordFactory.create( object=cls.object, data={ "geslachtsnaam": "Berg", "voorletters": "JP", "geboortedatum": "1977-08-07", "burgerlijkestaat": "ongehuwd", }, start_at=date(2001, 9, 3), end_at=date(2001, 9, 3), registration_at=date(2001, 9, 10), ) cls.record_40 = ObjectRecordFactory.create( object=cls.object, data={ "geslachtsnaam": "Bergh", "voorvoegsel": "van den", "voorletters": "JP", "geboortedatum": "1977-08-07", "burgerlijkestaat": "ongehuwd", }, start_at=date(2001, 9, 3), end_at=date(2005, 4, 23), registration_at=date(2001, 11, 2), ) cls.record_50 = ObjectRecordFactory.create( object=cls.object, data={ "geslachtsnaam": "Bergh", "voorvoegsel": "van den", "voorletters": "JP", "geboortedatum": "1977-08-07", "burgerlijkestaat": "gehuwd", }, start_at=date(2005, 4, 23), registration_at=date(2005, 4, 25), ) PermissionFactory.create( object_type=cls.object_type, mode=PermissionModes.read_and_write, token_auth=cls.token_auth, )
def test_filter_registration_date_list(self): # object 1 - show object1 = ObjectFactory.create(object_type=self.object_type) record11 = ObjectRecordFactory.create( object=object1, registration_at="2020-01-01" ) record12 = ObjectRecordFactory.create( object=object1, registration_at="2021-01-01" ) # object 2 - don't show record21 = ObjectRecordFactory.create( object__object_type=self.object_type, registration_at="2021-01-01" ) url = reverse_lazy("object-list") response = self.client.get(url, {"registrationDate": "2020-07-01"}) data = response.json() self.assertEqual(len(data), 1) self.assertEqual( data[0]["url"], f"http://testserver{reverse('object-detail', args=[object1.uuid])}", ) self.assertEqual(data[0]["record"]["index"], record11.index)
def test_delete_without_geo_headers(self): object = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create(object=object) url = reverse("object-detail", args=[object.uuid]) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_history_no_object_permissions(self): object = ObjectFactory.create() ObjectRecordFactory.create(object=object) url = reverse("object-history", args=[object.uuid]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_get_with_geo_headers(self): object = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create(object=object) url = reverse("object-detail", args=[object.uuid]) response = self.client.get(url, **GEO_READ_KWARGS) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertResponseHasGeoHeaders(response)
def test_filter_registration_date_detail_no_record(self): object = ObjectFactory.create(object_type=self.object_type) record = ObjectRecordFactory.create(object=object, registration_at="2021-01-01") url = reverse_lazy("object-detail", args=[object.uuid]) response = self.client.get(url, {"registrationDate": "2020-07-01"}) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def setUpTestData(cls): super().setUpTestData() cls.object_type = ObjectTypeFactory(service__api_root=OBJECT_TYPES_API) cls.object = ObjectFactory.create(object_type=cls.object_type) PermissionFactory.create( object_type=cls.object_type, mode=PermissionModes.read_and_write, token_auth=cls.token_auth, )
def test_send_notif_partial_update_object(self, mocker, mock_client): """ Check if notifications will be send when Object is created """ client = mock_client.return_value mock_service_oas_get(mocker, OBJECT_TYPES_API, "objecttypes") mocker.get( f"{self.object_type.url}/versions/1", json=mock_objecttype_version(self.object_type.url), ) mocker.get(self.object_type.url, json=mock_objecttype(self.object_type.url)) obj = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create(object=obj) url = reverse("object-detail", args=[obj.uuid]) data = { "type": self.object_type.url, "record": { "typeVersion": 1, "data": { "plantDate": "2020-04-12", "diameter": 30 }, "geometry": { "type": "Point", "coordinates": [4.910649523925713, 52.37240093589432], }, "startAt": "2020-01-01", }, } with capture_on_commit_callbacks(execute=True): response = self.client.patch(url, data, **GEO_WRITE_KWARGS) self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) data = response.json() client.create.assert_called_once_with( "notificaties", { "kanaal": "objecten", "hoofdObject": data["url"], "resource": "object", "resourceUrl": data["url"], "actie": "partial_update", "aanmaakdatum": "2018-09-07T02:00:00+02:00", "kenmerken": { "objectType": self.object_type.url, }, }, )
def test_list_objects_limited_to_object_permission(self): PermissionFactory.create( object_type=self.object_type, mode=PermissionModes.read_only, token_auth=self.token_auth, ) object = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create(object=object) ObjectFactory.create() url = reverse_lazy("object-list") response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual(len(data), 1) self.assertEqual( data[0]["url"], f"http://testserver{reverse('object-detail', args=[object.uuid])}", )
def test_destroy_with_read_only_perm(self): PermissionFactory.create( object_type=self.object_type, mode=PermissionModes.read_only, token_auth=self.token_auth, ) object = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create(object=object) url = reverse("object-detail", args=[object.uuid]) response = self.client.delete(url, **GEO_WRITE_KWARGS) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_history_with_read_only_permissions(self): PermissionFactory.create( object_type=self.object_type, mode=PermissionModes.read_only, token_auth=self.token_auth, ) object = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create(object=object) url = reverse("object-history", args=[object.uuid]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_filter_date_detail(self): object = ObjectFactory.create(object_type=self.object_type) record1 = ObjectRecordFactory.create( object=object, start_at="2020-01-01", end_at="2020-12-31" ) record2 = ObjectRecordFactory.create(object=object, start_at="2021-01-01") url = reverse_lazy("object-detail", args=[object.uuid]) response = self.client.get(url, {"date": "2020-07-01"}) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual(data["record"]["index"], record1.index)
def test_fields_invalid(self): object = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create( object=object, start_at=date.today(), ) url = reverse("object-list") response = self.client.get(url, {"fields": "url,someField"}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) data = response.json() self.assertEqual( data, [ "'fields' query parameter has invalid or unauthorized values: 'someField'" ], )
def test_retrieve_incorrect_auth_fields(self): PermissionFactory.create( object_type=self.object_type, mode=PermissionModes.read_only, token_auth=self.token_auth, use_fields=True, fields={"1": ["url", "type", "some"]}, ) object = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create(object=object, version=1) url = reverse("object-detail", args=[object.uuid]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.json(), ["Fields in the configured authorization are absent in the data: 'some'"], )
def test_retrieve_no_allowed_fields(self): PermissionFactory.create( object_type=self.object_type, mode=PermissionModes.read_only, token_auth=self.token_auth, use_fields=True, fields={"2": ["url", "type", "record"]}, ) object = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create( object=object, data={"name": "some", "desc": "some desc"}, version=1 ) url = reverse("object-detail", args=[object.uuid]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.json(), {}) self.assertIn("x-unauthorized-fields", response._headers)
def test_retrieve_without_query(self): PermissionFactory.create( object_type=self.object_type, mode=PermissionModes.read_only, token_auth=self.token_auth, use_fields=True, fields={"1": ["url", "type", "record__startAt"]}, ) object = ObjectFactory.create(object_type=self.object_type) record = ObjectRecordFactory.create(object=object, data={"name": "some"}, version=1) url = reverse("object-detail", args=[object.uuid]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.json(), { "url": f"http://testserver{url}", "type": self.object_type.url, "record": { "startAt": record.start_at.isoformat() }, }, ) self.assertEqual( set(response._headers["x-unauthorized-fields"][1].split(",")), { "uuid", "record__data__name", "record__correctionFor", "record__endAt", "record__correctedBy", "record__registrationAt", "record__index", "record__geometry__coordinates", "record__geometry__type", "record__typeVersion", }, )
def test_update_without_geo_headers(self): object = ObjectFactory.create(object_type=self.object_type) url = reverse("object-detail", args=[object.uuid]) data = { "type": self.object_type.url, "record": { "typeVersion": 1, "data": { "diameter": 30 }, "startAt": "2020-01-01", }, } for method in ("put", "patch"): with self.subTest(method=method): do_request = getattr(self.client, method) response = do_request(url, data) self.assertEqual(response.status_code, status.HTTP_412_PRECONDITION_FAILED)
def test_retrieve_object(self, m): object = ObjectFactory.create(object_type=self.object_type) object_record = ObjectRecordFactory.create( object=object, start_at=date.today(), geometry="POINT (4.910649523925713 52.37240093589432)", ) url = reverse("object-detail", args=[object.uuid]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual( data, { "url": f'http://testserver{reverse("object-detail", args=[object.uuid])}', "uuid": str(object.uuid), "type": object.object_type.url, "record": { "index": object_record.index, "typeVersion": object_record.version, "data": object_record.data, "geometry": json.loads(object_record.geometry.json), "startAt": object_record.start_at.isoformat(), "endAt": object_record.end_at, "registrationAt": object_record.registration_at.isoformat(), "correctionFor": None, "correctedBy": None, }, }, )
def test_send_notif_delete_object(self, mocker, mock_client): """ Check if notifications will be send when Object is created """ client = mock_client.return_value mock_service_oas_get(mocker, OBJECT_TYPES_API, "objecttypes") mocker.get( f"{self.object_type.url}/versions/1", json=mock_objecttype_version(self.object_type.url), ) obj = ObjectFactory.create(object_type=self.object_type) ObjectRecordFactory.create(object=obj) url = reverse("object-detail", args=[obj.uuid]) full_url = f"http://testserver{url}" with capture_on_commit_callbacks(execute=True): response = self.client.delete(url, **GEO_WRITE_KWARGS) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.data) client.create.assert_called_once_with( "notificaties", { "kanaal": "objecten", "hoofdObject": full_url, "resource": "object", "resourceUrl": full_url, "actie": "destroy", "aanmaakdatum": "2018-09-07T02:00:00+02:00", "kenmerken": { "objectType": self.object_type.url, }, }, )
def setUp(self) -> None: object = ObjectFactory.create() self.urls = [ reverse("object-list"), reverse("object-detail", args=[object.uuid]), ]