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

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 TestViewsHome(TestCase):
    def setUp(self):
        cache.clear()
        cache_django.clear()

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

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_view_home_index_auth_default_role(self):
        helper.add_user_with_perm(login="unittest", perms=[])
        response = self.client.get(reverse("app_home:index"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Welcome", status_code=200)

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

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_view_home_private_auth_noperm_but_default_role(self):
        helper.add_user_with_perm(login="unittest", perms=[])
        response = self.client.get(reverse("app_home:private"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Welcome", status_code=200)
        self.assertContains(response, "Logout", status_code=200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_view_home_authenticated(self):
        helper.add_user_with_perm(login="unittest", perms=["p_home_access"])
        response = self.client.get(reverse("app_home:private"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "unittest", status_code=200)
        self.assertContains(response, "Welcome", status_code=200)
        self.assertContains(response, "Logout", status_code=200)
