import pytest
from django.db import IntegrityError

from devices.models.device import Device
from devices.models.firewall import ArmaIndustrialFirewall

@pytest.mark.unit
@pytest.mark.django_db
class TestDevices:

    @pytest.fixture(autouse=True)
    def setup_tests(self, django_user_model):
        self.admin_user = django_user_model.objects.get(username='admin')
        Device.objects.create(ip='1.1.1.1', port='1500', type='firewall')

    def test_creating_device_and_firewall_with_same_port(self):
        device = Device.objects.create(ip='2.2.2.2', port='2500', type='endpoint')
        FIREWALL_DATA = {
            "name": "ADD IFTEST",
            "ip": "192.168.56.103",
            "key": "nWM0Pnj4w3DJHbkfIRQ2CbUdqIc0TMUYIHohRCSqWJ5TycVfLo3JlIyurmOXN7MaRMQv/hlUIPbD89Ng",
            "secret": "veREg8dbHC/V4hSCi6LBzuQ0NF5eeS/50d7K7Ahut6X0N/77peVQE5ucIJ/fyKhp0RNlbCHEcen2Rk8U",
            "port": "2500",
            "type": 'firewall'
        }
        with pytest.raises(IntegrityError) as e:
            ArmaIndustrialFirewall.objects.create(**FIREWALL_DATA)
            assert 'Key (port)=(2500) already exists.' in str(e.value)
