# (c) cavaliba.com - tests / test_api_version.py

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

import app_home.cache as cache
from tests import helper


class APIVersionTest(TestCase):
    def setUp(self):
        cache.clear()

    def _get(self, header=None):
        kwargs = {"headers": header} if header else {}
        return self.client.get(reverse("api:api_version"), **kwargs)

    def test_version_no_key(self):
        response = self._get()
        self.assertEqual(response.status_code, 401)

    def test_version_admin_key(self):
        header = helper.add_apikey_admin()
        response = self._get(header)
        self.assertEqual(response.status_code, 200)

    def test_version_with_p_info(self):
        header = helper.add_apikey(acl="p_info")
        response = self._get(header)
        self.assertEqual(response.status_code, 200)

    def test_version_without_p_info(self):
        header = helper.add_apikey(acl="p_schema_read")
        response = self._get(header)
        self.assertEqual(response.status_code, 401)

    def test_version_empty_acl(self):
        header = helper.add_apikey(acl="")
        response = self._get(header)
        self.assertEqual(response.status_code, 401)

    def test_version_role_admin_acl(self):
        header = helper.add_apikey(acl="role:role_admin")
        response = self._get(header)
        self.assertEqual(response.status_code, 200)
