File diff 897541fd8978 → a69e9814cf16
conntrackt/tests/test_views.py
Show inline comments
 
@@ -24,21 +24,27 @@ from conntrackt.views import InterfaceCr
 
from conntrackt.views import CommunicationCreateView, CommunicationUpdateView, CommunicationDeleteView
 

	
 
# Test imports.
 
from .forms import FormWithWidgetCSSClassFormMixin, FormWithPlaceholderFormMixin
 
from .helpers import PermissionTestMixin, create_get_request, generate_get_response, FakeMessages
 
from .views import RedirectToNextMixinView
 
from .factories import setup_test_data
 

	
 

	
 
class IndexViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    sufficient_permissions = ("view",)
 
    view_class = IndexView
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context_no_projects(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called and
 
        no projects are available.
 
        """
 

	
 
@@ -99,18 +105,23 @@ class IndexViewTest(PermissionTestMixin,
 
        # Validate the response.
 
        self.assertQuerysetEqual(response.context_data["locations"], ["<Location: Test Location 1>", "<Location: Test Location 2>"])
 

	
 

	
 
class ProjectViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    sufficient_permissions = ("view",)
 
    permission_test_view_kwargs = {"pk": "1"}
 
    view_class = ProjectView
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called.
 
        """
 

	
 
        # Get the view.
 
@@ -132,30 +143,35 @@ class ProjectViewTest(PermissionTestMixi
 

	
 
        # Fetch context data from response.
 
        location, entities = response.context_data["location_entities"][1]
 

	
 
        # Set-up expected context data values.
 
        expected_entities = ["<Entity: Test Entity 3 (Test Project 1 - Test Location 2)>",
 
                             "<Entity: Test Subnet (Test Project 1 - Test Location 2)>"]
 
                             "<Entity: Test Subnet 4 (Test Project 1 - Test Location 2)>"]
 

	
 
        # Validate context data.
 
        self.assertEqual(location.name, "Test Location 2")
 
        self.assertQuerysetEqual(entities, expected_entities)
 

	
 
        # Validate context data.
 
        self.assertEqual(str(response.context_data["project"]), "Test Project 1")
 

	
 

	
 
class EntityViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = EntityView
 
    sufficient_permissions = ("view",)
 
    permission_test_view_kwargs = {"pk": "1"}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Tests if the form comes pre-populated with proper content.
 
        """
 

	
 
        # Get the view.
 
@@ -167,13 +183,13 @@ class EntityViewTest(PermissionTestMixin
 
        # Set-up expected context data.
 
        expected_entity = Entity.objects.get(pk=1)
 

	
 
        expected_incoming_communications = ["<Communication: Test Entity 2 -> Test Entity 1 (TCP:22)>",
 
                                            "<Communication: Test Entity 2 -> Test Entity 1 (ICMP:8)>",
 
                                            "<Communication: Test Entity 3 -> Test Entity 1 (TCP:3306)>",
 
                                            "<Communication: Test Subnet -> Test Entity 1 (TCP:22)>"]
 
                                            "<Communication: Test Subnet 4 -> Test Entity 1 (TCP:22)>"]
 

	
 
        expected_outgoing_communications = ["<Communication: Test Entity 1 -> Test Entity 2 (UDP:123)>",
 
                                            "<Communication: Test Entity 1 -> Test Entity 3 (UDP:53)>"]
 

	
 
        expected_interfaces = ["<Interface: Test Entity 1 (192.168.1.1)>"]
 

	
 
@@ -184,18 +200,23 @@ class EntityViewTest(PermissionTestMixin
 
        self.assertEqual(response.context_data["entity"], expected_entity)
 
        self.assertTrue("entity_iptables" in response.context_data)
 

	
 

	
 
class EntityIptablesTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_function = staticmethod(entity_iptables)
 
    sufficient_permissions = ("view",)
 
    permission_test_view_kwargs = {"pk": "1"}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_invalid_entity(self):
 
        """
 
        Tests if a 404 is returned if no entity was found (invalid ID).
 
        """
 

	
 
        # Set-up a request.
 
@@ -248,18 +269,23 @@ class EntityIptablesTest(PermissionTestM
 
        self.assertContains(response, ":OUTPUT")
 
        self.assertContains(response, ":FORWARD")
 

	
 

	
 
class ProjectIptablesTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_function = staticmethod(project_iptables)
 
    sufficient_permissions = ("view",)
 
    permission_test_view_kwargs = {"project_id": 1}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_invalid_project(self):
 
        """
 
        Tests if a 404 is returned if no project was found (invalid ID).
 
        """
 

	
 
        # Set-up a request.
 
@@ -331,13 +357,13 @@ class ProjectIptablesTest(PermissionTest
 
        buff = StringIO(response.content)
 

	
 
        zipped_iptables = ZipFile(buff, "r", ZIP_DEFLATED)
 
        expected_zip_files = ["test_entity_1-iptables.conf",
 
                              "test_entity_2-iptables.conf",
 
                              "test_entity_3-iptables.conf",
 
                              "test_subnet-iptables.conf"]
 
                              "test_subnet_4-iptables.conf"]
 

	
 
        self.assertEqual(len(zipped_iptables.namelist()), 4)
 
        self.assertEqual(zipped_iptables.namelist(), expected_zip_files)
 

	
 
        for filename in expected_zip_files:
 
            iptables_file = zipped_iptables.read(filename)
 
@@ -382,18 +408,23 @@ class ProjectCreateViewTest(PermissionTe
 
    view_class = ProjectCreateView
 
    sufficient_permissions = ("add_project",)
 

	
 

	
 
class ProjectUpdateViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = ProjectUpdateView
 
    sufficient_permissions = ("change_project",)
 
    permission_test_view_kwargs = {"pk": 1}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called for
 
        specific project.
 
        """
 

	
 
@@ -406,18 +437,23 @@ class ProjectUpdateViewTest(PermissionTe
 
        self.assertEqual(response.context_data["project"].name, "Test Project 1")
 
        self.assertEqual(response.context_data["headline"], "Update project Test Project 1")
 

	
 

	
 
class ProjectDeleteViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = ProjectDeleteView
 
    sufficient_permissions = ("delete_project",)
 
    permission_test_view_kwargs = {"pk": "1"}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called for
 
        specific project.
 
        """
 

	
 
@@ -458,18 +494,23 @@ class LocationCreateViewTest(PermissionT
 
    view_class = LocationCreateView
 
    sufficient_permissions = ("add_location",)
 

	
 

	
 
class LocationUpdateViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = LocationUpdateView
 
    sufficient_permissions = ("change_location",)
 
    permission_test_view_kwargs = {"pk": 1}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called for
 
        specific location.
 
        """
 

	
 
@@ -482,18 +523,23 @@ class LocationUpdateViewTest(PermissionT
 
        self.assertEqual(response.context_data["location"].name, "Test Location 1")
 
        self.assertEqual(response.context_data["headline"], "Update location Test Location 1")
 

	
 

	
 
class LocationDeleteViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = LocationDeleteView
 
    sufficient_permissions = ("delete_location",)
 
    permission_test_view_kwargs = {"pk": "1"}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called for
 
        specific location.
 
        """
 

	
 
@@ -606,18 +652,23 @@ class EntityCreateViewTest(PermissionTes
 

	
 
        self.assertDictContainsSubset({"location": "1"}, initial)
 

	
 

	
 
class EntityDeleteViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = EntityDeleteView
 
    sufficient_permissions = ("delete_entity",)
 
    permission_test_view_kwargs = {"pk": 1}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called for
 
        specific entity.
 
        """
 

	
 
@@ -672,18 +723,23 @@ class EntityDeleteViewTest(PermissionTes
 

	
 
        self.assertEqual(response["Location"], reverse("project", args=(1,)))
 

	
 

	
 
class EntityUpdateViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = EntityUpdateView
 
    sufficient_permissions = ("change_entity",)
 
    permission_test_view_kwargs = {"pk": 1}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called for
 
        specific entity.
 
        """
 

	
 
@@ -766,18 +822,23 @@ class InterfaceCreateViewTest(Permission
 
        self.assertEqual(response["Location"], reverse("entity", args=(1,)))
 
        self.assertEqual(response.status_code, 302)
 

	
 

	
 
class InterfaceUpdateViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = InterfaceUpdateView
 
    sufficient_permissions = ("change_interface",)
 
    permission_test_view_kwargs = {"pk": 1}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called for
 
        specific entity.
 
        """
 

	
 
@@ -807,13 +868,13 @@ class InterfaceUpdateViewTest(Permission
 
        # Get the form.
 
        form = view.get_form(view.get_form_class())
 

	
 
        expected_entities = ["<Entity: Test Entity 1 (Test Project 1 - Test Location 1)>",
 
                             "<Entity: Test Entity 2 (Test Project 1 - Test Location 1)>",
 
                             "<Entity: Test Entity 3 (Test Project 1 - Test Location 2)>",
 
                             "<Entity: Test Subnet (Test Project 1 - Test Location 2)>"]
 
                             "<Entity: Test Subnet 4 (Test Project 1 - Test Location 2)>"]
 

	
 
        self.assertQuerysetEqual(form.fields["entity"].queryset, expected_entities)
 

	
 
    def test_success_url(self):
 
        """
 
        Validate that the success URL is set properly after update.
 
@@ -839,18 +900,23 @@ class InterfaceUpdateViewTest(Permission
 
        self.assertEqual(response["Location"], reverse("entity", args=(1,)))
 
        self.assertEqual(response.status_code, 302)
 

	
 

	
 
class InterfaceDeleteViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = InterfaceDeleteView
 
    sufficient_permissions = ("delete_interface",)
 
    permission_test_view_kwargs = {"pk": 1}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called for
 
        specific interface.
 
        """
 

	
 
@@ -1127,18 +1193,23 @@ class CommunicationCreateViewTest(Permis
 
        self.assertEqual(response["Location"], reverse("project", args=(1,)))
 
        self.assertEqual(response.status_code, 302)
 

	
 

	
 
class CommunicationUpdateViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = CommunicationUpdateView
 
    sufficient_permissions = ("change_communication",)
 
    permission_test_view_kwargs = {"pk": 1}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up.
 
        """
 

	
 
        # Get the view.
 
@@ -1167,13 +1238,13 @@ class CommunicationUpdateViewTest(Permis
 
        # Get the form.
 
        form = view.get_form(view.get_form_class())
 

	
 
        expected_interfaces = ["<Interface: Test Entity 1 (192.168.1.1)>",
 
                               "<Interface: Test Entity 2 (192.168.1.2)>",
 
                               "<Interface: Test Entity 3 (192.168.1.3)>",
 
                               "<Interface: Test Subnet (192.168.2.0/255.255.255.0)>"]
 
                               "<Interface: Test Subnet 4 (10.10.4.0/255.255.255.0)>"]
 

	
 
        self.assertQuerysetEqual(form.fields["source"].queryset, expected_interfaces)
 
        self.assertQuerysetEqual(form.fields["destination"].queryset, expected_interfaces)
 

	
 
    def test_success_url_next(self):
 
        """
 
@@ -1229,18 +1300,23 @@ class CommunicationUpdateViewTest(Permis
 
        self.assertEqual(response["Location"], reverse("project", args=(communication.source.entity.project.id,)))
 
        self.assertEqual(response.status_code, 302)
 

	
 

	
 
class CommunicationDeleteViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = CommunicationDeleteView
 
    sufficient_permissions = ("delete_communication",)
 
    permission_test_view_kwargs = {"pk": 1}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called for
 
        specific communication.
 
        """
 

	
 
@@ -1336,18 +1412,23 @@ class CommunicationDeleteViewTest(Permis
 

	
 
        self.assertEqual(response["Location"], reverse("entity", args=(communication.source.entity.pk,)))
 

	
 

	
 
class ProjectDiagramTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_function = staticmethod(project_diagram)
 
    sufficient_permissions = ("view",)
 
    permission_test_view_kwargs = {"pk": "1"}
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_invalid_project(self):
 
        """
 
        Tests if a 404 is returned if no project was found (invalid ID).
 
        """
 

	
 
        # Set-up a request.