mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Refs #2333 - Re-added the template rendering signal for testing purposes; however, the signal is not available during normal operation. It is only added as part of an instrumentation step that occurs during test framework setup. Previous attempt (r3659) was reverted (r3666) due to performance concerns.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3707 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
from cStringIO import StringIO
|
||||
from django.contrib.admin.views.decorators import LOGIN_FORM_KEY, _encode_post_data
|
||||
from django.core.handlers.base import BaseHandler
|
||||
from django.core.handlers.wsgi import WSGIRequest
|
||||
from django.dispatch import dispatcher
|
||||
from django.http import urlencode, SimpleCookie
|
||||
from django.template import signals
|
||||
from django.test import signals
|
||||
from django.utils.functional import curry
|
||||
|
||||
class ClientHandler(BaseHandler):
|
||||
@@ -96,7 +95,7 @@ class Client:
|
||||
HTML rendered to the end-user.
|
||||
"""
|
||||
def __init__(self, **defaults):
|
||||
self.handler = TestHandler()
|
||||
self.handler = ClientHandler()
|
||||
self.defaults = defaults
|
||||
self.cookie = SimpleCookie()
|
||||
|
||||
@@ -126,7 +125,7 @@ class Client:
|
||||
data = {}
|
||||
on_template_render = curry(store_rendered_templates, data)
|
||||
dispatcher.connect(on_template_render, signal=signals.template_rendered)
|
||||
|
||||
|
||||
response = self.handler(environ)
|
||||
|
||||
# Add any rendered template detail to the response
|
||||
@@ -180,29 +179,38 @@ class Client:
|
||||
def login(self, path, username, password, **extra):
|
||||
"""
|
||||
A specialized sequence of GET and POST to log into a view that
|
||||
is protected by @login_required or a similar access decorator.
|
||||
is protected by a @login_required access decorator.
|
||||
|
||||
path should be the URL of the login page, or of any page that
|
||||
is login protected.
|
||||
path should be the URL of the page that is login protected.
|
||||
|
||||
Returns True if login was successful; False if otherwise.
|
||||
Returns the response from GETting the requested URL after
|
||||
login is complete. Returns False if login process failed.
|
||||
"""
|
||||
# First, GET the login page.
|
||||
# This is required to establish the session.
|
||||
# First, GET the page that is login protected.
|
||||
# This page will redirect to the login page.
|
||||
response = self.get(path)
|
||||
if response.status_code != 302:
|
||||
return False
|
||||
|
||||
login_path, data = response['Location'].split('?')
|
||||
next = data.split('=')[1]
|
||||
|
||||
# Second, GET the login page; required to set up cookies
|
||||
response = self.get(login_path, **extra)
|
||||
if response.status_code != 200:
|
||||
return False
|
||||
|
||||
# Set up the block of form data required by the login page.
|
||||
|
||||
# Last, POST the login data.
|
||||
form_data = {
|
||||
'username': username,
|
||||
'password': password,
|
||||
'this_is_the_login_form': 1,
|
||||
'post_data': _encode_post_data({LOGIN_FORM_KEY: 1})
|
||||
'next' : next,
|
||||
}
|
||||
response = self.post(path, data=form_data, **extra)
|
||||
|
||||
# login page should give response 200 (if you requested the login
|
||||
# page specifically), or 302 (if you requested a login
|
||||
# protected page, to which the login can redirect).
|
||||
return response.status_code in (200,302)
|
||||
response = self.post(login_path, data=form_data, **extra)
|
||||
|
||||
# Login page should 302 redirect to the originally requested page
|
||||
if response.status_code != 302 or response['Location'] != path:
|
||||
return False
|
||||
|
||||
# Since we are logged in, request the actual page again
|
||||
return self.get(path)
|
||||
|
||||
1
django/test/signals.py
Normal file
1
django/test/signals.py
Normal file
@@ -0,0 +1 @@
|
||||
template_rendered = object()
|
||||
@@ -1,6 +1,7 @@
|
||||
import unittest, doctest
|
||||
from django.conf import settings
|
||||
from django.core import management
|
||||
from django.test.utils import setup_test_environment, teardown_test_environment
|
||||
from django.test.utils import create_test_db, destroy_test_db
|
||||
from django.test.testcases import OutputChecker, DocTestRunner
|
||||
|
||||
@@ -51,6 +52,7 @@ def run_tests(module_list, verbosity=1, extra_tests=[]):
|
||||
the module. A list of 'extra' tests may also be provided; these tests
|
||||
will be added to the test suite.
|
||||
"""
|
||||
setup_test_environment()
|
||||
|
||||
settings.DEBUG = False
|
||||
suite = unittest.TestSuite()
|
||||
@@ -66,3 +68,5 @@ def run_tests(module_list, verbosity=1, extra_tests=[]):
|
||||
management.syncdb(verbosity, interactive=False)
|
||||
unittest.TextTestRunner(verbosity=verbosity).run(suite)
|
||||
destroy_test_db(old_name, verbosity)
|
||||
|
||||
teardown_test_environment()
|
||||
|
||||
@@ -1,11 +1,40 @@
|
||||
import sys, time
|
||||
from django.conf import settings
|
||||
from django.db import connection, transaction, backend
|
||||
from django.dispatch import dispatcher
|
||||
from django.test import signals
|
||||
from django.template import Template
|
||||
|
||||
# The prefix to put on the default database name when creating
|
||||
# the test database.
|
||||
TEST_DATABASE_PREFIX = 'test_'
|
||||
|
||||
def instrumented_test_render(self, context):
|
||||
"""An instrumented Template render method, providing a signal
|
||||
that can be intercepted by the test system Client
|
||||
|
||||
"""
|
||||
dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context)
|
||||
return self.nodelist.render(context)
|
||||
|
||||
def setup_test_environment():
|
||||
"""Perform any global pre-test setup. This involves:
|
||||
|
||||
- Installing the instrumented test renderer
|
||||
|
||||
"""
|
||||
Template.original_render = Template.render
|
||||
Template.render = instrumented_test_render
|
||||
|
||||
def teardown_test_environment():
|
||||
"""Perform any global post-test teardown. This involves:
|
||||
|
||||
- Restoring the original test renderer
|
||||
|
||||
"""
|
||||
Template.render = Template.original_render
|
||||
del Template.original_render
|
||||
|
||||
def _set_autocommit(connection):
|
||||
"Make sure a connection is in autocommit mode."
|
||||
if hasattr(connection.connection, "autocommit"):
|
||||
@@ -75,4 +104,3 @@ def destroy_test_db(old_database_name, verbosity=1):
|
||||
time.sleep(1) # To avoid "database is being accessed by other users" errors.
|
||||
cursor.execute("DROP DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME))
|
||||
connection.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user