import os
import tempfile
from unittest.mock import patch

import pytest

from devices.models.device import Device
from devices.services.vector import VectorService

TMP_DIR_VECTOR = tempfile.TemporaryDirectory()


@patch('devices.services.vector.VECTOR_CONFIG_DIR', TMP_DIR_VECTOR.name)
@pytest.mark.unit
@pytest.mark.django_db
class TestVectorService:

    def test_create_vector_config(self):
        device = Device.objects.create(ip='2.2.2.2', port='9999', type='firewall')
        assert f'armaif_{device.pk}.toml' not in os.listdir(TMP_DIR_VECTOR.name)
        vector = VectorService(device)
        vector.update_config()
        assert f'armaif_{device.pk}.toml' in os.listdir(TMP_DIR_VECTOR.name)

    def test_delete_vector_config(self, remove_files_after_test_vector_config):
        device = Device.objects.create(ip='2.2.2.2', port='9999', type='firewall')
        test_file = open(f'{TMP_DIR_VECTOR.name}/armaif_{device.pk}.toml', 'w+')
        test_file.write('123')
        assert f'armaif_{device.pk}.toml' in os.listdir(TMP_DIR_VECTOR.name)
        vector = VectorService(device)
        vector.delete_config()
        assert f'armaif_{device.pk}.toml' not in os.listdir(TMP_DIR_VECTOR.name)
