Permalink
Browse files

test for ticket #18620

  • Loading branch information...
1 parent d9f2466 commit 3b4ef026a46cedf6bbc5c8ccb7528196bd8081c2 @mtigas mtigas committed Jul 12, 2012
Showing with 42 additions and 0 deletions.
  1. +42 −0 django/contrib/contenttypes/tests.py
@@ -8,6 +8,7 @@
from django.contrib.sites.models import Site
from django.http import HttpRequest, Http404
from django.test import TestCase
+from django.utils import unittest
from django.utils.encoding import smart_str
@@ -45,6 +46,13 @@ class FooWithBrokenAbsoluteUrl(FooWithoutUrl):
def get_absolute_url(self):
return "/users/%s/" % self.unknown_field
+if Site._meta.installed:
+ class FooWithSiteM2MAndUrl(FooWithUrl):
+ """
+ Fake model containing a `Site` many-to-many relationship.
+ """
+ sites = models.ManyToManyField(Site)
+
class ContentTypesTests(TestCase):
def setUp(self):
@@ -260,6 +268,40 @@ def test_shortcut_view_with_broken_get_absolute_url(self):
self.assertRaises(AttributeError, shortcut, request, user_ct.id, obj.id)
+ @unittest.skipIf(not Site._meta.installed,
+ "this tests against relationship behavior with the Sites framework")
+ def test_shortcut_view_m2m(self):
+ """
+ Check that the shortcut view (used for the admin "view on site"
+ functionality) returns a complete URL regardless of whether the sites
+ framework is installed
+ """
+ # Tests ticket #18620; prefer current_site rather than allowing
+ # M2M to select first domain via ABC order.
+ current_site = Site.objects.get_current() # example.com
+ other_site = Site(domain="a.example.com", name="a.example.com")
+ other_site.save()
+
+ # Build an object that belongs to both sites.
+ obj = FooWithSiteM2MAndUrl(name="john")
+ obj.save() # need saved object to set M2M relationship
+ obj.sites = [other_site, current_site]
+
+ # Check that shortcut view returns absolute URI for `current_site`
+ # and not `other_site`.
+ request = HttpRequest()
+ request.META = {
+ "SERVER_NAME": "Example.com",
+ "SERVER_PORT": "80",
+ }
+ user_ct = ContentType.objects.get_for_model(FooWithSiteM2MAndUrl)
+ response = shortcut(request, user_ct.id, obj.id)
+ self.assertEqual("http://%s/users/john/" % current_site.domain,
+ response._headers.get("location")[1])
+
+ obj.delete()
+ other_site.delete()
+
def test_missing_model(self):
"""
Ensures that displaying content types in admin (or anywhere) doesn't

0 comments on commit 3b4ef02

Please sign in to comment.