"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 30.05.22

"""
from django.test import Client

from django.contrib.auth.models import Group
from django.urls import reverse

from intervention.models import Revocation
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
from konova.tests.test_views import BaseViewTestCase


class UserViewTestCase(BaseViewTestCase):

    @classmethod
    def setUpTestData(cls) -> None:
        super().setUpTestData()

    def setUp(self) -> None:
        super().setUp()
        self.team.users.add(self.superuser)
        self.team.admins.add(self.superuser)
        # Prepare urls
        self.index_url = reverse("user:index", args=())
        self.notification_url = reverse("user:notifications", args=())
        self.api_token_url = reverse("user:api-token", args=())
        self.contact_url = reverse("user:contact", args=(self.superuser.id,))
        self.team_url = reverse("user:team-index", args=())
        self.new_team_url = reverse("user:team-new", args=())
        self.data_team_url = reverse("user:team-data", args=(self.team.id,))
        self.edit_team_url = reverse("user:team-edit", args=(self.team.id,))
        self.remove_team_url = reverse("user:team-remove", args=(self.team.id,))
        self.leave_team_url = reverse("user:team-leave", args=(self.team.id,))

    def test_views_anonymous_user(self):
        """ Check correct status code for all requests

        Assumption: User not logged in

        Returns:

        """
        # Unknown client
        client = Client()

        login_redirect_base = f"{self.login_url}?next="
        fail_urls = {
            self.index_url: f"{login_redirect_base}{self.index_url}",
            self.notification_url: f"{login_redirect_base}{self.notification_url}",
            self.api_token_url: f"{login_redirect_base}{self.api_token_url}",
            self.contact_url: f"{login_redirect_base}{self.contact_url}",
            self.team_url: f"{login_redirect_base}{self.team_url}",
            self.new_team_url: f"{login_redirect_base}{self.new_team_url}",
            self.data_team_url: f"{login_redirect_base}{self.data_team_url}",
            self.edit_team_url: f"{login_redirect_base}{self.edit_team_url}",
            self.remove_team_url: f"{login_redirect_base}{self.remove_team_url}",
            self.leave_team_url: f"{login_redirect_base}{self.leave_team_url}",
        }

        for url in fail_urls:
            response = client.get(url, follow=True)
            self.assertEqual(response.redirect_chain[0], (f"{self.login_url}?next={url}", 302), msg=f"Failed for {url}. Redirect chain is {response.redirect_chain}")

    def test_views_logged_in(self):
        """ Check correct status code for all requests

        Assumption: User logged in but has no groups

        Returns:

        """
        # Login client
        client = Client()
        client.login(username=self.superuser.username, password=self.superuser_pw)
        self.superuser.groups.set([])
        success_urls = [
            self.index_url,
            self.notification_url,
            self.contact_url,
            self.team_url,
            self.new_team_url,
            self.data_team_url,
            self.edit_team_url,
            self.remove_team_url,
            self.leave_team_url,
        ]

        fail_urls = [
            self.api_token_url,  # expects default permission
        ]

        self.assert_url_success(client, success_urls)
        self.assert_url_fail(client, fail_urls)

        # Check for modified default user permission
        self.superuser.groups.add(
            Group.objects.get(
                name=DEFAULT_GROUP
            )
        )

        success_url = [
            self.api_token_url,  # must work now
        ]

        self.assert_url_success(client, success_url)

