Further tests
* restructures compensation/tests into subtests for ecoaccount and compensation * adds tests for ema workflow * improved test data setup
This commit is contained in:
7
compensation/tests/ecoaccount/__init__.py
Normal file
7
compensation/tests/ecoaccount/__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: 07.02.22
|
||||
|
||||
"""
|
||||
194
compensation/tests/ecoaccount/test_views.py
Normal file
194
compensation/tests/ecoaccount/test_views.py
Normal file
@@ -0,0 +1,194 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 27.10.21
|
||||
|
||||
"""
|
||||
from django.urls import reverse
|
||||
from django.test import Client
|
||||
|
||||
from compensation.tests.compensation.test_views import CompensationViewTestCase
|
||||
from konova.settings import DEFAULT_GROUP
|
||||
|
||||
|
||||
class EcoAccountViewTestCase(CompensationViewTestCase):
|
||||
"""
|
||||
These tests focus on proper returned views depending on the user's groups privileges and login status
|
||||
|
||||
EcoAccounts can inherit the same tests used for compensations.
|
||||
|
||||
"""
|
||||
comp_state = None
|
||||
comp_action = None
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls) -> None:
|
||||
super().setUpTestData()
|
||||
state = cls.create_dummy_states()
|
||||
cls.eco_account.before_states.set([state])
|
||||
cls.eco_account.after_states.set([state])
|
||||
|
||||
action = cls.create_dummy_action()
|
||||
cls.eco_account.actions.set([action])
|
||||
|
||||
# Prepare urls
|
||||
cls.index_url = reverse("compensation:acc:index", args=())
|
||||
cls.new_url = reverse("compensation:acc:new", args=())
|
||||
cls.new_id_url = reverse("compensation:acc:new-id", args=())
|
||||
cls.detail_url = reverse("compensation:acc:detail", args=(cls.eco_account.id,))
|
||||
cls.log_url = reverse("compensation:acc:log", args=(cls.eco_account.id,))
|
||||
cls.edit_url = reverse("compensation:acc:edit", args=(cls.eco_account.id,))
|
||||
cls.remove_url = reverse("compensation:acc:remove", args=(cls.eco_account.id,))
|
||||
cls.report_url = reverse("compensation:acc:report", args=(cls.eco_account.id,))
|
||||
cls.state_new_url = reverse("compensation:acc:new-state", args=(cls.eco_account.id,))
|
||||
cls.action_new_url = reverse("compensation:acc:new-action", args=(cls.eco_account.id,))
|
||||
cls.deadline_new_url = reverse("compensation:acc:new-deadline", args=(cls.eco_account.id,))
|
||||
cls.new_doc_url = reverse("compensation:acc:new-doc", args=(cls.eco_account.id,))
|
||||
cls.state_remove_url = reverse("compensation:acc:state-remove", args=(cls.eco_account.id, cls.comp_state.id,))
|
||||
cls.action_remove_url = reverse("compensation:acc:action-remove", args=(cls.eco_account.id, cls.comp_action.id,))
|
||||
|
||||
def test_logged_in_no_groups_shared(self):
|
||||
""" Check correct status code for all requests
|
||||
|
||||
Assumption: User logged in and has no groups and data is shared
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
client = Client()
|
||||
client.login(username=self.superuser.username, password=self.superuser_pw)
|
||||
self.superuser.groups.set([])
|
||||
self.eco_account.share_with_list([self.superuser])
|
||||
|
||||
# Since the user has no groups, it does not matter that data has been shared. There SHOULD not be any difference
|
||||
# to a user without access, since the important permissions are missing
|
||||
success_urls = [
|
||||
self.index_url,
|
||||
self.detail_url,
|
||||
self.report_url,
|
||||
]
|
||||
fail_urls = [
|
||||
self.new_url,
|
||||
self.new_id_url,
|
||||
self.log_url,
|
||||
self.edit_url,
|
||||
self.remove_url,
|
||||
self.state_new_url,
|
||||
self.action_new_url,
|
||||
self.deadline_new_url,
|
||||
self.state_remove_url,
|
||||
self.action_remove_url,
|
||||
self.new_doc_url,
|
||||
]
|
||||
|
||||
self.assert_url_success(client, success_urls)
|
||||
self.assert_url_fail(client, fail_urls)
|
||||
|
||||
def test_logged_in_no_groups_unshared(self):
|
||||
""" Check correct status code for all requests
|
||||
|
||||
Assumption: User logged in and has no groups and data is shared
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
client = Client()
|
||||
client.login(username=self.superuser.username, password=self.superuser_pw)
|
||||
self.superuser.groups.set([])
|
||||
self.eco_account.share_with_list([])
|
||||
|
||||
# Since the user has no groups, it does not matter that data is unshared. There SHOULD not be any difference
|
||||
# to a user having shared access, since all important permissions are missing
|
||||
success_urls = [
|
||||
self.index_url,
|
||||
self.detail_url,
|
||||
self.report_url,
|
||||
]
|
||||
fail_urls = [
|
||||
self.new_url,
|
||||
self.new_id_url,
|
||||
self.log_url,
|
||||
self.edit_url,
|
||||
self.remove_url,
|
||||
self.state_new_url,
|
||||
self.action_new_url,
|
||||
self.deadline_new_url,
|
||||
self.state_remove_url,
|
||||
self.action_remove_url,
|
||||
self.new_doc_url,
|
||||
]
|
||||
|
||||
self.assert_url_success(client, success_urls)
|
||||
self.assert_url_fail(client, fail_urls)
|
||||
|
||||
def test_logged_in_default_group_shared(self):
|
||||
""" Check correct status code for all requests
|
||||
|
||||
Assumption: User logged in, is default group member and data is shared
|
||||
--> Default group necessary since all base functionalities depend on this group membership
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
client = Client()
|
||||
client.login(username=self.superuser.username, password=self.superuser_pw)
|
||||
group = self.groups.get(name=DEFAULT_GROUP)
|
||||
self.superuser.groups.set([group])
|
||||
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
||||
self.eco_account.share_with_list([self.superuser])
|
||||
|
||||
success_urls = [
|
||||
self.index_url,
|
||||
self.detail_url,
|
||||
self.report_url,
|
||||
self.new_url,
|
||||
self.new_id_url,
|
||||
self.edit_url,
|
||||
self.state_new_url,
|
||||
self.action_new_url,
|
||||
self.deadline_new_url,
|
||||
self.state_remove_url,
|
||||
self.action_remove_url,
|
||||
self.new_doc_url,
|
||||
self.log_url,
|
||||
self.remove_url,
|
||||
]
|
||||
self.assert_url_success(client, success_urls)
|
||||
|
||||
def test_logged_in_default_group_unshared(self):
|
||||
""" Check correct status code for all requests
|
||||
|
||||
Assumption: User logged in, is default group member and data is NOT shared
|
||||
--> Default group necessary since all base functionalities depend on this group membership
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
client = Client()
|
||||
client.login(username=self.superuser.username, password=self.superuser_pw)
|
||||
group = self.groups.get(name=DEFAULT_GROUP)
|
||||
self.superuser.groups.set([group])
|
||||
self.eco_account.share_with_list([])
|
||||
|
||||
success_urls = [
|
||||
self.index_url,
|
||||
self.detail_url,
|
||||
self.report_url,
|
||||
self.new_id_url,
|
||||
self.new_url,
|
||||
]
|
||||
fail_urls = [
|
||||
self.edit_url,
|
||||
self.state_new_url,
|
||||
self.action_new_url,
|
||||
self.deadline_new_url,
|
||||
self.state_remove_url,
|
||||
self.action_remove_url,
|
||||
self.new_doc_url,
|
||||
self.log_url,
|
||||
self.remove_url,
|
||||
]
|
||||
self.assert_url_fail(client, fail_urls)
|
||||
self.assert_url_success(client, success_urls)
|
||||
|
||||
84
compensation/tests/ecoaccount/test_workflow.py
Normal file
84
compensation/tests/ecoaccount/test_workflow.py
Normal file
@@ -0,0 +1,84 @@
|
||||
"""
|
||||
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user