From 25ab93457c2c294420fa50302834f3623200b690 Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Sat, 19 Dec 2009 15:25:16 +0000 Subject: [PATCH] Fixed #10927 - Content Types shortcut view throws 500s instead of 404s Thanks to Jeremy Dunck/Alex Gaynor for the patch git-svn-id: http://code.djangoproject.com/svn/django/trunk@11917 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/contenttypes/views.py | 2 +- tests/regressiontests/views/tests/defaults.py | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/django/contrib/contenttypes/views.py b/django/contrib/contenttypes/views.py index 4285be36cc..26961201cd 100644 --- a/django/contrib/contenttypes/views.py +++ b/django/contrib/contenttypes/views.py @@ -9,7 +9,7 @@ def shortcut(request, content_type_id, object_id): try: content_type = ContentType.objects.get(pk=content_type_id) obj = content_type.get_object_for_this_type(pk=object_id) - except ObjectDoesNotExist: + except (ObjectDoesNotExist, ValueError): raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id)) try: absurl = obj.get_absolute_url() diff --git a/tests/regressiontests/views/tests/defaults.py b/tests/regressiontests/views/tests/defaults.py index bf490d7cf0..1d1b3c3501 100644 --- a/tests/regressiontests/views/tests/defaults.py +++ b/tests/regressiontests/views/tests/defaults.py @@ -10,8 +10,8 @@ class DefaultsTests(TestCase): """Test django views in django/views/defaults.py""" fixtures = ['testdata.json'] - def test_shorcut_with_absolute_url(self): - "Can view a shortcut an Author object that has with a get_absolute_url method" + def test_shortcut_with_absolute_url(self): + "Can view a shortcut for an Author object that has a get_absolute_url method" for obj in Author.objects.all(): short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk) response = self.client.get(short_url) @@ -19,12 +19,34 @@ class DefaultsTests(TestCase): status_code=302, target_status_code=404) def test_shortcut_no_absolute_url(self): - "Shortcuts for an object that has with a get_absolute_url method raises 404" + "Shortcuts for an object that has no get_absolute_url method raises 404" for obj in Article.objects.all(): short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Article).id, obj.pk) response = self.client.get(short_url) self.assertEquals(response.status_code, 404) + def test_wrong_type_pk(self): + short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, 'nobody/expects') + response = self.client.get(short_url) + self.assertEquals(response.status_code, 404) + + def test_shortcut_bad_pk(self): + short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, '4242424242') + response = self.client.get(short_url) + self.assertEquals(response.status_code, 404) + + def test_nonint_content_type(self): + an_author = Author.objects.all()[0] + short_url = '/views/shortcut/%s/%s/' % ('spam', an_author.pk) + response = self.client.get(short_url) + self.assertEquals(response.status_code, 404) + + def test_bad_content_type(self): + an_author = Author.objects.all()[0] + short_url = '/views/shortcut/%s/%s/' % (4242424242, an_author.pk) + response = self.client.get(short_url) + self.assertEquals(response.status_code, 404) + def test_page_not_found(self): "A 404 status is returned by the page_not_found view" non_existing_urls = ['/views/non_existing_url/', # this is in urls.py