Files
@ 671a0bf3b7f7
Branch filter:
Location: conntrackt/conntrackt/tests/test_models.py
671a0bf3b7f7
3.4 KiB
text/x-python
Added representation tests for Entity. Added representation tests for Interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | # Django imports.
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.test import TestCase
# Application imports.
from conntrackt.models import Project, Location, Entity, Interface, Communication
class EntityTest(TestCase):
fixtures = ['test-data.json']
def test_incoming_communications(self):
"""
Test that we get correct list of incoming connections with the sample
data.
"""
entity = Entity.objects.get(name="Test Entity 1")
incoming = Communication.objects.filter(pk__in=(1, 2, 3, 5))
self.assertItemsEqual(entity.incoming_communications(), incoming)
def test_outgoing_communications(self):
"""
Test that we get correct list of outgoing connections with the sample
data.
"""
entity = Entity.objects.get(name="Test Entity 1")
outgoing = Communication.objects.filter(pk__in=(4, 6))
self.assertItemsEqual(entity.outgoing_communications(), outgoing)
def test_unique_communication(self):
"""
Test enforcement of unique communications.
"""
comm = Communication.objects.get(pk=1)
self.assertRaises(IntegrityError, Communication.objects.create, source=comm.source, destination=comm.destination, protocol=comm.protocol, port=comm.port, description="Duplicate communication.")
def test_project_same(self):
"""
Test enforcement of same project entities for communications.
"""
ent1 = Entity.objects.get(name="Test Entity 1")
ent1_eth0 = ent1.interface_set.get(name="eth0")
ent2 = Entity.objects.get(name="Other Project Test Entity")
ent2_eth0 = ent2.interface_set.get(name="eth0")
# Set-up a communication between different projects.
comm = Communication.objects.create(source=ent1_eth0, destination=ent2_eth0, protocol="ICMP", port="8", description="Ping.")
self.assertRaises(ValidationError, comm.full_clean)
def test_unsupported_protocol(self):
"""
Test enforcement of supported protocol.
"""
ent1 = Entity.objects.get(name="Test Entity 1")
ent1_eth0 = ent1.interface_set.get(name="eth0")
ent2 = Entity.objects.get(name="Test Entity 2")
ent2_eth0 = ent1.interface_set.get(name="eth0")
comm = Communication(source=ent1_eth0, destination=ent2_eth0, protocol="BOGUS", port="1234")
self.assertRaises(ValidationError, comm.full_clean)
def test_representation(self):
"""
Test the representation of entity.
"""
ent = Entity.objects.get(name="Test Entity 1")
representation = "Test Entity 1 (Test Project 1 - Test Location 1)"
self.assertEqual(str(ent), representation)
class InterfaceTest(TestCase):
fixtures = ['test-data.json']
def test_representation_single(self):
"""
Test representation of single IP address.
"""
interface = Entity.objects.get(name="Test Entity 1").interface_set.get(name="eth0")
representation = "Test Entity 1 (192.168.1.1)"
self.assertEqual(str(interface), representation)
def test_representation_subnet(self):
"""
Test representation of subnet.
"""
interface = Entity.objects.get(name="Test Subnet").interface_set.get(name="eth0")
representation = "Test Subnet (192.168.2.0/255.255.255.0)"
self.assertEqual(str(interface), representation)
|