# HG changeset patch # User Branko Majic # Date 2013-06-30 00:43:04 # Node ID 78bdb2323c9066dbc698d56fdb3e0676c45260b3 # Parent 6efc762eb16259336395e89abb095e3165dff95d Added tests for the index and project views. diff --git a/conntrackt/tests/test_views.py b/conntrackt/tests/test_views.py --- a/conntrackt/tests/test_views.py +++ b/conntrackt/tests/test_views.py @@ -0,0 +1,130 @@ +# 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"], ["", ""]) + 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, ["", + ""]) + + location, entities = response.context["location_entities"][1] + self.assertEqual(location.name, "Test Location 2") + self.assertQuerysetEqual(entities, ["", + ""]) + + 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") +