""" Author: Michel Peltriaux Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany Contact: michel.peltriaux@sgdnord.rlp.de Created on: 11.11.21 """ from django.urls import reverse from konova.settings import ETS_GROUP from konova.tests.test_views import BaseWorkflowTestCase from user.models import UserAction class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): @classmethod def setUpTestData(cls): super().setUpTestData() # Add user to conservation office group and give shared access to the account cls.superuser.groups.add(cls.groups.get(name=ETS_GROUP)) cls.eco_account.share_with_list([cls.superuser]) def test_deductability(self): """ 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 self.intervention.share_with(self.superuser) 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,)) test_surface = 10.00 post_data = { "surface": test_surface, "account": self.id, "intervention": self.intervention.id, } # Perform request --> expect to fail self.client_user.post(deduct_url, post_data) # Expect that no deduction has been created 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) # Make sure the deductible surface is high enough for the request self.eco_account.set_recorded(self.superuser) self.eco_account.refresh_from_db() self.eco_account.deductable_surface = test_surface + 1.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 self.assertEqual(1, self.eco_account.deductions.count()) self.assertEqual(1, self.intervention.deductions.count()) deduction = self.eco_account.deductions.first() self.assertEqual(deduction.surface, test_surface) 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)