# (c) cavaliba.com - test / views / admintools

from django.core.cache import cache as cache_django
from django.test import TestCase, override_settings
from django.urls import reverse

import app_home.cache as cache
from tests import helper


class TestViewsAdmintools(TestCase):
    def setUp(self):
        cache.clear()
        cache_django.clear()

    def test_admintools_noauth(self):
        response = self.client.get(reverse("app_home:admintools"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Please sign-in", status_code=200)

    def test_admintools_noperm(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(reverse("app_home:admintools"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertNotContains(response, "factoryResetModal", status_code=200)
        self.assertNotContains(response, "Admin tools", status_code=200)

    def test_admintools_revision_non_admin_user_denied(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(reverse("app_home:admintools_revision"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertNotContains(response, "Data Revisions", status_code=200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_admintools_access_with_permission(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(reverse("app_home:admintools"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "DB Reset", status_code=200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_admintools_modal_present(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(reverse("app_home:admintools"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "factoryResetModal", status_code=200)
        self.assertContains(response, "Confirm Factory Reset", status_code=200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_admintools_csrf_token_in_modal_form(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(reverse("app_home:admintools"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "csrfmiddlewaretoken", status_code=200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_admintools_revision_access_with_permission(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(reverse("app_home:admintools_revision"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Data Revisions", status_code=200)
        self.assertContains(response, "No more revision entries found", status_code=200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_admintools_revision_button_in_admintools_page(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(reverse("app_home:admintools"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Revision", status_code=200)
        self.assertContains(response, reverse("app_home:admintools_revision"), status_code=200)
        self.assertContains(
            response, "View and manage data revision tracking and history", status_code=200
        )
