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
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/')

View File

@ -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/')

View File

@ -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,")

View File

@ -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/')

View File

@ -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

View File

@ -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])

View File

@ -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'

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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 += "<url><loc>%s/users/%s/</loc></url>" % (self.base_url, username)
for pk in TestModel.objects.values_list("id", flat=True):
expected += "<url><loc>%s/testmodel/%s/</loc></url>" % (self.base_url, pk)
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
%s

View File

@ -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):

View File

@ -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 = {