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.
This commit is contained in:
Russell Keith-Magee 2013-03-23 09:57:48 +08:00
parent f7d945e325
commit 930af661ab
13 changed files with 96 additions and 32 deletions

View File

@ -1,6 +1,7 @@
import os import os
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.tests.utils import skipIfCustomUser
from django.test import TestCase, Client from django.test import TestCase, Client
from django.test.utils import override_settings from django.test.utils import override_settings
@ -38,6 +39,7 @@ class FlatpageCSRFTests(TestCase):
response = self.client.get('/flatpage_root/no_such_flatpage/') response = self.client.get('/flatpage_root/no_such_flatpage/')
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
@skipIfCustomUser
def test_view_authenticated_flatpage(self): def test_view_authenticated_flatpage(self):
"A flatpage served through a view can require authentication" "A flatpage served through a view can require authentication"
response = self.client.get('/flatpage_root/sekrit/') response = self.client.get('/flatpage_root/sekrit/')

View File

@ -1,6 +1,7 @@
import os import os
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.tests.utils import skipIfCustomUser
from django.contrib.flatpages.models import FlatPage from django.contrib.flatpages.models import FlatPage
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
@ -36,6 +37,7 @@ class FlatpageMiddlewareTests(TestCase):
response = self.client.get('/flatpage_root/no_such_flatpage/') response = self.client.get('/flatpage_root/no_such_flatpage/')
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
@skipIfCustomUser
def test_view_authenticated_flatpage(self): def test_view_authenticated_flatpage(self):
"A flatpage served through a view can require authentication" "A flatpage served through a view can require authentication"
response = self.client.get('/flatpage_root/sekrit/') response = self.client.get('/flatpage_root/sekrit/')
@ -57,6 +59,7 @@ class FlatpageMiddlewareTests(TestCase):
response = self.client.get('/no_such_flatpage/') response = self.client.get('/no_such_flatpage/')
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
@skipIfCustomUser
def test_fallback_authenticated_flatpage(self): def test_fallback_authenticated_flatpage(self):
"A flatpage served by the middleware can require authentication" "A flatpage served by the middleware can require authentication"
response = self.client.get('/sekrit/') response = self.client.get('/sekrit/')

View File

@ -1,6 +1,7 @@
import os import os
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import AnonymousUser, User 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.template import Template, Context, TemplateSyntaxError
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
@ -24,9 +25,6 @@ class FlatpageTemplateTagTests(TestCase):
fixtures = ['sample_flatpages'] fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls' 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): def test_get_flatpages_tag(self):
"The flatpage template tag retrives unregistered prefixed flatpages by default" "The flatpage template tag retrives unregistered prefixed flatpages by default"
out = Template( out = Template(
@ -51,8 +49,10 @@ class FlatpageTemplateTagTests(TestCase):
})) }))
self.assertEqual(out, "A Flatpage,A Nested Flatpage,") self.assertEqual(out, "A Flatpage,A Nested Flatpage,")
@skipIfCustomUser
def test_get_flatpages_tag_for_user(self): def test_get_flatpages_tag_for_user(self):
"The flatpage template tag retrives all flatpages for an authenticated user" "The flatpage template tag retrives all flatpages for an authenticated user"
me = User.objects.create_user('testuser', 'test@example.com', 's3krit')
out = Template( out = Template(
"{% load flatpages %}" "{% load flatpages %}"
"{% get_flatpages for me as flatpages %}" "{% get_flatpages for me as flatpages %}"
@ -60,7 +60,7 @@ class FlatpageTemplateTagTests(TestCase):
"{{ page.title }}," "{{ page.title }},"
"{% endfor %}" "{% endfor %}"
).render(Context({ ).render(Context({
'me': self.me 'me': me
})) }))
self.assertEqual(out, "A Flatpage,A Nested Flatpage,Sekrit Nested Flatpage,Sekrit Flatpage,") 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,") self.assertEqual(out, "A Nested Flatpage,")
@skipIfCustomUser
def test_get_flatpages_with_prefix_for_user(self): def test_get_flatpages_with_prefix_for_user(self):
"The flatpage template tag retrive prefixed flatpages for an authenticated user" "The flatpage template tag retrive prefixed flatpages for an authenticated user"
me = User.objects.create_user('testuser', 'test@example.com', 's3krit')
out = Template( out = Template(
"{% load flatpages %}" "{% load flatpages %}"
"{% get_flatpages '/location/' for me as location_flatpages %}" "{% get_flatpages '/location/' for me as location_flatpages %}"
@ -97,7 +99,7 @@ class FlatpageTemplateTagTests(TestCase):
"{{ page.title }}," "{{ page.title }},"
"{% endfor %}" "{% endfor %}"
).render(Context({ ).render(Context({
'me': self.me 'me': me
})) }))
self.assertEqual(out, "A Nested Flatpage,Sekrit Nested Flatpage,") self.assertEqual(out, "A Nested Flatpage,Sekrit Nested Flatpage,")

View File

@ -1,6 +1,7 @@
import os import os
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.tests.utils import skipIfCustomUser
from django.contrib.flatpages.models import FlatPage from django.contrib.flatpages.models import FlatPage
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
@ -36,6 +37,7 @@ class FlatpageViewTests(TestCase):
response = self.client.get('/flatpage_root/no_such_flatpage/') response = self.client.get('/flatpage_root/no_such_flatpage/')
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
@skipIfCustomUser
def test_view_authenticated_flatpage(self): def test_view_authenticated_flatpage(self):
"A flatpage served through a view can require authentication" "A flatpage served through a view can require authentication"
response = self.client.get('/flatpage_root/sekrit/') response = self.client.get('/flatpage_root/sekrit/')

View File

@ -5,10 +5,12 @@ from django.core import signing
from django.core.exceptions import SuspiciousOperation from django.core.exceptions import SuspiciousOperation
from django.http import HttpResponse 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.wizard.storage.cookie import CookieStorage
from django.contrib.formtools.tests.wizard.storage import get_request, TestStorage from django.contrib.formtools.tests.wizard.storage import get_request, TestStorage
@skipIfCustomUser
class TestCookieStorage(TestStorage, TestCase): class TestCookieStorage(TestStorage, TestCase):
def get_storage(self): def get_storage(self):
return CookieStorage return CookieStorage

View File

@ -2,6 +2,7 @@ from __future__ import unicode_literals
from django import forms, http from django import forms, http
from django.conf import settings from django.conf import settings
from django.db import models
from django.test import TestCase from django.test import TestCase
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.utils.importlib import import_module from django.utils.importlib import import_module
@ -29,26 +30,40 @@ def get_request(*args, **kwargs):
request.session = engine.SessionStore(None) request.session = engine.SessionStore(None)
return request return request
class Step1(forms.Form): class Step1(forms.Form):
name = forms.CharField() name = forms.CharField()
class Step2(forms.Form): class Step2(forms.Form):
name = forms.CharField() name = forms.CharField()
class Step3(forms.Form): class Step3(forms.Form):
data = forms.CharField() data = forms.CharField()
class CustomKwargsStep1(Step1): class CustomKwargsStep1(Step1):
def __init__(self, test=None, *args, **kwargs): def __init__(self, test=None, *args, **kwargs):
self.test = test self.test = test
return super(CustomKwargsStep1, self).__init__(*args, **kwargs) 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): class TestWizard(WizardView):
storage_name = 'django.contrib.formtools.wizard.storage.session.SessionStorage' storage_name = 'django.contrib.formtools.wizard.storage.session.SessionStorage'
@ -149,8 +164,8 @@ class FormTests(TestCase):
def test_form_instance(self): def test_form_instance(self):
request = get_request() request = get_request()
the_instance = User() the_instance = TestModel()
testform = TestWizard.as_view([('start', UserForm), ('step2', Step2)], testform = TestWizard.as_view([('start', TestModelForm), ('step2', Step2)],
instance_dict={'start': the_instance}) instance_dict={'start': the_instance})
response, instance = testform(request) response, instance = testform(request)
@ -163,12 +178,12 @@ class FormTests(TestCase):
def test_formset_instance(self): def test_formset_instance(self):
request = get_request() request = get_request()
the_instance1, created = User.objects.get_or_create( the_instance1, created = TestModel.objects.get_or_create(
username='testuser1') name='test object 1')
the_instance2, created = User.objects.get_or_create( the_instance2, created = TestModel.objects.get_or_create(
username='testuser2') name='test object 2')
testform = TestWizard.as_view([('start', UserFormSet), ('step2', Step2)], testform = TestWizard.as_view([('start', TestModelFormSet), ('step2', Step2)],
instance_dict={'start': User.objects.filter(username='testuser1')}) instance_dict={'start': TestModel.objects.filter(name='test object 1')})
response, instance = testform(request) response, instance = testform(request)
self.assertEqual(list(instance.get_form_instance('start')), [the_instance1]) self.assertEqual(list(instance.get_form_instance('start')), [the_instance1])

View File

@ -5,6 +5,7 @@ from django.http import QueryDict
from django.test import TestCase from django.test import TestCase
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.tests.utils import skipIfCustomUser
from django.contrib.formtools.wizard.views import (NamedUrlSessionWizardView, from django.contrib.formtools.wizard.views import (NamedUrlSessionWizardView,
NamedUrlCookieWizardView) NamedUrlCookieWizardView)
@ -276,6 +277,7 @@ class NamedWizardTests(object):
self.assertEqual(response.context['wizard']['steps'].current, 'form1') self.assertEqual(response.context['wizard']['steps'].current, 'form1')
@skipIfCustomUser
class NamedSessionWizardTests(NamedWizardTests, TestCase): class NamedSessionWizardTests(NamedWizardTests, TestCase):
wizard_urlname = 'nwiz_session' wizard_urlname = 'nwiz_session'
wizard_step_1_data = { wizard_step_1_data = {
@ -307,6 +309,7 @@ class NamedSessionWizardTests(NamedWizardTests, TestCase):
) )
@skipIfCustomUser
class NamedCookieWizardTests(NamedWizardTests, TestCase): class NamedCookieWizardTests(NamedWizardTests, TestCase):
wizard_urlname = 'nwiz_cookie' wizard_urlname = 'nwiz_cookie'
wizard_step_1_data = { wizard_step_1_data = {
@ -367,11 +370,13 @@ class TestNamedUrlCookieWizardView(NamedUrlCookieWizardView):
return response, self return response, self
@skipIfCustomUser
class NamedSessionFormTests(NamedFormTests, TestCase): class NamedSessionFormTests(NamedFormTests, TestCase):
formwizard_class = TestNamedUrlSessionWizardView formwizard_class = TestNamedUrlSessionWizardView
wizard_urlname = 'nwiz_session' wizard_urlname = 'nwiz_session'
@skipIfCustomUser
class NamedCookieFormTests(NamedFormTests, TestCase): class NamedCookieFormTests(NamedFormTests, TestCase):
formwizard_class = TestNamedUrlCookieWizardView formwizard_class = TestNamedUrlCookieWizardView
wizard_urlname = 'nwiz_cookie' wizard_urlname = 'nwiz_cookie'

View File

@ -1,9 +1,11 @@
from django.test import TestCase 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.tests.wizard.storage import TestStorage
from django.contrib.formtools.wizard.storage.session import SessionStorage from django.contrib.formtools.wizard.storage.session import SessionStorage
@skipIfCustomUser
class TestSessionStorage(TestStorage, TestCase): class TestSessionStorage(TestStorage, TestCase):
def get_storage(self): def get_storage(self):
return SessionStorage return SessionStorage

View File

@ -7,11 +7,19 @@ from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User 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.wizard.views import CookieWizardView
from django.contrib.formtools.tests.wizard.forms import UserForm, UserFormSet
from django.utils._os import upath 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): class WizardTests(object):
urls = 'django.contrib.formtools.tests.wizard.wizardtests.urls' urls = 'django.contrib.formtools.tests.wizard.wizardtests.urls'
@ -75,7 +83,7 @@ class WizardTests(object):
# ticket #19025: `form` should be included in context # ticket #19025: `form` should be included in context
form = response.context_data['wizard']['form'] 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): def test_form_finish(self):
response = self.client.get(self.wizard_url) response = self.client.get(self.wizard_url)
@ -196,6 +204,7 @@ class WizardTests(object):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@skipIfCustomUser
class SessionWizardTests(WizardTests, TestCase): class SessionWizardTests(WizardTests, TestCase):
wizard_url = '/wiz_session/' wizard_url = '/wiz_session/'
wizard_step_1_data = { wizard_step_1_data = {
@ -226,6 +235,8 @@ class SessionWizardTests(WizardTests, TestCase):
} }
) )
@skipIfCustomUser
class CookieWizardTests(WizardTests, TestCase): class CookieWizardTests(WizardTests, TestCase):
wizard_url = '/wiz_cookie/' wizard_url = '/wiz_cookie/'
wizard_step_1_data = { wizard_step_1_data = {
@ -256,6 +267,8 @@ class CookieWizardTests(WizardTests, TestCase):
} }
) )
@skipIfCustomUser
class WizardTestKwargs(TestCase): class WizardTestKwargs(TestCase):
wizard_url = '/wiz_other_template/' wizard_url = '/wiz_other_template/'
wizard_step_1_data = { wizard_step_1_data = {
@ -347,6 +360,7 @@ class WizardTestGenericViewInterface(TestCase):
self.assertEqual(response.context_data['another_key'], 'another_value') self.assertEqual(response.context_data['another_key'], 'another_value')
@skipIfCustomUser
class WizardFormKwargsOverrideTests(TestCase): class WizardFormKwargsOverrideTests(TestCase):
def setUp(self): def setUp(self):
super(WizardFormKwargsOverrideTests, self).setUp() super(WizardFormKwargsOverrideTests, self).setUp()

View File

@ -1,9 +1,24 @@
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core.cache import cache from django.core.cache import cache
from django.db import models
from django.test import TestCase 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): class SitemapTestsBase(TestCase):
protocol = 'http' protocol = 'http'
domain = 'example.com' if Site._meta.installed else 'testserver' 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.base_url = '%s://%s' % (self.protocol, self.domain)
self.old_Site_meta_installed = Site._meta.installed self.old_Site_meta_installed = Site._meta.installed
cache.clear() cache.clear()
# Create a user that will double as sitemap content # Create an object for sitemap content.
User.objects.create_user('testuser', 'test@example.com', 's3krit') TestModel.objects.create(name='Test Object')
def tearDown(self): def tearDown(self):
Site._meta.installed = self.old_Site_meta_installed Site._meta.installed = self.old_Site_meta_installed

View File

@ -1,9 +1,9 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.test.utils import override_settings from django.test.utils import override_settings
from .base import SitemapTestsBase from .base import TestModel, SitemapTestsBase
@override_settings(ABSOLUTE_URL_OVERRIDES={}) @override_settings(ABSOLUTE_URL_OVERRIDES={})
class GenericViewsSitemapTests(SitemapTestsBase): class GenericViewsSitemapTests(SitemapTestsBase):
@ -12,8 +12,8 @@ class GenericViewsSitemapTests(SitemapTestsBase):
"A minimal generic sitemap can be rendered" "A minimal generic sitemap can be rendered"
response = self.client.get('/generic/sitemap.xml') response = self.client.get('/generic/sitemap.xml')
expected = '' expected = ''
for username in User.objects.values_list("username", flat=True): for pk in TestModel.objects.values_list("id", flat=True):
expected += "<url><loc>%s/users/%s/</loc></url>" % (self.base_url, username) expected += "<url><loc>%s/testmodel/%s/</loc></url>" % (self.base_url, pk)
expected_content = """<?xml version="1.0" encoding="UTF-8"?> expected_content = """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
%s %s

View File

@ -4,7 +4,6 @@ import os
from datetime import date from datetime import date
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sitemaps import Sitemap, GenericSitemap from django.contrib.sitemaps import Sitemap, GenericSitemap
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core.exceptions import ImproperlyConfigured 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._os import upath
from django.utils.translation import activate, deactivate from django.utils.translation import activate, deactivate
from .base import SitemapTestsBase from .base import TestModel, SitemapTestsBase
class HTTPSitemapTests(SitemapTestsBase): class HTTPSitemapTests(SitemapTestsBase):
@ -128,10 +127,10 @@ class HTTPSitemapTests(SitemapTestsBase):
Check to make sure that the raw item is included with each Check to make sure that the raw item is included with each
Sitemap.get_url() url result. Sitemap.get_url() url result.
""" """
user_sitemap = GenericSitemap({'queryset': User.objects.all()}) test_sitemap = GenericSitemap({'queryset': TestModel.objects.all()})
def is_user(url): def is_testmodel(url):
return isinstance(url['item'], User) return isinstance(url['item'], TestModel)
item_in_url_info = all(map(is_user, user_sitemap.get_urls())) item_in_url_info = all(map(is_testmodel, test_sitemap.get_urls()))
self.assertTrue(item_in_url_info) self.assertTrue(item_in_url_info)
def test_cached_sitemap_index(self): def test_cached_sitemap_index(self):

View File

@ -4,6 +4,9 @@ from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap, vi
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.views.decorators.cache import cache_page from django.views.decorators.cache import cache_page
from django.contrib.sitemaps.tests.base import TestModel
class SimpleSitemap(Sitemap): class SimpleSitemap(Sitemap):
changefreq = "never" changefreq = "never"
priority = 0.5 priority = 0.5
@ -18,7 +21,7 @@ simple_sitemaps = {
} }
generic_sitemaps = { generic_sitemaps = {
'generic': GenericSitemap({'queryset': User.objects.all()}), 'generic': GenericSitemap({'queryset': TestModel.objects.all()}),
} }
flatpage_sitemaps = { flatpage_sitemaps = {