2022-02-08 09:27:28 +01:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 11.11.21
|
|
|
|
|
|
|
|
"""
|
2022-02-08 11:58:43 +01:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from django.contrib.gis.geos import MultiPolygon
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2022-02-08 09:27:28 +01:00
|
|
|
from django.urls import reverse
|
|
|
|
|
2022-02-09 14:49:56 +01:00
|
|
|
from compensation.models import EcoAccount, EcoAccountDeduction
|
2022-02-08 11:58:43 +01:00
|
|
|
from konova.settings import ETS_GROUP, DEFAULT_GROUP
|
2022-02-08 09:27:28 +01:00
|
|
|
from konova.tests.test_views import BaseWorkflowTestCase
|
|
|
|
from user.models import UserAction
|
|
|
|
|
|
|
|
|
|
|
|
class EcoAccountWorkflowTestCase(BaseWorkflowTestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
super().setUpTestData()
|
|
|
|
|
2022-02-08 11:58:43 +01:00
|
|
|
def setUp(self) -> None:
|
|
|
|
super().setUp()
|
2022-02-08 09:27:28 +01:00
|
|
|
# Add user to conservation office group and give shared access to the account
|
2022-02-08 11:58:43 +01:00
|
|
|
self.superuser.groups.add(self.groups.get(name=DEFAULT_GROUP))
|
|
|
|
self.superuser.groups.add(self.groups.get(name=ETS_GROUP))
|
2022-02-18 11:02:40 +01:00
|
|
|
self.eco_account.share_with_user_list([self.superuser])
|
2022-02-08 11:58:43 +01:00
|
|
|
|
|
|
|
def test_new(self):
|
|
|
|
""" Test the creation of an EcoAccount
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Prepare url and form data to be posted
|
|
|
|
new_url = reverse("compensation:acc:new")
|
|
|
|
test_id = self.create_dummy_string()
|
|
|
|
test_title = self.create_dummy_string()
|
|
|
|
test_geom = self.create_dummy_geometry()
|
2022-04-20 11:55:03 +02:00
|
|
|
geom_json = self.create_geojson(test_geom)
|
2022-02-08 11:58:43 +01:00
|
|
|
test_deductable_surface = 1000
|
|
|
|
test_conservation_office = self.get_conservation_office_code()
|
|
|
|
post_data = {
|
|
|
|
"identifier": test_id,
|
|
|
|
"title": test_title,
|
2022-04-20 11:55:03 +02:00
|
|
|
"geom": geom_json,
|
2022-10-12 08:59:38 +02:00
|
|
|
"surface": test_deductable_surface,
|
2022-02-08 11:58:43 +01:00
|
|
|
"conservation_office": test_conservation_office.id
|
|
|
|
}
|
|
|
|
self.client_user.post(new_url, post_data)
|
|
|
|
|
|
|
|
try:
|
|
|
|
acc = EcoAccount.objects.get(
|
|
|
|
identifier=test_id
|
|
|
|
)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
self.fail(msg="EcoAccount not created")
|
|
|
|
|
|
|
|
self.assertEqual(acc.identifier, test_id)
|
|
|
|
self.assertEqual(acc.title, test_title)
|
2022-10-12 08:59:38 +02:00
|
|
|
self.assertEqual(acc.deductable_surface, test_deductable_surface)
|
|
|
|
self.assertEqual(acc.deductable_rest, test_deductable_surface)
|
2022-02-08 11:58:43 +01:00
|
|
|
self.assert_equal_geometries(acc.geometry.geom, test_geom)
|
|
|
|
self.assertEqual(acc.log.count(), 1)
|
2022-11-25 09:05:06 +01:00
|
|
|
self.assertEqual(acc.created, acc.modified)
|
2022-02-08 11:58:43 +01:00
|
|
|
|
|
|
|
# Expect logs to be set
|
|
|
|
self.assertEqual(acc.log.count(), 1)
|
|
|
|
self.assertEqual(acc.log.first().action, UserAction.CREATED)
|
|
|
|
|
|
|
|
def test_edit(self):
|
|
|
|
""" Checks that the editing of an EcoAccount works
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-18 11:02:40 +01:00
|
|
|
self.eco_account.share_with_user(self.superuser)
|
2022-02-08 11:58:43 +01:00
|
|
|
|
|
|
|
url = reverse("compensation:acc:edit", args=(self.eco_account.id,))
|
|
|
|
pre_edit_log_count = self.eco_account.log.count()
|
|
|
|
|
|
|
|
new_title = self.create_dummy_string()
|
|
|
|
new_identifier = self.create_dummy_string()
|
|
|
|
new_comment = self.create_dummy_string()
|
|
|
|
new_geometry = MultiPolygon(srid=4326) # Create an empty geometry
|
|
|
|
test_conservation_office = self.get_conservation_office_code()
|
2022-10-12 08:59:38 +02:00
|
|
|
test_deductable_surface = self.eco_account.deductable_surface + 100
|
2022-02-08 11:58:43 +01:00
|
|
|
|
|
|
|
check_on_elements = {
|
|
|
|
self.eco_account.title: new_title,
|
|
|
|
self.eco_account.identifier: new_identifier,
|
|
|
|
self.eco_account.comment: new_comment,
|
|
|
|
self.eco_account.deductable_surface: test_deductable_surface,
|
|
|
|
}
|
|
|
|
for k, v in check_on_elements.items():
|
|
|
|
self.assertNotEqual(k, v)
|
|
|
|
|
|
|
|
post_data = {
|
|
|
|
"identifier": new_identifier,
|
|
|
|
"title": new_title,
|
|
|
|
"comment": new_comment,
|
|
|
|
"geom": new_geometry.geojson,
|
|
|
|
"surface": test_deductable_surface,
|
|
|
|
"conservation_office": test_conservation_office.id
|
|
|
|
}
|
2023-08-17 12:59:50 +02:00
|
|
|
response = self.client_user.post(url, post_data)
|
|
|
|
self.assertEqual(response.status_code, 302, msg=f"{response.content.decode('utf-8')}")
|
2022-02-08 11:58:43 +01:00
|
|
|
self.eco_account.refresh_from_db()
|
|
|
|
|
2023-08-17 12:59:50 +02:00
|
|
|
deductions_surface = self.eco_account.get_deductions_surface()
|
|
|
|
|
2022-02-08 11:58:43 +01:00
|
|
|
check_on_elements = {
|
|
|
|
self.eco_account.title: new_title,
|
|
|
|
self.eco_account.identifier: new_identifier,
|
|
|
|
self.eco_account.deductable_surface: test_deductable_surface,
|
2023-08-17 12:59:50 +02:00
|
|
|
self.eco_account.deductable_rest: test_deductable_surface - deductions_surface,
|
2022-02-08 11:58:43 +01:00
|
|
|
self.eco_account.comment: new_comment,
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v in check_on_elements.items():
|
|
|
|
self.assertEqual(k, v)
|
|
|
|
|
|
|
|
self.assert_equal_geometries(self.eco_account.geometry.geom, new_geometry)
|
|
|
|
|
|
|
|
# Expect logs to be set
|
|
|
|
self.assertEqual(pre_edit_log_count + 1, self.eco_account.log.count())
|
|
|
|
self.assertEqual(self.eco_account.log.first().action, UserAction.EDITED)
|
|
|
|
|
|
|
|
def test_recordability(self):
|
|
|
|
"""
|
|
|
|
This tests if the recordability of the EcoAccount is triggered by the quality of it's data (e.g. not all fields filled)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Add proper privilege for the user
|
2022-02-18 11:02:40 +01:00
|
|
|
self.eco_account.share_with_user(self.superuser)
|
2022-02-08 11:58:43 +01:00
|
|
|
pre_record_log_count = self.eco_account.log.count()
|
|
|
|
|
|
|
|
# Prepare url and form data
|
|
|
|
record_url = reverse("compensation:acc:record", args=(self.eco_account.id,))
|
|
|
|
post_data = {
|
|
|
|
"confirm": True,
|
|
|
|
}
|
|
|
|
self.eco_account.refresh_from_db()
|
|
|
|
|
|
|
|
# Make sure the account is not recorded
|
|
|
|
self.assertIsNone(self.eco_account.recorded)
|
|
|
|
|
|
|
|
# Run the request --> expect fail, since the account is not valid, yet
|
|
|
|
self.client_user.post(record_url, post_data)
|
|
|
|
|
|
|
|
# Check that the account is still not recorded
|
|
|
|
self.assertIsNone(self.eco_account.recorded)
|
|
|
|
|
|
|
|
# Now fill out the data for an ecoaccount
|
|
|
|
self.eco_account = self.fill_out_eco_account(self.eco_account)
|
|
|
|
|
|
|
|
# Rerun the request
|
|
|
|
self.client_user.post(record_url, post_data)
|
|
|
|
|
|
|
|
# Expect the EcoAccount now to be recorded
|
|
|
|
# Attention: We can only test the date part of the timestamp,
|
|
|
|
# since the delay in microseconds would lead to fail
|
|
|
|
self.eco_account.refresh_from_db()
|
|
|
|
recorded = self.eco_account.recorded
|
|
|
|
self.assertIsNotNone(recorded)
|
|
|
|
self.assertEqual(self.superuser, recorded.user)
|
|
|
|
self.assertEqual(UserAction.RECORDED, recorded.action)
|
|
|
|
self.assertEqual(datetime.date.today(), recorded.timestamp.date())
|
|
|
|
|
|
|
|
# Expect the user action to be in the log
|
|
|
|
self.assertIn(recorded, self.eco_account.log.all())
|
|
|
|
self.assertEqual(pre_record_log_count + 1, self.eco_account.log.count())
|
2022-02-08 09:27:28 +01:00
|
|
|
|
2022-02-09 14:49:56 +01:00
|
|
|
def test_new_deduction(self):
|
2022-02-08 09:27:28 +01:00
|
|
|
"""
|
|
|
|
This tests the deductability of an eco account.
|
|
|
|
|
|
|
|
An eco account should only be deductible if it is recorded.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Give user shared access to the dummy intervention, which will be needed here
|
2022-02-18 11:02:40 +01:00
|
|
|
self.intervention.share_with_user(self.superuser)
|
2022-02-08 09:27:28 +01:00
|
|
|
pre_deduction_acc_log_count = self.eco_account.log.count()
|
|
|
|
pre_deduction_int_log_count = self.intervention.log.count()
|
|
|
|
|
|
|
|
# Prepare data for deduction creation
|
|
|
|
deduct_url = reverse("compensation:acc:new-deduction", args=(self.eco_account.id,))
|
2022-11-18 13:24:36 +01:00
|
|
|
test_surface = 10.50
|
2022-02-08 09:27:28 +01:00
|
|
|
post_data = {
|
|
|
|
"surface": test_surface,
|
2022-02-09 14:49:56 +01:00
|
|
|
"account": self.eco_account.id,
|
2022-02-08 09:27:28 +01:00
|
|
|
"intervention": self.intervention.id,
|
|
|
|
}
|
|
|
|
# Perform request --> expect to fail
|
|
|
|
self.client_user.post(deduct_url, post_data)
|
|
|
|
|
2022-10-12 08:59:38 +02:00
|
|
|
# Expect that no deduction has been created since the eco account is not recorded, yet
|
2022-02-08 09:27:28 +01:00
|
|
|
self.assertEqual(0, self.eco_account.deductions.count())
|
|
|
|
self.assertEqual(0, self.intervention.deductions.count())
|
|
|
|
self.assertEqual(pre_deduction_acc_log_count, 0)
|
|
|
|
self.assertEqual(pre_deduction_int_log_count, 0)
|
|
|
|
|
|
|
|
# Now mock the eco account as it would be recorded (with invalid data)
|
2022-10-12 08:59:38 +02:00
|
|
|
# Make sure the deductible surface is valid for the request
|
2022-02-08 09:27:28 +01:00
|
|
|
self.eco_account.set_recorded(self.superuser)
|
|
|
|
self.eco_account.refresh_from_db()
|
2022-11-18 13:24:36 +01:00
|
|
|
self.eco_account.deductable_surface = test_surface + 1.0
|
2022-02-08 09:27:28 +01:00
|
|
|
self.eco_account.save()
|
|
|
|
self.assertIsNotNone(self.eco_account.recorded)
|
|
|
|
self.assertGreater(self.eco_account.deductable_surface, test_surface)
|
|
|
|
# Expect the recorded entry in the log
|
|
|
|
self.assertEqual(pre_deduction_acc_log_count + 1, self.eco_account.log.count())
|
|
|
|
self.assertTrue(self.eco_account.log.first().action == UserAction.RECORDED)
|
|
|
|
|
|
|
|
# Rerun the request
|
|
|
|
self.client_user.post(deduct_url, post_data)
|
|
|
|
|
|
|
|
# Expect that the deduction has been created
|
2022-10-12 08:59:38 +02:00
|
|
|
self.eco_account.refresh_from_db()
|
2022-02-08 09:27:28 +01:00
|
|
|
self.assertEqual(1, self.eco_account.deductions.count())
|
|
|
|
self.assertEqual(1, self.intervention.deductions.count())
|
2023-08-17 12:59:50 +02:00
|
|
|
deduction = self.eco_account.deductions.get(
|
|
|
|
surface=test_surface
|
|
|
|
)
|
2022-02-08 09:27:28 +01:00
|
|
|
self.assertEqual(deduction.surface, test_surface)
|
2022-10-12 08:59:38 +02:00
|
|
|
self.assertEqual(self.eco_account.deductable_rest, self.eco_account.deductable_surface - deduction.surface)
|
2022-02-08 09:27:28 +01:00
|
|
|
self.assertEqual(deduction.account, self.eco_account)
|
|
|
|
self.assertEqual(deduction.intervention, self.intervention)
|
|
|
|
|
|
|
|
# Expect entries in the log
|
|
|
|
self.assertEqual(pre_deduction_acc_log_count + 2, self.eco_account.log.count())
|
|
|
|
self.assertTrue(self.eco_account.log.first().action == UserAction.EDITED)
|
|
|
|
self.assertEqual(pre_deduction_int_log_count + 1, self.intervention.log.count())
|
|
|
|
self.assertTrue(self.intervention.log.first().action == UserAction.EDITED)
|
|
|
|
|
2022-02-09 14:49:56 +01:00
|
|
|
def test_edit_deduction(self):
|
2022-10-11 15:20:11 +02:00
|
|
|
test_surface = self.eco_account.deductable_rest
|
2022-02-09 14:49:56 +01:00
|
|
|
self.eco_account.set_recorded(self.superuser)
|
2022-02-18 11:02:40 +01:00
|
|
|
self.intervention.share_with_user(self.superuser)
|
2022-02-09 14:49:56 +01:00
|
|
|
self.eco_account.refresh_from_db()
|
2022-02-18 13:52:27 +01:00
|
|
|
self.assertTrue(self.superuser, self.intervention.is_shared_with(self.superuser))
|
2022-02-09 14:49:56 +01:00
|
|
|
|
|
|
|
deduction = EcoAccountDeduction.objects.create(
|
|
|
|
intervention=self.intervention,
|
|
|
|
account=self.eco_account,
|
2022-11-18 13:24:36 +01:00
|
|
|
surface=1.10
|
2022-02-09 14:49:56 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(1, self.intervention.deductions.count())
|
|
|
|
self.assertEqual(1, self.eco_account.deductions.count())
|
|
|
|
|
|
|
|
# Prepare url and form data to be posted
|
|
|
|
new_url = reverse("compensation:acc:edit-deduction", args=(self.eco_account.id, deduction.id))
|
|
|
|
post_data = {
|
|
|
|
"intervention": deduction.intervention.id,
|
|
|
|
"account": deduction.account.id,
|
|
|
|
"surface": test_surface,
|
|
|
|
}
|
|
|
|
pre_edit_intervention_log_count = self.intervention.log.count()
|
|
|
|
pre_edit_account_log_count = self.eco_account.log.count()
|
|
|
|
num_deductions_intervention = self.intervention.deductions.count()
|
|
|
|
num_deductions_account = self.eco_account.deductions.count()
|
|
|
|
|
|
|
|
self.client_user.post(new_url, post_data)
|
|
|
|
|
|
|
|
self.intervention.refresh_from_db()
|
|
|
|
self.eco_account.refresh_from_db()
|
|
|
|
deduction.refresh_from_db()
|
|
|
|
|
2022-10-12 08:59:38 +02:00
|
|
|
self.assertEqual(self.eco_account.deductable_rest, self.eco_account.deductable_surface - deduction.surface)
|
2022-02-09 14:49:56 +01:00
|
|
|
self.assertEqual(num_deductions_intervention, self.intervention.deductions.count())
|
|
|
|
self.assertEqual(num_deductions_account, self.eco_account.deductions.count())
|
|
|
|
self.assertEqual(deduction.surface, test_surface)
|
2022-02-08 09:27:28 +01:00
|
|
|
|
2022-02-09 14:49:56 +01:00
|
|
|
# Expect logs to be set
|
|
|
|
self.assertEqual(pre_edit_intervention_log_count + 1, self.intervention.log.count())
|
|
|
|
self.assertEqual(pre_edit_account_log_count + 1, self.eco_account.log.count())
|
|
|
|
self.assertEqual(self.intervention.log.first().action, UserAction.EDITED)
|
|
|
|
self.assertEqual(self.eco_account.log.first().action, UserAction.EDITED)
|
|
|
|
|
|
|
|
def test_remove_deduction(self):
|
|
|
|
intervention = self.deduction.intervention
|
|
|
|
account = self.deduction.account
|
2022-10-12 08:59:38 +02:00
|
|
|
deducted_surface = self.deduction.surface
|
2022-02-09 14:49:56 +01:00
|
|
|
|
|
|
|
# Prepare url and form data to be posted
|
|
|
|
new_url = reverse("compensation:acc:remove-deduction", args=(account.id, self.deduction.id))
|
|
|
|
post_data = {
|
|
|
|
"confirm": True,
|
|
|
|
}
|
|
|
|
|
2022-02-18 11:02:40 +01:00
|
|
|
intervention.share_with_user(self.superuser)
|
|
|
|
account.share_with_user(self.superuser)
|
2022-02-09 14:49:56 +01:00
|
|
|
|
|
|
|
pre_edit_intervention_log_count = intervention.log.count()
|
|
|
|
pre_edit_account_log_count = account.log.count()
|
2022-10-12 08:59:38 +02:00
|
|
|
pre_edit_account_rest = account.deductable_rest
|
2022-02-09 14:49:56 +01:00
|
|
|
num_deductions_intervention = intervention.deductions.count()
|
|
|
|
num_deductions_account = account.deductions.count()
|
|
|
|
|
|
|
|
self.client_user.post(new_url, post_data)
|
|
|
|
|
|
|
|
intervention.refresh_from_db()
|
|
|
|
account.refresh_from_db()
|
|
|
|
|
|
|
|
self.assertEqual(num_deductions_intervention - 1, intervention.deductions.count())
|
|
|
|
self.assertEqual(num_deductions_account - 1, account.deductions.count())
|
2022-10-12 08:59:38 +02:00
|
|
|
self.assertEqual(account.deductable_rest, pre_edit_account_rest + deducted_surface)
|
2022-02-09 14:49:56 +01:00
|
|
|
|
|
|
|
# Expect logs to be set
|
|
|
|
self.assertEqual(pre_edit_intervention_log_count + 1, intervention.log.count())
|
|
|
|
self.assertEqual(pre_edit_account_log_count + 1, account.log.count())
|
|
|
|
self.assertEqual(intervention.log.first().action, UserAction.EDITED)
|
|
|
|
self.assertEqual(account.log.first().action, UserAction.EDITED)
|
2022-04-19 14:04:20 +02:00
|
|
|
|
|
|
|
def test_non_editable_after_recording(self):
|
|
|
|
""" Tests that the eco_account can not be edited after being recorded
|
|
|
|
|
|
|
|
User must be redirected to another page
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.assertIsNotNone(self.eco_account)
|
|
|
|
self.assertFalse(self.eco_account.is_recorded)
|
|
|
|
edit_url = reverse("compensation:acc:edit", args=(self.eco_account.id,))
|
|
|
|
response = self.client_user.get(edit_url)
|
|
|
|
has_redirect = response.status_code == 302
|
|
|
|
self.assertFalse(has_redirect)
|
|
|
|
|
|
|
|
self.eco_account.set_recorded(self.user)
|
|
|
|
self.assertTrue(self.eco_account.is_recorded)
|
|
|
|
|
|
|
|
edit_url = reverse("compensation:acc:edit", args=(self.eco_account.id,))
|
|
|
|
response = self.client_user.get(edit_url)
|
|
|
|
has_redirect = response.status_code == 302
|
|
|
|
self.assertTrue(has_redirect)
|
|
|
|
self.eco_account.set_unrecorded(self.user)
|