import json
import pytest

from divvy.api import Divvy, DIVVY_V2_API_URL
from test_utils.mock import MockApi


class TestApi_UnitTests:

    @pytest.mark.unit
    def test_get_compliance_scores(self):
        # Set expected values
        expected_api_key = "divvy.api.key"
        expected_data = {"key1": "val1", "key2": "val2"}
        expected_data_json = json.dumps(expected_data)
        expected_resp_json = {"random": "stuff"}

        # Set up mocks
        mock_request = MockApi()
        setattr(mock_request, "url", DIVVY_V2_API_URL)
        setattr(mock_request, "method", "POST")
        setattr(mock_request, "headers", {})
        mock_resp = MockApi()
        setattr(mock_resp, "request", mock_request)
        mock_resp.on("raise_for_status").return_val(None)
        mock_resp.on("json").return_val(expected_resp_json)
        mock_http_session = MockApi()
        mock_http_session.on(
            "post",
            DIVVY_V2_API_URL + "/compliance/system/report",
            headers={
                "Accept": "application/json",
                "Api-Key": expected_api_key,
                "Content-Type": "application/json",
            },
            data=expected_data_json,
        ).return_val(mock_resp)

        # Test get_compliance_scores()
        divvy_api = Divvy(expected_api_key, http_session=mock_http_session)
        resp = divvy_api.get_compliance_scores(expected_data)
        assert resp == expected_resp_json
        mock_resp.assert_expectations()
        mock_http_session.assert_expectations()

    @pytest.mark.unit
    def test_get_cloud_listing(self):
        # Mock Request Object details.
        expected_api_key = "divvy.api.key"
        expected_data = {"key1": "val1", "key2": "val2"}
        expected_data_json = json.dumps(expected_data)
        expected_resp_json = {"random": "stuff"}

        # Creating Mock request object
        mock_request = MockApi()
        setattr(mock_request, "url", DIVVY_V2_API_URL)
        setattr(mock_request, "method", "POST")
        setattr(mock_request, "headers", {})

        # Creating Mock Response Object
        mock_resp = MockApi()
        setattr(mock_resp, "request", mock_request)

        mock_resp.on("raise_for_status").return_val(None)
        mock_resp.on("json").return_val(expected_resp_json)

        # Creating Mock Http Session
        mock_http_session = MockApi()
        mock_http_session.on(
            "post",
            DIVVY_V2_API_URL + "/public/clouds/list",
            headers={
                "Accept": "application/json",
                "Api-Key": expected_api_key,
                "Content-Type": "application/json",
            },
            data=expected_data_json,
        ).return_val(mock_resp)

        divvy_api = Divvy(expected_api_key, http_session=mock_http_session)
        resp = divvy_api.get_cloud_listing(expected_data)
        assert resp == expected_resp_json
        mock_resp.assert_expectations()
        mock_http_session.assert_expectations()

    @pytest.mark.unit
    def test_get_resources_with_tags(self):
        # Mock Request Object details.
        expected_api_key = "divvy.api.key"
        expected_data = {"key1": "val1", "key2": "val2", "cursor": None}
        expected_data_json = json.dumps(expected_data)
        # expected_resp_json = {'random': 'stuff'}
        expected_resp_json = {"resources": [], "total_count": None}

        # Creating Mock request object
        mock_request = MockApi()
        setattr(mock_request, "url", DIVVY_V2_API_URL)
        setattr(mock_request, "method", "POST")
        setattr(mock_request, "headers", {})

        # Creating Mock Response Object
        mock_resp = MockApi()
        setattr(mock_resp, "request", mock_request)

        mock_resp.on("raise_for_status").return_val(None)
        mock_resp.on("json").return_val(expected_resp_json)

        # Creating Mock Http Session
        mock_http_session = MockApi()
        mock_http_session.on(
            "post",
            DIVVY_V2_API_URL + "/public/resources/tags/search",
            headers={
                "Accept": "application/json",
                "Api-Key": expected_api_key,
                "Content-Type": "application/json",
            },
            data=expected_data_json,
        ).return_val(mock_resp)

        divvy_api = Divvy(expected_api_key, http_session=mock_http_session)
        resp = divvy_api.get_resources_with_tags(expected_data)
        assert resp == expected_resp_json
        mock_resp.assert_expectations()
        mock_http_session.assert_expectations()
