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 @@ -6,6 +6,13 @@ from django.test import TestCase # Application imports. from conntrackt.models import Project, Location, Entity, Interface, Communication +# Test imports. +from .factories import ProjectFactory, LocationFactory +from .factories import ServerEntityFactory, ServerInterfaceFactory +from .factories import SubnetEntityFactory, SubnetInterfaceFactory +from .factories import CommunicationFactory +from .factories import setup_test_data + class ProjectTest(TestCase): @@ -14,18 +21,16 @@ class ProjectTest(TestCase): Test if unique project name is enforced. """ - project1 = Project(name="Test Project", description="This is a test project.") - project1.save() + project = ProjectFactory() - project2 = Project(name="Test Project", description="This is a test project.") - self.assertRaises(IntegrityError, project2.save) + self.assertRaises(IntegrityError, ProjectFactory, name=project.name) def test_representation(self): """ Test the representation of project. """ - project = Project(name="Test Project", description="This is a test project.") + project = ProjectFactory(name="Test Project") self.assertEqual(str(project), "Test Project") @@ -34,7 +39,7 @@ class ProjectTest(TestCase): Tests if the absolute URL is generated properly. """ - project = Project(pk=1, name="Test Project", description="This is a test project.") + project = ProjectFactory(pk=1) self.assertEqual(project.get_absolute_url(), "/conntrackt/project/1/") @@ -43,27 +48,31 @@ class LocationTest(TestCase): def test_unique_name(self): """ - Test if unique locationn name is enforced. + Test if unique location name is enforced. """ - location1 = Location(name="Test Location", description="This is a test location.") - location1.save() + location = LocationFactory() - location2 = Location(name="Test Location", description="This is a test location.") - self.assertRaises(IntegrityError, location2.save) + self.assertRaises(IntegrityError, LocationFactory, name=location.name) def test_representation(self): """ Test the representation of location. """ - project = Location(name="Test Location", description="This is a test location.") + project = LocationFactory(name="Test Location") self.assertEqual(str(project), "Test Location") class EntityTest(TestCase): - fixtures = ['test-data.json'] + + def setUp(cls): + """ + Set-up some test data. + """ + + setup_test_data() def test_incoming_communications(self): """ @@ -71,7 +80,7 @@ class EntityTest(TestCase): data. """ - entity = Entity.objects.get(name="Test Entity 1") + entity = Entity.objects.get(pk=1) incoming = Communication.objects.filter(pk__in=(1, 2, 3, 5)) self.assertItemsEqual(entity.incoming_communications(), incoming) @@ -82,7 +91,7 @@ class EntityTest(TestCase): data. """ - entity = Entity.objects.get(name="Test Entity 1") + entity = Entity.objects.get(pk=1) outgoing = Communication.objects.filter(pk__in=(4, 6)) self.assertItemsEqual(entity.outgoing_communications(), outgoing) @@ -92,7 +101,7 @@ class EntityTest(TestCase): Test the representation of entity. """ - ent = Entity.objects.get(name="Test Entity 1") + ent = Entity.objects.get(pk=1) representation = "Test Entity 1 (Test Project 1 - Test Location 1)" self.assertEqual(str(ent), representation) @@ -104,9 +113,9 @@ class EntityTest(TestCase): entity1 = Entity.objects.get(pk=1) - entity2 = Entity(name=entity1.name, description="Duplicate entity.", project=entity1.project, location=entity1.location) + entity_dup = Entity(name=entity1.name, description="Duplicate entity.", project=entity1.project, location=entity1.location) - self.assertRaises(IntegrityError, entity2.save) + self.assertRaises(IntegrityError, entity_dup.save) def test_absolute_url(self): """ @@ -131,7 +140,13 @@ class EntityTest(TestCase): class InterfaceTest(TestCase): - fixtures = ['test-data.json'] + + def setUp(self): + """ + Set-up some test data. + """ + + setup_test_data() def test_unique_name(self): """ @@ -174,14 +189,20 @@ class InterfaceTest(TestCase): 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)" + interface = Entity.objects.get(pk=4).interface_set.get(name="switch0") + representation = "Test Subnet 4 (10.10.4.0/255.255.255.0)" self.assertEqual(str(interface), representation) class CommunicationTest(TestCase): - fixtures = ['test-data.json'] + + def setUp(self): + """ + Set-up some test data. + """ + + setup_test_data() def test_unique_communication(self): """ @@ -197,13 +218,13 @@ class CommunicationTest(TestCase): Test enforcement of same project entities for communications. """ - ent1 = Entity.objects.get(name="Test Entity 1") + ent1 = Entity.objects.get(pk=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") + ent5 = Entity.objects.get(pk=5) + ent5_eth0 = ent5.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.") + comm = Communication.objects.create(source=ent1_eth0, destination=ent5_eth0, protocol="ICMP", port="8", description="Ping.") self.assertRaisesRegexp(ValidationError, 'Source and destination entities do not belong to the same project', comm.full_clean) @@ -212,7 +233,7 @@ class CommunicationTest(TestCase): Test enforcement of differing entities for communication. """ - ent = Entity.objects.get(name="Test Entity 1") + ent = Entity.objects.get(pk=1) ent_eth0 = ent.interface_set.get(name="eth0") # Set-up a communication between same entity. @@ -225,9 +246,9 @@ class CommunicationTest(TestCase): Test enforcement of supported protocol. """ - ent1 = Entity.objects.get(name="Test Entity 1") + ent1 = Entity.objects.get(pk=1) ent1_eth0 = ent1.interface_set.get(name="eth0") - ent2 = Entity.objects.get(name="Test Entity 2") + ent2 = Entity.objects.get(pk=2) ent2_eth0 = ent2.interface_set.get(name="eth0") comm = Communication(source=ent1_eth0, destination=ent2_eth0, protocol="BOGUS", port="1234")