# (c) cavaliba.com - tests / api / role

import json

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

from tests import helper

import app_home.cache as cache


class APIRoleTest(TestCase):

    fixtures = ["test"]

    def setUp(self):
        cache.clear()
        self.header = helper.add_apikey_admin()
        helper.add_role("role_test01")
        helper.add_role("role_test02")
        helper.add_role("role_test03")


    def test_api_role_no_auth(self):
        response = self.client.get(reverse('api:api_role'))
        self.assertEqual(response.status_code, 401)


    def test_api_role_list(self):
        response = self.client.get(
            reverse('api:api_role'),
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertIsInstance(data, list)
        self.assertGreaterEqual(len(data), 3)


    def test_api_role_list_fields(self):
        response = self.client.get(
            reverse('api:api_role') + '?search=role_test',
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        entry = next(e for e in data if e["keyname"] == "role_test01")
        self.assertIn("classname", entry)
        self.assertEqual(entry["classname"], "_role")
        self.assertIn("keyname", entry)
        self.assertIn("users", entry)
        self.assertIn("subgroups", entry)
        self.assertIn("permissions", entry)


    def test_api_role_by_key(self):
        response = self.client.get(
            reverse('api:api_role', kwargs={'key': 'role_test01'}),
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertIsInstance(data, dict)
        self.assertEqual(data["keyname"], "role_test01")


    def test_api_role_by_id(self):
        from app_user.models import SireneGroup
        r = SireneGroup.objects.get(keyname="role_test02")
        response = self.client.get(
            reverse('api:api_role', kwargs={'id': r.id}),
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertIsInstance(data, dict)
        self.assertEqual(data["keyname"], "role_test02")


    def test_api_role_by_key_not_found(self):
        response = self.client.get(
            reverse('api:api_role', kwargs={'key': 'doesnotexist'}),
            headers=self.header,
        )
        self.assertEqual(response.status_code, 404)


    def test_api_role_by_id_not_found(self):
        response = self.client.get(
            reverse('api:api_role', kwargs={'id': 999999}),
            headers=self.header,
        )
        self.assertEqual(response.status_code, 404)


    def test_api_role_search(self):
        response = self.client.get(
            reverse('api:api_role') + '?search=role_test',
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertGreaterEqual(len(data), 3)


    def test_api_role_search_no_match(self):
        response = self.client.get(
            reverse('api:api_role') + '?search=zzznomatch',
            headers=self.header,
        )
        self.assertEqual(response.status_code, 404)
