File diff 018e7beed874 → 7086387253d4
conntrackt/tests/test_views.py
Show inline comments
 
@@ -4,12 +4,14 @@ from zipfile import ZipFile, ZIP_DEFLATE
 

	
 
# Django imports.
 
from django.core.urlresolvers import reverse
 
from django.test import RequestFactory
 
from django.test import TestCase
 
from django.test.client import Client
 
from django.contrib.auth.models import User, Permission
 

	
 
# Application imports
 
from conntrackt.models import Project, Location
 
from conntrackt.views import EntityCreateView
 

	
 

	
 
class ViewTest(TestCase):
 
@@ -746,3 +748,104 @@ class LocationDeleteViewTest(TestCase):
 
                                    follow=True)
 

	
 
        self.assertContains(response, "Location Test Location 1 has been removed.")
 

	
 

	
 
class EntityCreateViewTest(TestCase):
 

	
 
    def setUp(self):
 
        """
 
        Sets-up some data necessary for testing.
 
        """
 

	
 
        # Set-up some data for testing.
 
        Project.objects.create(name="Test Project 1", description="This is test project 1.")
 
        Project.objects.create(name="Test Project 2", description="This is test project 2.")
 
        Location.objects.create(name="Test Location 1", description="This is test location 1.")
 
        Location.objects.create(name="Test Location 2", description="This is test location 2.")
 

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

	
 
        User.objects.create_user("noperms", "noperms@example.com", "noperms")
 

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

	
 
        response = self.client.get(reverse("entity_create"))
 

	
 
        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.
 
        """
 

	
 
        user = User.objects.create_user("fullperms", "fullperms@example.com", "fullperms")
 
        user.user_permissions.add(Permission.objects.get(codename="add_entity"))
 

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

	
 
        response = self.client.get(reverse("entity_create"))
 

	
 
        self.assertEqual(response.status_code, 200)
 

	
 
    def test_form_project_limit(self):
 
        """
 
        Tests if the queryset is properly limitted to specific project if GET
 
        parameters is passed.
 
        """
 

	
 
        # Set-up the view.
 
        view = EntityCreateView()
 
        view.request = RequestFactory().get("/fake-path?project=1")
 
        view.object = None
 

	
 
        # Get the form.
 
        form = view.get_form(view.get_form_class())
 

	
 
        self.assertQuerysetEqual(form.fields["project"].queryset, ["<Project: Test Project 1>"])
 

	
 
    def test_form_location_limit(self):
 
        """
 
        Tests if the queryset is properly limitted to specific location if GET
 
        parameters is passed.
 
        """
 

	
 
        # Set-up the view.
 
        view = EntityCreateView()
 
        view.request = RequestFactory().get("/fake-path?location=1")
 
        view.object = None
 

	
 
        # Get the form.
 
        form = view.get_form(view.get_form_class())
 

	
 
        self.assertQuerysetEqual(form.fields["location"].queryset, ["<Location: Test Location 1>"])
 

	
 
    def test_initial_project(self):
 
        """
 
        Tests if the choice field for project is defaulted to project passed as
 
        part of GET parameters.
 
        """
 
        
 
        view = EntityCreateView()
 
        view.request = RequestFactory().get("/fake-path?project=1")
 
        view.object = None
 

	
 
        initial = view.get_initial()
 

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

	
 
    def test_initial_location(self):
 
        """
 
        Tests if the choice field for location is defaulted to location passed
 
        as part of GET parameters.
 
        """
 
        
 
        view = EntityCreateView()
 
        view.request = RequestFactory().get("/fake-path?location=1")
 
        view.object = None
 

	
 
        initial = view.get_initial()
 

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