Permalink
Browse files

Fixes #14543 -- ContentTypes tests failing if auth app is not install…

…ed. Thanks for the work on the patch, sayane and crayz_train.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16101 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
SmileyChris committed Apr 25, 2011
1 parent 449e84a commit a6c08a53d37cd91f2073f8744539a4297fd652f3
Showing with 42 additions and 5 deletions.
  1. +42 −5 django/contrib/contenttypes/tests.py
@@ -1,11 +1,33 @@
+import urllib
from django import db
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.contrib.contenttypes.views import shortcut
-from django.core.exceptions import ObjectDoesNotExist
-from django.http import HttpRequest
+from django.http import HttpRequest, Http404
from django.test import TestCase
+from django.db import models
+from django.utils.encoding import smart_str
+
+
+class FooWithoutUrl(models.Model):
+ """
+ Fake model not defining ``get_absolute_url`` for
+ :meth:`ContentTypesTests.test_shortcut_view_without_get_absolute_url`"""
+ name = models.CharField(max_length=30, unique=True)
+
+ def __unicode__(self):
+ return self.name
+
+
+class FooWithUrl(FooWithoutUrl):
+ """
+ Fake model defining ``get_absolute_url`` for
+ :meth:`ContentTypesTests.test_shortcut_view`
+ """
+
+ def get_absolute_url(self):
+ return "/users/%s/" % urllib.quote(smart_str(self.name))
class ContentTypesTests(TestCase):
@@ -58,9 +80,8 @@ def test_shortcut_view(self):
"SERVER_NAME": "Example.com",
"SERVER_PORT": "80",
}
- from django.contrib.auth.models import User
- user_ct = ContentType.objects.get_for_model(User)
- obj = User.objects.create(username="john")
+ user_ct = ContentType.objects.get_for_model(FooWithUrl)
+ obj = FooWithUrl.objects.create(name="john")
if Site._meta.installed:
current_site = Site.objects.get_current()
@@ -72,3 +93,19 @@ def test_shortcut_view(self):
response = shortcut(request, user_ct.id, obj.id)
self.assertEqual("http://Example.com/users/john/",
response._headers.get("location")[1])
+
+ def test_shortcut_view_without_get_absolute_url(self):
+ """
+ Check that the shortcut view (used for the admin "view on site"
+ functionality) returns 404 when get_absolute_url is not defined.
+ """
+
+ request = HttpRequest()
+ request.META = {
+ "SERVER_NAME": "Example.com",
+ "SERVER_PORT": "80",
+ }
+ user_ct = ContentType.objects.get_for_model(FooWithoutUrl)
+ obj = FooWithoutUrl.objects.create(name="john")
+
+ self.assertRaises(Http404, shortcut, request, user_ct.id, obj.id)

0 comments on commit a6c08a5

Please sign in to comment.