# (c) cavaliba.com - test / factory reset / access control

import tempfile

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 app_data.models import DataClass
from tests import helper


class TestFactoryResetAccess(TestCase):
    def setUp(self):
        cache.clear()
        cache_django.clear()
        self.url = reverse("app_home:admintools_factory_reset")
        self.tmpdir = tempfile.mkdtemp()
        self.schema_count = DataClass.objects.count()
        self.assertGreater(self.schema_count, 0)

    def _post(self, **data):
        # every folder pointed at a temp dir: a reset that wrongly goes through
        # must not wipe the developer's real files/ folder
        with override_settings(
            CAVALIBA_FILESTORE=self.tmpdir,
            CAVALIBA_EXPORT_FOLDER=self.tmpdir,
            CAVALIBA_MAIL_FOLDER=self.tmpdir,
            CAVALIBA_SMS_FOLDER=self.tmpdir,
        ):
            return self.client.post(self.url, data, follow=True)

    @override_settings(CAVALIBA_ADMIN_PASSWORD="s3cret")
    def test_noauth(self):
        response = self._post(admin_password="s3cret")
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Sign in to your account", status_code=200)
        self.assertEqual(DataClass.objects.count(), self.schema_count)

    @override_settings(CAVALIBA_AUTH_MODE="unittest", CAVALIBA_ADMIN_PASSWORD="s3cret")
    def test_admintools_permission_without_admin_flag_denied(self):
        helper.add_user_with_perm(login="unittest", perms=["p_admintools"])
        response = self._post(admin_password="s3cret")
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "An admin account is required.", status_code=200)
        self.assertEqual(DataClass.objects.count(), self.schema_count)

    @override_settings(CAVALIBA_AUTH_MODE="unittest", CAVALIBA_ADMIN_PASSWORD=None)
    def test_admin_without_env_password_denied(self):
        helper.add_admin_user(login="unittest")
        response = self._post(admin_password="s3cret")
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "CAVALIBA_ADMIN_PASSWORD", status_code=200)
        self.assertEqual(DataClass.objects.count(), self.schema_count)

    @override_settings(CAVALIBA_AUTH_MODE="unittest", CAVALIBA_ADMIN_PASSWORD="s3cret")
    def test_admin_with_wrong_password_denied(self):
        helper.add_admin_user(login="unittest")
        response = self._post(admin_password="wrong")
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Wrong admin account password.", status_code=200)
        self.assertEqual(DataClass.objects.count(), self.schema_count)

    @override_settings(CAVALIBA_AUTH_MODE="unittest", CAVALIBA_ADMIN_PASSWORD="s3cret")
    def test_get_redirects_without_reset(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(self.url, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Admin tools", status_code=200)
        self.assertEqual(DataClass.objects.count(), self.schema_count)
