From 0dd85f042755b973ad20e29c51e02af083421135 Mon Sep 17 00:00:00 2001 From: AltusBarry Date: Mon, 23 Oct 2017 12:06:34 +0200 Subject: [PATCH 1/3] Fixed check for method not allowed, added extra tests --- composer/tests/test_middleware.py | 55 +++++++++++++++++++++++++++---- composer/views.py | 15 +++++---- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/composer/tests/test_middleware.py b/composer/tests/test_middleware.py index 0c1465a..4f7e5dd 100644 --- a/composer/tests/test_middleware.py +++ b/composer/tests/test_middleware.py @@ -10,7 +10,7 @@ class MiddleWareTestCase(TestCase): @classmethod def setUpTestData(cls): super(MiddleWareTestCase, cls).setUpTestData() - cls.slot = Slot.objects.create(slot_name="content", url="/four-o-four/") + cls.slot = Slot.objects.create(slot_name="content", url="/not-a-four-o-four/") cls.slot.sites = Site.objects.all() cls.slot.save() cls.tile = Tile.objects.create( @@ -21,7 +21,7 @@ def setUpTestData(cls): cls.tile.save() def test_404(self): - response = self.client.get("/four-o-four/") + response = self.client.get("/not-a-four-o-four/") self.assertHTMLEqual(""" """ % self.tile.id, response.content) def test_404_no_slash(self): - response = self.client.get("/four-o-four") + response = self.client.get("/not-a-four-o-four") self.assertRedirects( response, - "/four-o-four/", + "/not-a-four-o-four/", status_code=301, target_status_code=200, fetch_redirect_response=True @@ -51,11 +51,54 @@ def test_404_no_slash(self): def test_404_no_slash_no_redirect(self): with self.settings(APPEND_SLASH=False): - response = self.client.get("/four-o-four") + response = self.client.get("/not-a-four-o-four") self.assertEqual(response.status_code, 404) def test_request_method_options(self): - response = self.client.options("/four-o-four/") + # options + response = self.client.options("/not-a-four-o-four/") self.assertEqual(response.status_code, 200) response = self.client.options("/does-not-exist/") self.assertEqual(response.status_code, 404) + + # put + response = self.client.put("/does-not-exist/") + self.assertEqual(response.status_code, 405) + response = self.client.put("/not-a-four-o-four/") + self.assertEqual(response.status_code, 405) + + #patch + response = self.client.patch("/does-not-exist/") + self.assertEqual(response.status_code, 405) + response = self.client.patch("/not-a-four-o-four/") + self.assertEqual(response.status_code, 405) + + # get + response = self.client.get("/does-not-exist/") + self.assertEqual(response.status_code, 404) + response = self.client.get("/not-a-four-o-four/") + self.assertEqual(response.status_code, 200) + + # post + response = self.client.post("/does-not-exist/") + self.assertEqual(response.status_code, 405) + response = self.client.post("/not-a-four-o-four/") + self.assertEqual(response.status_code, 405) + + # delete + response = self.client.delete("/does-not-exist/") + self.assertEqual(response.status_code, 405) + response = self.client.delete("/not-a-four-o-four/") + self.assertEqual(response.status_code, 405) + + # head + response = self.client.head("/does-not-exist/") + self.assertEqual(response.status_code, 404) + response = self.client.head("/not-a-four-o-four/") + self.assertEqual(response.status_code, 200) + + # trace + response = self.client.trace("/does-not-exist/") + self.assertEqual(response.status_code, 405) + response = self.client.trace("/not-a-four-o-four/") + self.assertEqual(response.status_code, 405) diff --git a/composer/views.py b/composer/views.py index 46ffa45..edd6f68 100644 --- a/composer/views.py +++ b/composer/views.py @@ -11,6 +11,15 @@ class SlotView(DetailView): model = Slot + def dispatch(self, request, *args, **kwargs): + # Always return the get method's response, except if this view manages + # to trigger the method not allowed code path. + handler = super(SlotView, self).dispatch(request, *args, **kwargs) + if handler.status_code != 405: + return self.get(request, *args, **kwargs) + else: + return handler + def get_object(self): # Return the slot based on the path url = self.request.path_info @@ -19,9 +28,3 @@ def get_object(self): url=self.request.path_info, slot_name="content" ) - - def post(self, request, *args, **kwargs): - return self.get(request, *args, **kwargs) - - def options(self, request, *args, **kwargs): - return self.get(request, *args, **kwargs) From 0d71c640e6fcaa8d0569628a7cd738182718b25f Mon Sep 17 00:00:00 2001 From: AltusBarry Date: Mon, 23 Oct 2017 10:50:16 +0200 Subject: [PATCH 2/3] Changed test method name, added comment for 405 status check --- composer/tests/test_middleware.py | 2 +- composer/views.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/composer/tests/test_middleware.py b/composer/tests/test_middleware.py index 4f7e5dd..a8bddfd 100644 --- a/composer/tests/test_middleware.py +++ b/composer/tests/test_middleware.py @@ -54,7 +54,7 @@ def test_404_no_slash_no_redirect(self): response = self.client.get("/not-a-four-o-four") self.assertEqual(response.status_code, 404) - def test_request_method_options(self): + def test_request_method_types(self): # options response = self.client.options("/not-a-four-o-four/") self.assertEqual(response.status_code, 200) diff --git a/composer/views.py b/composer/views.py index edd6f68..bd25d42 100644 --- a/composer/views.py +++ b/composer/views.py @@ -15,6 +15,9 @@ def dispatch(self, request, *args, **kwargs): # Always return the get method's response, except if this view manages # to trigger the method not allowed code path. handler = super(SlotView, self).dispatch(request, *args, **kwargs) + + # The method not allowed path will come back with a 405 "Method Not + # Allowed" status. This is checked for and allowed to proceed as usual. if handler.status_code != 405: return self.get(request, *args, **kwargs) else: From ad65a47e29c6f6973c3e9918fa10025112a2bac6 Mon Sep 17 00:00:00 2001 From: AltusBarry Date: Mon, 23 Oct 2017 10:51:20 +0200 Subject: [PATCH 3/3] Improved comment --- composer/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer/views.py b/composer/views.py index bd25d42..095b996 100644 --- a/composer/views.py +++ b/composer/views.py @@ -17,7 +17,8 @@ def dispatch(self, request, *args, **kwargs): handler = super(SlotView, self).dispatch(request, *args, **kwargs) # The method not allowed path will come back with a 405 "Method Not - # Allowed" status. This is checked for and allowed to proceed as usual. + # Allowed" status. This is checked for and should proceed as default + # implementation dictates. if handler.status_code != 405: return self.get(request, *args, **kwargs) else: