Skip to content
This repository has been archived by the owner on Oct 16, 2018. It is now read-only.

Fixed check for method not allowed, added extra tests #20

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 49 additions & 6 deletions composer/tests/test_middleware.py
Expand Up @@ -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(
Expand All @@ -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("""
<div id="header">
Header slot
Expand All @@ -40,22 +40,65 @@ def test_404(self):
</div>""" % 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
)

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename the test to test_request_method_types please.

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)
15 changes: 9 additions & 6 deletions composer/views.py
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is 405?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment please.

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
Expand All @@ -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)