### README / tests

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


import app_home.cache as cache
from app_home.configuration import get_configuration

from app_user.models import SireneUser
from app_user.models import SireneGroup
from app_user.role import role_get_by_name
from app_data.models import DataClass
from app_data.models import DataSchema
from app_data.models import DataInstance





class PipelineCRUDTest(TestCase):

    fixtures = ["test"]

    # @classmethod
    # def setUpClass(cls):
    #     super().setUpClass()
    #     # load schemas / yaml init
    #     cavaliba_init(verbose=True)

    # @classmethod
    #  def setUpTestData(cls):
    #      """Use setUpTestData instead of setUpClass for loading test data as DB operations"""
    #     super().setUpTestData()
    #     cavaliba_init(verbose=False)


    def setUp(self):
        cache.clear()
        cache_django.clear()

    def tearDown(self):
        super().tearDown()
        cache_django.clear()






class TestDemo(TestCase):
    
    fixtures = ["test"]
    def setUp(self):
        #print("setUp: Run once for every test method to setup clean data.")
        cache.init()
        
        user = SireneUser.objects.create(login="unittest", firstname="unittestname")
        user.save()

        role = role_get_by_name("role_sirene_operator")
        role.users.add(user)
        role.save()



--------------------------------


    # def test_anonymous_public(self):
    #     response = self.client.get(reverse('app_user:index'))
    #     self.assertEqual(response.status_code, 200)




class FixtureTest(TestCase):

    pass
    # fixtures = ["test_data.json"]

    # def setUp(self):

    #     init_configuration_to_db()
    #     aaa_init_permissions()


    #     role = AAARole.objects.create(name="unittest")
    #     role.save()
    #     for perm in AAAPermission.objects.all():
    #         role.permissions.add(perm)
    #     role.save()

    #     user = SireneUser.objects.create(login="unittest", name="unittestname")
    #     user.roles.add(role)
    #     user.save()

    #     scope_clean()



    # def test_model_user(self):
    #     user = SireneUser.objects.get(login="unittest")
    #     self.assertEqual(user.name, 'unittestname')


    # def test_model_site(self):
    #     site = Site.objects.get(name="site01")
    #     #self.assertIsNotNone(site.id)
    #     self.assertEquals(site.name,"site01")


    # def test_get_scope_members(self):

    #     users = scope_get_members([], "user:unittest")
    #     self.assertEquals(len(users),1)

    #     users = scope_get_members([], "site:site01")
    #     self.assertEquals(len(users),0)




# class NoAuthAccessTestCase(TestCase):

#     def setUp(self):
#         init_configuration_to_db()
#         aaa_init_permissions()


#     def test_noauth_public(self):
#         response = self.client.get(reverse('index'))
#         self.assertEqual(response.status_code, 200)

#     def test_noauth_anonymous(self):
#         response = self.client.get(reverse('anonymous'))
#         self.assertEqual(response.status_code, 302)



# class AnonymousAccessTestCase(TestCase):

#     def setUp(self):
#         init_configuration_to_db()
#         aaa_init_permissions()
#         conf = Configuration.objects.get(key="TRUSTED_ANONYMOUS_IPS")
#         conf.value="0.0.0.0/0"
#         conf.save()


#     def test_anonymous_public(self):
#         response = self.client.get(reverse('anonymous'))
#         self.assertEqual(response.status_code, 200)


#     def test_anonymous(self):
#         response = self.client.get(reverse('anonymous'))
#         self.assertEqual(response.status_code, 200)


#     def test_anonymous_private(self):
#         response = self.client.get(reverse('private'))
#         self.assertEqual(response.status_code, 302)


# ----------------------------------------------------------
# Authenticated access with ALL perms
# ----------------------------------------------------------

# @override_settings(SIRENE_AUTH_MODE="unittest")
# class AuthAccessAllPermTestCase(TestCase):

#     def setUp(self):

#         init_configuration_to_db()
#         aaa_init_permissions()

#         role = AAARole.objects.create(name="unittest")
#         role.save()
#         for perm in AAAPermission.objects.all():
#             role.permissions.add(perm)
#         role.save()

#         user = SireneUser.objects.create(login="unittest", name="unittestname")
#         user.roles.add(role)
#         user.save()

#         Site.objects.create(name="site01", description="site01")


#     def test_auth_allperm_public(self):
#         response = self.client.get(reverse('index'))
#         self.assertEqual(response.status_code, 200)


#     def test_auth_allperm_anonymous(self):
#         response = self.client.get(reverse('anonymous'))
#         self.assertEqual(response.status_code, 200)


#     def test_auth_allperm_private(self):
#         response = self.client.get(reverse('private'))
#         self.assertEqual(response.status_code, 200)

#     def test_auth_allperm_selector(self):
#         response = self.client.get(reverse('selector'))
#         self.assertEqual(response.status_code, 200)

#     def test_auth_allperm_archive(self):
#         response = self.client.get(reverse('archive'))
#         self.assertEqual(response.status_code, 200)

 

# ----------------------------------------------------------
# Authenticated access with NO perms
# ----------------------------------------------------------

# @override_settings(SIRENE_AUTH_MODE="unittest")
# class AuthAccessNoPermTestCase(TestCase):

#     def setUp(self):

#         init_configuration_to_db()
#         aaa_init_permissions()

#         role = AAARole.objects.create(name="unittest")
#         role.save()
#         # for perm in AAAPermission.objects.all():
#         #     role.permissions.add(perm)
#         # role.save()

#         user = SireneUser.objects.create(login="unittest", name="unittestname")
#         user.roles.add(role)
#         user.save()

#         Site.objects.create(name="site01", description="site01")


#     def test_auth_noperm_public(self):
#         response = self.client.get(reverse('index'))
#         #self.assertRedirects(response, '/accounts/login/?next=/catalog/mybooks/')
#         self.assertEqual(response.status_code, 200)


#     def test_auth_noperm_anonymous(self):
#         response = self.client.get(reverse('anonymous'))
#         #self.assertRedirects(response, '/accounts/login/?next=/catalog/mybooks/')
#         self.assertEqual(response.status_code, 200)


    # def test_auth_noperm_private(self):
    #     response = self.client.get(reverse('private'))
    #     self.assertEqual(response.status_code, 302)





# -----------
# TODO
# -----------
# cache
# Log
# cavaliba version 
# loader
# models
# views:
# - /status/
# - /private/
# - configuration
# - log / purge



class FixtureTest(TestCase):

    pass
    # fixtures = ["test_data.json"]

    # def setUp(self):

    #     init_configuration_to_db()
    #     aaa_init_permissions()


    #     role = AAARole.objects.create(name="unittest")
    #     role.save()
    #     for perm in AAAPermission.objects.all():
    #         role.permissions.add(perm)
    #     role.save()

    #     user = SireneUser.objects.create(login="unittest", name="unittestname")
    #     user.roles.add(role)
    #     user.save()

    #     scope_clean()



    # def test_model_user(self):
    #     user = SireneUser.objects.get(login="unittest")
    #     self.assertEqual(user.name, 'unittestname')


    # def test_model_site(self):
    #     site = Site.objects.get(name="site01")
    #     #self.assertIsNotNone(site.id)
    #     self.assertEquals(site.name,"site01")


    # def test_get_scope_members(self):

    #     users = scope_get_members([], "user:unittest")
    #     self.assertEquals(len(users),1)

    #     users = scope_get_members([], "site:site01")
    #     self.assertEquals(len(users),0)




# class NoAuthAccessTestCase(TestCase):

#     def setUp(self):
#         init_configuration_to_db()
#         aaa_init_permissions()


#     def test_noauth_public(self):
#         response = self.client.get(reverse('index'))
#         self.assertEqual(response.status_code, 200)

#     def test_noauth_anonymous(self):
#         response = self.client.get(reverse('anonymous'))
#         self.assertEqual(response.status_code, 302)



# class AnonymousAccessTestCase(TestCase):

#     def setUp(self):
#         init_configuration_to_db()
#         aaa_init_permissions()
#         conf = Configuration.objects.get(key="TRUSTED_ANONYMOUS_IPS")
#         conf.value="0.0.0.0/0"
#         conf.save()


#     def test_anonymous_public(self):
#         response = self.client.get(reverse('anonymous'))
#         self.assertEqual(response.status_code, 200)


#     def test_anonymous(self):
#         response = self.client.get(reverse('anonymous'))
#         self.assertEqual(response.status_code, 200)


#     def test_anonymous_private(self):
#         response = self.client.get(reverse('private'))
#         self.assertEqual(response.status_code, 302)


# ----------------------------------------------------------
# Authenticated access with ALL perms
# ----------------------------------------------------------

# @override_settings(SIRENE_AUTH_MODE="unittest")
# class AuthAccessAllPermTestCase(TestCase):

#     def setUp(self):

#         init_configuration_to_db()
#         aaa_init_permissions()

#         role = AAARole.objects.create(name="unittest")
#         role.save()
#         for perm in AAAPermission.objects.all():
#             role.permissions.add(perm)
#         role.save()

#         user = SireneUser.objects.create(login="unittest", name="unittestname")
#         user.roles.add(role)
#         user.save()

#         Site.objects.create(name="site01", description="site01")


#     def test_auth_allperm_public(self):
#         response = self.client.get(reverse('index'))
#         self.assertEqual(response.status_code, 200)


#     def test_auth_allperm_anonymous(self):
#         response = self.client.get(reverse('anonymous'))
#         self.assertEqual(response.status_code, 200)


#     def test_auth_allperm_private(self):
#         response = self.client.get(reverse('private'))
#         self.assertEqual(response.status_code, 200)

#     def test_auth_allperm_selector(self):
#         response = self.client.get(reverse('selector'))
#         self.assertEqual(response.status_code, 200)


 

# ----------------------------------------------------------
# Authenticated access with NO perms
# ----------------------------------------------------------

# @override_settings(SIRENE_AUTH_MODE="unittest")
# class AuthAccessNoPermTestCase(TestCase):

#     def setUp(self):

#         init_configuration_to_db()
#         aaa_init_permissions()

#         role = AAARole.objects.create(name="unittest")
#         role.save()
#         # for perm in AAAPermission.objects.all():
#         #     role.permissions.add(perm)
#         # role.save()

#         user = SireneUser.objects.create(login="unittest", name="unittestname")
#         user.roles.add(role)
#         user.save()

#         Site.objects.create(name="site01", description="site01")


#     def test_auth_noperm_public(self):
#         response = self.client.get(reverse('index'))
#         #self.assertRedirects(response, '/accounts/login/?next=/catalog/mybooks/')
#         self.assertEqual(response.status_code, 200)


#     def test_auth_noperm_anonymous(self):
#         response = self.client.get(reverse('anonymous'))
#         #self.assertRedirects(response, '/accounts/login/?next=/catalog/mybooks/')
#         self.assertEqual(response.status_code, 200)


    # def test_auth_noperm_private(self):
    #     response = self.client.get(reverse('private'))
    #     self.assertEqual(response.status_code, 302)



