diff --git a/caps/tests/test_views.py b/caps/tests/test_views.py index c38922531..be50728e8 100644 --- a/caps/tests/test_views.py +++ b/caps/tests/test_views.py @@ -284,3 +284,15 @@ def test_search_results_detects_postcode(self): self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "caps/search_results.html") self.assertRegex(response.content, rb"Looking for your local council") + + +class TestMarkDownView(TestCase): + def test_page_works(self): + url = reverse("content", args=("mrp",)) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + def test_missing_template_404(self): + url = reverse("content", args=("missing-template",)) + response = self.client.get(url) + self.assertEqual(response.status_code, 404) diff --git a/caps/views.py b/caps/views.py index e2dd2ec4d..b2b7a107b 100755 --- a/caps/views.py +++ b/caps/views.py @@ -23,9 +23,10 @@ Value, When, ) -from django.http import HttpResponse, JsonResponse +from django.http import Http404, HttpResponse, JsonResponse from django.shortcuts import redirect, render from django.template import Context, Template +from django.template.exceptions import TemplateDoesNotExist from django.template.loader import get_template from django.utils.safestring import mark_safe from django.views.generic import DetailView, ListView, TemplateView, View @@ -901,7 +902,10 @@ def get_context_data(self, **kwargs): # sanitise the slug to prevent directory traversal markdown_slug = re.sub(r"[^a-zA-Z0-9_-]", "", markdown_slug) template_path = Path("caps", "markdown/{}.md".format(markdown_slug)) - template = get_template(template_path) + try: + template = get_template(template_path) + except TemplateDoesNotExist: + raise Http404 markdown_body = template.template.source.strip()