import json
import logging
from unittest import mock
import pytest
import requests_mock
import requests
import os

from signtractions.resources import exceptions
from signtractions.resources.utils import misc
from .utils.misc import sort_dictionary_sortable_values, compare_logs, mock_entry_point

# flake8: noqa: E501

LOG = logging.getLogger()
logging.basicConfig()
LOG.setLevel(logging.INFO)


def test_run_entrypoint(caplog):
    def side_effect_entrypoint(*args):
        LOG.warning("log generated by entrypoint")
        LOG.info("environment variable: {0}".format(os.environ["PYXIS_PASSWORD"]))
        return {"pyxis": "data"}

    with mock_entry_point(
        "pubtools-pyxis", "console_scripts", "pubtools-pyxis-get-operator-indices"
    ) as entrypoint:
        entrypoint.side_effect = side_effect_entrypoint
        ret_val = misc.run_entrypoint(
            ("pubtools-pyxis", "console_scripts", "pubtools-pyxis-get-operator-indices"),
            "pubtools-pyxis-get-operator-indices",
            [
                "--pyxis-krb-principal",
                "some-principal@REDHAT.COM",
                "--organization",
                "redhat-operators",
                "--ocp-versions-range",
                "v4.5",
                "--pyxis-krb-ktfile",
                "/etc/pub/some.keytab",
            ],
            {"PYXIS_PASSWORD": "some-password"},
        )
        expected_logs = [
            "Running task with arguments:",
            "pubtools-pyxis-get-operator-indices --pyxis-krb-principal some-principal@REDHAT\.COM --organization \\\\",
            "redhat-operators --ocp-versions-range v4.5 --pyxis-krb-ktfile /etc/pub/some\.keytab",
            "log generated by entrypoint",
            "environment variable: some-password",
        ]
        compare_logs(caplog, expected_logs)


def test_run_entrypoint_mod(caplog):
    def side_effect_entrypoint(*args):
        LOG.warning("log generated by entrypoint")
        return {"return": "value"}

    with mock_entry_point(
        "pubtools-sign", "modules", "pubtools-sign-cosign-signature-list"
    ) as entrypoint:
        entrypoint.side_effect = side_effect_entrypoint
        ret_val = misc.run_entrypoint_mod(
            ("pubtools-sign", "modules", "pubtools-sign-cosign-signature-list"),
            None,
            ["some-ep-argument"],
        )
        expected_logs = [
            "log generated by entrypoint",
        ]
        compare_logs(caplog, expected_logs)


def test_run_entrypoint_without_args(caplog):
    def side_effect_entrypoint(*args):
        LOG.warning("log generated by entrypoint")
        LOG.info("environment variable: {0}".format(os.environ["PYXIS_PASSWORD"]))
        return {"pyxis": "data"}

    with mock_entry_point(
        "pubtools-pyxis", "console_scripts", "pubtools-pyxis-get-operator-indices"
    ) as entrypoint:
        entrypoint.side_effect = side_effect_entrypoint
        ret_val = misc.run_entrypoint(
            ("pubtools-pyxis", "console_scripts", "pubtools-pyxis-get-operator-indices"),
            "pubtools-pyxis-get-operator-indices",
            [],
            {"PYXIS_PASSWORD": "some-password"},
        )
        expected_logs = [
            "Running task with arguments:",
            "pubtools-pyxis-get-operator-indices",
            "log generated by entrypoint",
            "environment variable: some-password",
        ]
        compare_logs(caplog, expected_logs)
