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
 
@@ -86,13 +86,14 @@
 
      </table>
 
      {% else %}
 
      <p>No incoming communications towards this entity.</p>
 
      {% 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>
 
  </div>
 

	
 
  <div class="span6">
 
@@ -110,13 +111,14 @@
 
      </table>
 
      {% else %}
 
      <p>No outgoing communications from this entity.</p>
 
      {% 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>
 
  </div>
 

	
 
  <div class="span12">
conntrackt/tests/test_views.py
Show inline comments
 
@@ -1059,16 +1059,16 @@ class CommunicationCreateViewTest(Permis
 

	
 
        # Get the initial values.
 
        initial = view.get_initial()
 

	
 
        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.
 
        view = CommunicationCreateView.as_view()
 

	
 
        # Generate the request.
 
@@ -1076,53 +1076,26 @@ class CommunicationCreateViewTest(Permis
 
        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?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.
 
        view = CommunicationCreateView.as_view()
 

	
 
        # Generate the request.
 
@@ -1137,13 +1110,13 @@ class CommunicationCreateViewTest(Permis
 
        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"], reverse("project", args=(1,)))
 
        self.assertEqual(response.status_code, 302)
 

	
 

	
 
class CommunicationUpdateViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 
@@ -1187,15 +1160,16 @@ class CommunicationUpdateViewTest(Permis
 
                               "<Interface: Test Entity 3 (192.168.1.3)>",
 
                               "<Interface: Test Subnet (192.168.2.0/255.255.255.0)>"]
 

	
 
        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.
 
        view = CommunicationUpdateView.as_view()
 

	
 
        # Get the communication object.
 
@@ -1203,51 +1177,26 @@ class CommunicationUpdateViewTest(Permis
 

	
 
        # Generate the request.
 
        post_data = {"source": communication.source.pk,
 
                     "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.
 
        view = CommunicationUpdateView.as_view()
 

	
 
        # Get the communication object.
 
@@ -1262,13 +1211,13 @@ class CommunicationUpdateViewTest(Permis
 
        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=(communication.source.entity.pk,)))
 
        self.assertEqual(response["Location"], reverse("project", args=(communication.source.entity.project.id,)))
 
        self.assertEqual(response.status_code, 302)
 

	
 

	
 
class CommunicationDeleteViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
conntrackt/views.py
Show inline comments
 
@@ -704,21 +704,17 @@ class CommunicationCreateView(MultiplePe
 

	
 
    def get_success_url(self):
 
        """
 
        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):
 
    """
 
    View for updating an existing communication.
 
    """
 
@@ -756,21 +752,17 @@ class CommunicationUpdateView(MultiplePe
 

	
 
    def get_success_url(self):
 
        """
 
        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):
 
    """
 
    View for deleting an communication.
 
    """
0 comments (0 inline, 0 general)