def test_list_objects_without_object_permissions(self): ObjectFactory.create_batch(2) url = reverse_lazy("object-list") response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.json()), 0)
def test_delete_without_geo_headers(self): object = ObjectFactory.create(object_type=OBJECT_TYPE) url = reverse("object-detail", args=[object.uuid]) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_get_with_incorrect_get_headers(self): object = ObjectFactory.create(object_type=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_filter_registration_date_list(self): # object 1 - show object1 = ObjectFactory.create(object_type=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=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_filter_object_type(self): object = ObjectFactory.create(object_type=OBJECT_TYPE) ObjectRecordFactory.create(object=object) ObjectFactory.create(object_type=OTHER_OBJECT_TYPE) response = self.client.get(self.url, {"type": OBJECT_TYPE}) 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_retrieve_object(self, m): object = ObjectFactory.create(object_type=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])}', "type": object.object_type, "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_history_no_object_permissions(self): object = ObjectFactory.create() 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=OBJECT_TYPE) 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=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 test_destroy_with_read_only_perm(self): ObjectPermissionFactory.create(object_type=OBJECT_TYPE, mode=PermissionModes.read_only, users=[self.user]) object = ObjectFactory.create(object_type=OBJECT_TYPE) 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): ObjectPermissionFactory.create(object_type=OBJECT_TYPE, mode=PermissionModes.read_only, users=[self.user]) object = ObjectFactory.create(object_type=OBJECT_TYPE) url = reverse("object-history", args=[object.uuid]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_list_objects_limited_to_object_permission(self): ObjectPermissionFactory.create(object_type=OBJECT_TYPE, mode=PermissionModes.read_only, users=[self.user]) object = ObjectFactory.create(object_type=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_non_auth(self): object = ObjectFactory.create() urls = [ reverse("object-list"), reverse("object-detail", args=[object.uuid]) ] for url in urls: with self.subTest(url=url): response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_filter_date_detail(self): object = ObjectFactory.create(object_type=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_update_without_geo_headers(self): object = ObjectFactory.create(object_type=OBJECT_TYPE) url = reverse("object-detail", args=[object.uuid]) data = { "type": OBJECT_TYPE, "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)