Changeset - 491fa548a42f
[Not reviewed]
default
0 3 0
Branko Majic (branko) - 11 years ago 2013-07-24 22:25:56
branko@majic.rs
CONNT-6: Reworked the way success URL is constructed when creating communication. Includes updates to tests.
3 files changed with 26 insertions and 83 deletions:
0 comments (0 inline, 0 general)
conntrackt/templates/conntrackt/entity_detail.html
Show inline comments
 
@@ -89,7 +89,8 @@
 
      {% endif %}
 
      <div>
 
        {% with entity_id=entity.id|slugify %}
 
        {% html_link "Add communication" "communication_create" class="btn btn-primary btn-mini" get="to_entity="|add:entity_id %}
 
        {% url "entity" entity.id as next %}
 
        {% html_link "Add communication" "communication_create" class="btn btn-primary btn-mini" get="to_entity="|add:entity_id|add:"&next="|add:next %}
 
        {% endwith %}
 
      </div>
 
    </div>
 
@@ -113,7 +114,8 @@
 
      {% endif %}
 
      <div>
 
        {% with entity_id=entity.id|slugify %}
 
        {% html_link "Add communication" "communication_create" class="btn btn-primary btn-mini" get="from_entity="|add:entity_id %}
 
        {% url "entity" entity.id as next %}
 
        {% html_link "Add communication" "communication_create" class="btn btn-primary btn-mini" get="from_entity="|add:entity_id|add:"&next="|add:next %}
 
        {% endwith %}
 
      </div>
 
    </div>
conntrackt/tests/test_views.py
Show inline comments
 
@@ -1062,10 +1062,10 @@ class CommunicationCreateViewTest(Permis
 

	
 
        self.assertEqual(len(initial), 0)
 

	
 
    def test_success_url_from_entity(self):
 
    def test_success_url_next(self):
 
        """
 
        Validate that the success URL is set properly after communication is
 
        created if origin entity is provided.
 
        created if "next" GET parameter is provided.
 
        """
 

	
 
        # Get the view.
 
@@ -1079,47 +1079,20 @@ class CommunicationCreateViewTest(Permis
 
                     "protocol": "TCP",
 
                     "port": "22",
 
                     "description": "SSH."}
 
        request = RequestFactory().post("/fake-path?from_entity=1", data=post_data)
 
        request = RequestFactory().post("/fake-path?next=/next-page", data=post_data)
 
        request.user = mock.Mock()
 
        request._dont_enforce_csrf_checks = True
 

	
 
        # Get the response.
 
        response = view(request)
 

	
 
        self.assertEqual(response["Location"], reverse("entity", args=(1,)))
 
        self.assertEqual(response["Location"], "/next-page")
 
        self.assertEqual(response.status_code, 302)
 

	
 
    def test_success_url_to_entity(self):
 
    def test_success_url_no_next(self):
 
        """
 
        Validate that the success URL is set properly after communication is
 
        created if destination entity is provided.
 
        """
 

	
 
        # Get the view.
 
        view = CommunicationCreateView.as_view()
 

	
 
        # Generate the request.
 
        source = Interface.objects.get(pk=1)
 
        destination = Interface.objects.get(pk=2)
 
        post_data = {"source": source.pk,
 
                     "destination": destination.pk,
 
                     "protocol": "TCP",
 
                     "port": "22",
 
                     "description": "SSH."}
 
        request = RequestFactory().post("/fake-path?to_entity=2", data=post_data)
 
        request.user = mock.Mock()
 
        request._dont_enforce_csrf_checks = True
 

	
 
        # Get the response.
 
        response = view(request)
 

	
 
        self.assertEqual(response["Location"], reverse("entity", args=(2,)))
 
        self.assertEqual(response.status_code, 302)
 

	
 
    def test_success_url_no_entity(self):
 
        """
 
        Validate that the success URL is set properly after communication is
 
        created if no entity source/destinantion is passed on.
 
        created if no "next" GET parameter is provided.
 
        """
 

	
 
        # Get the view.
 
@@ -1140,7 +1113,7 @@ class CommunicationCreateViewTest(Permis
 
        # Get the response.
 
        response = view(request)
 

	
 
        self.assertEqual(response["Location"], reverse("entity", args=(1,)))
 
        self.assertEqual(response["Location"], reverse("project", args=(1,)))
 
        self.assertEqual(response.status_code, 302)
 

	
 

	
 
@@ -1190,9 +1163,10 @@ class CommunicationUpdateViewTest(Permis
 
        self.assertQuerysetEqual(form.fields["source"].queryset, expected_interfaces)
 
        self.assertQuerysetEqual(form.fields["destination"].queryset, expected_interfaces)
 

	
 
    def test_success_url_from_entity(self):
 
    def test_success_url_next(self):
 
        """
 
        Validate that the success URL is set properly after update.
 
        Validate that the success URL is set properly after update if GET
 
        parameter is passed.
 
        """
 

	
 
        # Get the view.
 
@@ -1206,45 +1180,20 @@ class CommunicationUpdateViewTest(Permis
 
                     "destination": communication.destination.pk,
 
                     "protocol": communication.protocol,
 
                     "port": communication.port}
 
        request = RequestFactory().post("/fake-path?from_entity=1", data=post_data)
 
        request = RequestFactory().post("/fake-path?next=/next-page", data=post_data)
 
        request.user = mock.Mock()
 
        request._dont_enforce_csrf_checks = True
 

	
 
        # Get the response.
 
        response = view(request, pk=1)
 

	
 
        self.assertEqual(response["Location"], reverse("entity", args=(1,)))
 
        self.assertEqual(response["Location"], "/next-page")
 
        self.assertEqual(response.status_code, 302)
 

	
 
    def test_success_url_to_entity(self):
 
        """
 
        Validate that the success URL is set properly after update.
 
    def test_success_url_no_next(self):
 
        """
 

	
 
        # Get the view.
 
        view = CommunicationUpdateView.as_view()
 

	
 
        # Get the communication object.
 
        communication = Communication.objects.get(pk=1)
 

	
 
        # Generate the request.
 
        post_data = {"source": communication.source.pk,
 
                     "destination": communication.destination.pk,
 
                     "protocol": communication.protocol,
 
                     "port": communication.port}
 
        request = RequestFactory().post("/fake-path?to_entity=1", data=post_data)
 
        request.user = mock.Mock()
 
        request._dont_enforce_csrf_checks = True
 

	
 
        # Get the response.
 
        response = view(request, pk=1)
 

	
 
        self.assertEqual(response["Location"], reverse("entity", args=(1,)))
 
        self.assertEqual(response.status_code, 302)
 

	
 
    def test_success_url_no_entity(self):
 
        """
 
        Validate that the success URL is set properly after update.
 
        Validate that the success URL is set properly after communication is
 
        created if no "next" GET parameter is provided.
 
        """
 

	
 
        # Get the view.
 
@@ -1265,7 +1214,7 @@ class CommunicationUpdateViewTest(Permis
 
        # Get the response.
 
        response = view(request, pk=1)
 

	
 
        self.assertEqual(response["Location"], reverse("entity", args=(communication.source.entity.pk,)))
 
        self.assertEqual(response["Location"], reverse("project", args=(communication.source.entity.project.id,)))
 
        self.assertEqual(response.status_code, 302)
 

	
 

	
conntrackt/views.py
Show inline comments
 
@@ -707,15 +707,11 @@ class CommunicationCreateView(MultiplePe
 
        Returns the URL to which the user should be redirected after a
 
        communication has been created.
 

	
 
        The URL will be set to entity details page of an entity that was
 
        provided as part of the from/to GET request (in that order), or as a
 
        fallback it'll direct the user to source interface's entity details
 
        page.
 
        The URL will either point to value provided via GET parameter "next", or
 
        to project page to which the communication belongs.
 
        """
 

	
 
        entity_id = self.request.GET.get("from_entity", None) or self.request.GET.get("to_entity", None) or self.object.source.entity.pk
 

	
 
        return reverse("entity", args=(entity_id,))
 
        return self.request.GET.get("next", reverse("project", args=(self.object.source.entity.project.pk,)))
 

	
 

	
 
class CommunicationUpdateView(MultiplePermissionsRequiredMixin, UpdateView):
 
@@ -759,15 +755,11 @@ class CommunicationUpdateView(MultiplePe
 
        Returns the URL to which the user should be redirected after a
 
        communication has been created.
 

	
 
        The URL will be set to entity details page of an entity that was
 
        provided as part of the from/to GET request (in that order), or as a
 
        fallback it'll direct the user to source interface's entity details
 
        page.
 
        The URL will either point to value provided via GET parameter "next", or
 
        to project page to which the communication belongs.
 
        """
 

	
 
        entity_id = self.request.GET.get("from_entity", None) or self.request.GET.get("to_entity", None) or self.object.source.entity.pk
 

	
 
        return reverse("entity", args=(entity_id,))
 
        return self.request.GET.get("next", reverse("project", args=(self.object.source.entity.project.pk,)))
 

	
 

	
 
class CommunicationDeleteView(MultiplePermissionsRequiredMixin, DeleteView):
0 comments (0 inline, 0 general)