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 |
| 1 | import urllib |
| 2 | from django import db, http |
2 | 3 | from django.conf import settings |
3 | 4 | from django.contrib.contenttypes.models import ContentType |
4 | 5 | from django.contrib.sites.models import Site |
… |
… |
from django.contrib.contenttypes.views import shortcut
|
6 | 7 | from django.core.exceptions import ObjectDoesNotExist |
7 | 8 | from django.http import HttpRequest |
8 | 9 | from django.test import TestCase |
| 10 | from django.db import models |
| 11 | from django.utils.translation import ugettext_lazy as _ |
| 12 | from django.utils.encoding import smart_str |
9 | 13 | |
| 14 | class 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 | |
| 27 | class 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 |
10 | 36 | |
11 | 37 | class ContentTypesTests(TestCase): |
12 | 38 | |
… |
… |
class ContentTypesTests(TestCase):
|
58 | 84 | "SERVER_NAME": "Example.com", |
59 | 85 | "SERVER_PORT": "80", |
60 | 86 | } |
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") |
64 | 89 | |
65 | 90 | if Site._meta.installed: |
66 | 91 | current_site = Site.objects.get_current() |
… |
… |
class ContentTypesTests(TestCase):
|
72 | 97 | response = shortcut(request, user_ct.id, obj.id) |
73 | 98 | self.assertEqual("http://Example.com/users/john/", |
74 | 99 | 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) |