Changeset - 78bdb2323c90
[Not reviewed]
default
0 1 0
Branko Majic (branko) - 11 years ago 2013-06-30 00:43:04
branko@majic.rs
Added tests for the index and project views.
1 file changed with 130 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conntrackt/tests/test_views.py
Show inline comments
 
# Django imports.
 
from django.core.urlresolvers import reverse
 
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
 

	
 

	
 
class ViewTest(TestCase):
 
    """
 
    Abstract test class that initalises the fixtures, sets-up a client, and
 
    sets-up a test user.
 
    """
 

	
 
    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="view"))
 
        self.user["noperms"] = User.objects.create_user("noperms", "noperms@example.com", "noperms")
 

	
 

	
 
class IndexViewTest(ViewTest):
 

	
 
    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("index"))
 

	
 
        self.assertEqual(response.status_code, 403)
 
        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("index"))
 

	
 
        self.assertEqual(response.status_code, 200)
 

	
 
    def test_no_projects(self):
 
        """
 
        Tests the index view when no projects are defined.
 
        """
 

	
 
        Project.objects.all().delete()
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 
        response = self.client.get(reverse("index"))
 

	
 
        self.assertEqual(response.status_code, 200)
 
        self.assertContains(response, "Currently there are no projects defined in the database. Use the administration pages in order to add a new project.")
 

	
 
    def test_projects_available(self):
 
        """
 
        Tests if projects are shown or not.
 
        """
 

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

	
 
        response = self.client.get(reverse("index"))
 
        
 
        self.assertQuerysetEqual(response.context["projects"], ["<Project: Test Project 1>", "<Project: Test Project 2>"])
 
        self.assertContains(response, "Test Project 1")
 
        self.assertContains(response, "Test Project 2")
 

	
 

	
 
class ProjectViewTest(ViewTest):
 

	
 
    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("project", args=(1,)))
 

	
 
        self.assertEqual(response.status_code, 403)
 
        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("project", args=(1,)))
 

	
 
        self.assertEqual(response.status_code, 200)
 

	
 
    def test_project_show(self):
 
        """
 
        Tests if the project information is shown properly.
 
        """
 

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

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

	
 
        location, entities = response.context["location_entities"][0]
 
        self.assertEqual(location.name, "Test Location 1")
 
        self.assertQuerysetEqual(entities, ["<Entity: Test Entity 1 (Test Project 1 - Test Location 1)>",
 
                                            "<Entity: Test Entity 2 (Test Project 1 - Test Location 1)>"])
 

	
 
        location, entities = response.context["location_entities"][1]
 
        self.assertEqual(location.name, "Test Location 2")
 
        self.assertQuerysetEqual(entities, ["<Entity: Test Entity 3 (Test Project 1 - Test Location 2)>",
 
                                            "<Entity: Test Subnet (Test Project 1 - Test Location 2)>"])
 

	
 
        self.assertEqual(str(response.context["project"]), "Test Project 1")
 
        self.assertContains(response, "Test Project 1")
 
        self.assertContains(response, "Test Location 1")
 
        self.assertContains(response, "Test Location 2")
 

	
0 comments (0 inline, 0 general)