| 1 |
""" |
| 2 |
Tests for Django's bundled context processors. |
| 3 |
""" |
| 4 |
|
| 5 |
from django.test import SimpleTestCase, TestCase, override_settings |
| 6 |
|
| 7 |
|
| 8 |
@override_settings( |
| 9 |
ROOT_URLCONF="context_processors.urls", |
| 10 |
TEMPLATES=[ |
| 11 |
{ |
| 12 |
"BACKEND": "django.template.backends.django.DjangoTemplates", |
| 13 |
"APP_DIRS": True, |
| 14 |
"OPTIONS": { |
| 15 |
"context_processors": [ |
| 16 |
"django.template.context_processors.request", |
| 17 |
], |
| 18 |
}, |
| 19 |
} |
| 20 |
], |
| 21 |
) |
| 22 |
class RequestContextProcessorTests(SimpleTestCase): |
| 23 |
""" |
| 24 |
Tests for the ``django.template.context_processors.request`` processor. |
| 25 |
""" |
| 26 |
|
| 27 |
def test_request_attributes(self): |
| 28 |
""" |
| 29 |
The request object is available in the template and that its |
| 30 |
attributes can't be overridden by GET and POST parameters (#3828). |
| 31 |
""" |
| 32 |
url = "/request_attrs/" |
| 33 |
# We should have the request object in the template. |
| 34 |
response = self.client.get(url) |
| 35 |
self.assertContains(response, "Have request") |
| 36 |
# Test is_secure. |
| 37 |
response = self.client.get(url) |
| 38 |
self.assertContains(response, "Not secure") |
| 39 |
response = self.client.get(url, {"is_secure": "blah"}) |
| 40 |
self.assertContains(response, "Not secure") |
| 41 |
response = self.client.post(url, {"is_secure": "blah"}) |
| 42 |
self.assertContains(response, "Not secure") |
| 43 |
# Test path. |
| 44 |
response = self.client.get(url) |
| 45 |
self.assertContains(response, url) |
| 46 |
response = self.client.get(url, {"path": "/blah/"}) |
| 47 |
self.assertContains(response, url) |
| 48 |
response = self.client.post(url, {"path": "/blah/"}) |
| 49 |
self.assertContains(response, url) |
| 50 |
|
| 51 |
|
| 52 |
@override_settings( |
| 53 |
DEBUG=True, |
| 54 |
INTERNAL_IPS=["127.0.0.1"], |
| 55 |
ROOT_URLCONF="context_processors.urls", |
| 56 |
TEMPLATES=[ |
| 57 |
{ |
| 58 |
"BACKEND": "django.template.backends.django.DjangoTemplates", |
| 59 |
"APP_DIRS": True, |
| 60 |
"OPTIONS": { |
| 61 |
"context_processors": [ |
| 62 |
"django.template.context_processors.debug", |
| 63 |
], |
| 64 |
}, |
| 65 |
} |
| 66 |
], |
| 67 |
) |
| 68 |
class DebugContextProcessorTests(TestCase): |
| 69 |
""" |
| 70 |
Tests for the ``django.template.context_processors.debug`` processor. |
| 71 |
""" |
| 72 |
|
| 73 |
databases = {"default", "other"} |
| 74 |
|
| 75 |
def test_debug(self): |
| 76 |
url = "/debug/" |
| 77 |
# We should have the debug flag in the template. |
| 78 |
response = self.client.get(url) |
| 79 |
self.assertContains(response, "Have debug") |
| 80 |
|
| 81 |
# And now we should not |
| 82 |
with override_settings(DEBUG=False): |
| 83 |
response = self.client.get(url) |
| 84 |
self.assertNotContains(response, "Have debug") |
| 85 |
|
| 86 |
def test_sql_queries(self): |
| 87 |
""" |
| 88 |
Test whether sql_queries represents the actual amount |
| 89 |
of queries executed. (#23364) |
| 90 |
""" |
| 91 |
url = "/debug/" |
| 92 |
response = self.client.get(url) |
| 93 |
self.assertContains(response, "First query list: 0") |
| 94 |
self.assertContains(response, "Second query list: 1") |
| 95 |
# Check we have not actually memoized connection.queries |
| 96 |
self.assertContains(response, "Third query list: 2") |
| 97 |
# Check queries for DB connection 'other' |
| 98 |
self.assertContains(response, "Fourth query list: 3") |