Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #20048, #20060 -- Modified tests for contrib apps sensi…

…tive to custom User models.

Thanks to matiasb for the report of #20060 and the draft patch for #20048.

(cherry picked from commit 930af66)
  • Loading branch information...
commit d35e621633640797d2db15ec2ff05ff97a3b80a6 1 parent f7ca464
Russell Keith-Magee authored March 23, 2013
2  django/contrib/flatpages/tests/csrf.py
... ...
@@ -1,6 +1,7 @@
1 1
 import os
2 2
 from django.conf import settings
3 3
 from django.contrib.auth.models import User
  4
+from django.contrib.auth.tests.utils import skipIfCustomUser
4 5
 from django.test import TestCase, Client
5 6
 from django.test.utils import override_settings
6 7
 
@@ -38,6 +39,7 @@ def test_view_non_existent_flatpage(self):
38 39
         response = self.client.get('/flatpage_root/no_such_flatpage/')
39 40
         self.assertEqual(response.status_code, 404)
40 41
 
  42
+    @skipIfCustomUser
41 43
     def test_view_authenticated_flatpage(self):
42 44
         "A flatpage served through a view can require authentication"
43 45
         response = self.client.get('/flatpage_root/sekrit/')
3  django/contrib/flatpages/tests/middleware.py
... ...
@@ -1,6 +1,7 @@
1 1
 import os
2 2
 from django.conf import settings
3 3
 from django.contrib.auth.models import User
  4
+from django.contrib.auth.tests.utils import skipIfCustomUser
4 5
 from django.contrib.flatpages.models import FlatPage
5 6
 from django.test import TestCase
6 7
 from django.test.utils import override_settings
@@ -36,6 +37,7 @@ def test_view_non_existent_flatpage(self):
36 37
         response = self.client.get('/flatpage_root/no_such_flatpage/')
37 38
         self.assertEqual(response.status_code, 404)
38 39
 
  40
+    @skipIfCustomUser
39 41
     def test_view_authenticated_flatpage(self):
40 42
         "A flatpage served through a view can require authentication"
41 43
         response = self.client.get('/flatpage_root/sekrit/')
@@ -57,6 +59,7 @@ def test_fallback_non_existent_flatpage(self):
57 59
         response = self.client.get('/no_such_flatpage/')
58 60
         self.assertEqual(response.status_code, 404)
59 61
 
  62
+    @skipIfCustomUser
60 63
     def test_fallback_authenticated_flatpage(self):
61 64
         "A flatpage served by the middleware can require authentication"
62 65
         response = self.client.get('/sekrit/')
12  django/contrib/flatpages/tests/templatetags.py
... ...
@@ -1,6 +1,7 @@
1 1
 import os
2 2
 from django.conf import settings
3 3
 from django.contrib.auth.models import AnonymousUser, User
  4
+from django.contrib.auth.tests.utils import skipIfCustomUser
4 5
 from django.template import Template, Context, TemplateSyntaxError
5 6
 from django.test import TestCase
6 7
 from django.test.utils import override_settings
@@ -24,9 +25,6 @@ class FlatpageTemplateTagTests(TestCase):
24 25
     fixtures = ['sample_flatpages']
25 26
     urls = 'django.contrib.flatpages.tests.urls'
26 27
 
27  
-    def setUp(self):
28  
-        self.me = User.objects.create_user('testuser', 'test@example.com', 's3krit')
29  
-
30 28
     def test_get_flatpages_tag(self):
31 29
         "The flatpage template tag retrives unregistered prefixed flatpages by default"
32 30
         out = Template(
@@ -51,8 +49,10 @@ def test_get_flatpages_tag_for_anon_user(self):
51 49
             }))
52 50
         self.assertEqual(out, "A Flatpage,A Nested Flatpage,")
53 51
 
  52
+    @skipIfCustomUser
54 53
     def test_get_flatpages_tag_for_user(self):
55 54
         "The flatpage template tag retrives all flatpages for an authenticated user"
  55
+        me = User.objects.create_user('testuser', 'test@example.com', 's3krit')
56 56
         out = Template(
57 57
                 "{% load flatpages %}"
58 58
                 "{% get_flatpages for me as flatpages %}"
@@ -60,7 +60,7 @@ def test_get_flatpages_tag_for_user(self):
60 60
                 "{{ page.title }},"
61 61
                 "{% endfor %}"
62 62
             ).render(Context({
63  
-                'me': self.me
  63
+                'me': me
64 64
             }))
65 65
         self.assertEqual(out, "A Flatpage,A Nested Flatpage,Sekrit Nested Flatpage,Sekrit Flatpage,")
66 66
 
@@ -88,8 +88,10 @@ def test_get_flatpages_with_prefix_for_anon_user(self):
88 88
             }))
89 89
         self.assertEqual(out, "A Nested Flatpage,")
90 90
 
  91
+    @skipIfCustomUser
91 92
     def test_get_flatpages_with_prefix_for_user(self):
92 93
         "The flatpage template tag retrive prefixed flatpages for an authenticated user"
  94
+        me = User.objects.create_user('testuser', 'test@example.com', 's3krit')
93 95
         out = Template(
94 96
                 "{% load flatpages %}"
95 97
                 "{% get_flatpages '/location/' for me as location_flatpages %}"
@@ -97,7 +99,7 @@ def test_get_flatpages_with_prefix_for_user(self):
97 99
                 "{{ page.title }},"
98 100
                 "{% endfor %}"
99 101
             ).render(Context({
100  
-                'me': self.me
  102
+                'me': me
101 103
             }))
102 104
         self.assertEqual(out, "A Nested Flatpage,Sekrit Nested Flatpage,")
103 105
 
2  django/contrib/flatpages/tests/views.py
... ...
@@ -1,6 +1,7 @@
1 1
 import os
2 2
 from django.conf import settings
3 3
 from django.contrib.auth.models import User
  4
+from django.contrib.auth.tests.utils import skipIfCustomUser
4 5
 from django.contrib.flatpages.models import FlatPage
5 6
 from django.test import TestCase
6 7
 from django.test.utils import override_settings
@@ -36,6 +37,7 @@ def test_view_non_existent_flatpage(self):
36 37
         response = self.client.get('/flatpage_root/no_such_flatpage/')
37 38
         self.assertEqual(response.status_code, 404)
38 39
 
  40
+    @skipIfCustomUser
39 41
     def test_view_authenticated_flatpage(self):
40 42
         "A flatpage served through a view can require authentication"
41 43
         response = self.client.get('/flatpage_root/sekrit/')
2  django/contrib/formtools/tests/wizard/cookiestorage.py
@@ -5,10 +5,12 @@
5 5
 from django.core.exceptions import SuspiciousOperation
6 6
 from django.http import HttpResponse
7 7
 
  8
+from django.contrib.auth.tests.utils import skipIfCustomUser
8 9
 from django.contrib.formtools.wizard.storage.cookie import CookieStorage
9 10
 from django.contrib.formtools.tests.wizard.storage import get_request, TestStorage
10 11
 
11 12
 
  13
+@skipIfCustomUser
12 14
 class TestCookieStorage(TestStorage, TestCase):
13 15
     def get_storage(self):
14 16
         return CookieStorage
37  django/contrib/formtools/tests/wizard/forms.py
@@ -2,6 +2,7 @@
2 2
 
3 3
 from django import forms, http
4 4
 from django.conf import settings
  5
+from django.db import models
5 6
 from django.test import TestCase
6 7
 from django.template.response import TemplateResponse
7 8
 from django.utils.importlib import import_module
@@ -29,26 +30,40 @@ def get_request(*args, **kwargs):
29 30
     request.session = engine.SessionStore(None)
30 31
     return request
31 32
 
  33
+
32 34
 class Step1(forms.Form):
33 35
     name = forms.CharField()
34 36
 
  37
+
35 38
 class Step2(forms.Form):
36 39
     name = forms.CharField()
37 40
 
  41
+
38 42
 class Step3(forms.Form):
39 43
     data = forms.CharField()
40 44
 
  45
+
41 46
 class CustomKwargsStep1(Step1):
42 47
 
43 48
     def __init__(self, test=None, *args, **kwargs):
44 49
         self.test = test
45 50
         return super(CustomKwargsStep1, self).__init__(*args, **kwargs)
46 51
 
47  
-class UserForm(forms.ModelForm):
  52
+
  53
+class TestModel(models.Model):
  54
+    name = models.CharField(max_length=100)
  55
+
48 56
     class Meta:
49  
-        model = User
  57
+        app_label = 'formtools'
  58
+
  59
+
  60
+class TestModelForm(forms.ModelForm):
  61
+    class Meta:
  62
+        model = TestModel
  63
+
  64
+
  65
+TestModelFormSet = forms.models.modelformset_factory(TestModel, form=TestModelForm, extra=2)
50 66
 
51  
-UserFormSet = forms.models.modelformset_factory(User, form=UserForm, extra=2)
52 67
 
53 68
 class TestWizard(WizardView):
54 69
     storage_name = 'django.contrib.formtools.wizard.storage.session.SessionStorage'
@@ -149,8 +164,8 @@ def test_form_initial(self):
149 164
 
150 165
     def test_form_instance(self):
151 166
         request = get_request()
152  
-        the_instance = User()
153  
-        testform = TestWizard.as_view([('start', UserForm), ('step2', Step2)],
  167
+        the_instance = TestModel()
  168
+        testform = TestWizard.as_view([('start', TestModelForm), ('step2', Step2)],
154 169
             instance_dict={'start': the_instance})
155 170
         response, instance = testform(request)
156 171
 
@@ -163,12 +178,12 @@ def test_form_instance(self):
163 178
 
164 179
     def test_formset_instance(self):
165 180
         request = get_request()
166  
-        the_instance1, created = User.objects.get_or_create(
167  
-            username='testuser1')
168  
-        the_instance2, created = User.objects.get_or_create(
169  
-            username='testuser2')
170  
-        testform = TestWizard.as_view([('start', UserFormSet), ('step2', Step2)],
171  
-            instance_dict={'start': User.objects.filter(username='testuser1')})
  181
+        the_instance1, created = TestModel.objects.get_or_create(
  182
+            name='test object 1')
  183
+        the_instance2, created = TestModel.objects.get_or_create(
  184
+            name='test object 2')
  185
+        testform = TestWizard.as_view([('start', TestModelFormSet), ('step2', Step2)],
  186
+            instance_dict={'start': TestModel.objects.filter(name='test object 1')})
172 187
         response, instance = testform(request)
173 188
 
174 189
         self.assertEqual(list(instance.get_form_instance('start')), [the_instance1])
5  django/contrib/formtools/tests/wizard/namedwizardtests/tests.py
@@ -5,6 +5,7 @@
5 5
 from django.test import TestCase
6 6
 
7 7
 from django.contrib.auth.models import User
  8
+from django.contrib.auth.tests.utils import skipIfCustomUser
8 9
 
9 10
 from django.contrib.formtools.wizard.views import (NamedUrlSessionWizardView,
10 11
                                                    NamedUrlCookieWizardView)
@@ -276,6 +277,7 @@ def test_form_reset(self):
276 277
         self.assertEqual(response.context['wizard']['steps'].current, 'form1')
277 278
 
278 279
 
  280
+@skipIfCustomUser
279 281
 class NamedSessionWizardTests(NamedWizardTests, TestCase):
280 282
     wizard_urlname = 'nwiz_session'
281 283
     wizard_step_1_data = {
@@ -307,6 +309,7 @@ class NamedSessionWizardTests(NamedWizardTests, TestCase):
307 309
     )
308 310
 
309 311
 
  312
+@skipIfCustomUser
310 313
 class NamedCookieWizardTests(NamedWizardTests, TestCase):
311 314
     wizard_urlname = 'nwiz_cookie'
312 315
     wizard_step_1_data = {
@@ -367,11 +370,13 @@ def dispatch(self, request, *args, **kwargs):
367 370
         return response, self
368 371
 
369 372
 
  373
+@skipIfCustomUser
370 374
 class NamedSessionFormTests(NamedFormTests, TestCase):
371 375
     formwizard_class = TestNamedUrlSessionWizardView
372 376
     wizard_urlname = 'nwiz_session'
373 377
 
374 378
 
  379
+@skipIfCustomUser
375 380
 class NamedCookieFormTests(NamedFormTests, TestCase):
376 381
     formwizard_class = TestNamedUrlCookieWizardView
377 382
     wizard_urlname = 'nwiz_cookie'
2  django/contrib/formtools/tests/wizard/sessionstorage.py
... ...
@@ -1,9 +1,11 @@
1 1
 from django.test import TestCase
2 2
 
  3
+from django.contrib.auth.tests.utils import skipIfCustomUser
3 4
 from django.contrib.formtools.tests.wizard.storage import TestStorage
4 5
 from django.contrib.formtools.wizard.storage.session import SessionStorage
5 6
 
6 7
 
  8
+@skipIfCustomUser
7 9
 class TestSessionStorage(TestStorage, TestCase):
8 10
     def get_storage(self):
9 11
         return SessionStorage
18  django/contrib/formtools/tests/wizard/wizardtests/tests.py
@@ -7,11 +7,19 @@
7 7
 from django.test.client import RequestFactory
8 8
 from django.conf import settings
9 9
 from django.contrib.auth.models import User
  10
+from django.contrib.auth.tests.utils import skipIfCustomUser
10 11
 from django.contrib.formtools.wizard.views import CookieWizardView
11  
-from django.contrib.formtools.tests.wizard.forms import UserForm, UserFormSet
12 12
 from django.utils._os import upath
13 13
 
14 14
 
  15
+class UserForm(forms.ModelForm):
  16
+    class Meta:
  17
+        model = User
  18
+
  19
+
  20
+UserFormSet = forms.models.modelformset_factory(User, form=UserForm, extra=2)
  21
+
  22
+
15 23
 class WizardTests(object):
16 24
     urls = 'django.contrib.formtools.tests.wizard.wizardtests.urls'
17 25
 
@@ -75,7 +83,7 @@ def test_template_context(self):
75 83
 
76 84
         # ticket #19025: `form` should be included in context
77 85
         form = response.context_data['wizard']['form']
78  
-        self.assertEqual(response.context_data['form'], form)            
  86
+        self.assertEqual(response.context_data['form'], form)
79 87
 
80 88
     def test_form_finish(self):
81 89
         response = self.client.get(self.wizard_url)
@@ -196,6 +204,7 @@ def test_form_refresh(self):
196 204
         self.assertEqual(response.status_code, 200)
197 205
 
198 206
 
  207
+@skipIfCustomUser
199 208
 class SessionWizardTests(WizardTests, TestCase):
200 209
     wizard_url = '/wiz_session/'
201 210
     wizard_step_1_data = {
@@ -226,6 +235,8 @@ class SessionWizardTests(WizardTests, TestCase):
226 235
         }
227 236
     )
228 237
 
  238
+
  239
+@skipIfCustomUser
229 240
 class CookieWizardTests(WizardTests, TestCase):
230 241
     wizard_url = '/wiz_cookie/'
231 242
     wizard_step_1_data = {
@@ -256,6 +267,8 @@ class CookieWizardTests(WizardTests, TestCase):
256 267
         }
257 268
     )
258 269
 
  270
+
  271
+@skipIfCustomUser
259 272
 class WizardTestKwargs(TestCase):
260 273
     wizard_url = '/wiz_other_template/'
261 274
     wizard_step_1_data = {
@@ -347,6 +360,7 @@ def get_context_data(self, **kwargs):
347 360
         self.assertEqual(response.context_data['another_key'], 'another_value')
348 361
 
349 362
 
  363
+@skipIfCustomUser
350 364
 class WizardFormKwargsOverrideTests(TestCase):
351 365
     def setUp(self):
352 366
         super(WizardFormKwargsOverrideTests, self).setUp()
19  django/contrib/sitemaps/tests/base.py
... ...
@@ -1,9 +1,24 @@
1 1
 from django.contrib.auth.models import User
2 2
 from django.contrib.sites.models import Site
3 3
 from django.core.cache import cache
  4
+from django.db import models
4 5
 from django.test import TestCase
5 6
 
6 7
 
  8
+class TestModel(models.Model):
  9
+    "A test model for "
  10
+    name = models.CharField(max_length=100)
  11
+
  12
+    class Meta:
  13
+        app_label = 'sitemaps'
  14
+
  15
+    def __unicode__(self):
  16
+        return self.name
  17
+
  18
+    def get_absolute_url(self):
  19
+        return '/testmodel/%s/' % self.id
  20
+
  21
+
7 22
 class SitemapTestsBase(TestCase):
8 23
     protocol = 'http'
9 24
     domain = 'example.com' if Site._meta.installed else 'testserver'
@@ -13,8 +28,8 @@ def setUp(self):
13 28
         self.base_url = '%s://%s' % (self.protocol, self.domain)
14 29
         self.old_Site_meta_installed = Site._meta.installed
15 30
         cache.clear()
16  
-        # Create a user that will double as sitemap content
17  
-        User.objects.create_user('testuser', 'test@example.com', 's3krit')
  31
+        # Create an object for sitemap content.
  32
+        TestModel.objects.create(name='Test Object')
18 33
 
19 34
     def tearDown(self):
20 35
         Site._meta.installed = self.old_Site_meta_installed
8  django/contrib/sitemaps/tests/generic.py
... ...
@@ -1,9 +1,9 @@
1 1
 from __future__ import unicode_literals
2 2
 
3  
-from django.contrib.auth.models import User
4 3
 from django.test.utils import override_settings
5 4
 
6  
-from .base import SitemapTestsBase
  5
+from .base import TestModel, SitemapTestsBase
  6
+
7 7
 
8 8
 @override_settings(ABSOLUTE_URL_OVERRIDES={})
9 9
 class GenericViewsSitemapTests(SitemapTestsBase):
@@ -12,8 +12,8 @@ def test_generic_sitemap(self):
12 12
         "A minimal generic sitemap can be rendered"
13 13
         response = self.client.get('/generic/sitemap.xml')
14 14
         expected = ''
15  
-        for username in User.objects.values_list("username", flat=True):
16  
-            expected += "<url><loc>%s/users/%s/</loc></url>" % (self.base_url, username)
  15
+        for pk in TestModel.objects.values_list("id", flat=True):
  16
+            expected += "<url><loc>%s/testmodel/%s/</loc></url>" % (self.base_url, pk)
17 17
         expected_content = """<?xml version="1.0" encoding="UTF-8"?>
18 18
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
19 19
 %s
11  django/contrib/sitemaps/tests/http.py
@@ -4,7 +4,6 @@
4 4
 from datetime import date
5 5
 
6 6
 from django.conf import settings
7  
-from django.contrib.auth.models import User
8 7
 from django.contrib.sitemaps import Sitemap, GenericSitemap
9 8
 from django.contrib.sites.models import Site
10 9
 from django.core.exceptions import ImproperlyConfigured
@@ -14,7 +13,7 @@
14 13
 from django.utils._os import upath
15 14
 from django.utils.translation import activate, deactivate
16 15
 
17  
-from .base import SitemapTestsBase
  16
+from .base import TestModel, SitemapTestsBase
18 17
 
19 18
 
20 19
 class HTTPSitemapTests(SitemapTestsBase):
@@ -128,10 +127,10 @@ def test_sitemap_item(self):
128 127
         Check to make sure that the raw item is included with each
129 128
         Sitemap.get_url() url result.
130 129
         """
131  
-        user_sitemap = GenericSitemap({'queryset': User.objects.all()})
132  
-        def is_user(url):
133  
-            return isinstance(url['item'], User)
134  
-        item_in_url_info = all(map(is_user, user_sitemap.get_urls()))
  130
+        test_sitemap = GenericSitemap({'queryset': TestModel.objects.all()})
  131
+        def is_testmodel(url):
  132
+            return isinstance(url['item'], TestModel)
  133
+        item_in_url_info = all(map(is_testmodel, test_sitemap.get_urls()))
135 134
         self.assertTrue(item_in_url_info)
136 135
 
137 136
     def test_cached_sitemap_index(self):
5  django/contrib/sitemaps/tests/urls/http.py
@@ -4,6 +4,9 @@
4 4
 from django.contrib.auth.models import User
5 5
 from django.views.decorators.cache import cache_page
6 6
 
  7
+from django.contrib.sitemaps.tests.base import TestModel
  8
+
  9
+
7 10
 class SimpleSitemap(Sitemap):
8 11
     changefreq = "never"
9 12
     priority = 0.5
@@ -18,7 +21,7 @@ def items(self):
18 21
 }
19 22
 
20 23
 generic_sitemaps = {
21  
-    'generic': GenericSitemap({'queryset': User.objects.all()}),
  24
+    'generic': GenericSitemap({'queryset': TestModel.objects.all()}),
22 25
 }
23 26
 
24 27
 flatpage_sitemaps = {

0 notes on commit d35e621

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