Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

test for ticket #18620

  • Loading branch information...
commit 3b4ef026a46cedf6bbc5c8ccb7528196bd8081c2 1 parent d9f2466
Mike Tigas authored July 12, 2012

Showing 1 changed file with 42 additions and 0 deletions. Show diff stats Hide diff stats

  1. 42  django/contrib/contenttypes/tests.py
42  django/contrib/contenttypes/tests.py
@@ -8,6 +8,7 @@
8 8
 from django.contrib.sites.models import Site
9 9
 from django.http import HttpRequest, Http404
10 10
 from django.test import TestCase
  11
+from django.utils import unittest
11 12
 from django.utils.encoding import smart_str
12 13
 
13 14
 
@@ -45,6 +46,13 @@ class FooWithBrokenAbsoluteUrl(FooWithoutUrl):
45 46
     def get_absolute_url(self):
46 47
         return "/users/%s/" % self.unknown_field
47 48
 
  49
+if Site._meta.installed:
  50
+    class FooWithSiteM2MAndUrl(FooWithUrl):
  51
+        """
  52
+        Fake model containing a `Site` many-to-many relationship.
  53
+        """
  54
+        sites = models.ManyToManyField(Site)
  55
+
48 56
 class ContentTypesTests(TestCase):
49 57
 
50 58
     def setUp(self):
@@ -260,6 +268,40 @@ def test_shortcut_view_with_broken_get_absolute_url(self):
260 268
 
261 269
         self.assertRaises(AttributeError, shortcut, request, user_ct.id, obj.id)
262 270
 
  271
+    @unittest.skipIf(not Site._meta.installed,
  272
+                     "this tests against relationship behavior with the Sites framework")
  273
+    def test_shortcut_view_m2m(self):
  274
+        """
  275
+        Check that the shortcut view (used for the admin "view on site"
  276
+        functionality) returns a complete URL regardless of whether the sites
  277
+        framework is installed
  278
+        """
  279
+        # Tests ticket #18620; prefer current_site rather than allowing
  280
+        # M2M to select first domain via ABC order.
  281
+        current_site = Site.objects.get_current()  # example.com
  282
+        other_site = Site(domain="a.example.com", name="a.example.com")
  283
+        other_site.save()
  284
+
  285
+        # Build an object that belongs to both sites.
  286
+        obj = FooWithSiteM2MAndUrl(name="john")
  287
+        obj.save()  # need saved object to set M2M relationship
  288
+        obj.sites = [other_site, current_site]
  289
+
  290
+        # Check that shortcut view returns absolute URI for `current_site`
  291
+        # and not `other_site`.
  292
+        request = HttpRequest()
  293
+        request.META = {
  294
+            "SERVER_NAME": "Example.com",
  295
+            "SERVER_PORT": "80",
  296
+        }
  297
+        user_ct = ContentType.objects.get_for_model(FooWithSiteM2MAndUrl)
  298
+        response = shortcut(request, user_ct.id, obj.id)
  299
+        self.assertEqual("http://%s/users/john/" % current_site.domain,
  300
+                         response._headers.get("location")[1])
  301
+
  302
+        obj.delete()
  303
+        other_site.delete()
  304
+
263 305
     def test_missing_model(self):
264 306
         """
265 307
         Ensures that displaying content types in admin (or anywhere) doesn't

0 notes on commit 3b4ef02

Please sign in to comment.
Something went wrong with that request. Please try again.