PageRenderTime 635ms CodeModel.GetById 81ms app.highlight 459ms RepoModel.GetById 63ms app.codeStats 1ms

/tests/regressiontests/middleware_exceptions/tests.py

https://code.google.com/p/mango-py/
Python | 782 lines | 607 code | 124 blank | 51 comment | 5 complexity | 72fc7d08be54e7a0c542ef92e2db015e MD5 | raw file
  1import sys
  2
  3from django.conf import settings
  4from django.core.signals import got_request_exception
  5from django.http import HttpResponse
  6from django.template.response import TemplateResponse
  7from django.template import Template
  8from django.test import TestCase
  9
 10class TestException(Exception):
 11    pass
 12
 13# A middleware base class that tracks which methods have been called
 14
 15class TestMiddleware(object):
 16    def __init__(self):
 17        self.process_request_called = False
 18        self.process_view_called = False
 19        self.process_response_called = False
 20        self.process_template_response_called = False
 21        self.process_exception_called = False
 22
 23    def process_request(self, request):
 24        self.process_request_called = True
 25
 26    def process_view(self, request, view_func, view_args, view_kwargs):
 27        self.process_view_called = True
 28
 29    def process_template_response(self, request, response):
 30        self.process_template_response_called = True
 31        return response
 32
 33    def process_response(self, request, response):
 34        self.process_response_called = True
 35        return response
 36
 37    def process_exception(self, request, exception):
 38        self.process_exception_called = True
 39
 40# Middleware examples that do the right thing
 41
 42class RequestMiddleware(TestMiddleware):
 43    def process_request(self, request):
 44        super(RequestMiddleware, self).process_request(request)
 45        return HttpResponse('Request Middleware')
 46
 47class ViewMiddleware(TestMiddleware):
 48    def process_view(self, request, view_func, view_args, view_kwargs):
 49        super(ViewMiddleware, self).process_view(request, view_func, view_args, view_kwargs)
 50        return HttpResponse('View Middleware')
 51
 52class ResponseMiddleware(TestMiddleware):
 53    def process_response(self, request, response):
 54        super(ResponseMiddleware, self).process_response(request, response)
 55        return HttpResponse('Response Middleware')
 56
 57class TemplateResponseMiddleware(TestMiddleware):
 58    def process_template_response(self, request, response):
 59        super(TemplateResponseMiddleware, self).process_template_response(request, response)
 60        return TemplateResponse(request, Template('Template Response Middleware'))
 61
 62class ExceptionMiddleware(TestMiddleware):
 63    def process_exception(self, request, exception):
 64        super(ExceptionMiddleware, self).process_exception(request, exception)
 65        return HttpResponse('Exception Middleware')
 66
 67
 68# Sample middlewares that raise exceptions
 69
 70class BadRequestMiddleware(TestMiddleware):
 71    def process_request(self, request):
 72        super(BadRequestMiddleware, self).process_request(request)
 73        raise TestException('Test Request Exception')
 74
 75class BadViewMiddleware(TestMiddleware):
 76    def process_view(self, request, view_func, view_args, view_kwargs):
 77        super(BadViewMiddleware, self).process_view(request, view_func, view_args, view_kwargs)
 78        raise TestException('Test View Exception')
 79
 80class BadTemplateResponseMiddleware(TestMiddleware):
 81    def process_template_response(self, request, response):
 82        super(BadTemplateResponseMiddleware, self).process_template_response(request, response)
 83        raise TestException('Test Template Response Exception')
 84
 85class BadResponseMiddleware(TestMiddleware):
 86    def process_response(self, request, response):
 87        super(BadResponseMiddleware, self).process_response(request, response)
 88        raise TestException('Test Response Exception')
 89
 90class BadExceptionMiddleware(TestMiddleware):
 91    def process_exception(self, request, exception):
 92        super(BadExceptionMiddleware, self).process_exception(request, exception)
 93        raise TestException('Test Exception Exception')
 94
 95
 96class BaseMiddlewareExceptionTest(TestCase):
 97    def setUp(self):
 98        self.exceptions = []
 99        got_request_exception.connect(self._on_request_exception)
100        self.client.handler.load_middleware()
101
102    def tearDown(self):
103        got_request_exception.disconnect(self._on_request_exception)
104        self.exceptions = []
105
106    def _on_request_exception(self, sender, request, **kwargs):
107        self.exceptions.append(sys.exc_info())
108
109    def _add_middleware(self, middleware):
110        self.client.handler._request_middleware.insert(0, middleware.process_request)
111        self.client.handler._view_middleware.insert(0, middleware.process_view)
112        self.client.handler._template_response_middleware.append(middleware.process_template_response)
113        self.client.handler._response_middleware.append(middleware.process_response)
114        self.client.handler._exception_middleware.append(middleware.process_exception)
115
116    def assert_exceptions_handled(self, url, errors, extra_error=None):
117        try:
118            response = self.client.get(url)
119        except TestException, e:
120            # Test client intentionally re-raises any exceptions being raised
121            # during request handling. Hence actual testing that exception was
122            # properly handled is done by relying on got_request_exception
123            # signal being sent.
124            pass
125        except Exception, e:
126            if type(extra_error) != type(e):
127                self.fail("Unexpected exception: %s" % e)
128        self.assertEqual(len(self.exceptions), len(errors))
129        for i, error in enumerate(errors):
130            exception, value, tb = self.exceptions[i]
131            self.assertEqual(value.args, (error, ))
132
133    def assert_middleware_usage(self, middleware, request, view, template_response, response, exception):
134        self.assertEqual(middleware.process_request_called, request)
135        self.assertEqual(middleware.process_view_called, view)
136        self.assertEqual(middleware.process_template_response_called, template_response)
137        self.assertEqual(middleware.process_response_called, response)
138        self.assertEqual(middleware.process_exception_called, exception)
139
140
141class MiddlewareTests(BaseMiddlewareExceptionTest):
142
143    def test_process_request_middleware(self):
144        pre_middleware = TestMiddleware()
145        middleware = RequestMiddleware()
146        post_middleware = TestMiddleware()
147        self._add_middleware(post_middleware)
148        self._add_middleware(middleware)
149        self._add_middleware(pre_middleware)
150        self.assert_exceptions_handled('/middleware_exceptions/view/', [])
151
152        # Check that the right middleware methods have been invoked
153        self.assert_middleware_usage(pre_middleware,  True,  False, False, True, False)
154        self.assert_middleware_usage(middleware,      True,  False, False, True, False)
155        self.assert_middleware_usage(post_middleware, False, False, False, True, False)
156
157    def test_process_view_middleware(self):
158        pre_middleware = TestMiddleware()
159        middleware = ViewMiddleware()
160        post_middleware = TestMiddleware()
161        self._add_middleware(post_middleware)
162        self._add_middleware(middleware)
163        self._add_middleware(pre_middleware)
164        self.assert_exceptions_handled('/middleware_exceptions/view/', [])
165
166        # Check that the right middleware methods have been invoked
167        self.assert_middleware_usage(pre_middleware,  True, True,  False, True, False)
168        self.assert_middleware_usage(middleware,      True, True,  False, True, False)
169        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
170
171    def test_process_response_middleware(self):
172        pre_middleware = TestMiddleware()
173        middleware = ResponseMiddleware()
174        post_middleware = TestMiddleware()
175        self._add_middleware(post_middleware)
176        self._add_middleware(middleware)
177        self._add_middleware(pre_middleware)
178        self.assert_exceptions_handled('/middleware_exceptions/view/', [])
179
180        # Check that the right middleware methods have been invoked
181        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
182        self.assert_middleware_usage(middleware,      True, True, False, True, False)
183        self.assert_middleware_usage(post_middleware, True, True, False, True, False)
184
185    def test_process_template_response_middleware(self):
186        pre_middleware = TestMiddleware()
187        middleware = TemplateResponseMiddleware()
188        post_middleware = TestMiddleware()
189        self._add_middleware(post_middleware)
190        self._add_middleware(middleware)
191        self._add_middleware(pre_middleware)
192        self.assert_exceptions_handled('/middleware_exceptions/template_response/', [])
193
194        # Check that the right middleware methods have been invoked
195        self.assert_middleware_usage(pre_middleware,  True, True, True, True, False)
196        self.assert_middleware_usage(middleware,      True, True, True, True, False)
197        self.assert_middleware_usage(post_middleware, True, True, True, True, False)
198
199    def test_process_exception_middleware(self):
200        pre_middleware = TestMiddleware()
201        middleware = ExceptionMiddleware()
202        post_middleware = TestMiddleware()
203        self._add_middleware(post_middleware)
204        self._add_middleware(middleware)
205        self._add_middleware(pre_middleware)
206        self.assert_exceptions_handled('/middleware_exceptions/view/', [])
207
208        # Check that the right middleware methods have been invoked
209        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
210        self.assert_middleware_usage(middleware,      True, True, False, True, False)
211        self.assert_middleware_usage(post_middleware, True, True, False, True, False)
212
213    def test_process_request_middleware_not_found(self):
214        pre_middleware = TestMiddleware()
215        middleware = RequestMiddleware()
216        post_middleware = TestMiddleware()
217        self._add_middleware(post_middleware)
218        self._add_middleware(middleware)
219        self._add_middleware(pre_middleware)
220        self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
221
222        # Check that the right middleware methods have been invoked
223        self.assert_middleware_usage(pre_middleware,  True,  False, False, True, False)
224        self.assert_middleware_usage(middleware,      True,  False, False, True, False)
225        self.assert_middleware_usage(post_middleware, False, False, False, True, False)
226
227    def test_process_view_middleware_not_found(self):
228        pre_middleware = TestMiddleware()
229        middleware = ViewMiddleware()
230        post_middleware = TestMiddleware()
231        self._add_middleware(post_middleware)
232        self._add_middleware(middleware)
233        self._add_middleware(pre_middleware)
234        self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
235
236        # Check that the right middleware methods have been invoked
237        self.assert_middleware_usage(pre_middleware,  True, True,  False, True, False)
238        self.assert_middleware_usage(middleware,      True, True,  False, True, False)
239        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
240
241    def test_process_template_response_middleware_not_found(self):
242        pre_middleware = TestMiddleware()
243        middleware = TemplateResponseMiddleware()
244        post_middleware = TestMiddleware()
245        self._add_middleware(post_middleware)
246        self._add_middleware(middleware)
247        self._add_middleware(pre_middleware)
248        self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
249
250        # Check that the right middleware methods have been invoked
251        self.assert_middleware_usage(pre_middleware,  True, True, False, True, True)
252        self.assert_middleware_usage(middleware,      True, True, False, True, True)
253        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
254
255    def test_process_response_middleware_not_found(self):
256        pre_middleware = TestMiddleware()
257        middleware = ResponseMiddleware()
258        post_middleware = TestMiddleware()
259        self._add_middleware(post_middleware)
260        self._add_middleware(middleware)
261        self._add_middleware(pre_middleware)
262        self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
263
264        # Check that the right middleware methods have been invoked
265        self.assert_middleware_usage(pre_middleware,  True, True, False, True, True)
266        self.assert_middleware_usage(middleware,      True, True, False, True, True)
267        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
268
269    def test_process_exception_middleware_not_found(self):
270        pre_middleware = TestMiddleware()
271        middleware = ExceptionMiddleware()
272        post_middleware = TestMiddleware()
273        self._add_middleware(post_middleware)
274        self._add_middleware(middleware)
275        self._add_middleware(pre_middleware)
276        self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
277
278        # Check that the right middleware methods have been invoked
279        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
280        self.assert_middleware_usage(middleware,      True, True, False, True, True)
281        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
282
283    def test_process_request_middleware_exception(self):
284        pre_middleware = TestMiddleware()
285        middleware = RequestMiddleware()
286        post_middleware = TestMiddleware()
287        self._add_middleware(post_middleware)
288        self._add_middleware(middleware)
289        self._add_middleware(pre_middleware)
290        self.assert_exceptions_handled('/middleware_exceptions/error/', [])
291
292        # Check that the right middleware methods have been invoked
293        self.assert_middleware_usage(pre_middleware,  True,  False, False, True, False)
294        self.assert_middleware_usage(middleware,      True,  False, False, True, False)
295        self.assert_middleware_usage(post_middleware, False, False, False, True, False)
296
297    def test_process_view_middleware_exception(self):
298        pre_middleware = TestMiddleware()
299        middleware = ViewMiddleware()
300        post_middleware = TestMiddleware()
301        self._add_middleware(post_middleware)
302        self._add_middleware(middleware)
303        self._add_middleware(pre_middleware)
304        self.assert_exceptions_handled('/middleware_exceptions/error/', [])
305
306        # Check that the right middleware methods have been invoked
307        self.assert_middleware_usage(pre_middleware,  True, True,  False, True, False)
308        self.assert_middleware_usage(middleware,      True, True,  False, True, False)
309        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
310
311    def test_process_response_middleware_exception(self):
312        pre_middleware = TestMiddleware()
313        middleware = ResponseMiddleware()
314        post_middleware = TestMiddleware()
315        self._add_middleware(post_middleware)
316        self._add_middleware(middleware)
317        self._add_middleware(pre_middleware)
318        self.assert_exceptions_handled('/middleware_exceptions/error/', ['Error in view'], Exception())
319
320        # Check that the right middleware methods have been invoked
321        self.assert_middleware_usage(pre_middleware,  True, True, False, True, True)
322        self.assert_middleware_usage(middleware,      True, True, False, True, True)
323        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
324
325    def test_process_exception_middleware_exception(self):
326        pre_middleware = TestMiddleware()
327        middleware = ExceptionMiddleware()
328        post_middleware = TestMiddleware()
329        self._add_middleware(post_middleware)
330        self._add_middleware(middleware)
331        self._add_middleware(pre_middleware)
332        self.assert_exceptions_handled('/middleware_exceptions/error/', [])
333
334        # Check that the right middleware methods have been invoked
335        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
336        self.assert_middleware_usage(middleware,      True, True, False, True, True)
337        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
338
339    def test_process_request_middleware_null_view(self):
340        pre_middleware = TestMiddleware()
341        middleware = RequestMiddleware()
342        post_middleware = TestMiddleware()
343        self._add_middleware(post_middleware)
344        self._add_middleware(middleware)
345        self._add_middleware(pre_middleware)
346        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [])
347
348        # Check that the right middleware methods have been invoked
349        self.assert_middleware_usage(pre_middleware,  True,  False, False, True, False)
350        self.assert_middleware_usage(middleware,      True,  False, False, True, False)
351        self.assert_middleware_usage(post_middleware, False, False, False, True, False)
352
353    def test_process_view_middleware_null_view(self):
354        pre_middleware = TestMiddleware()
355        middleware = ViewMiddleware()
356        post_middleware = TestMiddleware()
357        self._add_middleware(post_middleware)
358        self._add_middleware(middleware)
359        self._add_middleware(pre_middleware)
360        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [])
361
362        # Check that the right middleware methods have been invoked
363        self.assert_middleware_usage(pre_middleware,  True, True,  False, True, False)
364        self.assert_middleware_usage(middleware,      True, True,  False, True, False)
365        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
366
367    def test_process_response_middleware_null_view(self):
368        pre_middleware = TestMiddleware()
369        middleware = ResponseMiddleware()
370        post_middleware = TestMiddleware()
371        self._add_middleware(post_middleware)
372        self._add_middleware(middleware)
373        self._add_middleware(pre_middleware)
374        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
375                "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object.",
376            ],
377            ValueError())
378
379        # Check that the right middleware methods have been invoked
380        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
381        self.assert_middleware_usage(middleware,      True, True, False, True, False)
382        self.assert_middleware_usage(post_middleware, True, True, False, True, False)
383
384    def test_process_exception_middleware_null_view(self):
385        pre_middleware = TestMiddleware()
386        middleware = ExceptionMiddleware()
387        post_middleware = TestMiddleware()
388        self._add_middleware(post_middleware)
389        self._add_middleware(middleware)
390        self._add_middleware(pre_middleware)
391        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
392                "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object."
393            ],
394            ValueError())
395
396        # Check that the right middleware methods have been invoked
397        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
398        self.assert_middleware_usage(middleware,      True, True, False, True, False)
399        self.assert_middleware_usage(post_middleware, True, True, False, True, False)
400
401    def test_process_request_middleware_permission_denied(self):
402        pre_middleware = TestMiddleware()
403        middleware = RequestMiddleware()
404        post_middleware = TestMiddleware()
405        self._add_middleware(post_middleware)
406        self._add_middleware(middleware)
407        self._add_middleware(pre_middleware)
408        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
409
410        # Check that the right middleware methods have been invoked
411        self.assert_middleware_usage(pre_middleware,  True,  False, False, True, False)
412        self.assert_middleware_usage(middleware,      True,  False, False, True, False)
413        self.assert_middleware_usage(post_middleware, False, False, False, True, False)
414
415    def test_process_view_middleware_permission_denied(self):
416        pre_middleware = TestMiddleware()
417        middleware = ViewMiddleware()
418        post_middleware = TestMiddleware()
419        self._add_middleware(post_middleware)
420        self._add_middleware(middleware)
421        self._add_middleware(pre_middleware)
422        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
423
424        # Check that the right middleware methods have been invoked
425        self.assert_middleware_usage(pre_middleware,  True, True,  False, True, False)
426        self.assert_middleware_usage(middleware,      True, True,  False, True, False)
427        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
428
429    def test_process_response_middleware_permission_denied(self):
430        pre_middleware = TestMiddleware()
431        middleware = ResponseMiddleware()
432        post_middleware = TestMiddleware()
433        self._add_middleware(post_middleware)
434        self._add_middleware(middleware)
435        self._add_middleware(pre_middleware)
436        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
437
438        # Check that the right middleware methods have been invoked
439        self.assert_middleware_usage(pre_middleware,  True, True, False, True, True)
440        self.assert_middleware_usage(middleware,      True, True, False, True, True)
441        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
442
443    def test_process_exception_middleware_permission_denied(self):
444        pre_middleware = TestMiddleware()
445        middleware = ExceptionMiddleware()
446        post_middleware = TestMiddleware()
447        self._add_middleware(post_middleware)
448        self._add_middleware(middleware)
449        self._add_middleware(pre_middleware)
450        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
451
452        # Check that the right middleware methods have been invoked
453        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
454        self.assert_middleware_usage(middleware,      True, True, False, True, True)
455        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
456
457    def test_process_template_response_error(self):
458        middleware = TestMiddleware()
459        self._add_middleware(middleware)
460        self.assert_exceptions_handled('/middleware_exceptions/template_response_error/', [])
461
462        # Check that the right middleware methods have been invoked
463        self.assert_middleware_usage(middleware, True, True, True, True, False)
464
465
466class BadMiddlewareTests(BaseMiddlewareExceptionTest):
467
468    def test_process_request_bad_middleware(self):
469        pre_middleware = TestMiddleware()
470        bad_middleware = BadRequestMiddleware()
471        post_middleware = TestMiddleware()
472        self._add_middleware(post_middleware)
473        self._add_middleware(bad_middleware)
474        self._add_middleware(pre_middleware)
475        self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test Request Exception'])
476
477        # Check that the right middleware methods have been invoked
478        self.assert_middleware_usage(pre_middleware,  True,  False, False, True, False)
479        self.assert_middleware_usage(bad_middleware,  True,  False, False, True, False)
480        self.assert_middleware_usage(post_middleware, False, False, False, True, False)
481
482    def test_process_view_bad_middleware(self):
483        pre_middleware = TestMiddleware()
484        bad_middleware = BadViewMiddleware()
485        post_middleware = TestMiddleware()
486        self._add_middleware(post_middleware)
487        self._add_middleware(bad_middleware)
488        self._add_middleware(pre_middleware)
489        self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test View Exception'])
490
491        # Check that the right middleware methods have been invoked
492        self.assert_middleware_usage(pre_middleware,  True, True,  False, True, False)
493        self.assert_middleware_usage(bad_middleware,  True, True,  False, True, False)
494        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
495
496    def test_process_template_response_bad_middleware(self):
497        pre_middleware = TestMiddleware()
498        bad_middleware = BadTemplateResponseMiddleware()
499        post_middleware = TestMiddleware()
500        self._add_middleware(post_middleware)
501        self._add_middleware(bad_middleware)
502        self._add_middleware(pre_middleware)
503        self.assert_exceptions_handled('/middleware_exceptions/template_response/', ['Test Template Response Exception'])
504
505        # Check that the right middleware methods have been invoked
506        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
507        self.assert_middleware_usage(bad_middleware,  True, True, True,  True, False)
508        self.assert_middleware_usage(post_middleware, True, True, True,  True, False)
509
510    def test_process_response_bad_middleware(self):
511        pre_middleware = TestMiddleware()
512        bad_middleware = BadResponseMiddleware()
513        post_middleware = TestMiddleware()
514        self._add_middleware(post_middleware)
515        self._add_middleware(bad_middleware)
516        self._add_middleware(pre_middleware)
517        self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test Response Exception'])
518
519        # Check that the right middleware methods have been invoked
520        self.assert_middleware_usage(pre_middleware,  True, True, False, False, False)
521        self.assert_middleware_usage(bad_middleware,  True, True, False, True,  False)
522        self.assert_middleware_usage(post_middleware, True, True, False, True,  False)
523
524    def test_process_exception_bad_middleware(self):
525        pre_middleware = TestMiddleware()
526        bad_middleware = BadExceptionMiddleware()
527        post_middleware = TestMiddleware()
528        self._add_middleware(post_middleware)
529        self._add_middleware(bad_middleware)
530        self._add_middleware(pre_middleware)
531        self.assert_exceptions_handled('/middleware_exceptions/view/', [])
532
533        # Check that the right middleware methods have been invoked
534        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
535        self.assert_middleware_usage(bad_middleware,  True, True, False, True, False)
536        self.assert_middleware_usage(post_middleware, True, True, False, True, False)
537
538    def test_process_request_bad_middleware_not_found(self):
539        pre_middleware = TestMiddleware()
540        bad_middleware = BadRequestMiddleware()
541        post_middleware = TestMiddleware()
542        self._add_middleware(post_middleware)
543        self._add_middleware(bad_middleware)
544        self._add_middleware(pre_middleware)
545        self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Request Exception'])
546
547        # Check that the right middleware methods have been invoked
548        self.assert_middleware_usage(pre_middleware,  True,  False, False, True, False)
549        self.assert_middleware_usage(bad_middleware,  True,  False, False, True, False)
550        self.assert_middleware_usage(post_middleware, False, False, False, True, False)
551
552    def test_process_view_bad_middleware_not_found(self):
553        pre_middleware = TestMiddleware()
554        bad_middleware = BadViewMiddleware()
555        post_middleware = TestMiddleware()
556        self._add_middleware(post_middleware)
557        self._add_middleware(bad_middleware)
558        self._add_middleware(pre_middleware)
559        self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test View Exception'])
560
561        # Check that the right middleware methods have been invoked
562        self.assert_middleware_usage(pre_middleware,  True, True,  False, True, False)
563        self.assert_middleware_usage(bad_middleware,  True, True,  False, True, False)
564        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
565
566    def test_process_response_bad_middleware_not_found(self):
567        pre_middleware = TestMiddleware()
568        bad_middleware = BadResponseMiddleware()
569        post_middleware = TestMiddleware()
570        self._add_middleware(post_middleware)
571        self._add_middleware(bad_middleware)
572        self._add_middleware(pre_middleware)
573        self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Response Exception'])
574
575        # Check that the right middleware methods have been invoked
576        self.assert_middleware_usage(pre_middleware,  True, True, False, False, True)
577        self.assert_middleware_usage(bad_middleware,  True, True, False, True,  True)
578        self.assert_middleware_usage(post_middleware, True, True, False, True,  True)
579
580    def test_process_exception_bad_middleware_not_found(self):
581        pre_middleware = TestMiddleware()
582        bad_middleware = BadExceptionMiddleware()
583        post_middleware = TestMiddleware()
584        self._add_middleware(post_middleware)
585        self._add_middleware(bad_middleware)
586        self._add_middleware(pre_middleware)
587        self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Exception Exception'])
588
589        # Check that the right middleware methods have been invoked
590        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
591        self.assert_middleware_usage(bad_middleware,  True, True, False, True, True)
592        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
593
594    def test_process_request_bad_middleware_exception(self):
595        pre_middleware = TestMiddleware()
596        bad_middleware = BadRequestMiddleware()
597        post_middleware = TestMiddleware()
598        self._add_middleware(post_middleware)
599        self._add_middleware(bad_middleware)
600        self._add_middleware(pre_middleware)
601        self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test Request Exception'])
602
603        # Check that the right middleware methods have been invoked
604        self.assert_middleware_usage(pre_middleware,  True,  False, False, True, False)
605        self.assert_middleware_usage(bad_middleware,  True,  False, False, True, False)
606        self.assert_middleware_usage(post_middleware, False, False, False, True, False)
607
608    def test_process_view_bad_middleware_exception(self):
609        pre_middleware = TestMiddleware()
610        bad_middleware = BadViewMiddleware()
611        post_middleware = TestMiddleware()
612        self._add_middleware(post_middleware)
613        self._add_middleware(bad_middleware)
614        self._add_middleware(pre_middleware)
615        self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test View Exception'])
616
617        # Check that the right middleware methods have been invoked
618        self.assert_middleware_usage(pre_middleware,  True, True,  False, True, False)
619        self.assert_middleware_usage(bad_middleware,  True, True,  False, True, False)
620        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
621
622    def test_process_response_bad_middleware_exception(self):
623        pre_middleware = TestMiddleware()
624        bad_middleware = BadResponseMiddleware()
625        post_middleware = TestMiddleware()
626        self._add_middleware(post_middleware)
627        self._add_middleware(bad_middleware)
628        self._add_middleware(pre_middleware)
629        self.assert_exceptions_handled('/middleware_exceptions/error/', ['Error in view', 'Test Response Exception'])
630
631        # Check that the right middleware methods have been invoked
632        self.assert_middleware_usage(pre_middleware,  True, True, False, False, True)
633        self.assert_middleware_usage(bad_middleware,  True, True, False, True,  True)
634        self.assert_middleware_usage(post_middleware, True, True, False, True,  True)
635
636    def test_process_exception_bad_middleware_exception(self):
637        pre_middleware = TestMiddleware()
638        bad_middleware = BadExceptionMiddleware()
639        post_middleware = TestMiddleware()
640        self._add_middleware(post_middleware)
641        self._add_middleware(bad_middleware)
642        self._add_middleware(pre_middleware)
643        self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test Exception Exception'])
644
645        # Check that the right middleware methods have been invoked
646        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
647        self.assert_middleware_usage(bad_middleware,  True, True, False, True, True)
648        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
649
650    def test_process_request_bad_middleware_null_view(self):
651        pre_middleware = TestMiddleware()
652        bad_middleware = BadRequestMiddleware()
653        post_middleware = TestMiddleware()
654        self._add_middleware(post_middleware)
655        self._add_middleware(bad_middleware)
656        self._add_middleware(pre_middleware)
657        self.assert_exceptions_handled('/middleware_exceptions/null_view/', ['Test Request Exception'])
658
659        # Check that the right middleware methods have been invoked
660        self.assert_middleware_usage(pre_middleware,  True,  False, False, True, False)
661        self.assert_middleware_usage(bad_middleware,  True,  False, False, True, False)
662        self.assert_middleware_usage(post_middleware, False, False, False, True, False)
663
664    def test_process_view_bad_middleware_null_view(self):
665        pre_middleware = TestMiddleware()
666        bad_middleware = BadViewMiddleware()
667        post_middleware = TestMiddleware()
668        self._add_middleware(post_middleware)
669        self._add_middleware(bad_middleware)
670        self._add_middleware(pre_middleware)
671        self.assert_exceptions_handled('/middleware_exceptions/null_view/', ['Test View Exception'])
672
673        # Check that the right middleware methods have been invoked
674        self.assert_middleware_usage(pre_middleware,  True, True,  False, True, False)
675        self.assert_middleware_usage(bad_middleware,  True, True,  False, True, False)
676        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
677
678    def test_process_response_bad_middleware_null_view(self):
679        pre_middleware = TestMiddleware()
680        bad_middleware = BadResponseMiddleware()
681        post_middleware = TestMiddleware()
682        self._add_middleware(post_middleware)
683        self._add_middleware(bad_middleware)
684        self._add_middleware(pre_middleware)
685        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
686                "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object.",
687                'Test Response Exception'
688            ])
689
690        # Check that the right middleware methods have been invoked
691        self.assert_middleware_usage(pre_middleware,  True, True, False, False, False)
692        self.assert_middleware_usage(bad_middleware,  True, True, False, True,  False)
693        self.assert_middleware_usage(post_middleware, True, True, False, True,  False)
694
695    def test_process_exception_bad_middleware_null_view(self):
696        pre_middleware = TestMiddleware()
697        bad_middleware = BadExceptionMiddleware()
698        post_middleware = TestMiddleware()
699        self._add_middleware(post_middleware)
700        self._add_middleware(bad_middleware)
701        self._add_middleware(pre_middleware)
702        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
703                "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object."
704            ],
705            ValueError())
706
707        # Check that the right middleware methods have been invoked
708        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
709        self.assert_middleware_usage(bad_middleware,  True, True, False, True, False)
710        self.assert_middleware_usage(post_middleware, True, True, False, True, False)
711
712    def test_process_request_bad_middleware_permission_denied(self):
713        pre_middleware = TestMiddleware()
714        bad_middleware = BadRequestMiddleware()
715        post_middleware = TestMiddleware()
716        self._add_middleware(post_middleware)
717        self._add_middleware(bad_middleware)
718        self._add_middleware(pre_middleware)
719        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Request Exception'])
720
721        # Check that the right middleware methods have been invoked
722        self.assert_middleware_usage(pre_middleware,  True,  False, False, True, False)
723        self.assert_middleware_usage(bad_middleware,  True,  False, False, True, False)
724        self.assert_middleware_usage(post_middleware, False, False, False, True, False)
725
726    def test_process_view_bad_middleware_permission_denied(self):
727        pre_middleware = TestMiddleware()
728        bad_middleware = BadViewMiddleware()
729        post_middleware = TestMiddleware()
730        self._add_middleware(post_middleware)
731        self._add_middleware(bad_middleware)
732        self._add_middleware(pre_middleware)
733        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test View Exception'])
734
735        # Check that the right middleware methods have been invoked
736        self.assert_middleware_usage(pre_middleware,  True, True,  False, True, False)
737        self.assert_middleware_usage(bad_middleware,  True, True,  False, True, False)
738        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
739
740    def test_process_response_bad_middleware_permission_denied(self):
741        pre_middleware = TestMiddleware()
742        bad_middleware = BadResponseMiddleware()
743        post_middleware = TestMiddleware()
744        self._add_middleware(post_middleware)
745        self._add_middleware(bad_middleware)
746        self._add_middleware(pre_middleware)
747        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Response Exception'])
748
749        # Check that the right middleware methods have been invoked
750        self.assert_middleware_usage(pre_middleware,  True, True, False, False, True)
751        self.assert_middleware_usage(bad_middleware,  True, True, False, True,  True)
752        self.assert_middleware_usage(post_middleware, True, True, False, True,  True)
753
754    def test_process_exception_bad_middleware_permission_denied(self):
755        pre_middleware = TestMiddleware()
756        bad_middleware = BadExceptionMiddleware()
757        post_middleware = TestMiddleware()
758        self._add_middleware(post_middleware)
759        self._add_middleware(bad_middleware)
760        self._add_middleware(pre_middleware)
761        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Exception Exception'])
762
763        # Check that the right middleware methods have been invoked
764        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
765        self.assert_middleware_usage(bad_middleware,  True, True, False, True, True)
766        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
767
768
769_missing = object()
770class RootUrlconfTests(TestCase):
771    def test_missing_root_urlconf(self):
772        try:
773            original_ROOT_URLCONF = settings.ROOT_URLCONF
774            del settings.ROOT_URLCONF
775        except AttributeError:
776            original_ROOT_URLCONF = _missing
777        self.assertRaises(AttributeError,
778            self.client.get, "/middleware_exceptions/view/"
779        )
780
781        if original_ROOT_URLCONF is not _missing:
782            settings.ROOT_URLCONF = original_ROOT_URLCONF