Code

Ticket #16507: failure.py

File failure.py, 2.5 KB (added by andymckay, 2 years ago)

Hopefully this demonstrates the problem

Line 
1from django.conf import settings
2from django.core.urlresolvers import reverse
3from django.test import TestCase
4from django.conf import urls as url_handlers
5from django.template import Context, Template
6from django.contrib.messages.tests import urls
7from django import http
8from django.test.utils import override_settings
9from django.contrib.messages import constants
10from django.contrib.messages.api import MessageFailure
11
12
13class Test500(TestCase):
14
15    def render_error(self, *args, **kw):
16        error_template = Template("{% url about-site %}")
17        # This template will work, because it doesn't do a reverse match on
18        # on something on the destroyed ROOT_URLCONF.
19        #error_template = Template("works")
20        return http.HttpResponse(error_template.render(Context({})))
21
22    # These are the override_settings defined in the contrib/messages/tests/base.py,
23    # copied here so you don't get lead astray
24    @override_settings(
25        INSTALLED_APPS=filter(
26            lambda app:app!='django.contrib.messages', settings.INSTALLED_APPS),
27        MIDDLEWARE_CLASSES=filter(
28            lambda m:'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES),
29        TEMPLATE_CONTEXT_PROCESSORS=filter(
30            lambda p:'context_processors.messages' not in p,
31                 settings.TEMPLATE_CONTEXT_PROCESSORS),
32        MESSAGE_LEVEL=constants.DEBUG
33    )
34    def test_error_handling_page(self):
35        # This is the key here. This is done in django/test/testcases.py _urlconf_setup.
36        # Where the url conf is overridden in the test.
37        settings.ROOT_URLCONF = urls
38        # When the client has a problem and raises the 500 message down below, it will
39        # propogate up the handler 500 specified here.
40        url_handlers.handler500 = self.render_error
41        show_url = reverse('django.contrib.messages.tests.urls.show')
42        add_url = reverse('django.contrib.messages.tests.urls.add', args=('warning',))
43        data = {
44            'messages': ['Test message %d' % x for x in xrange(10)],
45        }
46        # This is copied from contrib/messages/tests/base.py and will raise a MessageFailure error
47        #
48        # So really the problem is that we are testing the propogation into the 500 handler
49        # as well. We shouldn't be doing all that, what we should be doing is the following.
50        # Then render_error never gets called.
51        #settings.DEBUG = True
52        #settings.DEBUG_PROPAGATE_EXCEPTION = True
53        self.assertRaises(MessageFailure, self.client.post, add_url, data, follow=True)