From 930af661abdb4eb7322f1ab06f21df9ed04af43d Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 23 Mar 2013 09:57:48 +0800 Subject: [PATCH] Fixed #20048, #20060 -- Modified tests for contrib apps sensitive to custom User models. Thanks to matiasb for the report of #20060 and the draft patch for #20048. --- django/contrib/flatpages/tests/csrf.py | 2 + django/contrib/flatpages/tests/middleware.py | 3 ++ .../contrib/flatpages/tests/templatetags.py | 12 +++--- django/contrib/flatpages/tests/views.py | 2 + .../formtools/tests/wizard/cookiestorage.py | 2 + .../contrib/formtools/tests/wizard/forms.py | 39 +++++++++++++------ .../tests/wizard/namedwizardtests/tests.py | 5 +++ .../formtools/tests/wizard/sessionstorage.py | 2 + .../tests/wizard/wizardtests/tests.py | 18 ++++++++- django/contrib/sitemaps/tests/base.py | 19 ++++++++- django/contrib/sitemaps/tests/generic.py | 8 ++-- django/contrib/sitemaps/tests/http.py | 11 +++--- django/contrib/sitemaps/tests/urls/http.py | 5 ++- 13 files changed, 96 insertions(+), 32 deletions(-) diff --git a/django/contrib/flatpages/tests/csrf.py b/django/contrib/flatpages/tests/csrf.py index e64c4bb1025..59996d98244 100644 --- a/django/contrib/flatpages/tests/csrf.py +++ b/django/contrib/flatpages/tests/csrf.py @@ -1,6 +1,7 @@ import os from django.conf import settings from django.contrib.auth.models import User +from django.contrib.auth.tests.utils import skipIfCustomUser from django.test import TestCase, Client from django.test.utils import override_settings @@ -38,6 +39,7 @@ class FlatpageCSRFTests(TestCase): response = self.client.get('/flatpage_root/no_such_flatpage/') self.assertEqual(response.status_code, 404) + @skipIfCustomUser def test_view_authenticated_flatpage(self): "A flatpage served through a view can require authentication" response = self.client.get('/flatpage_root/sekrit/') diff --git a/django/contrib/flatpages/tests/middleware.py b/django/contrib/flatpages/tests/middleware.py index 4afa4ff9822..1ad8fa93b3f 100644 --- a/django/contrib/flatpages/tests/middleware.py +++ b/django/contrib/flatpages/tests/middleware.py @@ -1,6 +1,7 @@ import os from django.conf import settings from django.contrib.auth.models import User +from django.contrib.auth.tests.utils import skipIfCustomUser from django.contrib.flatpages.models import FlatPage from django.test import TestCase from django.test.utils import override_settings @@ -36,6 +37,7 @@ class FlatpageMiddlewareTests(TestCase): response = self.client.get('/flatpage_root/no_such_flatpage/') self.assertEqual(response.status_code, 404) + @skipIfCustomUser def test_view_authenticated_flatpage(self): "A flatpage served through a view can require authentication" response = self.client.get('/flatpage_root/sekrit/') @@ -57,6 +59,7 @@ class FlatpageMiddlewareTests(TestCase): response = self.client.get('/no_such_flatpage/') self.assertEqual(response.status_code, 404) + @skipIfCustomUser def test_fallback_authenticated_flatpage(self): "A flatpage served by the middleware can require authentication" response = self.client.get('/sekrit/') diff --git a/django/contrib/flatpages/tests/templatetags.py b/django/contrib/flatpages/tests/templatetags.py index aebc62277e6..1a6f3c29752 100644 --- a/django/contrib/flatpages/tests/templatetags.py +++ b/django/contrib/flatpages/tests/templatetags.py @@ -1,6 +1,7 @@ import os from django.conf import settings from django.contrib.auth.models import AnonymousUser, User +from django.contrib.auth.tests.utils import skipIfCustomUser from django.template import Template, Context, TemplateSyntaxError from django.test import TestCase from django.test.utils import override_settings @@ -24,9 +25,6 @@ class FlatpageTemplateTagTests(TestCase): fixtures = ['sample_flatpages'] urls = 'django.contrib.flatpages.tests.urls' - def setUp(self): - self.me = User.objects.create_user('testuser', 'test@example.com', 's3krit') - def test_get_flatpages_tag(self): "The flatpage template tag retrives unregistered prefixed flatpages by default" out = Template( @@ -51,8 +49,10 @@ class FlatpageTemplateTagTests(TestCase): })) self.assertEqual(out, "A Flatpage,A Nested Flatpage,") + @skipIfCustomUser def test_get_flatpages_tag_for_user(self): "The flatpage template tag retrives all flatpages for an authenticated user" + me = User.objects.create_user('testuser', 'test@example.com', 's3krit') out = Template( "{% load flatpages %}" "{% get_flatpages for me as flatpages %}" @@ -60,7 +60,7 @@ class FlatpageTemplateTagTests(TestCase): "{{ page.title }}," "{% endfor %}" ).render(Context({ - 'me': self.me + 'me': me })) self.assertEqual(out, "A Flatpage,A Nested Flatpage,Sekrit Nested Flatpage,Sekrit Flatpage,") @@ -88,8 +88,10 @@ class FlatpageTemplateTagTests(TestCase): })) self.assertEqual(out, "A Nested Flatpage,") + @skipIfCustomUser def test_get_flatpages_with_prefix_for_user(self): "The flatpage template tag retrive prefixed flatpages for an authenticated user" + me = User.objects.create_user('testuser', 'test@example.com', 's3krit') out = Template( "{% load flatpages %}" "{% get_flatpages '/location/' for me as location_flatpages %}" @@ -97,7 +99,7 @@ class FlatpageTemplateTagTests(TestCase): "{{ page.title }}," "{% endfor %}" ).render(Context({ - 'me': self.me + 'me': me })) self.assertEqual(out, "A Nested Flatpage,Sekrit Nested Flatpage,") diff --git a/django/contrib/flatpages/tests/views.py b/django/contrib/flatpages/tests/views.py index b69bd6abb42..0448b212272 100644 --- a/django/contrib/flatpages/tests/views.py +++ b/django/contrib/flatpages/tests/views.py @@ -1,6 +1,7 @@ import os from django.conf import settings from django.contrib.auth.models import User +from django.contrib.auth.tests.utils import skipIfCustomUser from django.contrib.flatpages.models import FlatPage from django.test import TestCase from django.test.utils import override_settings @@ -36,6 +37,7 @@ class FlatpageViewTests(TestCase): response = self.client.get('/flatpage_root/no_such_flatpage/') self.assertEqual(response.status_code, 404) + @skipIfCustomUser def test_view_authenticated_flatpage(self): "A flatpage served through a view can require authentication" response = self.client.get('/flatpage_root/sekrit/') diff --git a/django/contrib/formtools/tests/wizard/cookiestorage.py b/django/contrib/formtools/tests/wizard/cookiestorage.py index 3f26b85f7d3..060e8260b56 100644 --- a/django/contrib/formtools/tests/wizard/cookiestorage.py +++ b/django/contrib/formtools/tests/wizard/cookiestorage.py @@ -5,10 +5,12 @@ from django.core import signing from django.core.exceptions import SuspiciousOperation from django.http import HttpResponse +from django.contrib.auth.tests.utils import skipIfCustomUser from django.contrib.formtools.wizard.storage.cookie import CookieStorage from django.contrib.formtools.tests.wizard.storage import get_request, TestStorage +@skipIfCustomUser class TestCookieStorage(TestStorage, TestCase): def get_storage(self): return CookieStorage diff --git a/django/contrib/formtools/tests/wizard/forms.py b/django/contrib/formtools/tests/wizard/forms.py index 51cfaa661b6..f6e177e5a4a 100644 --- a/django/contrib/formtools/tests/wizard/forms.py +++ b/django/contrib/formtools/tests/wizard/forms.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from django import forms, http from django.conf import settings +from django.db import models from django.test import TestCase from django.template.response import TemplateResponse from django.utils.importlib import import_module @@ -29,26 +30,40 @@ def get_request(*args, **kwargs): request.session = engine.SessionStore(None) return request + class Step1(forms.Form): name = forms.CharField() + class Step2(forms.Form): name = forms.CharField() + class Step3(forms.Form): data = forms.CharField() + class CustomKwargsStep1(Step1): def __init__(self, test=None, *args, **kwargs): self.test = test return super(CustomKwargsStep1, self).__init__(*args, **kwargs) -class UserForm(forms.ModelForm): - class Meta: - model = User -UserFormSet = forms.models.modelformset_factory(User, form=UserForm, extra=2) +class TestModel(models.Model): + name = models.CharField(max_length=100) + + class Meta: + app_label = 'formtools' + + +class TestModelForm(forms.ModelForm): + class Meta: + model = TestModel + + +TestModelFormSet = forms.models.modelformset_factory(TestModel, form=TestModelForm, extra=2) + class TestWizard(WizardView): storage_name = 'django.contrib.formtools.wizard.storage.session.SessionStorage' @@ -149,8 +164,8 @@ class FormTests(TestCase): def test_form_instance(self): request = get_request() - the_instance = User() - testform = TestWizard.as_view([('start', UserForm), ('step2', Step2)], + the_instance = TestModel() + testform = TestWizard.as_view([('start', TestModelForm), ('step2', Step2)], instance_dict={'start': the_instance}) response, instance = testform(request) @@ -163,12 +178,12 @@ class FormTests(TestCase): def test_formset_instance(self): request = get_request() - the_instance1, created = User.objects.get_or_create( - username='testuser1') - the_instance2, created = User.objects.get_or_create( - username='testuser2') - testform = TestWizard.as_view([('start', UserFormSet), ('step2', Step2)], - instance_dict={'start': User.objects.filter(username='testuser1')}) + the_instance1, created = TestModel.objects.get_or_create( + name='test object 1') + the_instance2, created = TestModel.objects.get_or_create( + name='test object 2') + testform = TestWizard.as_view([('start', TestModelFormSet), ('step2', Step2)], + instance_dict={'start': TestModel.objects.filter(name='test object 1')}) response, instance = testform(request) self.assertEqual(list(instance.get_form_instance('start')), [the_instance1]) diff --git a/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py b/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py index 214f19a04d7..824eb7e37e9 100644 --- a/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py +++ b/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py @@ -5,6 +5,7 @@ from django.http import QueryDict from django.test import TestCase from django.contrib.auth.models import User +from django.contrib.auth.tests.utils import skipIfCustomUser from django.contrib.formtools.wizard.views import (NamedUrlSessionWizardView, NamedUrlCookieWizardView) @@ -276,6 +277,7 @@ class NamedWizardTests(object): self.assertEqual(response.context['wizard']['steps'].current, 'form1') +@skipIfCustomUser class NamedSessionWizardTests(NamedWizardTests, TestCase): wizard_urlname = 'nwiz_session' wizard_step_1_data = { @@ -307,6 +309,7 @@ class NamedSessionWizardTests(NamedWizardTests, TestCase): ) +@skipIfCustomUser class NamedCookieWizardTests(NamedWizardTests, TestCase): wizard_urlname = 'nwiz_cookie' wizard_step_1_data = { @@ -367,11 +370,13 @@ class TestNamedUrlCookieWizardView(NamedUrlCookieWizardView): return response, self +@skipIfCustomUser class NamedSessionFormTests(NamedFormTests, TestCase): formwizard_class = TestNamedUrlSessionWizardView wizard_urlname = 'nwiz_session' +@skipIfCustomUser class NamedCookieFormTests(NamedFormTests, TestCase): formwizard_class = TestNamedUrlCookieWizardView wizard_urlname = 'nwiz_cookie' diff --git a/django/contrib/formtools/tests/wizard/sessionstorage.py b/django/contrib/formtools/tests/wizard/sessionstorage.py index 85410725c9c..0bd9fd8ecd7 100644 --- a/django/contrib/formtools/tests/wizard/sessionstorage.py +++ b/django/contrib/formtools/tests/wizard/sessionstorage.py @@ -1,9 +1,11 @@ from django.test import TestCase +from django.contrib.auth.tests.utils import skipIfCustomUser from django.contrib.formtools.tests.wizard.storage import TestStorage from django.contrib.formtools.wizard.storage.session import SessionStorage +@skipIfCustomUser class TestSessionStorage(TestStorage, TestCase): def get_storage(self): return SessionStorage diff --git a/django/contrib/formtools/tests/wizard/wizardtests/tests.py b/django/contrib/formtools/tests/wizard/wizardtests/tests.py index 4aaea7d56e6..1ee5dbdc787 100644 --- a/django/contrib/formtools/tests/wizard/wizardtests/tests.py +++ b/django/contrib/formtools/tests/wizard/wizardtests/tests.py @@ -7,11 +7,19 @@ from django.test import TestCase from django.test.client import RequestFactory from django.conf import settings from django.contrib.auth.models import User +from django.contrib.auth.tests.utils import skipIfCustomUser from django.contrib.formtools.wizard.views import CookieWizardView -from django.contrib.formtools.tests.wizard.forms import UserForm, UserFormSet from django.utils._os import upath +class UserForm(forms.ModelForm): + class Meta: + model = User + + +UserFormSet = forms.models.modelformset_factory(User, form=UserForm, extra=2) + + class WizardTests(object): urls = 'django.contrib.formtools.tests.wizard.wizardtests.urls' @@ -75,7 +83,7 @@ class WizardTests(object): # ticket #19025: `form` should be included in context form = response.context_data['wizard']['form'] - self.assertEqual(response.context_data['form'], form) + self.assertEqual(response.context_data['form'], form) def test_form_finish(self): response = self.client.get(self.wizard_url) @@ -196,6 +204,7 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) +@skipIfCustomUser class SessionWizardTests(WizardTests, TestCase): wizard_url = '/wiz_session/' wizard_step_1_data = { @@ -226,6 +235,8 @@ class SessionWizardTests(WizardTests, TestCase): } ) + +@skipIfCustomUser class CookieWizardTests(WizardTests, TestCase): wizard_url = '/wiz_cookie/' wizard_step_1_data = { @@ -256,6 +267,8 @@ class CookieWizardTests(WizardTests, TestCase): } ) + +@skipIfCustomUser class WizardTestKwargs(TestCase): wizard_url = '/wiz_other_template/' wizard_step_1_data = { @@ -347,6 +360,7 @@ class WizardTestGenericViewInterface(TestCase): self.assertEqual(response.context_data['another_key'], 'another_value') +@skipIfCustomUser class WizardFormKwargsOverrideTests(TestCase): def setUp(self): super(WizardFormKwargsOverrideTests, self).setUp() diff --git a/django/contrib/sitemaps/tests/base.py b/django/contrib/sitemaps/tests/base.py index bbe8229aae5..099dba72565 100644 --- a/django/contrib/sitemaps/tests/base.py +++ b/django/contrib/sitemaps/tests/base.py @@ -1,9 +1,24 @@ from django.contrib.auth.models import User from django.contrib.sites.models import Site from django.core.cache import cache +from django.db import models from django.test import TestCase +class TestModel(models.Model): + "A test model for " + name = models.CharField(max_length=100) + + class Meta: + app_label = 'sitemaps' + + def __unicode__(self): + return self.name + + def get_absolute_url(self): + return '/testmodel/%s/' % self.id + + class SitemapTestsBase(TestCase): protocol = 'http' domain = 'example.com' if Site._meta.installed else 'testserver' @@ -13,8 +28,8 @@ class SitemapTestsBase(TestCase): self.base_url = '%s://%s' % (self.protocol, self.domain) self.old_Site_meta_installed = Site._meta.installed cache.clear() - # Create a user that will double as sitemap content - User.objects.create_user('testuser', 'test@example.com', 's3krit') + # Create an object for sitemap content. + TestModel.objects.create(name='Test Object') def tearDown(self): Site._meta.installed = self.old_Site_meta_installed diff --git a/django/contrib/sitemaps/tests/generic.py b/django/contrib/sitemaps/tests/generic.py index ae054c95c28..5b265736b31 100644 --- a/django/contrib/sitemaps/tests/generic.py +++ b/django/contrib/sitemaps/tests/generic.py @@ -1,9 +1,9 @@ from __future__ import unicode_literals -from django.contrib.auth.models import User from django.test.utils import override_settings -from .base import SitemapTestsBase +from .base import TestModel, SitemapTestsBase + @override_settings(ABSOLUTE_URL_OVERRIDES={}) class GenericViewsSitemapTests(SitemapTestsBase): @@ -12,8 +12,8 @@ class GenericViewsSitemapTests(SitemapTestsBase): "A minimal generic sitemap can be rendered" response = self.client.get('/generic/sitemap.xml') expected = '' - for username in User.objects.values_list("username", flat=True): - expected += "%s/users/%s/" % (self.base_url, username) + for pk in TestModel.objects.values_list("id", flat=True): + expected += "%s/testmodel/%s/" % (self.base_url, pk) expected_content = """ %s diff --git a/django/contrib/sitemaps/tests/http.py b/django/contrib/sitemaps/tests/http.py index 4a1cf66b171..1a91d970f3e 100644 --- a/django/contrib/sitemaps/tests/http.py +++ b/django/contrib/sitemaps/tests/http.py @@ -4,7 +4,6 @@ import os from datetime import date from django.conf import settings -from django.contrib.auth.models import User from django.contrib.sitemaps import Sitemap, GenericSitemap from django.contrib.sites.models import Site from django.core.exceptions import ImproperlyConfigured @@ -14,7 +13,7 @@ from django.utils.formats import localize from django.utils._os import upath from django.utils.translation import activate, deactivate -from .base import SitemapTestsBase +from .base import TestModel, SitemapTestsBase class HTTPSitemapTests(SitemapTestsBase): @@ -128,10 +127,10 @@ class HTTPSitemapTests(SitemapTestsBase): Check to make sure that the raw item is included with each Sitemap.get_url() url result. """ - user_sitemap = GenericSitemap({'queryset': User.objects.all()}) - def is_user(url): - return isinstance(url['item'], User) - item_in_url_info = all(map(is_user, user_sitemap.get_urls())) + test_sitemap = GenericSitemap({'queryset': TestModel.objects.all()}) + def is_testmodel(url): + return isinstance(url['item'], TestModel) + item_in_url_info = all(map(is_testmodel, test_sitemap.get_urls())) self.assertTrue(item_in_url_info) def test_cached_sitemap_index(self): diff --git a/django/contrib/sitemaps/tests/urls/http.py b/django/contrib/sitemaps/tests/urls/http.py index 018e46a482e..56103f4ec23 100644 --- a/django/contrib/sitemaps/tests/urls/http.py +++ b/django/contrib/sitemaps/tests/urls/http.py @@ -4,6 +4,9 @@ from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap, vi from django.contrib.auth.models import User from django.views.decorators.cache import cache_page +from django.contrib.sitemaps.tests.base import TestModel + + class SimpleSitemap(Sitemap): changefreq = "never" priority = 0.5 @@ -18,7 +21,7 @@ simple_sitemaps = { } generic_sitemaps = { - 'generic': GenericSitemap({'queryset': User.objects.all()}), + 'generic': GenericSitemap({'queryset': TestModel.objects.all()}), } flatpage_sitemaps = {