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

import json

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

from tests import helper

import app_home.cache as cache
from app_user.models import SirenePermission


class APIPermissionTest(TestCase):

    fixtures = ["test"]

    def setUp(self):
        cache.clear()
        self.header = helper.add_apikey_admin()
        SirenePermission.objects.get_or_create(keyname="p_test01", defaults={"displayname": "Test 01"})
        SirenePermission.objects.get_or_create(keyname="p_test02", defaults={"displayname": "Test 02"})
        SirenePermission.objects.get_or_create(keyname="p_test03", defaults={"displayname": "Test 03"})


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


    def test_api_permission_list(self):
        response = self.client.get(
            reverse('api:api_permission'),
            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_permission_list_fields(self):
        response = self.client.get(
            reverse('api:api_permission') + '?search=p_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"] == "p_test01")
        self.assertIn("classname", entry)
        self.assertEqual(entry["classname"], "_permission")
        self.assertIn("keyname", entry)


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


    def test_api_permission_by_id(self):
        p = SirenePermission.objects.get(keyname="p_test02")
        response = self.client.get(
            reverse('api:api_permission', kwargs={'id': p.id}),
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertIsInstance(data, dict)
        self.assertEqual(data["keyname"], "p_test02")


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


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


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


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