# HG changeset patch # User Branko Majic # Date 2013-06-25 23:31:01 # Node ID 30e997a9d6c96e8a4f2d4da803cf3b57f633d5a3 # Parent 109d23889f37599ccb545e764804ee548d80cf0b Added test fixtures. Switched tests to use fixed textures. Minor clean-up of homepage template. diff --git a/conntrackt/fixtures/test-data.json b/conntrackt/fixtures/test-data.json new file mode 100644 --- /dev/null +++ b/conntrackt/fixtures/test-data.json @@ -0,0 +1,205 @@ +[ +{ + "pk": 1, + "model": "conntrackt.project", + "fields": { + "name": "Test Project 1", + "description": "This is a test project 1." + } +}, +{ + "pk": 2, + "model": "conntrackt.project", + "fields": { + "name": "Test Project 2", + "description": "This is a test project 2." + } +}, +{ + "pk": 1, + "model": "conntrackt.location", + "fields": { + "name": "Test Location 1", + "description": "This is a test location 1." + } +}, +{ + "pk": 2, + "model": "conntrackt.location", + "fields": { + "name": "Test Location 2", + "description": "This is a test location 2." + } +}, +{ + "pk": 1, + "model": "conntrackt.entity", + "fields": { + "project": 1, + "location": 1, + "name": "Test Entity 1", + "description": "This is a test entity 1." + } +}, +{ + "pk": 2, + "model": "conntrackt.entity", + "fields": { + "project": 1, + "location": 1, + "name": "Test Entity 2", + "description": "This is a test entity 2." + } +}, +{ + "pk": 3, + "model": "conntrackt.entity", + "fields": { + "project": 1, + "location": 2, + "name": "Test Entity 3", + "description": "This is a test entity 3." + } +}, +{ + "pk": 4, + "model": "conntrackt.entity", + "fields": { + "project": 1, + "location": 2, + "name": "Test Subnet", + "description": "This is a test subnet." + } +}, +{ + "pk": 5, + "model": "conntrackt.entity", + "fields": { + "project": 2, + "location": 1, + "name": "Other Project Test Entity", + "description": "This is a test entity from different project." + } +}, +{ + "pk": 1, + "model": "conntrackt.interface", + "fields": { + "netmask": "255.255.255.255", + "entity": 1, + "name": "eth0", + "address": "192.168.1.1", + "description": "Main network interface." + } +}, +{ + "pk": 2, + "model": "conntrackt.interface", + "fields": { + "netmask": "255.255.255.255", + "entity": 2, + "name": "eth0", + "address": "192.168.1.2", + "description": "Main network interface." + } +}, +{ + "pk": 3, + "model": "conntrackt.interface", + "fields": { + "netmask": "255.255.255.255", + "entity": 3, + "name": "eth0", + "address": "192.168.1.3", + "description": "Main network interface." + } +}, +{ + "pk": 4, + "model": "conntrackt.interface", + "fields": { + "netmask": "255.255.255.0", + "entity": 4, + "name": "eth0", + "address": "192.168.2.0", + "description": "Main network interface." + } +}, +{ + "pk": 5, + "model": "conntrackt.interface", + "fields": { + "netmask": "255.255.255.255", + "entity": 5, + "name": "eth0", + "address": "192.168.100.1", + "description": "Main network interface." + } +}, +{ + "pk": 1, + "model": "conntrackt.communication", + "fields": { + "source": 2, + "destination": 1, + "protocol": "TCP", + "port": 22, + "description": "SSH." + } +}, +{ + "pk": 2, + "model": "conntrackt.communication", + "fields": { + "source": 2, + "destination": 1, + "protocol": "ICMP", + "port": 8, + "description": "Ping." + } +}, +{ + "pk": 3, + "model": "conntrackt.communication", + "fields": { + "source": 3, + "destination": 1, + "protocol": "TCP", + "port": 3306, + "description": "MySQL." + } +}, +{ + "pk": 4, + "model": "conntrackt.communication", + "fields": { + "source": 1, + "destination": 3, + "protocol": "UDP", + "port": 53, + "description": "DNS." + } +}, +{ + "pk": 5, + "model": "conntrackt.communication", + "fields": { + "source": 4, + "destination": 1, + "protocol": "TCP", + "port": 22, + "description": "SSH." + } +}, +{ + "pk": 6, + "model": "conntrackt.communication", + "fields": { + "source": 1, + "destination": 2, + "protocol": "UDP", + "port": 123, + "description": "NTP." + } +} +] diff --git a/conntrackt/templates/conntrackt/index.html b/conntrackt/templates/conntrackt/index.html --- a/conntrackt/templates/conntrackt/index.html +++ b/conntrackt/templates/conntrackt/index.html @@ -4,8 +4,12 @@ {% load conntrackt_tags %} {% block content %} -

Welcome to Conntrackt

-
Below you may find the list of projects that are available to you. Clicking on the project will take you to the summary page for that particular project. Clicking on an entity inside of a project will take you to the summary page of an entity.
+
+

Welcome to Conntrackt

+
+
+
Below you may find the list of projects that are available to you. Clicking on the project will take you to the summary page for that particular project. Clicking on an entity inside of a project will take you to the summary page of an entity.
+

{% if projects %} @@ -15,7 +19,7 @@
{% endfor %} {% else %} -
Currently there are no project defined in the database. Use the administration pages in order to add a new project.
+
Currently there are no projects defined in the database. Use the administration pages in order to add a new project.
{% endif %} 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 @@ -0,0 +1,70 @@ +# 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)