File diff 843dbaebdae4 → dadf26dab8ba
conntrackt/tests/test_views.py
Show inline comments
 
@@ -31,11 +31,12 @@ from django.core.urlresolvers import rev
 
from django.http import Http404
 
from django.test import RequestFactory
 
from django.test import TestCase
 
from django.utils.http import urlquote
 

	
 
# Application imports
 
from conntrackt.models import Project, Location, Entity, Interface, Communication
 

	
 
from conntrackt.views import IndexView
 
from conntrackt.views import IndexView, SearchView
 
from conntrackt.views import entity_iptables, project_iptables, project_diagram
 

	
 
from conntrackt.views import ProjectView, ProjectCreateView, ProjectUpdateView, ProjectDeleteView
 
@@ -1532,3 +1533,74 @@ class RedirectToNextMixinTest(TestCase):
 
        view.next_parameter = "custom"
 

	
 
        self.assertEqual("/next", view.get_success_url())
 

	
 

	
 
class SearchViewTest(PermissionTestMixin, TestCase):
 

	
 
    sufficient_permissions = ("view",)
 
    view_class = SearchView
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
 

	
 
    def test_empty_query_error_message(self):
 
        """
 
        Verifies that an error is reported to the user in case an empty query is
 
        submitted.
 
        """
 

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

	
 
        # Generate the request
 
        request = RequestFactory().get("/fake-path?q=")
 
        request.user = mock.Mock()
 
        request._dont_enforce_csrf_checks = True
 
        request._messages = FakeMessages()
 

	
 
        # Get the response.
 
        response = view(request)
 

	
 
        self.assertIn("Search query is not allowed to be empty.", request._messages.messages)
 

	
 
    def test_strip_search_term(self):
 
        """
 
        Verifies that the search term is stripped when search is performed.
 
        """
 

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

	
 
        # Set-up a request.
 
        search_term = " \t \t something with lots of tabs \t  \t"
 
        stripped_search_term = "something with lots of tabs"
 
        request = RequestFactory().get("/fake-path?q=%s" % urlquote(search_term))
 
        request.user = mock.Mock()
 
        request._dont_enforce_csrf_checks = True
 

	
 
        # Get the response.
 
        response = view(request)
 

	
 
        # Validate the response.
 
        self.assertEqual(stripped_search_term, response.context_data["search_term"])
 

	
 
    def test_no_query_context(self):
 
        """
 
        Tests that context is not set if no query was sent.
 
        """
 

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

	
 
        # Set-up a request.
 
        response = generate_get_response(view)
 

	
 
        self.assertNotIn("entities", response.context_data)
 
        self.assertNotIn("projects", response.context_data)
 
        self.assertNotIn("search_term", response.context_data)
 
        # Only the "view" context variable should be present.
 
        self.assertEqual(1, len(response.context_data))