#31 API Tests
* adds creation tests with minimum data for intervention, compensation, ema and ecoaccount * fixes bug where empty geometry would not be created properly using the API * reworks key fetching from POST data, so inproperly stated keys will lead to an error for the API user, instead of silently working and use default data * adds some logical checks for deductable_surface of eco account creation using api * fixes bug that would have occured on creating compensations via api
This commit is contained in:
7
api/tests/v1/create/__init__.py
Normal file
7
api/tests/v1/create/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 27.01.22
|
||||
|
||||
"""
|
||||
19
api/tests/v1/create/compensation_create_post_body.json
Normal file
19
api/tests/v1/create/compensation_create_post_body.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"type": "MultiPolygon",
|
||||
"coordinates": [
|
||||
],
|
||||
"properties": {
|
||||
"title": "Test_compensation",
|
||||
"is_cef": false,
|
||||
"is_coherence_keeping": false,
|
||||
"intervention": "MUST_BE_SET_IN_TEST",
|
||||
"before_states": [
|
||||
],
|
||||
"after_states": [
|
||||
],
|
||||
"actions": [
|
||||
],
|
||||
"deadlines": [
|
||||
]
|
||||
}
|
||||
}
|
||||
25
api/tests/v1/create/ecoaccount_create_post_body.json
Normal file
25
api/tests/v1/create/ecoaccount_create_post_body.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"type": "MultiPolygon",
|
||||
"coordinates": [
|
||||
],
|
||||
"properties": {
|
||||
"title": "Test_ecoaccount",
|
||||
"deductable_surface": 10000.0,
|
||||
"responsible": {
|
||||
"conservation_office": null,
|
||||
"conservation_file_number": null,
|
||||
"handler": null
|
||||
},
|
||||
"legal": {
|
||||
"agreement_date": null
|
||||
},
|
||||
"before_states": [
|
||||
],
|
||||
"after_states": [
|
||||
],
|
||||
"actions": [
|
||||
],
|
||||
"deadlines": [
|
||||
]
|
||||
}
|
||||
}
|
||||
21
api/tests/v1/create/ema_create_post_body.json
Normal file
21
api/tests/v1/create/ema_create_post_body.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"type": "MultiPolygon",
|
||||
"coordinates": [
|
||||
],
|
||||
"properties": {
|
||||
"title": "Test_ema",
|
||||
"responsible": {
|
||||
"conservation_office": null,
|
||||
"conservation_file_number": null,
|
||||
"handler": null
|
||||
},
|
||||
"before_states": [
|
||||
],
|
||||
"after_states": [
|
||||
],
|
||||
"actions": [
|
||||
],
|
||||
"deadlines": [
|
||||
]
|
||||
}
|
||||
}
|
||||
21
api/tests/v1/create/intervention_create_post_body.json
Normal file
21
api/tests/v1/create/intervention_create_post_body.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"type": "MultiPolygon",
|
||||
"coordinates": [
|
||||
],
|
||||
"properties": {
|
||||
"title": "Test_intervention",
|
||||
"responsible": {
|
||||
"registration_office": null,
|
||||
"registration_file_number": null,
|
||||
"conservation_office": null,
|
||||
"conservation_file_number": null,
|
||||
"handler": null
|
||||
},
|
||||
"legal": {
|
||||
"registration_date": null,
|
||||
"binding_date": null,
|
||||
"process_type": null,
|
||||
"laws": []
|
||||
}
|
||||
}
|
||||
}
|
||||
105
api/tests/v1/create/test_api_create.py
Normal file
105
api/tests/v1/create/test_api_create.py
Normal file
@@ -0,0 +1,105 @@
|
||||
"""
|
||||
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
|
||||
|
||||
from api.tests.v1.test_api_sharing import BaseAPIV1TestCase
|
||||
|
||||
|
||||
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):
|
||||
""" Tests api creation of bare minimum interventions
|
||||
|
||||
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):
|
||||
""" Tests api creation of bare minimum interventions
|
||||
|
||||
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):
|
||||
""" Tests api creation of bare minimum interventions
|
||||
|
||||
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):
|
||||
""" Tests api creation of bare minimum interventions
|
||||
|
||||
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)
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 27.01.22
|
||||
|
||||
"""
|
||||
from konova.settings import DEFAULT_GROUP
|
||||
from konova.tests.test_views import BaseTestCase
|
||||
|
||||
|
||||
class BaseAPIV1TestCase(BaseTestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
super().setUpTestData()
|
||||
|
||||
cls.superuser.get_API_token()
|
||||
cls.superuser.api_token.is_active = True
|
||||
cls.superuser.api_token.save()
|
||||
default_group = cls.groups.get(name=DEFAULT_GROUP)
|
||||
cls.superuser.groups.add(default_group)
|
||||
|
||||
cls.header_data = {
|
||||
"HTTP_ksptoken": cls.superuser.api_token.token,
|
||||
}
|
||||
|
||||
|
||||
class APIV1CreateTestCase(BaseAPIV1TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
super().setUpTestData()
|
||||
@@ -2,11 +2,27 @@ import json
|
||||
|
||||
from django.urls import reverse
|
||||
|
||||
from api.tests.v1.test_api_create import BaseAPIV1TestCase
|
||||
from konova.settings import DEFAULT_GROUP
|
||||
from konova.tests.test_views import BaseTestCase
|
||||
from konova.utils.user_checks import is_default_group_only
|
||||
|
||||
|
||||
class BaseAPIV1TestCase(BaseTestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
super().setUpTestData()
|
||||
|
||||
cls.superuser.get_API_token()
|
||||
cls.superuser.api_token.is_active = True
|
||||
cls.superuser.api_token.save()
|
||||
default_group = cls.groups.get(name=DEFAULT_GROUP)
|
||||
cls.superuser.groups.add(default_group)
|
||||
|
||||
cls.header_data = {
|
||||
"HTTP_ksptoken": cls.superuser.api_token.token,
|
||||
}
|
||||
|
||||
|
||||
class APIV1SharingTestCase(BaseAPIV1TestCase):
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user