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 @@ -10,8 +10,8 @@ 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 +from conntrackt.models import Project, Location, Entity +from conntrackt.views import EntityCreateView, InterfaceCreateView class ViewTest(TestCase): @@ -992,3 +992,75 @@ class EntityUpdateViewTest(TestCase): self.assertContains(response, ">Edit entity Test Entity 1<") self.assertContains(response, 'value="Test Entity 1"') self.assertContains(response, "This is a test entity 1.") + + +class InterfaceCreateViewTest(TestCase): + + def setUp(self): + """ + Sets-up some data necessary for testing. + """ + + # Set-up some data for testing. + project = Project.objects.create(name="Test Project", description="This is test project.") + location = Location.objects.create(name="Test Location", description="This is test location.") + Entity.objects.create(name="Test Entity 1", description="This is test entity 1.", project=project, location=location) + Entity.objects.create(name="Test Entity 2", description="This is test entity 2.", project=project, location=location) + + 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("interface_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_interface")) + + self.client.login(username="fullperms", password="fullperms") + + response = self.client.get(reverse("interface_create")) + + self.assertEqual(response.status_code, 200) + + def test_form_entity_limit(self): + """ + Tests if the queryset is properly limitted to specific entity if GET + parameter is passed. + """ + + # Set-up the view. + view = InterfaceCreateView() + view.request = RequestFactory().get("/fake-path?entity=1") + view.object = None + + # Get the form. + form = view.get_form(view.get_form_class()) + + self.assertQuerysetEqual(form.fields["entity"].queryset, [""]) + + def test_initial_project(self): + """ + Tests if the choice field for entity is defaulted to entity passed as + part of GET parameters. + """ + + view = InterfaceCreateView() + view.request = RequestFactory().get("/fake-path?entity=1") + view.object = None + + initial = view.get_initial() + + self.assertDictContainsSubset({"entity": "1"}, initial) +