2022-01-27 17:09:38 +01:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 27.01.22
|
|
|
|
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
2022-01-28 09:44:16 +01:00
|
|
|
from api.tests.v1.share.test_api_sharing import BaseAPIV1TestCase
|
2022-01-27 17:09:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class APIV1CreateTestCase(BaseAPIV1TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
super().setUpTestData()
|
|
|
|
|
|
|
|
def _run_create_request(self, url, data):
|
|
|
|
data = json.dumps(data)
|
|
|
|
response = self.client.post(
|
|
|
|
url,
|
|
|
|
data=data,
|
|
|
|
content_type="application/json",
|
|
|
|
**self.header_data
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
|
|
|
def _test_create_object(self, url, post_body):
|
|
|
|
""" Tests the API creation of a new data object.
|
|
|
|
|
|
|
|
Post body data stored in a local json file
|
|
|
|
|
|
|
|
Args:
|
|
|
|
url (str): The api creation url
|
|
|
|
post_body (dict): The post body content as dict
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
response = self._run_create_request(url, post_body)
|
|
|
|
self.assertEqual(response.status_code, 200, msg=response.content)
|
|
|
|
content = json.loads(response.content)
|
|
|
|
self.assertIsNotNone(content.get("id", None), msg=response.content)
|
|
|
|
|
|
|
|
def test_create_intervention(self):
|
2022-01-28 16:21:23 +01:00
|
|
|
""" Tests api creation
|
2022-01-27 17:09:38 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
url = reverse("api:v1:intervention")
|
|
|
|
json_file_path = "api/tests/v1/create/intervention_create_post_body.json"
|
|
|
|
with open(json_file_path) as json_file:
|
|
|
|
post_body = json.load(fp=json_file)
|
|
|
|
self._test_create_object(url, post_body)
|
|
|
|
|
|
|
|
def test_create_compensation(self):
|
2022-01-28 16:21:23 +01:00
|
|
|
""" Tests api creation
|
2022-01-27 17:09:38 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
url = reverse("api:v1:compensation")
|
|
|
|
json_file_path = "api/tests/v1/create/compensation_create_post_body.json"
|
|
|
|
with open(json_file_path) as json_file:
|
|
|
|
post_body = json.load(fp=json_file)
|
|
|
|
post_body["properties"]["intervention"] = str(self.intervention.id)
|
|
|
|
|
|
|
|
# Expect this first request to fail, since user has no shared access on the intervention, we want to create
|
|
|
|
# a compensation for
|
|
|
|
response = self._run_create_request(url, post_body)
|
|
|
|
self.assertEqual(response.status_code, 500, msg=response.content)
|
|
|
|
content = json.loads(response.content)
|
|
|
|
self.assertGreater(len(content.get("errors", [])), 0, msg=response.content)
|
|
|
|
|
|
|
|
# Add the user to the shared users of the intervention and try again! Now everything should work as expected.
|
|
|
|
self.intervention.users.add(self.superuser)
|
|
|
|
self._test_create_object(url, post_body)
|
|
|
|
|
|
|
|
def test_create_eco_account(self):
|
2022-01-28 16:21:23 +01:00
|
|
|
""" Tests api creation
|
2022-01-27 17:09:38 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
url = reverse("api:v1:ecoaccount")
|
|
|
|
json_file_path = "api/tests/v1/create/ecoaccount_create_post_body.json"
|
|
|
|
with open(json_file_path) as json_file:
|
|
|
|
post_body = json.load(fp=json_file)
|
|
|
|
self._test_create_object(url, post_body)
|
|
|
|
|
|
|
|
def test_create_ema(self):
|
2022-01-28 16:21:23 +01:00
|
|
|
""" Tests api creation
|
2022-01-27 17:09:38 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
url = reverse("api:v1:ema")
|
|
|
|
json_file_path = "api/tests/v1/create/ema_create_post_body.json"
|
|
|
|
with open(json_file_path) as json_file:
|
|
|
|
post_body = json.load(fp=json_file)
|
|
|
|
self._test_create_object(url, post_body)
|
|
|
|
|
2022-01-28 16:21:23 +01:00
|
|
|
def test_create_deduction(self):
|
|
|
|
""" Tests api creation
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-18 11:02:40 +01:00
|
|
|
self.intervention.share_with_user(self.superuser)
|
|
|
|
self.eco_account.share_with_user(self.superuser)
|
2022-01-28 16:21:23 +01:00
|
|
|
|
|
|
|
url = reverse("api:v1:deduction")
|
|
|
|
json_file_path = "api/tests/v1/create/deduction_create_post_body.json"
|
|
|
|
with open(json_file_path) as json_file:
|
|
|
|
post_body = json.load(fp=json_file)
|
|
|
|
post_body["intervention"] = str(self.intervention.id)
|
|
|
|
post_body["eco_account"] = str(self.eco_account.id)
|
|
|
|
self._test_create_object(url, post_body)
|
|
|
|
|