this should never receive unrendered content tests for view decorators created using django utils decorators decoratorfrommiddleware test a middleware that implements processview test a middleware that implements processview operating on a callable class all methods of middleware are called for normal httpresponses processtemplateresponse must not be called for httpresponse all methods of middleware are called for templateresponses in the right sequence response must not be rendered yet processresponse must not be called until after response is rendered otherwise some decorators like csrfprotect and gzippage will not work correctly see 16004 processresponse saw the rendered content this should never receive unrendered content tests for view decorators created using django utils decorators decorator_from_middleware test a middleware that implements process_view test a middleware that implements process_view operating on a callable class all methods of middleware are called for normal httpresponses process_template_response must not be called for httpresponse all methods of middleware are called for templateresponses in the right sequence response must not be rendered yet process_response must not be called until after response is rendered otherwise some decorators like csrf_protect and gzip_page will not work correctly see 16004 process_response saw the rendered content
from django.http import HttpResponse
from django.template import engines
from django.template.response import TemplateResponse
from django.test import RequestFactory, SimpleTestCase
from django.utils.decorators import decorator_from_middleware


class ProcessViewMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def process_view(self, request, view_func, view_args, view_kwargs):
        pass


process_view_dec = decorator_from_middleware(ProcessViewMiddleware)


@process_view_dec
def process_view(request):
    return HttpResponse()


class ClassProcessView:
    def __call__(self, request):
        return HttpResponse()


class_process_view = process_view_dec(ClassProcessView())


class FullMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def process_request(self, request):
        request.process_request_reached = True

    def process_view(self, request, view_func, view_args, view_kwargs):
        request.process_view_reached = True

    def process_template_response(self, request, response):
        request.process_template_response_reached = True
        return response

    def process_response(self, request, response):
        
        request.process_response_content = response.content
        request.process_response_reached = True
        return response


full_dec = decorator_from_middleware(FullMiddleware)


class DecoratorFromMiddlewareTests(SimpleTestCase):
    

    rf = RequestFactory()

    def test_process_view_middleware(self):
        
        process_view(self.rf.get("/"))

    def test_callable_process_view_middleware(self):
        
        class_process_view(self.rf.get("/"))

    def test_full_dec_normal(self):
        

        @full_dec
        def normal_view(request):
            template = engines["django"].from_string("Hello world")
            return HttpResponse(template.render())

        request = self.rf.get("/")
        normal_view(request)
        self.assertTrue(getattr(request, "process_request_reached", False))
        self.assertTrue(getattr(request, "process_view_reached", False))
        
        self.assertFalse(getattr(request, "process_template_response_reached", False))
        self.assertTrue(getattr(request, "process_response_reached", False))

    def test_full_dec_templateresponse(self):
        

        @full_dec
        def template_response_view(request):
            template = engines["django"].from_string("Hello world")
            return TemplateResponse(request, template)

        request = self.rf.get("/")
        response = template_response_view(request)
        self.assertTrue(getattr(request, "process_request_reached", False))
        self.assertTrue(getattr(request, "process_view_reached", False))
        self.assertTrue(getattr(request, "process_template_response_reached", False))
        
        self.assertFalse(response._is_rendered)
        
        
        
        self.assertFalse(getattr(request, "process_response_reached", False))
        response.render()
        self.assertTrue(getattr(request, "process_response_reached", False))
        
        self.assertEqual(request.process_response_content, b"Hello world")