# (c) cavaliba.com - test / views / IAM user

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

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_user_debug(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(reverse("app_data:debug"))
        self.assertEqual(response.status_code, 200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_user_logout(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(reverse("app_data:logout"), follow=True)
        self.assertEqual(response.status_code, 200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_user_list(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(
            reverse("app_data:instance_list", kwargs={"classname": "user"}), follow=True
        )
        self.assertEqual(response.status_code, 200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_user_detail(self):
        admin = helper.add_admin_user(login="unittest")
        response = self.client.get(
            reverse("app_data:instance_detail", kwargs={"id": admin.id}), follow=True
        )
        self.assertEqual(response.status_code, 200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_user_edit_new(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(
            reverse("app_data:instance_new", kwargs={"classname": "user"}), follow=True
        )
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "csrfmiddlewaretoken")

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_user_edit_1(self):
        admin = helper.add_admin_user(login="unittest")
        response = self.client.get(
            reverse("app_data:instance_edit", kwargs={"id": admin.id}), follow=True
        )
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "csrfmiddlewaretoken")

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_user_edit_post(self):
        admin = helper.add_admin_user(login="unittest")
        url = reverse("app_data:instance_edit", kwargs={"id": admin.id})

        response = self.client.get(url, follow=True)
        self.assertEqual(response.status_code, 200)

        response = self.client.post(url, data={}, follow=True)
        self.assertEqual(response.status_code, 200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_user_delete_1(self):
        admin = helper.add_admin_user(login="unittest")
        user = helper.add_user(login="todelete")
        response = self.client.post(
            reverse("app_data:instance_delete", kwargs={"id": user.id}), follow=True
        )
        self.assertEqual(response.status_code, 200)
