Skip to content

Commit

Permalink
Updates to the test suite to allow for newly deprecated and removed f…
Browse files Browse the repository at this point in the history
…eatures

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15990 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
russellm committed Apr 2, 2011
1 parent eb70cae commit 2680899
Show file tree
Hide file tree
Showing 28 changed files with 255 additions and 298 deletions.
77 changes: 77 additions & 0 deletions django/contrib/auth/tests/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# from django.conf import settings
from django.contrib.auth import authenticate
from django.db.models import Q
from django.test import TestCase
# from django.template import Template

class AuthContextProcessorTests(TestCase):
"""
Tests for the ``django.contrib.auth.context_processors.auth`` processor
"""
urls = 'regressiontests.context_processors.urls'
fixtures = ['context-processors-users.xml']

def test_session_not_accessed(self):
"""
Tests that the session is not accessed simply by including
the auth context processor
"""
response = self.client.get('/auth_processor_no_attr_access/')
self.assertContains(response, "Session not accessed")

def test_session_is_accessed(self):
"""
Tests that the session is accessed if the auth context processor
is used and relevant attributes accessed.
"""
response = self.client.get('/auth_processor_attr_access/')
self.assertContains(response, "Session accessed")

def test_perms_attrs(self):
self.client.login(username='super', password='secret')
response = self.client.get('/auth_processor_perms/')
self.assertContains(response, "Has auth permissions")

def test_message_attrs(self):
self.client.login(username='super', password='secret')
response = self.client.get('/auth_processor_messages/')
self.assertContains(response, "Message 1")

def test_user_attrs(self):
"""
Test that the lazy objects returned behave just like the wrapped objects.
"""
# These are 'functional' level tests for common use cases. Direct
# testing of the implementation (SimpleLazyObject) is in the 'utils'
# tests.
self.client.login(username='super', password='secret')
user = authenticate(username='super', password='secret')
response = self.client.get('/auth_processor_user/')
self.assertContains(response, "unicode: super")
self.assertContains(response, "id: 100")
self.assertContains(response, "username: super")
# bug #12037 is tested by the {% url %} in the template:
self.assertContains(response, "url: /userpage/super/")

# See if this object can be used for queries where a Q() comparing
# a user can be used with another Q() (in an AND or OR fashion).
# This simulates what a template tag might do with the user from the
# context. Note that we don't need to execute a query, just build it.
#
# The failure case (bug #12049) on Python 2.4 with a LazyObject-wrapped
# User is a fatal TypeError: "function() takes at least 2 arguments
# (0 given)" deep inside deepcopy().
#
# Python 2.5 and 2.6 succeeded, but logged internally caught exception
# spew:
#
# Exception RuntimeError: 'maximum recursion depth exceeded while
# calling a Python object' in <type 'exceptions.AttributeError'>
# ignored"
query = Q(user=response.context['user']) & Q(someflag=True)

# Tests for user equality. This is hard because User defines
# equality in a non-duck-typing way
# See bug #12060
self.assertEqual(response.context['user'], user)
self.assertEqual(user, response.context['user'])
38 changes: 37 additions & 1 deletion django/contrib/auth/tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.conf.urls.defaults import patterns
from django.conf.urls.defaults import patterns, url
from django.contrib.auth.urls import urlpatterns
from django.contrib.auth.views import password_reset
from django.contrib.auth.decorators import login_required
Expand All @@ -13,6 +13,35 @@ def remote_user_auth_view(request):
c = RequestContext(request, {})
return HttpResponse(t.render(c))

def auth_processor_no_attr_access(request):
r1 = render_to_response('context_processors/auth_attrs_no_access.html',
RequestContext(request, {}, processors=[context_processors.auth]))
# *After* rendering, we check whether the session was accessed
return render_to_response('context_processors/auth_attrs_test_access.html',
{'session_accessed':request.session.accessed})

def auth_processor_attr_access(request):
r1 = render_to_response('context_processors/auth_attrs_access.html',
RequestContext(request, {}, processors=[context_processors.auth]))
return render_to_response('context_processors/auth_attrs_test_access.html',
{'session_accessed':request.session.accessed})

def auth_processor_user(request):
return render_to_response('context_processors/auth_attrs_user.html',
RequestContext(request, {}, processors=[context_processors.auth]))

def auth_processor_perms(request):
return render_to_response('context_processors/auth_attrs_perms.html',
RequestContext(request, {}, processors=[context_processors.auth]))

def auth_processor_messages(request):
request.user.message_set.create(message="Message 1")
return render_to_response('context_processors/auth_attrs_messages.html',
RequestContext(request, {}, processors=[context_processors.auth]))

def userpage(request):
pass

# special urls for auth test cases
urlpatterns = urlpatterns + patterns('',
(r'^logout/custom_query/$', 'django.contrib.auth.views.logout', dict(redirect_field_name='follow')),
Expand All @@ -21,5 +50,12 @@ def remote_user_auth_view(request):
(r'^password_reset_from_email/$', 'django.contrib.auth.views.password_reset', dict(from_email='[email protected]')),
(r'^login_required/$', login_required(password_reset)),
(r'^login_required_login_url/$', login_required(password_reset, login_url='/somewhere/')),

(r'^auth_processor_no_attr_access/$', auth_processor_no_attr_access),
(r'^auth_processor_attr_access/$', auth_processor_attr_access),
(r'^auth_processor_user/$', auth_processor_user),
(r'^auth_processor_perms/$', auth_processor_perms),
(r'^auth_processor_messages/$', auth_processor_messages),
url(r'^userpage/(.+)/$', userpage, name="userpage"),
)

3 changes: 0 additions & 3 deletions tests/modeltests/field_subclassing/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
from django.utils import simplejson as json
from django.utils.encoding import force_unicode

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning, module='django.db.models.fields.subclassing')

class Small(object):
"""
A simple class to show that non-trivial Python objects can be used as
Expand Down
6 changes: 4 additions & 2 deletions tests/regressiontests/admin_scripts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ def run_test(self, script, args, settings_file=None, apps=None):
# Build the command line
executable = sys.executable
arg_string = ' '.join(['%s' % arg for arg in args])
# Silence the DeprecationWarning caused by having a locale directory
# in the project directory.
if ' ' in executable:
cmd = '""%s" "%s" %s"' % (executable, script, arg_string)
cmd = '""%s" -Wignore:::django.utils.translation "%s" %s"' % (executable, script, arg_string)
else:
cmd = '%s "%s" %s' % (executable, script, arg_string)
cmd = '%s -Wignore:::django.utils.translation "%s" %s' % (executable, script, arg_string)

# Move to the test directory and run
os.chdir(test_dir)
Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/backends/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.core.management.color import no_style
from django.db import backend, connection, connections, DEFAULT_DB_ALIAS, IntegrityError
from django.db.backends.signals import connection_created
from django.db.backends.postgresql import version as pg_version
from django.db.backends.postgresql_psycopg2 import version as pg_version
from django.test import TestCase, skipUnlessDBFeature, TransactionTestCase
from django.utils import unittest

Expand Down
14 changes: 0 additions & 14 deletions tests/regressiontests/comment_tests/tests/feed_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,3 @@ def test_feed(self):
self.assertContains(response, '<title>example.com comments</title>')
self.assertContains(response, '<link>http://example.com/</link>')
self.assertContains(response, '</rss>')


class LegacyCommentFeedTests(CommentFeedTests):
feed_url = '/rss/legacy/comments/'

def setUp(self):
self._warnings_state = get_warnings_state()
warnings.filterwarnings("ignore", category=DeprecationWarning,
module='django.contrib.syndication.views')
warnings.filterwarnings("ignore", category=DeprecationWarning,
module='django.contrib.syndication.feeds')

def tearDown(self):
restore_warnings_state(self._warnings_state)
1 change: 0 additions & 1 deletion tests/regressiontests/comment_tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
)

urlpatterns += patterns('',
(r'^rss/legacy/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds}),
(r'^rss/comments/$', LatestCommentFeed()),
)
88 changes: 1 addition & 87 deletions tests/regressiontests/context_processors/tests.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
"""
Tests for Django's bundled context processors.
"""
import warnings

from django.conf import settings
from django.contrib.auth import authenticate
from django.db.models import Q
from django.test import TestCase
from django.template import Template


class RequestContextProcessorTests(TestCase):
"""
Expand Down Expand Up @@ -40,84 +35,3 @@ def test_request_attributes(self):
response = self.client.post(url, {'path': '/blah/'})
self.assertContains(response, url)

class AuthContextProcessorTests(TestCase):
"""
Tests for the ``django.contrib.auth.context_processors.auth`` processor
"""
urls = 'regressiontests.context_processors.urls'
fixtures = ['context-processors-users.xml']

def setUp(self):
self.save_warnings_state()
warnings.filterwarnings('ignore', category=DeprecationWarning,
module='django.contrib.auth.models')
warnings.filterwarnings('ignore', category=DeprecationWarning,
module='django.core.context_processors')

def tearDown(self):
self.restore_warnings_state()

def test_session_not_accessed(self):
"""
Tests that the session is not accessed simply by including
the auth context processor
"""
response = self.client.get('/auth_processor_no_attr_access/')
self.assertContains(response, "Session not accessed")

def test_session_is_accessed(self):
"""
Tests that the session is accessed if the auth context processor
is used and relevant attributes accessed.
"""
response = self.client.get('/auth_processor_attr_access/')
self.assertContains(response, "Session accessed")

def test_perms_attrs(self):
self.client.login(username='super', password='secret')
response = self.client.get('/auth_processor_perms/')
self.assertContains(response, "Has auth permissions")

def test_message_attrs(self):
self.client.login(username='super', password='secret')
response = self.client.get('/auth_processor_messages/')
self.assertContains(response, "Message 1")

def test_user_attrs(self):
"""
Test that the lazy objects returned behave just like the wrapped objects.
"""
# These are 'functional' level tests for common use cases. Direct
# testing of the implementation (SimpleLazyObject) is in the 'utils'
# tests.
self.client.login(username='super', password='secret')
user = authenticate(username='super', password='secret')
response = self.client.get('/auth_processor_user/')
self.assertContains(response, "unicode: super")
self.assertContains(response, "id: 100")
self.assertContains(response, "username: super")
# bug #12037 is tested by the {% url %} in the template:
self.assertContains(response, "url: /userpage/super/")

# See if this object can be used for queries where a Q() comparing
# a user can be used with another Q() (in an AND or OR fashion).
# This simulates what a template tag might do with the user from the
# context. Note that we don't need to execute a query, just build it.
#
# The failure case (bug #12049) on Python 2.4 with a LazyObject-wrapped
# User is a fatal TypeError: "function() takes at least 2 arguments
# (0 given)" deep inside deepcopy().
#
# Python 2.5 and 2.6 succeeded, but logged internally caught exception
# spew:
#
# Exception RuntimeError: 'maximum recursion depth exceeded while
# calling a Python object' in <type 'exceptions.AttributeError'>
# ignored"
query = Q(user=response.context['user']) & Q(someflag=True)

# Tests for user equality. This is hard because User defines
# equality in a non-duck-typing way
# See bug #12060
self.assertEqual(response.context['user'], user)
self.assertEqual(user, response.context['user'])
6 changes: 0 additions & 6 deletions tests/regressiontests/context_processors/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,4 @@

urlpatterns = patterns('',
(r'^request_attrs/$', views.request_processor),
(r'^auth_processor_no_attr_access/$', views.auth_processor_no_attr_access),
(r'^auth_processor_attr_access/$', views.auth_processor_attr_access),
(r'^auth_processor_user/$', views.auth_processor_user),
(r'^auth_processor_perms/$', views.auth_processor_perms),
(r'^auth_processor_messages/$', views.auth_processor_messages),
url(r'^userpage/(.+)/$', views.userpage, name="userpage"),
)
29 changes: 0 additions & 29 deletions tests/regressiontests/context_processors/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,3 @@
def request_processor(request):
return render_to_response('context_processors/request_attrs.html',
RequestContext(request, {}, processors=[context_processors.request]))

def auth_processor_no_attr_access(request):
r1 = render_to_response('context_processors/auth_attrs_no_access.html',
RequestContext(request, {}, processors=[context_processors.auth]))
# *After* rendering, we check whether the session was accessed
return render_to_response('context_processors/auth_attrs_test_access.html',
{'session_accessed':request.session.accessed})

def auth_processor_attr_access(request):
r1 = render_to_response('context_processors/auth_attrs_access.html',
RequestContext(request, {}, processors=[context_processors.auth]))
return render_to_response('context_processors/auth_attrs_test_access.html',
{'session_accessed':request.session.accessed})

def auth_processor_user(request):
return render_to_response('context_processors/auth_attrs_user.html',
RequestContext(request, {}, processors=[context_processors.auth]))

def auth_processor_perms(request):
return render_to_response('context_processors/auth_attrs_perms.html',
RequestContext(request, {}, processors=[context_processors.auth]))

def auth_processor_messages(request):
request.user.message_set.create(message="Message 1")
return render_to_response('context_processors/auth_attrs_messages.html',
RequestContext(request, {}, processors=[context_processors.auth]))

def userpage(request):
pass
8 changes: 0 additions & 8 deletions tests/regressiontests/csrf_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ class CsrfViewMiddlewareTest(TestCase):
_csrf_id_cookie = "<1>\xc2\xa1"
_csrf_id = "1"

def setUp(self):
self.save_warnings_state()
warnings.filterwarnings('ignore', category=DeprecationWarning,
module='django.middleware.csrf')

def tearDown(self):
self.restore_warnings_state()

def _get_GET_no_csrf_cookie_request(self):
return TestingHttpRequest()

Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/forms/localflavor/cz.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setUp(self):
self.save_warnings_state()
warnings.filterwarnings(
"ignore",
category=PendingDeprecationWarning,
category=DeprecationWarning,
module='django.contrib.localflavor.cz.forms'
)

Expand Down
14 changes: 7 additions & 7 deletions tests/regressiontests/i18n/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ def tearDown(self):
settings.LOCALE_PATHS = self.old_locale_paths

def test_warn_if_project_has_locale_subdir(self):
"""Test that PendingDeprecationWarning is generated when a deprecated project level locale/ subdir is present."""
"""Test that DeprecationWarning is generated when a deprecated project level locale/ subdir is present."""
project_path = join(dirname(abspath(__file__)), '..')
warnings.filterwarnings('error',
"Translations in the project directory aren't supported anymore\. Use the LOCALE_PATHS setting instead\.",
PendingDeprecationWarning)
DeprecationWarning)
_trans.__dict__ = {}
self.assertRaises(PendingDeprecationWarning, django.utils.translation.ugettext, 'Time')
self.assertRaises(DeprecationWarning, django.utils.translation.ugettext, 'Time')

def test_no_warn_if_project_and_locale_paths_overlap(self):
"""Test that PendingDeprecationWarning isn't generated when a deprecated project level locale/ subdir is also included in LOCALE_PATHS."""
"""Test that DeprecationWarning isn't generated when a deprecated project level locale/ subdir is also included in LOCALE_PATHS."""
project_path = join(dirname(abspath(__file__)), '..')
settings.LOCALE_PATHS += (normpath(join(project_path, 'locale')),)
warnings.filterwarnings('error',
"Translations in the project directory aren't supported anymore\. Use the LOCALE_PATHS setting instead\.",
PendingDeprecationWarning)
DeprecationWarning)
_trans.__dict__ = {}
try:
django.utils.translation.ugettext('Time')
except PendingDeprecationWarning:
self.fail("PendingDeprecationWarning shouldn't be raised when settings/project locale and a LOCALE_PATHS member point to the same file system location.")
except DeprecationWarning:
self.fail("DeprecationWarning shouldn't be raised when settings/project locale and a LOCALE_PATHS member point to the same file system location.")
Loading

0 comments on commit 2680899

Please sign in to comment.