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

import json

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

import app_home.cache as cache
from app_data.revision import revision_add_raw
from tests import helper


class APIAssetTest(TestCase):
    def setUp(self):
        cache.clear()
        self.header = helper.add_apikey_admin()
        helper.add_schema(classname="assettest")
        helper.add_instance(classname="assettest", keyname="obj01")
        for i in range(5):
            revision_add_raw(classname="assettest", keyname="obj01", action="edit")

    def test_api_asset(self):
        response = self.client.get(
            reverse("api:api_asset", kwargs={"skey": "assettest", "key": "obj01"}),
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual("obj01", data["keyname"])


class APIAssetRevisionTest(TestCase):
    def setUp(self):
        cache.clear()
        self.header = helper.add_apikey_admin()
        helper.add_schema(classname="revtest")
        helper.add_instance(classname="revtest", keyname="obj01")
        for i in range(5):
            revision_add_raw(classname="revtest", keyname="obj01", action="edit")

    def test_api_asset_no_rev_param(self):
        response = self.client.get(
            reverse("api:api_asset", kwargs={"skey": "revtest", "key": "obj01"}),
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertNotIn("_revision", data)

    def test_api_asset_rev_zero(self):
        response = self.client.get(
            reverse("api:api_asset", kwargs={"skey": "revtest", "key": "obj01"}) + "?rev=0",
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertNotIn("_revision", data)

    def test_api_asset_rev_positive(self):
        response = self.client.get(
            reverse("api:api_asset", kwargs={"skey": "revtest", "key": "obj01"}) + "?rev=3",
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertIn("_revision", data)
        self.assertEqual(len(data["_revision"]), 3)

    def test_api_asset_rev_entry_fields(self):
        response = self.client.get(
            reverse("api:api_asset", kwargs={"skey": "revtest", "key": "obj01"}) + "?rev=1",
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        entry = data["_revision"][0]
        self.assertIn("login", entry)
        self.assertIn("date", entry)
        self.assertIn("action", entry)

    def test_api_asset_rev_capped_at_available(self):
        response = self.client.get(
            reverse("api:api_asset", kwargs={"skey": "revtest", "key": "obj01"}) + "?rev=100",
            headers=self.header,
        )
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["_revision"]), 5)
