"""
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.models import DeadlineType, Deadline
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()
        
    def setUp(self) -> None:
        super().setUp()
        state = self.create_dummy_states()
        self.eco_account.before_states.set([state])
        self.eco_account.after_states.set([state])

        action = self.create_dummy_action()
        self.eco_account.actions.set([action])

        # Prepare urls
        self.index_url = reverse("compensation:acc:index", args=())
        self.new_url = reverse("compensation:acc:new", args=())
        self.new_id_url = reverse("compensation:acc:new-id", args=())
        self.detail_url = reverse("compensation:acc:detail", args=(self.eco_account.id,))
        self.log_url = reverse("compensation:acc:log", args=(self.eco_account.id,))
        self.edit_url = reverse("compensation:acc:edit", args=(self.eco_account.id,))
        self.remove_url = reverse("compensation:acc:remove", args=(self.eco_account.id,))
        self.report_url = reverse("compensation:acc:report", args=(self.eco_account.id,))

        self.state_new_url = reverse("compensation:acc:new-state", args=(self.eco_account.id,))
        self.state_edit_url = reverse("compensation:acc:state-edit", args=(self.eco_account.id, self.comp_state.id))
        self.state_remove_url = reverse("compensation:acc:state-remove", args=(self.eco_account.id, self.comp_state.id,))

        self.action_new_url = reverse("compensation:acc:new-action", args=(self.eco_account.id,))
        self.action_edit_url = reverse("compensation:acc:action-edit", args=(self.eco_account.id, self.comp_action.id))
        self.action_remove_url = reverse("compensation:acc:action-remove", args=(self.eco_account.id, self.comp_action.id,))

        self.deadline = Deadline.objects.get_or_create(
            type=DeadlineType.FINISHED,
            date="2020-01-01",
            comment="DEADLINE COMMENT"
        )[0]
        self.eco_account.deadlines.add(self.deadline)

        self.deadline_new_url = reverse("compensation:acc:new-deadline", args=(self.eco_account.id,))
        self.deadline_edit_url = reverse("compensation:acc:deadline-edit", args=(self.eco_account.id, self.deadline.id))
        self.deadline_remove_url = reverse("compensation:acc:deadline-remove", args=(self.eco_account.id, self.deadline.id))

        self.new_doc_url = reverse("compensation:acc:new-doc", args=(self.eco_account.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_user_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.state_edit_url,
            self.state_remove_url,
            self.action_new_url,
            self.action_edit_url,
            self.action_remove_url,
            self.deadline_new_url,
            self.deadline_edit_url,
            self.deadline_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_user_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.state_edit_url,
            self.state_remove_url,
            self.action_new_url,
            self.action_edit_url,
            self.action_remove_url,
            self.deadline_new_url,
            self.deadline_edit_url,
            self.deadline_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_user_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.state_edit_url,
            self.state_remove_url,
            self.action_new_url,
            self.action_edit_url,
            self.action_remove_url,
            self.new_doc_url,
            self.deadline_new_url,
            self.deadline_edit_url,
            self.deadline_remove_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_user_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.state_edit_url,
            self.state_remove_url,
            self.action_new_url,
            self.action_edit_url,
            self.action_remove_url,
            self.new_doc_url,
            self.log_url,
            self.remove_url,
            self.deadline_new_url,
            self.deadline_edit_url,
            self.deadline_remove_url,
        ]
        self.assert_url_fail(client, fail_urls)
        self.assert_url_success(client, success_urls)

