Browse Source

Fix search 500 error with invalid query (#389)

* Raise exception on ContentType model versus string name of content type.
* Added test for URL search parameters to return 200 instead of 500 error.

Co-authored-by: Roxanna Coldiron <roxanna@coderedcorp.com>
Roxanna Coldiron 4 years ago
parent
commit
d732f820d3
2 changed files with 12 additions and 1 deletions
  1. 11 0
      coderedcms/tests/test_urls.py
  2. 1 1
      coderedcms/views.py

+ 11 - 0
coderedcms/tests/test_urls.py

@@ -47,6 +47,17 @@ class TestSiteURLs(unittest.TestCase):
         self.assertEqual(response.status_code, 200)
         self.assertNotEqual(response.context['results'], None)
 
+        response = self.client.get(reverse(
+            'codered_search'),
+            {
+                's': 'keyword',
+                't': 't',
+            },
+            follow=False
+        )
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.context['results'], None)
+
 
 @pytest.mark.django_db
 class TestEventURLs(unittest.TestCase):

+ 1 - 1
coderedcms/views.py

@@ -74,7 +74,7 @@ def search(request):
                 try:
                     model = ContentType.objects.get(model=search_model).model_class()
                     results = model.objects.live().search(search_query)
-                except search_model.DoesNotExist:
+                except ContentType.DoesNotExist:
                     results = None
             else:
                 results = CoderedPage.objects.live().order_by('-last_published_at').search(search_query)  # noqa