import os

from unittest import mock
import pytest

from assets.models.assets import Asset
from console.models import Connection, ConnectionType
from core.utils import dtnow

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


@pytest.fixture()
def create_elk_function_test_data():
    """ Creating Assets, that are needed for testing connection creation process """
    test_asset_ips = ['192.168.1.20', '192.168.1.21', '10.10.1.21', '10.10.1.11', '17.234.12.34']
    Asset.objects.bulk_create([Asset(name=f'{i}_pc', ip=i, sensor='armaif_1') for i in test_asset_ips])


@pytest.fixture()
def create_filter_test_data():
    """ Fixture for adding auto network map test data for filters tests """
    # Purge any other Asset and Connection instances
    Asset.objects.all().delete()
    Connection.objects.all().delete()
    test_asset_ips = ['192.168.1.21', '192.168.1.22', '192.168.1.23', '192.168.1.24', '192.168.1.25']
    Asset.objects.bulk_create([Asset(name=f'{i}_pc', ip=i, sensor='armaif_1') for i in test_asset_ips])
    test_connections_addresses_list = [
        ['192.168.1.21', '192.168.1.22'],
        ['192.168.1.21', '192.168.1.23'],
        ['192.168.1.21', '192.168.1.24'],
    ]
    tcp_protocol = ConnectionType.objects.create(name='TCP')
    udp_protocol = ConnectionType.objects.create(name='UDP')
    Connection.objects.bulk_create([Connection(src_asset=Asset.objects.get(ip=i[0]),
                                               dst_asset=Asset.objects.get(ip=i[1]),
                                               connection_protocol=tcp_protocol) for i in test_connections_addresses_list])
    with mock.patch('django.utils.timezone.now') as mocked_search:
        mocked_search.return_value = dtnow(days=10)
        Connection.objects.create(src_asset=Asset.objects.get(ip='192.168.1.22'),
                                  dst_asset=Asset.objects.get(ip='192.168.1.24'),
                                  connection_protocol=udp_protocol)
