diff --git a/conntrackt/tests/test_models.py b/conntrackt/tests/test_models.py --- a/conntrackt/tests/test_models.py +++ b/conntrackt/tests/test_models.py @@ -32,43 +32,6 @@ class EntityTest(TestCase): 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. @@ -102,3 +65,79 @@ class InterfaceTest(TestCase): 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)) +