""" Author: Michel Peltriaux Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany Contact: michel.peltriaux@sgdnord.rlp.de Created on: 26.10.21 """ from django.db.models import Q from django.urls import reverse from django.test.client import Client from compensation.tests.test_views import CompensationViewTestCase from ema.models import Ema from intervention.models import Responsibility from konova.models import Geometry from konova.settings import DEFAULT_GROUP, ETS_GROUP from user.models import UserActionLogEntry, UserAction class EmaViewTestCase(CompensationViewTestCase): """ Test cases for EMA. Since we inherit most tests functions from CompensationViewTestCase, we only need to add some EMA specific test functions """ ema = None @classmethod def setUpTestData(cls) -> None: super().setUpTestData() # Create dummy data and related objects, like states or actions cls.create_dummy_data() state = cls.create_dummy_states() action = cls.create_dummy_action() cls.ema.before_states.set([state]) cls.ema.after_states.set([state]) cls.ema.actions.set([action]) # Prepare urls cls.index_url = reverse("ema:index", args=()) cls.new_url = reverse("ema:new", args=()) cls.new_id_url = reverse("ema:new-id", args=()) cls.detail_url = reverse("ema:detail", args=(cls.ema.id,)) cls.log_url = reverse("ema:log", args=(cls.ema.id,)) cls.edit_url = reverse("ema:edit", args=(cls.ema.id,)) cls.remove_url = reverse("ema:remove", args=(cls.ema.id,)) cls.share_url = reverse("ema:share", args=(cls.ema.id, cls.ema.access_token,)) cls.share_create_url = reverse("ema:share-create", args=(cls.ema.id,)) cls.record_url = reverse("ema:record", args=(cls.ema.id,)) cls.report_url = reverse("ema:report", args=(cls.ema.id,)) cls.new_doc_url = reverse("ema:new-doc", args=(cls.ema.id,)) cls.state_new_url = reverse("ema:new-state", args=(cls.ema.id,)) cls.action_new_url = reverse("ema:new-action", args=(cls.ema.id,)) cls.deadline_new_url = reverse("ema:new-deadline", args=(cls.ema.id,)) cls.state_remove_url = reverse("ema:state-remove", args=(cls.ema.id, state.id,)) cls.action_remove_url = reverse("ema:action-remove", args=(cls.ema.id, action.id,)) @classmethod def create_dummy_data(cls): # Create dummy data # Create log entry action = UserActionLogEntry.objects.create( user=cls.superuser, action=UserAction.CREATED, ) # Create responsible data object responsibility_data = Responsibility.objects.create() geometry = Geometry.objects.create() cls.ema = Ema.objects.create( identifier="TEST", title="Test_title", created=action, geometry=geometry, responsible=responsibility_data, comment="Test", ) def test_logged_in_default_group_shared(self): """ Check correct status code for all requests OVERWRITES DEFAULT COMPENSATION TEST METHOD DUE TO SPECIFIC BEHAVIOUR OF EMAS Assumption: User logged in, is default group member and data is shared Normally default group would give access to all base functionalities. In case of EMAs we expect these requests to fail, since a user must be part of the ets group as well, not only default. 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 does not have any effect in here, since the default group will prohibit further functionality access # to this user self.ema.share_with_list([self.superuser]) success_urls = [ self.index_url, self.detail_url, self.report_url, ] fail_urls = [ 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_fail(client, fail_urls) self.assert_url_success(client, success_urls) def test_logged_in_default_group_unshared(self): """ Check correct status code for all requests OVERWRITES DEFAULT COMPENSATION TEST METHOD DUE TO SPECIFIC BEHAVIOUR OF EMAS Assumption: User logged in, is default group member and data is NOT shared Normally default group would give access to all base functionalities. In case of EMAs we expect these requests to fail, since a user must be part of the ets group as well, not only default. We check on the same tests as in the _shared test, since we want to make sure there is no difference in between shared and unshared, if the user is only part of the default group. 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 does not have any effect in here, since the default group will prohibit further functionality access # to this user self.ema.share_with_list([]) success_urls = [ self.index_url, self.detail_url, self.report_url, ] fail_urls = [ 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_fail(client, fail_urls) self.assert_url_success(client, success_urls) def test_logged_in_ets_group_shared(self): """ Check correct status code for all requests Assumption: User logged in, is conservation office group member and data is shared For EMAs we expect a user to be ETS and default group member to have full access to all functionalities, normally provided for default group members. Returns: """ client = Client() client.login(username=self.superuser.username, password=self.superuser_pw) groups = self.groups.filter(Q(name=ETS_GROUP)|Q(name=DEFAULT_GROUP)) self.superuser.groups.set(groups) # Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state self.ema.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_ets_group_unshared(self): """ Check correct status code for all requests Assumption: User logged in, is conservation office group member and data is NOT shared For EMAs we expect a user to be ETS and default group member to have full access to all functionalities, normally provided for default group members. Returns: """ client = Client() client.login(username=self.superuser.username, password=self.superuser_pw) groups = self.groups.filter(Q(name=ETS_GROUP)|Q(name=DEFAULT_GROUP)) self.superuser.groups.set(groups) # Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state self.ema.share_with_list([]) success_urls = [ self.index_url, self.detail_url, self.report_url, self.new_url, self.new_id_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_success(client, success_urls) self.assert_url_fail(client, fail_urls)