| 1 | from django import db |
| 2 | from django.conf import settings |
| 3 | from django.contrib.contenttypes.models import ContentType |
| 4 | from django.contrib.contenttypes.views import shortcut |
| 5 | from django.http import HttpRequest |
| 6 | from django.test import TestCase |
| 7 | |
| 8 | |
| 9 | class ContentTypesRegression(TestCase): |
| 10 | |
| 11 | def setUp(self): |
| 12 | self.old_DEBUG = settings.DEBUG |
| 13 | settings.DEBUG = True |
| 14 | ContentType.objects.clear_cache() |
| 15 | db.reset_queries() |
| 16 | |
| 17 | def tearDown(self): |
| 18 | settings.DEBUG = self.old_DEBUG |
| 19 | |
| 20 | def test_shortcut_attribute_error(self): |
| 21 | """ |
| 22 | Check that the shortcut view (used for the admin "view on site" |
| 23 | functionality) does not catch an AttributeError raised by the model's |
| 24 | get_absolute_url method |
| 25 | """ |
| 26 | |
| 27 | request = HttpRequest() |
| 28 | request.META = { |
| 29 | "SERVER_NAME": "Example.com", |
| 30 | "SERVER_PORT": "80", |
| 31 | } |
| 32 | from django.contrib.auth.models import Group |
| 33 | Group.get_absolute_url = lambda x: x.invalid_field |
| 34 | user_ct = ContentType.objects.get_for_model(Group) |
| 35 | obj = Group.objects.create(name="staff") |
| 36 | with self.assertRaises(AttributeError): |
| 37 | shortcut(request, user_ct.id, obj.id) |