Files
@ 7b736c4262c5
Branch filter:
Location: conntrackt/conntrackt/tests/test_views.py
7b736c4262c5
4.7 KiB
text/x-python
Fixed the filename generation for project/location iptables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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"], ["<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")
|