def test_update_draft_save_button(self): object_type = ObjectTypeFactory.create() ObjectVersionFactory.create(object_type=object_type, status=ObjectVersionStatus.draft) url = reverse("admin:core_objecttype_change", args=[object_type.id]) get_response = self.app.get(url) save_button = get_response.html.find("input", {"name": "_save"}) self.assertIsNotNone(save_button)
def test_delete_version(self): object_type = ObjectTypeFactory.create() object_version = ObjectVersionFactory.create(object_type=object_type) url = reverse( "objectversion-detail", args=[object_type.uuid, object_version.version] ) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(ObjectVersion.objects.count(), 0)
def test_display_successfully_without_versions(self): object_type = ObjectTypeFactory.create() url = reverse("admin:core_objecttype_change", args=[object_type.id]) get_response = self.app.get(url) self.assertEqual(get_response.status_code, 200) form = get_response.form self.assertEqual(form["versions-0-id"].value, "")
def test_display_only_last_version(self): object_type = ObjectTypeFactory.create() ObjectVersionFactory.create_batch(3, object_type=object_type) url = reverse("admin:core_objecttype_change", args=[object_type.id]) get_response = self.app.get(url) form = get_response.form self.assertEqual(int(form["versions-TOTAL_FORMS"].value), 1) self.assertEqual(int(form["versions-0-id"].value), object_type.last_version.id)
def test_filter_public_data(self): object_type_1 = ObjectTypeFactory.create( data_classification=DataClassificationChoices.open ) object_type_2 = ObjectTypeFactory.create( data_classification=DataClassificationChoices.intern ) response = self.client.get( self.url, {"dataClassification": DataClassificationChoices.open} ) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json()["results"] self.assertEqual(len(data), 1) self.assertEqual( data[0]["url"], f"http://testserver{reverse('objecttype-detail', args=[object_type_1.uuid])}", )
def test_get_objecttypes(self): object_type = ObjectTypeFactory.create() object_version = ObjectVersionFactory.create(object_type=object_type) url = reverse("objecttype-detail", args=[object_type.uuid]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.json(), { "url": f"http://testserver{url}", "uuid": str(object_type.uuid), "name": object_type.name, "namePlural": object_type.name_plural, "description": object_type.description, "dataClassification": object_type.data_classification, "maintainerOrganization": object_type.maintainer_organization, "maintainerDepartment": object_type.maintainer_department, "contactPerson": object_type.contact_person, "contactEmail": object_type.contact_email, "source": object_type.source, "updateFrequency": object_type.update_frequency, "providerOrganization": object_type.provider_organization, "documentationUrl": object_type.documentation_url, "labels": object_type.labels, "createdAt": "2020-01-01", "modifiedAt": "2020-01-01", "allowGeometry": object_type.allow_geometry, "versions": [ f"http://testserver{reverse('objectversion-detail', args=[object_type.uuid, object_version.version])}" ], }, )
def test_delete_puclished_version_fail(self): object_type = ObjectTypeFactory.create() object_version = ObjectVersionFactory.create( object_type=object_type, status=ObjectVersionStatus.published) url = reverse("objectversion-detail", args=[object_type.uuid, object_version.version]) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) data = response.json() self.assertEqual(data["non_field_errors"], ["Only draft versions can be destroyed"])
def test_update_objecttype(self): object_type = ObjectTypeFactory.create( data_classification=DataClassificationChoices.intern) url = reverse("objecttype-detail", args=[object_type.uuid]) response = self.client.patch( url, {"dataClassification": DataClassificationChoices.open}) self.assertEqual(response.status_code, status.HTTP_200_OK) object_type.refresh_from_db() self.assertEqual(object_type.data_classification, DataClassificationChoices.open)
def test_create_version_with_incorrect_schema_fail(self): object_type = ObjectTypeFactory.create() url = reverse("objectversion-list", args=[object_type.uuid]) data = { "jsonSchema": { "title": "Tree", "$schema": "http://json-schema.org/draft-07/schema#", "type": "any", } } response = self.client.post(url, data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertTrue("jsonSchema" in response.json())
def test_delete_objecttype_with_versions_fail(self): object_type = ObjectTypeFactory.create() ObjectVersionFactory.create(object_type=object_type) url = reverse("objecttype-detail", args=[object_type.uuid]) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) data = response.json() self.assertEqual( data["non_field_errors"], [ "All related versions should be destroyed before destroying the objecttype" ], )
def test_publish_draft(self): object_type = ObjectTypeFactory.create() object_version = ObjectVersionFactory.create(object_type=object_type) url = reverse("admin:core_objecttype_change", args=[object_type.id]) get_response = self.app.get(url) publish_button = get_response.html.find("input", {"name": "_publish"}) self.assertIsNotNone(publish_button) form = get_response.form response = form.submit("_publish") self.assertEqual(response.status_code, 302) object_version.refresh_from_db() self.assertEqual(object_version.status, ObjectVersionStatus.published) self.assertEqual(object_version.published_at, date.today())
def test_create_version(self): object_type = ObjectTypeFactory.create() data = {"jsonSchema": JSON_SCHEMA, "status": ObjectVersionStatus.published} url = reverse("objectversion-list", args=[object_type.uuid]) response = self.client.post(url, data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(ObjectVersion.objects.count(), 1) object_version = ObjectVersion.objects.get() self.assertEqual(object_version.object_type, object_type) self.assertEqual(object_version.json_schema, JSON_SCHEMA) self.assertEqual(object_version.version, 1) self.assertEqual(object_version.status, ObjectVersionStatus.published) self.assertEqual(object_version.created_at, date(2020, 1, 1)) self.assertEqual(object_version.modified_at, date(2020, 1, 1)) self.assertEqual(object_version.published_at, date(2020, 1, 1))
def test_display_all_versions_in_history(self): object_type = ObjectTypeFactory.create() ObjectVersionFactory.create_batch(3, object_type=object_type) url = reverse("admin:core_objecttype_history", args=[object_type.id]) response = self.app.get(url) object_type = response.context["object"] self.assertEqual(response.status_code, 200) table = response.html.find(id="change-history") table_rows = table.tbody.find_all("tr") self.assertEqual(len(table_rows), 3) for object_version, row in zip(object_type.ordered_versions, table_rows): row_version = row.find("td") self.assertEqual(int(row_version.text), object_version.version)
def test_update_draft_invalid_json_schema(self): old_schema = { "type": "object", "title": "Tree", "$schema": "http://json-schema.org/draft-07/schema#", "properties": { "diameter": { "type": "integer", "description": "size in cm." } }, } object_type = ObjectTypeFactory.create() object_version = ObjectVersionFactory.create(object_type=object_type, json_schema=old_schema) url = reverse("admin:core_objecttype_change", args=[object_type.id]) get_response = self.app.get(url) save_button = get_response.html.find("input", {"name": "_save"}) self.assertIsNotNone(save_button) form = get_response.form form["versions-0-json_schema"] = "{}{" response = form.submit() self.assertEqual(response.status_code, 200) object_version.refresh_from_db() self.assertEqual(object_version.json_schema, old_schema) error_list = response.html.find("ul", {"class": "errorlist"}) # The submitted value is shown in the error self.assertIn("{}{", error_list.text) json_schema_field = response.form.fields["versions-0-json_schema"][0] # Verify that the value of the JSON schema field is the fallback value self.assertEqual(json_schema_field.value, json.dumps(old_schema))
def test_get_objecttypes_with_versions(self): object_types = ObjectTypeFactory.create_batch(2) object_versions = [ ObjectVersionFactory.create(object_type=object_type) for object_type in object_types ] for i, object_type in enumerate(object_types): with self.subTest(object_type=object_type): url = reverse("objecttype-detail", args=[object_type.uuid]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual(len(data["versions"]), 1) self.assertEqual( data["versions"], [ f"http://testserver{reverse('objectversion-detail', args=[object_type.uuid, object_versions[i].version])}" ], )
def test_get_versions(self): object_type = ObjectTypeFactory.create() object_version = ObjectVersionFactory.create(object_type=object_type) url = reverse("objectversion-list", args=[object_type.uuid]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.json(), [ { "url": f"http://testserver{reverse('objectversion-detail', args=[object_type.uuid, object_version.version])}", "version": object_version.version, "objectType": f"http://testserver{reverse('objecttype-detail', args=[object_version.object_type.uuid])}", "status": object_version.status, "createdAt": "2020-01-01", "modifiedAt": "2020-01-01", "publishedAt": None, "jsonSchema": JSON_SCHEMA, } ], )
def setUp(self) -> None: object_type = ObjectTypeFactory.create() self.urls = [ reverse("objecttype-list"), reverse("objecttype-detail", args=[object_type.uuid]), ]