# Standard library imports. from StringIO import StringIO from zipfile import ZipFile, ZIP_DEFLATED # 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): """ 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.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.assertContains(response, "There are no projects defined.") def test_no_locations(self): """ Tests the index view when no locations are defined. """ Location.objects.all().delete() self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("index")) self.assertContains(response, "There are no locations defined.") 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") def test_locations_available(self): """ Tests if locations are shown or not. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("index")) self.assertQuerysetEqual(response.context["locations"], ["", ""]) self.assertContains(response, "Test Location 1") self.assertContains(response, "Test Location 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.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") class EntityViewTest(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("entity", 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("entity", args=(1,))) self.assertEqual(response.status_code, 200) def test_entity_show(self): """ Tests if the entity information is shown properly. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("entity", args=(1,))) expected_incoming_communications = [" Test Entity 1 (TCP:22)>", " Test Entity 1 (ICMP:8)>", " Test Entity 1 (TCP:3306)>", " Test Entity 1 (TCP:22)>",] expected_outgoing_communications = [" Test Entity 2 (UDP:123)>", " Test Entity 3 (UDP:53)>"] expected_interfaces = [""] self.assertQuerysetEqual(response.context["interfaces"], expected_interfaces) self.assertQuerysetEqual(response.context["incoming_communications"], expected_incoming_communications) self.assertQuerysetEqual(response.context["outgoing_communications"], expected_outgoing_communications) self.assertEqual(str(response.context["entity"]), "Test Entity 1 (Test Project 1 - Test Location 1)") self.assertContains(response, "Test Entity 1") self.assertContains(response, ":INPUT") self.assertContains(response, ":OUTPUT") self.assertContains(response, ":FORWARD") class EntityIptablesTest(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("entity_iptables", 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("entity_iptables", args=(1,))) self.assertEqual(response.status_code, 200) def test_no_entity(self): """ Tests if a 404 is returned if no entity was found (invalid ID). """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("entity_iptables", args=(200,))) self.assertEqual(response.status_code, 404) def test_content_type(self): """ Test if correct content type is being returned by the response. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("entity_iptables", args=(1,))) self.assertEqual(response['Content-Type'], "text/plain") def test_content_disposition(self): """ Test if the correct content disposition has been set. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("entity_iptables", args=(1,))) self.assertEqual(response['Content-Disposition'], "attachment; filename=test_entity_1-iptables.conf") def test_entity_iptables_show(self): """ Test if the entity's iptables are being show or not. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("entity_iptables", args=(1,))) self.assertContains(response, ":INPUT") self.assertContains(response, ":OUTPUT") self.assertContains(response, ":FORWARD") class ProjectIptablesTest(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_iptables", 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("project_iptables", args=(1,))) self.assertEqual(response.status_code, 200) def test_invalid_project(self): """ Tests if a 404 is returned if invalid project is specified. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("project_iptables", args=(200,))) self.assertEqual(response.status_code, 404) response = self.client.get(reverse("project_location_iptables", args=(200, 1))) self.assertEqual(response.status_code, 404) def test_invalid_location(self): """ Tests if a 404 is returned if invalid location is specified. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("project_location_iptables", args=(1, 200))) self.assertEqual(response.status_code, 404) def test_content_type(self): """ Test if correct content type is being returned by the response. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("project_iptables", args=(1,))) self.assertEqual(response['Content-Type'], "application/zip") def test_content_disposition(self): """ Test if the correct content disposition has been set. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("project_iptables", args=(1,))) self.assertEqual(response['Content-Disposition'], 'attachment; filename="test_project_1-iptables.zip"') response = self.client.get(reverse("project_location_iptables", args=(1, 1))) self.assertEqual(response['Content-Disposition'], 'attachment; filename="test_project_1-test_location_1-iptables.zip"') def test_project_entities_show(self): """ Test if the project's iptables are being shown or not. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("project_iptables", args=(1,))) buff = StringIO(response.content) zipped_iptables = ZipFile(buff, "r", ZIP_DEFLATED) expected_zip_files = ["test_entity_1-iptables.conf", "test_entity_2-iptables.conf", "test_entity_3-iptables.conf", "test_subnet-iptables.conf"] self.assertEqual(len(zipped_iptables.namelist()), 4) self.assertEqual(zipped_iptables.namelist(), expected_zip_files) for filename in expected_zip_files: iptables_file = zipped_iptables.read(filename) self.assertIn(":INPUT", iptables_file) self.assertIn(":OUTPUT", iptables_file) self.assertIn(":FORWARD", iptables_file) zipped_iptables.close() def test_project_location_entities_show(self): """ Test if the project location's iptables are being shown or not. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("project_location_iptables", args=(1, 1))) buff = StringIO(response.content) zipped_iptables = ZipFile(buff, "r", ZIP_DEFLATED) expected_zip_files = ["test_entity_1-iptables.conf", "test_entity_2-iptables.conf"] self.assertEqual(len(zipped_iptables.namelist()), 2) self.assertEqual(zipped_iptables.namelist(), expected_zip_files) for filename in expected_zip_files: iptables_file = zipped_iptables.read(filename) self.assertIn(":INPUT", iptables_file) self.assertIn(":OUTPUT", iptables_file) self.assertIn(":FORWARD", iptables_file) zipped_iptables.close() class ProjectCreateViewTest(TestCase): 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="add_project")) 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("project_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. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("project_create")) self.assertEqual(response.status_code, 200) def test_form_styling(self): """ Tests if proper form styling is being applied. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("project_create")) self.assertContains(response, 'class="span6 textinput') self.assertContains(response, 'class="span6 textarea') self.assertContains(response, 'placeholder="New Project"') self.assertContains(response, 'placeholder="Description for new project."') class ProjectUpdateViewTest(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_project")) 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("project_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("project_update", args=(1,))) self.assertEqual(response.status_code, 200) def test_form_styling(self): """ Tests if proper form styling is being applied. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("project_update", args=(1,))) self.assertContains(response, 'class="span6 textinput') self.assertContains(response, 'class="span6 textarea') self.assertContains(response, 'placeholder="Project name"') self.assertContains(response, 'placeholder="Description for project."') 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("project_update", args=(1,))) self.assertContains(response, ">Edit project Test Project 1<") self.assertContains(response, 'value="Test Project 1"') self.assertContains(response, "This is a test project 1.") class ProjectDeleteViewTest(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_project")) 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("project_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("project_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("project_delete", args=(1,))) self.assertContains(response, ">Remove project Test Project 1<") self.assertContains(response, "Are you sure you want to remove this project?") def test_message(self): """ Tests if the message gets added when the project is deleted. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("project_delete", args=(1,))) response = self.client.post(reverse("project_delete", args=(1,)), {'csrfmiddlewaretoken': response.context['request'].META['CSRF_COOKIE']}, follow=True) self.assertContains(response, "Project Test Project 1 has been removed.") class LocationCreateViewTest(TestCase): 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="add_location")) 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("location_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. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("location_create")) self.assertEqual(response.status_code, 200) def test_form_styling(self): """ Tests if proper form styling is being applied. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("location_create")) self.assertContains(response, 'class="span6 textinput') self.assertContains(response, 'class="span6 textarea') self.assertContains(response, 'placeholder="New Location"') self.assertContains(response, 'placeholder="Description for new location."') class LocationUpdateViewTest(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_location")) 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("location_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("location_update", args=(1,))) self.assertEqual(response.status_code, 200) def test_form_styling(self): """ Tests if proper form styling is being applied. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("location_update", args=(1,))) self.assertContains(response, 'class="span6 textinput') self.assertContains(response, 'class="span6 textarea') self.assertContains(response, 'placeholder="Location name"') self.assertContains(response, 'placeholder="Description for location."') 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("location_update", args=(1,))) self.assertContains(response, ">Edit location Test Location 1<") self.assertContains(response, 'value="Test Location 1"') self.assertContains(response, "This is a test location 1.") class LocationDeleteViewTest(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_location")) 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("location_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("location_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("location_delete", args=(1,))) self.assertContains(response, ">Remove location Test Location 1<") self.assertContains(response, "Are you sure you want to remove this location?") def test_message(self): """ Tests if the message gets added when the location is deleted. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("location_delete", args=(1,))) response = self.client.post(reverse("location_delete", args=(1,)), {'csrfmiddlewaretoken': response.context['request'].META['CSRF_COOKIE']}, 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, [""]) 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, [""]) 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) class EntityDeleteViewTest(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_entity")) 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("entity_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("entity_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("entity_delete", args=(1,))) self.assertContains(response, ">Remove entity Test Entity 1<") self.assertContains(response, "Are you sure you want to remove this entity?") def test_message(self): """ Tests if the message gets added when the entity is deleted. """ self.client.login(username="fullperms", password="fullperms") response = self.client.get(reverse("entity_delete", args=(1,))) response = self.client.post(reverse("entity_delete", args=(1,)), {'csrfmiddlewaretoken': response.context['request'].META['CSRF_COOKIE']}, follow=True) self.assertContains(response, "Entity Test Entity 1 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("entity_delete", args=(1,))) response = self.client.post(reverse("entity_delete", args=(1,)), {'csrfmiddlewaretoken': response.context['request'].META['CSRF_COOKIE']}, follow=True) self.assertEqual(response.context["request"].META["PATH_INFO"], reverse("project", args=(1,))) class EntityUpdateViewTest(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_entity")) 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("entity_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("entity_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("entity_update", args=(1,))) self.assertContains(response, ">Edit entity Test Entity 1<") self.assertContains(response, 'value="Test Entity 1"') self.assertContains(response, "This is a test entity 1.")