Skip to content

Commit

Permalink
return 404 if markdown template not found
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Nov 14, 2023
1 parent d0bc64c commit e930cbe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 12 additions & 0 deletions caps/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 6 additions & 2 deletions caps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit e930cbe

Please sign in to comment.