import json
import pytest
from CiscoEmailSecurity import Client


def get_fetch_data():
    with open('./test_data.json', 'r') as f:
        return json.loads(f.read())


test_data = get_fetch_data()


def test_date_to_cisco_date():
    from CiscoEmailSecurity import date_to_cisco_date
    res = date_to_cisco_date('2019-11-20 09:36:09')
    assert res == '2019-11-20T09:36:09.000Z'


@pytest.mark.parametrize(
    "limit, expected",
    [
        ('', 20),
        ('100', 20),
        ('15', 15)
    ]
)
def test_set_limit(limit, expected):
    from CiscoEmailSecurity import set_limit
    res = set_limit(limit)
    assert res == expected


def test_set_var_to_output_prefix():
    from CiscoEmailSecurity import set_var_to_output_prefix
    res = set_var_to_output_prefix('mail_incoming_traffic_summary')
    assert res == 'MailIncomingTrafficSummary'


def test_build_url_params_for_list_report():
    """
    Given:
        Arguments To flirt with.
    When:
        The function builds a URL filter from these arguments.
    Then:
        We check that the URL filter matches what the command asks for.

    """
    from CiscoEmailSecurity import build_url_params_for_list_report
    res = build_url_params_for_list_report(test_data['args_for_list_report'], 'reporting_system')
    assert res == test_data['url_params_for_list_reports']


def test_build_url_params_for_list_messages():
    """
    Given:
        Arguments To flirt with.
    When:
        The function builds a URL filter from these arguments.
    Then:
        We check that the URL filter matches what the command asks for.

    """
    from CiscoEmailSecurity import build_url_params_for_list_messages
    res = build_url_params_for_list_messages(test_data['args_for_list_messages'])
    assert res == test_data['url_params_for_list_messages']


def test_build_url_params_for_get_details():
    """
    Given:
        Arguments To flirt with.
    When:
        The function builds a URL filter from these arguments.
    Then:
        We check that the URL filter matches what the command asks for.

    """
    from CiscoEmailSecurity import build_url_params_for_get_details
    res = build_url_params_for_get_details(test_data['args_for_get_details'])
    assert res == test_data['url_params_for_get_details']


def test_build_url_params_for_spam_quarantine():
    """
    Given:
        Arguments To flirt with.
    When:
        The function builds a URL filter from these arguments.
    Then:
        We check that the URL filter matches what the command asks for.

    """
    from CiscoEmailSecurity import build_url_params_for_spam_quarantine
    res = build_url_params_for_spam_quarantine(test_data['args_for_spam_quarantine'])
    assert res == test_data['url_params_for_spam_quarantine']


def test_list_search_messages_command(requests_mock):
    """
    Given:
        Arguments for command - list_search_messages.
    When:
        The API gives us results according to the arguments we sent.
    Then:
        We check that what is in context (outputs, outputs_prefix, outputs_key_field)
        is what should be according to the arguments we sent to the API.

    """
    from CiscoEmailSecurity import list_search_messages_command
    requests_mock.post("https://ciscoemailsecurity/sma/api/v2.0/login", json=test_data['data_for_login'])
    requests_mock.get("https://ciscoemailsecurity/sma/api/v2.0/message-tracking/messages?"
                      "startDate=2017-02-14T09:51:46.000-0600.000Z&endDate=2017-02-14T09:51:46.000-0600.000Z"
                      "&searchOption=messages&ciscoHost=All_Hosts&offset=0&limit=20",
                      json=test_data['search_messages_response_data'])

    client = Client({"credentials": {"identifier": "a", "password": "b"}, "base_url": "https://ciscoemailsecurity/",
                     "insecure": False, "proxy": False, "timeout": "2000"})
    res = list_search_messages_command(client, {"start_date": "2017-02-14T09:51:46.000-0600",
                                                "end_date": "2017-02-14T09:51:46.000-0600"})
    assert res.outputs == test_data['search_messages_context']
    assert res.outputs_prefix == 'CiscoEmailSecurity.Message'
    assert res.outputs_key_field == 'attributes.mid'


def test_messages_to_human_readable():
    """
    Given:
        Messages response data.
    When:
        The function arranges the data and returns it in the Markdown table.
    Then:
        We check that the table that the function returns corresponds to the data that the function received.

    """
    from CiscoEmailSecurity import messages_to_human_readable
    res = messages_to_human_readable(test_data['search_messages_context'])
    assert res == test_data['messages_human_readable']


def test_list_get_message_details_command(requests_mock):
    """
    Given:
        Arguments for command - list_get_message_details.
    When:
        The API gives us results according to the arguments we sent.
    Then:
        We check that what is in context (outputs, outputs_prefix, outputs_key_field)
        is what should be according to the arguments we sent to the API.

    """
    from CiscoEmailSecurity import list_get_message_details_command
    requests_mock.post("https://ciscoemailsecurity/sma/api/v2.0/login", json=test_data['data_for_login'])
    requests_mock.get("https://ciscoemailsecurity/sma/api/v2.0/message-tracking/details?"
                      "startDate=2017-02-14T09:51:46.000-0600.000Z&endDate=2017-02-14T09:51:46.000-0600.000Z&"
                      "mid=None&icid=None",
                      json=test_data['get_message_details_response_data'])