# (c) cavaliba.com - tests / instance enable-disable views

from django.test import override_settings
from django.test import TestCase
from django.urls import reverse

from tests import helper
from app_data.models import DataInstance


def _make_instance():
    helper.add_schema('tschema_ed', displayname='ED Schema')
    helper.add_instance('tschema_ed', 'inst_ed01')
    return DataInstance.objects.get(classname='tschema_ed', keyname='inst_ed01')


class InstanceEnableDisableViewTest(TestCase):

    fixtures = ["init"]

    def setUp(self):
        pass


    # ------------------------------------------------------------------
    # instance_enable
    # ------------------------------------------------------------------

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_enable_post_enables_disabled_instance(self):
        helper.add_admin_user(login="unittest")
        iobj = _make_instance()
        iobj.is_enabled = False
        iobj.save()

        response = self.client.post(
            reverse('app_data:instance_enable', kwargs={'id': iobj.id}),
            follow=True)

        self.assertEqual(response.status_code, 200)
        self.assertTrue(DataInstance.objects.get(id=iobj.id).is_enabled)


    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_enable_post_on_already_enabled_instance(self):
        helper.add_admin_user(login="unittest")
        iobj = _make_instance()
        iobj.is_enabled = True
        iobj.save()

        response = self.client.post(
            reverse('app_data:instance_enable', kwargs={'id': iobj.id}),
            follow=True)

        self.assertEqual(response.status_code, 200)
        self.assertTrue(DataInstance.objects.get(id=iobj.id).is_enabled)


    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_enable_get_rejected_instance_unchanged(self):
        helper.add_admin_user(login="unittest")
        iobj = _make_instance()
        iobj.is_enabled = False
        iobj.save()

        response = self.client.get(
            reverse('app_data:instance_enable', kwargs={'id': iobj.id}),
            follow=True)

        self.assertEqual(response.status_code, 200)
        self.assertFalse(DataInstance.objects.get(id=iobj.id).is_enabled)


    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_enable_no_update_permission_redirects(self):
        helper.add_user_with_perm(login="unittest", perms=["p_data_access"])
        iobj = _make_instance()
        iobj.is_enabled = False
        iobj.save()

        response = self.client.post(
            reverse('app_data:instance_enable', kwargs={'id': iobj.id}),
            follow=True)

        self.assertEqual(response.status_code, 200)
        self.assertFalse(DataInstance.objects.get(id=iobj.id).is_enabled)


    def test_enable_noauth_redirects(self):
        iobj = _make_instance()

        response = self.client.post(
            reverse('app_data:instance_enable', kwargs={'id': iobj.id}))

        self.assertEqual(response.status_code, 302)


    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_enable_unknown_id_redirects_to_private(self):
        helper.add_admin_user(login="unittest")

        response = self.client.post(
            reverse('app_data:instance_enable', kwargs={'id': 999999}),
            follow=True)

        self.assertEqual(response.status_code, 200)


    # ------------------------------------------------------------------
    # instance_disable
    # ------------------------------------------------------------------

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_disable_post_disables_enabled_instance(self):
        helper.add_admin_user(login="unittest")
        iobj = _make_instance()
        iobj.is_enabled = True
        iobj.save()

        response = self.client.post(
            reverse('app_data:instance_disable', kwargs={'id': iobj.id}),
            follow=True)

        self.assertEqual(response.status_code, 200)
        self.assertFalse(DataInstance.objects.get(id=iobj.id).is_enabled)


    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_disable_post_on_already_disabled_instance(self):
        helper.add_admin_user(login="unittest")
        iobj = _make_instance()
        iobj.is_enabled = False
        iobj.save()

        response = self.client.post(
            reverse('app_data:instance_disable', kwargs={'id': iobj.id}),
            follow=True)

        self.assertEqual(response.status_code, 200)
        self.assertFalse(DataInstance.objects.get(id=iobj.id).is_enabled)


    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_disable_get_rejected_instance_unchanged(self):
        helper.add_admin_user(login="unittest")
        iobj = _make_instance()
        iobj.is_enabled = True
        iobj.save()

        response = self.client.get(
            reverse('app_data:instance_disable', kwargs={'id': iobj.id}),
            follow=True)

        self.assertEqual(response.status_code, 200)
        self.assertTrue(DataInstance.objects.get(id=iobj.id).is_enabled)


    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_disable_no_update_permission_redirects(self):
        helper.add_user_with_perm(login="unittest", perms=["p_data_access"])
        iobj = _make_instance()
        iobj.is_enabled = True
        iobj.save()

        response = self.client.post(
            reverse('app_data:instance_disable', kwargs={'id': iobj.id}),
            follow=True)

        self.assertEqual(response.status_code, 200)
        self.assertTrue(DataInstance.objects.get(id=iobj.id).is_enabled)


    def test_disable_noauth_redirects(self):
        iobj = _make_instance()

        response = self.client.post(
            reverse('app_data:instance_disable', kwargs={'id': iobj.id}))

        self.assertEqual(response.status_code, 302)


    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_disable_unknown_id_redirects_to_private(self):
        helper.add_admin_user(login="unittest")

        response = self.client.post(
            reverse('app_data:instance_disable', kwargs={'id': 999999}),
            follow=True)

        self.assertEqual(response.status_code, 200)
