File diff 2a0c8cb4797c → 72d37f849053
conntrackt/tests/test_views.py
Show inline comments
 
@@ -10,8 +10,9 @@ from django.test.client import Client
 
from django.contrib.auth.models import User, Permission
 

	
 
# Application imports
 
from conntrackt.models import Project, Location, Entity
 
from conntrackt.views import EntityCreateView, InterfaceCreateView
 
from conntrackt.models import Project, Location, Entity, Interface
 
from conntrackt.views import EntityCreateView
 
from conntrackt.views import InterfaceCreateView, InterfaceUpdateView
 

	
 

	
 
class ViewTest(TestCase):
 
@@ -1064,3 +1065,175 @@ class InterfaceCreateViewTest(TestCase):
 

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

	
 

	
 
class InterfaceUpdateViewTest(TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    def setUp(self):
 
        # Set-up web client.
 
        self.client = Client()
 

	
 
        # Set-up users with different view permissions.
 
        self.user = {}
 
        self.user["fullperms"] = User.objects.create_user("fullperms", "fullperms@example.com", "fullperms")
 
        self.user["fullperms"].user_permissions.add(Permission.objects.get(codename="change_interface"))
 
        self.user["noperms"] = User.objects.create_user("noperms", "noperms@example.com", "noperms")
 

	
 
    def test_permission_denied(self):
 
        """
 
        Tests if permission will be denied for client without sufficient privileges.
 
        """
 

	
 
        self.client.login(username="noperms", password="noperms")
 

	
 
        response = self.client.get(reverse("interface_update", args=(1,)))
 

	
 
        self.assertContains(response, "You have insufficient privileges to access this resource. Please contact your local system administrator if you believe you should have been granted access.", status_code=403)
 

	
 
    def test_permission_granted(self):
 
        """
 
        Tests if permission will be granted for user with correct privileges.
 
        """
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("interface_update", args=(1,)))
 

	
 
        self.assertEqual(response.status_code, 200)
 

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

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("interface_update", args=(1,)))
 

	
 
        self.assertContains(response, ">Edit interface eth0<")
 
        self.assertContains(response, 'value="eth0"')
 
        self.assertContains(response, "Main network interface.")
 

	
 
    def test_form_entity_limit(self):
 
        """
 
        Tests if the queryset is properly limitted to specific project's
 
        entities.
 
        """
 

	
 
        # Set-up the view.
 
        view = InterfaceUpdateView()
 
        view.request = RequestFactory().get("/fake-path/1")
 
        view.object = Interface.objects.get(pk=1)
 

	
 
        # 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)>"]
 

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

	
 
    def test_success_url(self):
 
        """
 
        Validate that the success URL is set properly after update.
 
        """
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        interface = Interface.objects.get(pk=1)
 
        
 
        response = self.client.get(reverse("interface_update", args=(1,)))
 

	
 
        response = self.client.post(reverse("interface_update", args=(1,)),
 
                                    {'csrfmiddlewaretoken': response.context['request'].META['CSRF_COOKIE'],
 
                                     "name": interface.name,
 
                                     "description": interface.name,
 
                                     "entity": "1",
 
                                     "address": "192.168.1.1",
 
                                     "netmask": "255.255.255.255"},
 
                                    follow=True)
 

	
 
        self.assertEqual(response.context["request"].META["PATH_INFO"], reverse("entity", args=(1,)))
 

	
 

	
 
class InterfaceDeleteViewTest(TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    def setUp(self):
 
        # Set-up web client.
 
        self.client = Client()
 

	
 
        # Set-up users with different view permissions.
 
        self.user = {}
 
        self.user["fullperms"] = User.objects.create_user("fullperms", "fullperms@example.com", "fullperms")
 
        self.user["fullperms"].user_permissions.add(Permission.objects.get(codename="delete_interface"))
 
        self.user["fullperms"].user_permissions.add(Permission.objects.get(codename="view"))
 
        self.user["noperms"] = User.objects.create_user("noperms", "noperms@example.com", "noperms")
 

	
 
    def test_permission_denied(self):
 
        """
 
        Tests if permission will be denied for client without sufficient privileges.
 
        """
 

	
 
        self.client.login(username="noperms", password="noperms")
 

	
 
        response = self.client.get(reverse("interface_delete", args=(1,)))
 

	
 
        self.assertContains(response, "You have insufficient privileges to access this resource. Please contact your local system administrator if you believe you should have been granted access.", status_code=403)
 

	
 
    def test_permission_granted(self):
 
        """
 
        Tests if permission will be granted for user with correct privileges.
 
        """
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("interface_delete", args=(1,)))
 

	
 
        self.assertEqual(response.status_code, 200)
 

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

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("interface_delete", args=(1,)))
 

	
 
        self.assertContains(response, ">Remove interface eth0<")
 
        self.assertContains(response, "Are you sure you want to remove this interface?")
 

	
 
    def test_message(self):
 
        """
 
        Tests if the message gets added when the interface is deleted.
 
        """
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("interface_delete", args=(1,)))
 

	
 
        response = self.client.post(reverse("interface_delete", args=(1,)),
 
                                    {'csrfmiddlewaretoken': response.context['request'].META['CSRF_COOKIE']},
 
                                    follow=True)
 

	
 
        self.assertContains(response, "Interface eth0 has been removed.")
 

	
 
    def test_success_url(self):
 
        """
 
        Validate that the success URL is set properly after delete.
 
        """
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 
        
 
        response = self.client.get(reverse("interface_delete", args=(1,)))
 

	
 
        response = self.client.post(reverse("interface_delete", args=(1,)),
 
                                    {'csrfmiddlewaretoken': response.context['request'].META['CSRF_COOKIE']},
 
                                    follow=True)
 

	
 
        self.assertEqual(response.context["request"].META["PATH_INFO"], reverse("entity", args=(1,)))