Ticket #14543: 14543.3.patch

File 14543.3.patch, 3.3 KB (added by crayz_train, 14 years ago)

works against r15864

  • django/contrib/contenttypes/tests.py

    diff --git a/django/contrib/contenttypes/tests.py b/django/contrib/contenttypes/tests.py
    index 790193b..13a7299 100644
    a b  
    1 from django import db
     1import urllib
     2from django import db, http
    23from django.conf import settings
    34from django.contrib.contenttypes.models import ContentType
    45from django.contrib.sites.models import Site
    from django.contrib.contenttypes.views import shortcut  
    67from django.core.exceptions import ObjectDoesNotExist
    78from django.http import HttpRequest
    89from django.test import TestCase
     10from django.db import models
     11from django.utils.translation import ugettext_lazy as _
     12from django.utils.encoding import smart_str
    913
     14class FooWithUrl(models.Model):
     15    """fake model defining get_absolute_url for test_shortcut_view"""
     16    fooname = models.CharField(_('fooname'), max_length=30, unique=True, help_text=_("Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters"))
     17    class Meta:
     18        verbose_name = _('foo_with_url')
     19        verbose_name_plural = _('fooz_with_url')
     20
     21    def __unicode__(self):
     22        return self.fooname
     23
     24    def get_absolute_url(self):
     25        return "/users/%s/" % urllib.quote(smart_str(self.fooname))
     26
     27class FooWithoutUrl(models.Model):
     28    """fake model not defining get_absolute_url for test_shortcut_view"""
     29    fooname = models.CharField(_('fooname'), max_length=30, unique=True, help_text=_("Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters"))
     30    class Meta:
     31        verbose_name = _('foo_without_url')
     32        verbose_name_plural = _('fooz_without_url')
     33
     34    def __unicode__(self):
     35        return self.fooname
    1036
    1137class ContentTypesTests(TestCase):
    1238
    class ContentTypesTests(TestCase):  
    5884            "SERVER_NAME": "Example.com",
    5985            "SERVER_PORT": "80",
    6086        }
    61         from django.contrib.auth.models import User
    62         user_ct = ContentType.objects.get_for_model(User)
    63         obj = User.objects.create(username="john")
     87        user_ct = ContentType.objects.get_for_model(FooWithUrl)
     88        obj = FooWithUrl.objects.create(fooname="john")
    6489
    6590        if Site._meta.installed:
    6691            current_site = Site.objects.get_current()
    class ContentTypesTests(TestCase):  
    7297        response = shortcut(request, user_ct.id, obj.id)
    7398        self.assertEqual("http://Example.com/users/john/",
    7499                         response._headers.get("location")[1])
     100
     101    def test_shortcut_view_negative(self):
     102        """
     103        Check that the shortcut view (used for the admin "view on site"
     104        functionality) returns 404 when get_absolute_url is not
     105        defined
     106        """
     107
     108        request = HttpRequest()
     109        request.META = {
     110            "SERVER_NAME": "Example.com",
     111            "SERVER_PORT": "80",
     112        }
     113        user_ct = ContentType.objects.get_for_model(FooWithoutUrl)
     114        obj = FooWithoutUrl.objects.create(fooname="john")
     115
     116        if Site._meta.installed:
     117            current_site = Site.objects.get_current()
     118            with self.assertRaises(http.Http404):
     119                response = shortcut(request, user_ct.id, obj.id)
     120
     121        Site._meta.installed = False
     122        with self.assertRaises(http.Http404):
     123            response = shortcut(request, user_ct.id, obj.id)
Back to Top