From 2d483b7af15a620540099b564ff75832c1783927 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Fri, 11 Jan 2019 10:33:38 +0000
Subject: [PATCH] Make sure we 404 on for pages that don't exist

---
 opentech/apply/funds/tests/test_views.py | 13 ++++++++++++-
 opentech/apply/funds/views.py            |  7 +++++--
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/opentech/apply/funds/tests/test_views.py b/opentech/apply/funds/tests/test_views.py
index 2b1cc1c3b..f0209aa6a 100644
--- a/opentech/apply/funds/tests/test_views.py
+++ b/opentech/apply/funds/tests/test_views.py
@@ -556,7 +556,10 @@ class ByRoundTestCase(BaseViewTestCase):
     base_view_name = 'by_round'
 
     def get_kwargs(self, instance):
-        return {'pk': instance.id}
+        try:
+            return {'pk': instance.id}
+        except AttributeError:
+            return {'pk': instance['id']}
 
 
 class TestStaffSubmissionByRound(ByRoundTestCase):
@@ -578,6 +581,10 @@ class TestStaffSubmissionByRound(ByRoundTestCase):
         response = self.get_page(page)
         self.assertEqual(response.status_code, 404)
 
+    def test_cant_access_non_existing_page(self):
+        response = self.get_page({'id': 555})
+        self.assertEqual(response.status_code, 404)
+
 
 class TestApplicantSubmissionByRound(ByRoundTestCase):
     user_factory = UserFactory
@@ -597,3 +604,7 @@ class TestApplicantSubmissionByRound(ByRoundTestCase):
         page = new_round.get_site().root_page
         response = self.get_page(page)
         self.assertEqual(response.status_code, 403)
+
+    def test_cant_access_non_existing_page(self):
+        response = self.get_page({'id': 555})
+        self.assertEqual(response.status_code, 403)
diff --git a/opentech/apply/funds/views.py b/opentech/apply/funds/views.py
index 043ae8c9f..15f46128b 100644
--- a/opentech/apply/funds/views.py
+++ b/opentech/apply/funds/views.py
@@ -76,8 +76,11 @@ class SubmissionsByRound(BaseAdminSubmissionsTable):
 
     def get_queryset(self):
         # We want to only show lab or Rounds in this view, their base class is Page
-        self.obj = Page.objects.get(pk=self.kwargs.get('pk'))
-        self.obj = self.obj.specific
+        try:
+            self.obj = Page.objects.get(pk=self.kwargs.get('pk')).specific
+        except Page.DoesNotExist:
+            raise Http404(_("No Round or Lab found matching the query"))
+
         if not isinstance(self.obj, (LabBase, RoundBase)):
             raise Http404(_("No Round or Lab found matching the query"))
         return super().get_queryset().filter(Q(round=self.obj) | Q(page=self.obj))
-- 
GitLab