from FeedCiscoSecureMalwareAnalytics import Client, fetch_indicators, fetch_indicators_command
from CommonServerPython import *
from test_data.feed_data import banking_dns_response, sinkholed_ip_dns_response


def test_fetch_indicators(requests_mock):
    """Unit test
    Given
    - fetch incidents command
    - feed name
    - command raw response
    When
    - run the fetch indicators command.
    Then
    - Validate creates indicators and unifies if they are the same
    """
    first_fetch = arg_to_datetime(arg='today', arg_name='First fetch')
    client = Client(api_key='1234', verify=False, feed_name=['sinkholed-ip-dns'], first_fetch=first_fetch,
                    tlp_color="", feed_tags="")
    requests_mock.get(
        f'https://panacea.threatgrid.com/api/v3/feeds/sinkholed-ip-dns.json?api_key={client._api_key}',
        json=sinkholed_ip_dns_response,
    )
    indicators, status = fetch_indicators(client, None)
    assert len(indicators) == 15


def test_fetch_indicators_command_list(requests_mock, mocker):
    """Unit test
    Given
    - fetch incidents command
    - list of feed names
    - command raw response
    When
    - run the fetch indicators command with 2 feed names.
    Then
    - Validate creates indicators and unifies if they are the same
    _ Validate that the fields: 'FeedRelatedIndicators' and 'tags' have been updated properly
    """
    first_fetch = arg_to_datetime(arg='today', arg_name='First fetch')
    client = Client(
        api_key='1234', verify=False, feed_name=['sinkholed-ip-dns', 'banking-dns'], first_fetch=first_fetch,
        tlp_color="", feed_tags="")
    requests_mock.get(
        f'https://panacea.threatgrid.com/api/v3/feeds/sinkholed-ip-dns.json?api_key={client._api_key}',
        json=sinkholed_ip_dns_response,
    )
    requests_mock.get(
        f'https://panacea.threatgrid.com/api/v3/feeds/banking-dns.json?api_key={client._api_key}',
        json=banking_dns_response,
    )

    a = mocker.patch.object(demisto, "createIndicators")
    fetch_indicators_command(client)

    fetch_indicators_command(client)
    for indicator in a.call_args.args[0]:
        if indicator['value'] == 'Example1.com':
            assert len(indicator['fields']['FeedRelatedIndicators']) == 6
            assert len(indicator['fields']['Tags']) == 2
        if indicator['value'] == 'Example3.com':
            assert len(indicator['fields']['FeedRelatedIndicators']) == 2
            assert len(indicator['fields']['Tags']) == 1
    assert len(a.call_args.args[0]) == 28

from FeedCiscoSecureMalwareAnalytics import Client, fetch_indicators, fetch_indicators_command
from CommonServerPython import *
from test_data.feed_data import banking_dns_response, sinkholed_ip_dns_response


def test_fetch_indicators(requests_mock):
    """Unit test
    Given
    - fetch incidents command
    - feed name
    - command raw response
    When
    - run the fetch indicators command.
    Then
    - Validate creates indicators and unifies if they are the same
    """
    first_fetch = arg_to_datetime(arg='today', arg_name='First fetch')
    client = Client(api_key='1234', verify=False, feed_name=['sinkholed-ip-dns'], first_fetch=first_fetch,
                    tlp_color="", feed_tags="")
    requests_mock.get(
        f'https://panacea.threatgrid.com/api/v3/feeds/sinkholed-ip-dns.json?api_key={client._api_key}',
        json=sinkholed_ip_dns_response,
    )
    indicators, status = fetch_indicators(client, None)
    assert len(indicators) == 15


def test_fetch_indicators_command_list(requests_mock, mocker):
    """Unit test
    Given
    - fetch incidents command
    - list of feed names
    - command raw response
    When
    - run the fetch indicators command with 2 feed names.
    Then
    - Validate creates indicators and unifies if they are the same
    _ Validate that the fields: 'FeedRelatedIndicators' and 'tags' have been updated properly
    """
    first_fetch = arg_to_datetime(arg='today', arg_name='First fetch')
    client = Client(
        api_key='1234', verify=False, feed_name=['sinkholed-ip-dns', 'banking-dns'], first_fetch=first_fetch,
        tlp_color="", feed_tags="")
    requests_mock.get(
        f'https://panacea.threatgrid.com/api/v3/feeds/sinkholed-ip-dns.json?api_key={client._api_key}',
        json=sinkholed_ip_dns_response,
    )
    requests_mock.get(
        f'https://panacea.threatgrid.com/api/v3/feeds/banking-dns.json?api_key={client._api_key}',
        json=banking_dns_response,
    )

    a = mocker.patch.object(demisto, "createIndicators")
    fetch_indicators_command(client)

    fetch_indicators_command(client)
    for indicator in a.call_args.args[0]:
        if indicator['value'] == 'Example1.com':
            assert len(indicator['fields']['FeedRelatedIndicators']) == 6
            assert len(indicator['fields']['Tags']) == 2
        if indicator['value'] == 'Example3.com':
            assert len(indicator['fields']['FeedRelatedIndicators']) == 2
            assert len(indicator['fields']['Tags']) == 1
    assert len(a.call_args.args[0]) == 28

Unit test     Given     - fetch incidents command     - feed name     - command raw response     When     - run the fetch indicators command.     Then     - Validate creates indicators and unifies if they are the same Unit test     Given     - fetch incidents command     - list of feed names     - command raw response     When     - run the fetch indicators command with 2 feed names.     Then     - Validate creates indicators and unifies if they are the same     _ Validate that the fields: 'FeedRelatedIndicators' and 'tags' have been updated properly