""" Author: Michel Peltriaux Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany Contact: michel.peltriaux@sgdnord.rlp.de Created on: 28.01.22 """ import json from django.urls import reverse from api.tests.v1.share.test_api_sharing import BaseAPIV1TestCase class APIV1GetTestCase(BaseAPIV1TestCase): @classmethod def setUpTestData(cls): super().setUpTestData() def _run_get_request(self, url): response = self.client.get( url, **self.header_data ) return response def _test_get_object(self, obj, url): """ Tests the API GET of a data object. Args: url (str): The api get url Returns: """ response = self._run_get_request(url) content = json.loads(response.content) self.assertIn("rpp", content) self.assertIn("p", content) self.assertIn("next", content) self.assertIn("results", content) paginated_content = content["results"] geojson = paginated_content[str(obj.id)] self.assertEqual(response.status_code, 200, msg=response.content) return geojson def _assert_geojson_format(self, geojson): try: geojson["type"] geojson["coordinates"] props = geojson["properties"] props["id"] props["identifier"] props["title"] props["created_on"] props["modified_on"] except KeyError as e: self.fail(e) def test_get_intervention(self): """ Tests api GET Returns: """ self.intervention.share_with_user(self.superuser) url = reverse("api:v1:intervention", args=(str(self.intervention.id),)) geojson = self._test_get_object(self.intervention, url) self._assert_geojson_format(geojson) try: props = geojson["properties"] props["responsible"] props["responsible"]["registration_office"] props["responsible"]["registration_file_number"] props["responsible"]["conservation_office"] props["responsible"]["conservation_file_number"] props["legal"]["registration_date"] props["legal"]["binding_date"] props["legal"]["process_type"] props["legal"]["laws"] props["compensations"] props["payments"] props["deductions"] except KeyError as e: self.fail(e) def test_get_shared(self): """ Tests api GET on shared info of the intervention Returns: """ self.intervention.share_with_user(self.superuser) self.intervention.share_with_team(self.team) url = reverse("api:v1:intervention-share", args=(str(self.intervention.id),)) response = self._run_get_request(url) content = json.loads(response.content) self.assertIn("users", content) self.assertIn(self.superuser.username, content["users"]) self.assertEqual(1, len(content["users"])) self.assertIn("teams", content) self.assertEqual(1, len(content["teams"])) for team in content["teams"]: self.assertEqual(team["id"], str(self.team.id)) self.assertEqual(team["name"], self.team.name) def test_get_compensation(self): """ Tests api GET Returns: """ self.intervention.share_with_user(self.superuser) self.compensation.intervention = self.intervention self.compensation.save() url = reverse("api:v1:compensation", args=(str(self.compensation.id),)) geojson = self._test_get_object(self.compensation, url) self._assert_geojson_format(geojson) try: props = geojson["properties"] props["is_cef"] props["is_coherence_keeping"] props["is_pik"] props["intervention"] props["intervention"]["id"] props["intervention"]["identifier"] props["intervention"]["title"] props["before_states"] props["after_states"] props["actions"] props["deadlines"] except KeyError as e: self.fail(e) def test_get_eco_account(self): """ Tests api GET Returns: """ self.eco_account.share_with_user(self.superuser) url = reverse("api:v1:ecoaccount", args=(str(self.eco_account.id),)) geojson = self._test_get_object(self.eco_account, url) self._assert_geojson_format(geojson) try: props = geojson["properties"] props["deductable_surface"] props["deductable_surface_available"] props["responsible"] props["responsible"]["conservation_office"] props["responsible"]["conservation_file_number"] props["responsible"]["handler"] props["legal"] props["legal"]["agreement_date"] props["before_states"] props["after_states"] props["actions"] props["deadlines"] props["deductions"] except KeyError as e: self.fail(e) def test_get_ema(self): """ Tests api GET Returns: """ self.ema.share_with_user(self.superuser) url = reverse("api:v1:ema", args=(str(self.ema.id),)) geojson = self._test_get_object(self.ema, url) self._assert_geojson_format(geojson) try: props = geojson["properties"] props["responsible"] props["responsible"]["conservation_office"] props["responsible"]["conservation_file_number"] props["responsible"]["handler"] props["before_states"] props["after_states"] props["actions"] props["deadlines"] except KeyError as e: self.fail(e) def test_get_deduction(self): """ Tests api GET Returns: """ self.deduction.intervention.share_with_user(self.superuser) url = reverse("api:v1:deduction", args=(str(self.deduction.id),)) _json = self._test_get_object(self.deduction, url) try: _json["id"] _json["eco_account"] _json["eco_account"]["id"] _json["eco_account"]["identifier"] _json["eco_account"]["title"] _json["surface"] _json["intervention"] _json["intervention"]["id"] _json["intervention"]["identifier"] _json["intervention"]["title"] except KeyError as e: self.fail(e)