Files
@ 5deeefd8d0fc
Branch filter:
Location: conntrackt/conntrackt/tests/test_models.py
5deeefd8d0fc
7.2 KiB
text/x-python
CONNT-4: Added support for updating entity information.
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | # 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 ProjectTest(TestCase):
def test_unique_name(self):
"""
Test if unique project name is enforced.
"""
project1 = Project(name="Test Project", description="This is a test project.")
project1.save()
project2 = Project(name="Test Project", description="This is a test project.")
self.assertRaises(IntegrityError, project2.save)
def test_representation(self):
"""
Test the representation of project.
"""
project = Project(name="Test Project", description="This is a test project.")
self.assertEqual(str(project), "Test Project")
def test_absolute_url(self):
"""
Tests if the absolute URL is generated properly.
"""
project = Project(pk=1, name="Test Project", description="This is a test project.")
self.assertEqual(project.get_absolute_url(), "/conntrackt/project/1/")
class LocationTest(TestCase):
def test_unique_name(self):
"""
Test if unique locationn name is enforced.
"""
location1 = Location(name="Test Location", description="This is a test location.")
location1.save()
location2 = Location(name="Test Location", description="This is a test location.")
self.assertRaises(IntegrityError, location2.save)
def test_representation(self):
"""
Test the representation of location.
"""
project = Location(name="Test Location", description="This is a test location.")
self.assertEqual(str(project), "Test Location")
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_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)
def test_unique_name(self):
"""
Test if unique entity name is enforced across same project.
"""
entity1 = Entity.objects.get(pk=1)
entity2 = Entity(name=entity1.name, description="Duplicate entity.", project=entity1.project, location=entity1.location)
self.assertRaises(IntegrityError, entity2.save)
def test_absolute_url(self):
"""
Tests if the absolute URL is generated properly.
"""
entity = Entity.objects.get(pk=1)
self.assertEqual(entity.get_absolute_url(), "/conntrackt/entity/1/")
class InterfaceTest(TestCase):
fixtures = ['test-data.json']
def test_unique_name(self):
"""
Test if unique interface name is enforced across same entity.
"""
entity = Entity.objects.get(pk=1)
interface = entity.interface_set.get(pk=1)
duplicate = Interface(name=interface.name, description="Duplicate interface.", entity=entity, address="10.10.10.10", netmask="255.255.255.255")
self.assertRaises(IntegrityError, duplicate.save)
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)
class CommunicationTest(TestCase):
fixtures = ['test-data.json']
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.assertRaisesRegexp(ValidationError, 'Source and destination entities do not belong to the same project', comm.full_clean)
def test_same_entity(self):
"""
Test enforcement of differing entities for communication.
"""
ent = Entity.objects.get(name="Test Entity 1")
ent_eth0 = ent.interface_set.get(name="eth0")
# Set-up a communication between same entity.
comm = Communication.objects.create(source=ent_eth0, destination=ent_eth0, protocol="ICMP", port="8", description="Ping.")
self.assertRaisesRegexp(ValidationError, "Source and destination entities are identical.", 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 = ent2.interface_set.get(name="eth0")
comm = Communication(source=ent1_eth0, destination=ent2_eth0, protocol="BOGUS", port="1234")
self.assertRaisesRegexp(ValidationError, "BOGUS is not a supported protocol.", comm.full_clean)
def test_edit_link(self):
"""
Tests the function for getting the edit link string.
"""
comm = Communication.objects.get(pk=1)
self.assertEqual("Edit", comm.edit_link())
def test_representation(self):
"""
Test the representation of communication.
"""
comm = Communication.objects.get(pk=1)
expected = "Test Entity 2 -> Test Entity 1 (TCP:22)"
self.assertEqual(expected, str(comm))
|