tests for django s bundled context processors tests for the django template contextprocessors request processor the request object is available in the template and that its attributes can t be overridden by get and post parameters 3828 we should have the request object in the template test issecure test path tests for the django template contextprocessors debug processor we should have the debug flag in the template and now we should not test whether sqlqueries represents the actual amount of queries executed 23364 check we have not actually memoized connection queries check queries for db connection other tests for the django template context_processors request processor the request object is available in the template and that its attributes can t be overridden by get and post parameters 3828 we should have the request object in the template test is_secure test path tests for the django template context_processors debug processor we should have the debug flag in the template and now we should not test whether sql_queries represents the actual amount of queries executed 23364 check we have not actually memoized connection queries check queries for db connection other
from django.test import SimpleTestCase, TestCase, override_settings


@override_settings(
    ROOT_URLCONF="context_processors.urls",
    TEMPLATES=[
        {
            "BACKEND": "django.template.backends.django.DjangoTemplates",
            "APP_DIRS": True,
            "OPTIONS": {
                "context_processors": [
                    "django.template.context_processors.request",
                ],
            },
        }
    ],
)
class RequestContextProcessorTests(SimpleTestCase):
    

    def test_request_attributes(self):
        
        url = "/request_attrs/"
        
        response = self.client.get(url)
        self.assertContains(response, "Have request")
        
        response = self.client.get(url)
        self.assertContains(response, "Not secure")
        response = self.client.get(url, {"is_secure": "blah"})
        self.assertContains(response, "Not secure")
        response = self.client.post(url, {"is_secure": "blah"})
        self.assertContains(response, "Not secure")
        
        response = self.client.get(url)
        self.assertContains(response, url)
        response = self.client.get(url, {"path": "/blah/"})
        self.assertContains(response, url)
        response = self.client.post(url, {"path": "/blah/"})
        self.assertContains(response, url)


@override_settings(
    DEBUG=True,
    INTERNAL_IPS=["127.0.0.1"],
    ROOT_URLCONF="context_processors.urls",
    TEMPLATES=[
        {
            "BACKEND": "django.template.backends.django.DjangoTemplates",
            "APP_DIRS": True,
            "OPTIONS": {
                "context_processors": [
                    "django.template.context_processors.debug",
                ],
            },
        }
    ],
)
class DebugContextProcessorTests(TestCase):
    

    databases = {"default", "other"}

    def test_debug(self):
        url = "/debug/"
        
        response = self.client.get(url)
        self.assertContains(response, "Have debug")

        
        with override_settings(DEBUG=False):
            response = self.client.get(url)
            self.assertNotContains(response, "Have debug")

    def test_sql_queries(self):
        
        url = "/debug/"
        response = self.client.get(url)
        self.assertContains(response, "First query list: 0")
        self.assertContains(response, "Second query list: 1")
        
        self.assertContains(response, "Third query list: 2")
        
        self.assertContains(response, "Fourth query list: 3")