2012-05-31 15:21:13 +00:00
|
|
|
"""
|
|
|
|
Regression tests for the Test Client, especially the customized assertions.
|
|
|
|
"""
|
2013-10-28 19:34:09 +00:00
|
|
|
import itertools
|
2015-01-28 12:35:27 +00:00
|
|
|
import os
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.contrib.auth.signals import user_logged_in, user_logged_out
|
|
|
|
from django.http import HttpResponse
|
2015-12-22 18:50:16 +00:00
|
|
|
from django.template import (
|
|
|
|
Context, RequestContext, TemplateSyntaxError, engines,
|
|
|
|
)
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.template.response import SimpleTemplateResponse
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import (
|
2016-12-16 23:13:34 +00:00
|
|
|
Client, SimpleTestCase, TestCase, modify_settings, override_settings,
|
2015-04-17 21:38:20 +00:00
|
|
|
)
|
2014-10-17 13:46:42 +00:00
|
|
|
from django.test.client import RedirectCycleError, RequestFactory, encode_file
|
2017-01-20 14:24:05 +00:00
|
|
|
from django.test.utils import ContextList
|
2015-12-30 15:51:16 +00:00
|
|
|
from django.urls import NoReverseMatch, reverse
|
2017-01-26 19:58:33 +00:00
|
|
|
from django.utils.translation import gettext_lazy
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2013-06-04 16:37:44 +00:00
|
|
|
from .models import CustomUser
|
2013-05-15 23:14:28 +00:00
|
|
|
from .views import CustomTestException
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class TestDataMixin:
|
2015-02-23 00:53:57 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2016-02-05 20:56:52 +00:00
|
|
|
cls.u1 = User.objects.create_user(username='testclient', password='password')
|
|
|
|
cls.staff = User.objects.create_user(username='staff', password='password', is_staff=True)
|
2015-02-23 00:53:57 +00:00
|
|
|
|
|
|
|
|
2014-12-17 21:51:42 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class AssertContainsTests(SimpleTestCase):
|
2014-01-14 15:43:27 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_contains(self):
|
|
|
|
"Responses can be inspected for content, including counting repeated substrings"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/no_template_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
self.assertNotContains(response, 'never')
|
|
|
|
self.assertContains(response, 'never', 0)
|
|
|
|
self.assertContains(response, 'once')
|
|
|
|
self.assertContains(response, 'once', 1)
|
|
|
|
self.assertContains(response, 'twice')
|
|
|
|
self.assertContains(response, 'twice', 2)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'text', status_code=999)
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Couldn't retrieve content: Response code was 200 (expected 999)", str(e))
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'text', status_code=999, msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Couldn't retrieve content: Response code was 200 (expected 999)", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertNotContains(response, 'text', status_code=999)
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Couldn't retrieve content: Response code was 200 (expected 999)", str(e))
|
|
|
|
try:
|
|
|
|
self.assertNotContains(response, 'text', status_code=999, msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Couldn't retrieve content: Response code was 200 (expected 999)", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertNotContains(response, 'once')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Response should not contain 'once'", str(e))
|
|
|
|
try:
|
|
|
|
self.assertNotContains(response, 'once', msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Response should not contain 'once'", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'never', 1)
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Found 0 instances of 'never' in response (expected 1)", str(e))
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'never', 1, msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Found 0 instances of 'never' in response (expected 1)", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'once', 0)
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Found 1 instances of 'once' in response (expected 0)", str(e))
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'once', 0, msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Found 1 instances of 'once' in response (expected 0)", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'once', 2)
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Found 1 instances of 'once' in response (expected 2)", str(e))
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'once', 2, msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Found 1 instances of 'once' in response (expected 2)", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'twice', 1)
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Found 2 instances of 'twice' in response (expected 1)", str(e))
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'twice', 1, msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Found 2 instances of 'twice' in response (expected 1)", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'thrice')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Couldn't find 'thrice' in response", str(e))
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'thrice', msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Couldn't find 'thrice' in response", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'thrice', 3)
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Found 0 instances of 'thrice' in response (expected 3)", str(e))
|
|
|
|
try:
|
|
|
|
self.assertContains(response, 'thrice', 3, msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Found 0 instances of 'thrice' in response (expected 3)", str(e))
|
|
|
|
|
|
|
|
def test_unicode_contains(self):
|
|
|
|
"Unicode characters can be found in template context"
|
2013-11-02 21:02:56 +00:00
|
|
|
# Regression test for #10183
|
2014-01-14 15:43:27 +00:00
|
|
|
r = self.client.get('/check_unicode/')
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertContains(r, 'さかき')
|
2017-02-07 17:05:47 +00:00
|
|
|
self.assertContains(r, b'\xe5\xb3\xa0'.decode())
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_unicode_not_contains(self):
|
|
|
|
"Unicode characters can be searched for, and not found in template context"
|
2013-11-02 21:02:56 +00:00
|
|
|
# Regression test for #10183
|
2014-01-14 15:43:27 +00:00
|
|
|
r = self.client.get('/check_unicode/')
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertNotContains(r, 'はたけ')
|
2017-02-07 17:05:47 +00:00
|
|
|
self.assertNotContains(r, b'\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode())
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2013-04-11 08:36:12 +00:00
|
|
|
def test_binary_contains(self):
|
2014-01-14 15:43:27 +00:00
|
|
|
r = self.client.get('/check_binary/')
|
2013-04-12 18:00:49 +00:00
|
|
|
self.assertContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e')
|
2013-04-11 08:36:12 +00:00
|
|
|
with self.assertRaises(AssertionError):
|
2013-04-12 18:00:49 +00:00
|
|
|
self.assertContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e', count=2)
|
|
|
|
|
|
|
|
def test_binary_not_contains(self):
|
2014-01-14 15:43:27 +00:00
|
|
|
r = self.client.get('/check_binary/')
|
2013-04-12 18:00:49 +00:00
|
|
|
self.assertNotContains(r, b'%ODF-1.4\r\n%\x93\x8c\x8b\x9e')
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
self.assertNotContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e')
|
2013-04-11 08:36:12 +00:00
|
|
|
|
2012-09-18 18:58:40 +00:00
|
|
|
def test_nontext_contains(self):
|
2014-01-14 15:43:27 +00:00
|
|
|
r = self.client.get('/no_template_view/')
|
2017-01-26 19:58:33 +00:00
|
|
|
self.assertContains(r, gettext_lazy('once'))
|
2012-09-18 18:58:40 +00:00
|
|
|
|
|
|
|
def test_nontext_not_contains(self):
|
2014-01-14 15:43:27 +00:00
|
|
|
r = self.client.get('/no_template_view/')
|
2017-01-26 19:58:33 +00:00
|
|
|
self.assertNotContains(r, gettext_lazy('never'))
|
2012-09-18 18:58:40 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_assert_contains_renders_template_response(self):
|
2016-10-27 07:53:39 +00:00
|
|
|
"""
|
|
|
|
An unrendered SimpleTemplateResponse may be used in assertContains().
|
2012-05-31 15:21:13 +00:00
|
|
|
"""
|
2015-01-09 21:59:00 +00:00
|
|
|
template = engines['django'].from_string('Hello')
|
|
|
|
response = SimpleTemplateResponse(template)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertContains(response, 'Hello')
|
|
|
|
|
|
|
|
def test_assert_contains_using_non_template_response(self):
|
2016-10-27 07:53:39 +00:00
|
|
|
""" auto-rendering does not affect responses that aren't
|
2012-05-31 15:21:13 +00:00
|
|
|
instances (or subclasses) of SimpleTemplateResponse.
|
|
|
|
Refs #15826.
|
|
|
|
"""
|
|
|
|
response = HttpResponse('Hello')
|
|
|
|
self.assertContains(response, 'Hello')
|
|
|
|
|
|
|
|
def test_assert_not_contains_renders_template_response(self):
|
2016-10-27 07:53:39 +00:00
|
|
|
"""
|
|
|
|
An unrendered SimpleTemplateResponse may be used in assertNotContains().
|
2012-05-31 15:21:13 +00:00
|
|
|
"""
|
2015-01-09 21:59:00 +00:00
|
|
|
template = engines['django'].from_string('Hello')
|
|
|
|
response = SimpleTemplateResponse(template)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertNotContains(response, 'Bye')
|
|
|
|
|
|
|
|
def test_assert_not_contains_using_non_template_response(self):
|
2016-10-27 07:53:39 +00:00
|
|
|
"""
|
|
|
|
auto-rendering does not affect responses that aren't instances (or
|
|
|
|
subclasses) of SimpleTemplateResponse.
|
2012-05-31 15:21:13 +00:00
|
|
|
"""
|
|
|
|
response = HttpResponse('Hello')
|
|
|
|
self.assertNotContains(response, 'Bye')
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2016-02-05 20:56:52 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-02-23 00:53:57 +00:00
|
|
|
class AssertTemplateUsedTests(TestDataMixin, TestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_no_context(self):
|
|
|
|
"Template usage assertions work then templates aren't in use"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/no_template_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2016-10-27 07:53:39 +00:00
|
|
|
# The no template case doesn't mess with the template assertions
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertTemplateNotUsed(response, 'GET Template')
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertTemplateUsed(response, 'GET Template')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("No templates used to render the response", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertTemplateUsed(response, 'GET Template', msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: No templates used to render the response", str(e))
|
|
|
|
|
2014-04-14 19:13:49 +00:00
|
|
|
with self.assertRaises(AssertionError) as context:
|
|
|
|
self.assertTemplateUsed(response, 'GET Template', count=2)
|
|
|
|
self.assertIn(
|
|
|
|
"No templates used to render the response",
|
|
|
|
str(context.exception))
|
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_single_context(self):
|
|
|
|
"Template assertions work when there is a single context"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/post_view/', {})
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertTemplateNotUsed(response, 'Empty GET Template')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Template 'Empty GET Template' was used unexpectedly in rendering the response", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertTemplateNotUsed(response, 'Empty GET Template', msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Template 'Empty GET Template' was used unexpectedly in rendering the response", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertTemplateUsed(response, 'Empty POST Template')
|
|
|
|
except AssertionError as e:
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertIn(
|
|
|
|
"Template 'Empty POST Template' was not a template used to "
|
|
|
|
"render the response. Actual template(s) used: Empty GET Template",
|
|
|
|
str(e)
|
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertTemplateUsed(response, 'Empty POST Template', msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertIn(
|
|
|
|
"abc: Template 'Empty POST Template' was not a template used "
|
|
|
|
"to render the response. Actual template(s) used: Empty GET Template",
|
|
|
|
str(e)
|
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2014-04-14 19:13:49 +00:00
|
|
|
with self.assertRaises(AssertionError) as context:
|
|
|
|
self.assertTemplateUsed(response, 'Empty GET Template', count=2)
|
|
|
|
self.assertIn(
|
|
|
|
"Template 'Empty GET Template' was expected to be rendered 2 "
|
|
|
|
"time(s) but was actually rendered 1 time(s).",
|
|
|
|
str(context.exception))
|
|
|
|
|
|
|
|
with self.assertRaises(AssertionError) as context:
|
|
|
|
self.assertTemplateUsed(
|
|
|
|
response, 'Empty GET Template', msg_prefix='abc', count=2)
|
|
|
|
self.assertIn(
|
|
|
|
"abc: Template 'Empty GET Template' was expected to be rendered 2 "
|
|
|
|
"time(s) but was actually rendered 1 time(s).",
|
|
|
|
str(context.exception))
|
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_multiple_context(self):
|
|
|
|
"Template assertions work when there are multiple contexts"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'email': 'foo@example.com',
|
|
|
|
'value': 37,
|
|
|
|
'single': 'b',
|
2013-10-26 19:15:03 +00:00
|
|
|
'multi': ('b', 'c', 'e')
|
2012-05-31 15:21:13 +00:00
|
|
|
}
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post('/form_view_with_template/', post_data)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertContains(response, 'POST data OK')
|
|
|
|
try:
|
|
|
|
self.assertTemplateNotUsed(response, "form_view.html")
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Template 'form_view.html' was used unexpectedly in rendering the response", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertTemplateNotUsed(response, 'base.html')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Template 'base.html' was used unexpectedly in rendering the response", str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertTemplateUsed(response, "Valid POST Template")
|
|
|
|
except AssertionError as e:
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertIn(
|
|
|
|
"Template 'Valid POST Template' was not a template used to "
|
|
|
|
"render the response. Actual template(s) used: form_view.html, base.html",
|
|
|
|
str(e)
|
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2014-04-14 19:13:49 +00:00
|
|
|
with self.assertRaises(AssertionError) as context:
|
|
|
|
self.assertTemplateUsed(response, 'base.html', count=2)
|
|
|
|
self.assertIn(
|
|
|
|
"Template 'base.html' was expected to be rendered 2 "
|
|
|
|
"time(s) but was actually rendered 1 time(s).",
|
|
|
|
str(context.exception))
|
|
|
|
|
|
|
|
def test_template_rendered_multiple_times(self):
|
|
|
|
"""Template assertions work when a template is rendered multiple times."""
|
|
|
|
response = self.client.get('/render_template_multiple_times/')
|
|
|
|
|
|
|
|
self.assertTemplateUsed(response, 'base.html', count=2)
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class AssertRedirectsTests(SimpleTestCase):
|
2014-01-14 15:43:27 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_redirect_page(self):
|
|
|
|
"An assertion is raised if the original page couldn't be retrieved as expected"
|
|
|
|
# This page will redirect with code 301, not 302
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/permanent_redirect_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
try:
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, '/get_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Response didn't redirect as expected: Response code was 301 (expected 302)", str(e))
|
|
|
|
|
|
|
|
try:
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, '/get_view/', msg_prefix='abc')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Response didn't redirect as expected: Response code was 301 (expected 302)", str(e))
|
|
|
|
|
|
|
|
def test_lost_query(self):
|
|
|
|
"An assertion is raised if the redirect location doesn't preserve GET parameters"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/redirect_view/', {'var': 'value'})
|
2012-05-31 15:21:13 +00:00
|
|
|
try:
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, '/get_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
2015-03-13 22:40:14 +00:00
|
|
|
self.assertIn("Response redirected to '/get_view/?var=value', expected '/get_view/'", str(e))
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
try:
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, '/get_view/', msg_prefix='abc')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
2015-03-13 22:40:14 +00:00
|
|
|
self.assertIn("abc: Response redirected to '/get_view/?var=value', expected '/get_view/'", str(e))
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_incorrect_target(self):
|
|
|
|
"An assertion is raised if the response redirects to another target"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/permanent_redirect_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
try:
|
|
|
|
# Should redirect to get_view
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, '/some_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Response didn't redirect as expected: Response code was 301 (expected 302)", str(e))
|
|
|
|
|
|
|
|
def test_target_page(self):
|
|
|
|
"An assertion is raised if the response redirect target cannot be retrieved as expected"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/double_redirect_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
try:
|
|
|
|
# The redirect target responds with a 301 code, not 200
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, 'http://testserver/permanent_redirect_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertIn(
|
|
|
|
"Couldn't retrieve redirection page '/permanent_redirect_view/': "
|
|
|
|
"response code was 301 (expected 200)",
|
|
|
|
str(e)
|
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# The redirect target responds with a 301 code, not 200
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, 'http://testserver/permanent_redirect_view/', msg_prefix='abc')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertIn(
|
|
|
|
"abc: Couldn't retrieve redirection page '/permanent_redirect_view/': "
|
|
|
|
"response code was 301 (expected 200)",
|
|
|
|
str(e)
|
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_redirect_chain(self):
|
|
|
|
"You can follow a redirect chain of multiple redirects"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/redirects/further/more/', {}, follow=True)
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertRedirects(response, '/no_template_view/', status_code=302, target_status_code=200)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
self.assertEqual(len(response.redirect_chain), 1)
|
2015-03-13 22:40:14 +00:00
|
|
|
self.assertEqual(response.redirect_chain[0], ('/no_template_view/', 302))
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_multiple_redirect_chain(self):
|
|
|
|
"You can follow a redirect chain of multiple redirects"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/redirects/', {}, follow=True)
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertRedirects(response, '/no_template_view/', status_code=302, target_status_code=200)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
self.assertEqual(len(response.redirect_chain), 3)
|
2015-03-13 22:40:14 +00:00
|
|
|
self.assertEqual(response.redirect_chain[0], ('/redirects/further/', 302))
|
|
|
|
self.assertEqual(response.redirect_chain[1], ('/redirects/further/more/', 302))
|
|
|
|
self.assertEqual(response.redirect_chain[2], ('/no_template_view/', 302))
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_redirect_chain_to_non_existent(self):
|
2017-02-03 01:43:21 +00:00
|
|
|
"You can follow a chain to a nonexistent view."
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/redirect_to_non_existent_view2/', {}, follow=True)
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertRedirects(response, '/non_existent_view/', status_code=302, target_status_code=404)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_redirect_chain_to_self(self):
|
|
|
|
"Redirections to self are caught and escaped"
|
2014-10-17 13:46:42 +00:00
|
|
|
with self.assertRaises(RedirectCycleError) as context:
|
|
|
|
self.client.get('/redirect_to_self/', {}, follow=True)
|
|
|
|
response = context.exception.last_response
|
2012-05-31 15:21:13 +00:00
|
|
|
# The chain of redirects stops once the cycle is detected.
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertRedirects(response, '/redirect_to_self/', status_code=302, target_status_code=302)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(len(response.redirect_chain), 2)
|
|
|
|
|
2014-10-17 13:46:42 +00:00
|
|
|
def test_redirect_to_self_with_changing_query(self):
|
|
|
|
"Redirections don't loop forever even if query is changing"
|
|
|
|
with self.assertRaises(RedirectCycleError):
|
|
|
|
self.client.get('/redirect_to_self_with_changing_query_view/', {'counter': '0'}, follow=True)
|
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_circular_redirect(self):
|
|
|
|
"Circular redirect chains are caught and escaped"
|
2014-10-17 13:46:42 +00:00
|
|
|
with self.assertRaises(RedirectCycleError) as context:
|
|
|
|
self.client.get('/circular_redirect_1/', {}, follow=True)
|
|
|
|
response = context.exception.last_response
|
2012-05-31 15:21:13 +00:00
|
|
|
# The chain of redirects will get back to the starting point, but stop there.
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertRedirects(response, '/circular_redirect_2/', status_code=302, target_status_code=302)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(len(response.redirect_chain), 4)
|
|
|
|
|
|
|
|
def test_redirect_chain_post(self):
|
|
|
|
"A redirect chain will be followed from an initial POST post"
|
2016-04-08 02:04:45 +00:00
|
|
|
response = self.client.post('/redirects/', {'nothing': 'to_send'}, follow=True)
|
|
|
|
self.assertRedirects(response, '/no_template_view/', 302, 200)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(len(response.redirect_chain), 3)
|
|
|
|
|
|
|
|
def test_redirect_chain_head(self):
|
|
|
|
"A redirect chain will be followed from an initial HEAD request"
|
2016-04-08 02:04:45 +00:00
|
|
|
response = self.client.head('/redirects/', {'nothing': 'to_send'}, follow=True)
|
|
|
|
self.assertRedirects(response, '/no_template_view/', 302, 200)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(len(response.redirect_chain), 3)
|
|
|
|
|
|
|
|
def test_redirect_chain_options(self):
|
|
|
|
"A redirect chain will be followed from an initial OPTIONS request"
|
2016-04-08 02:04:45 +00:00
|
|
|
response = self.client.options('/redirects/', follow=True)
|
|
|
|
self.assertRedirects(response, '/no_template_view/', 302, 200)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(len(response.redirect_chain), 3)
|
|
|
|
|
|
|
|
def test_redirect_chain_put(self):
|
|
|
|
"A redirect chain will be followed from an initial PUT request"
|
2016-04-08 02:04:45 +00:00
|
|
|
response = self.client.put('/redirects/', follow=True)
|
|
|
|
self.assertRedirects(response, '/no_template_view/', 302, 200)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(len(response.redirect_chain), 3)
|
|
|
|
|
|
|
|
def test_redirect_chain_delete(self):
|
|
|
|
"A redirect chain will be followed from an initial DELETE request"
|
2016-04-08 02:04:45 +00:00
|
|
|
response = self.client.delete('/redirects/', follow=True)
|
|
|
|
self.assertRedirects(response, '/no_template_view/', 302, 200)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(len(response.redirect_chain), 3)
|
|
|
|
|
2016-06-03 22:02:38 +00:00
|
|
|
@modify_settings(ALLOWED_HOSTS={'append': 'otherserver'})
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_redirect_to_different_host(self):
|
|
|
|
"The test client will preserve scheme, host and port changes"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/redirect_other_host/', follow=True)
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertRedirects(
|
|
|
|
response, 'https://otherserver:8443/no_template_view/',
|
|
|
|
status_code=302, target_status_code=200
|
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
# We can't use is_secure() or get_host()
|
|
|
|
# because response.request is a dictionary, not an HttpRequest
|
|
|
|
self.assertEqual(response.request.get('wsgi.url_scheme'), 'https')
|
|
|
|
self.assertEqual(response.request.get('SERVER_NAME'), 'otherserver')
|
|
|
|
self.assertEqual(response.request.get('SERVER_PORT'), '8443')
|
2016-06-03 22:02:38 +00:00
|
|
|
# assertRedirects() can follow redirect to 'otherserver' too.
|
|
|
|
response = self.client.get('/redirect_other_host/', follow=False)
|
|
|
|
self.assertRedirects(
|
|
|
|
response, 'https://otherserver:8443/no_template_view/',
|
|
|
|
status_code=302, target_status_code=200
|
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_redirect_chain_on_non_redirect_page(self):
|
|
|
|
"An assertion is raised if the original page couldn't be retrieved as expected"
|
|
|
|
# This page will redirect with code 301, not 302
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/get_view/', follow=True)
|
2012-05-31 15:21:13 +00:00
|
|
|
try:
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, '/get_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Response didn't redirect as expected: Response code was 200 (expected 302)", str(e))
|
|
|
|
|
|
|
|
try:
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, '/get_view/', msg_prefix='abc')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Response didn't redirect as expected: Response code was 200 (expected 302)", str(e))
|
|
|
|
|
|
|
|
def test_redirect_on_non_redirect_page(self):
|
|
|
|
"An assertion is raised if the original page couldn't be retrieved as expected"
|
|
|
|
# This page will redirect with code 301, not 302
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/get_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
try:
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, '/get_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("Response didn't redirect as expected: Response code was 200 (expected 302)", str(e))
|
|
|
|
|
|
|
|
try:
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertRedirects(response, '/get_view/', msg_prefix='abc')
|
2012-05-31 15:21:13 +00:00
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: Response didn't redirect as expected: Response code was 200 (expected 302)", str(e))
|
|
|
|
|
2013-10-28 19:34:09 +00:00
|
|
|
def test_redirect_scheme(self):
|
|
|
|
"An assertion is raised if the response doesn't have the scheme specified in expected_url"
|
|
|
|
|
|
|
|
# For all possible True/False combinations of follow and secure
|
|
|
|
for follow, secure in itertools.product([True, False], repeat=2):
|
|
|
|
# always redirects to https
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/https_redirect_view/', follow=follow, secure=secure)
|
2013-10-28 19:34:09 +00:00
|
|
|
# the goal scheme is https
|
2015-01-18 21:43:57 +00:00
|
|
|
self.assertRedirects(response, 'https://testserver/secure_view/', status_code=302)
|
2013-10-28 19:34:09 +00:00
|
|
|
with self.assertRaises(AssertionError):
|
2015-01-18 21:43:57 +00:00
|
|
|
self.assertRedirects(response, 'http://testserver/secure_view/', status_code=302)
|
2013-10-28 19:34:09 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class AssertFormErrorTests(SimpleTestCase):
|
2014-01-14 15:43:27 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_unknown_form(self):
|
|
|
|
"An assertion is raised if the form name is unknown"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'email': 'not an email address',
|
|
|
|
'value': 37,
|
|
|
|
'single': 'b',
|
2013-10-26 19:15:03 +00:00
|
|
|
'multi': ('b', 'c', 'e')
|
2012-05-31 15:21:13 +00:00
|
|
|
}
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post('/form_view/', post_data)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertTemplateUsed(response, "Invalid POST Template")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("The form 'wrong_form' was not used to render the response", str(e))
|
|
|
|
try:
|
|
|
|
self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.', msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: The form 'wrong_form' was not used to render the response", str(e))
|
|
|
|
|
|
|
|
def test_unknown_field(self):
|
|
|
|
"An assertion is raised if the field name is unknown"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'email': 'not an email address',
|
|
|
|
'value': 37,
|
|
|
|
'single': 'b',
|
2013-10-26 19:15:03 +00:00
|
|
|
'multi': ('b', 'c', 'e')
|
2012-05-31 15:21:13 +00:00
|
|
|
}
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post('/form_view/', post_data)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertTemplateUsed(response, "Invalid POST Template")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertFormError(response, 'form', 'some_field', 'Some error.')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("The form 'form' in context 0 does not contain the field 'some_field'", str(e))
|
|
|
|
try:
|
|
|
|
self.assertFormError(response, 'form', 'some_field', 'Some error.', msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: The form 'form' in context 0 does not contain the field 'some_field'", str(e))
|
|
|
|
|
|
|
|
def test_noerror_field(self):
|
|
|
|
"An assertion is raised if the field doesn't have any errors"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'email': 'not an email address',
|
|
|
|
'value': 37,
|
|
|
|
'single': 'b',
|
2013-10-26 19:15:03 +00:00
|
|
|
'multi': ('b', 'c', 'e')
|
2012-05-31 15:21:13 +00:00
|
|
|
}
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post('/form_view/', post_data)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertTemplateUsed(response, "Invalid POST Template")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertFormError(response, 'form', 'value', 'Some error.')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("The field 'value' on form 'form' in context 0 contains no errors", str(e))
|
|
|
|
try:
|
|
|
|
self.assertFormError(response, 'form', 'value', 'Some error.', msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
|
|
|
self.assertIn("abc: The field 'value' on form 'form' in context 0 contains no errors", str(e))
|
|
|
|
|
|
|
|
def test_unknown_error(self):
|
|
|
|
"An assertion is raised if the field doesn't contain the provided error"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'email': 'not an email address',
|
|
|
|
'value': 37,
|
|
|
|
'single': 'b',
|
2013-10-26 19:15:03 +00:00
|
|
|
'multi': ('b', 'c', 'e')
|
2012-05-31 15:21:13 +00:00
|
|
|
}
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post('/form_view/', post_data)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertTemplateUsed(response, "Invalid POST Template")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertFormError(response, 'form', 'email', 'Some error.')
|
|
|
|
except AssertionError as e:
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertIn(
|
2017-01-20 14:24:05 +00:00
|
|
|
"The field 'email' on form 'form' in context 0 does not "
|
|
|
|
"contain the error 'Some error.' (actual errors: "
|
|
|
|
"['Enter a valid email address.'])",
|
|
|
|
str(e)
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
try:
|
|
|
|
self.assertFormError(response, 'form', 'email', 'Some error.', msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertIn(
|
2017-01-20 14:24:05 +00:00
|
|
|
"abc: The field 'email' on form 'form' in context 0 does "
|
|
|
|
"not contain the error 'Some error.' (actual errors: "
|
|
|
|
"['Enter a valid email address.'])",
|
|
|
|
str(e)
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_unknown_nonfield_error(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
An assertion is raised if the form's non field errors doesn't contain
|
|
|
|
the provided error.
|
2012-05-31 15:21:13 +00:00
|
|
|
"""
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'email': 'not an email address',
|
|
|
|
'value': 37,
|
|
|
|
'single': 'b',
|
2013-10-26 19:15:03 +00:00
|
|
|
'multi': ('b', 'c', 'e')
|
2012-05-31 15:21:13 +00:00
|
|
|
}
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post('/form_view/', post_data)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertTemplateUsed(response, "Invalid POST Template")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertFormError(response, 'form', None, 'Some error.')
|
|
|
|
except AssertionError as e:
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertIn(
|
|
|
|
"The form 'form' in context 0 does not contain the non-field "
|
|
|
|
"error 'Some error.' (actual errors: )",
|
|
|
|
str(e)
|
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
try:
|
|
|
|
self.assertFormError(response, 'form', None, 'Some error.', msg_prefix='abc')
|
|
|
|
except AssertionError as e:
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertIn(
|
|
|
|
"abc: The form 'form' in context 0 does not contain the "
|
|
|
|
"non-field error 'Some error.' (actual errors: )",
|
|
|
|
str(e)
|
|
|
|
)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class AssertFormsetErrorTests(SimpleTestCase):
|
2013-02-08 23:45:26 +00:00
|
|
|
msg_prefixes = [("", {}), ("abc: ", {"msg_prefix": "abc"})]
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2013-02-08 23:45:26 +00:00
|
|
|
def setUp(self):
|
|
|
|
"""Makes response object for testing field and non-field errors"""
|
|
|
|
# For testing field and non-field errors
|
|
|
|
self.response_form_errors = self.getResponse({
|
|
|
|
'form-TOTAL_FORMS': '2',
|
|
|
|
'form-INITIAL_FORMS': '2',
|
|
|
|
'form-0-text': 'Raise non-field error',
|
|
|
|
'form-0-email': 'not an email address',
|
|
|
|
'form-0-value': 37,
|
|
|
|
'form-0-single': 'b',
|
2013-10-26 19:15:03 +00:00
|
|
|
'form-0-multi': ('b', 'c', 'e'),
|
2013-02-08 23:45:26 +00:00
|
|
|
'form-1-text': 'Hello World',
|
|
|
|
'form-1-email': 'email@domain.com',
|
|
|
|
'form-1-value': 37,
|
|
|
|
'form-1-single': 'b',
|
2013-10-26 19:15:03 +00:00
|
|
|
'form-1-multi': ('b', 'c', 'e'),
|
2013-02-08 23:45:26 +00:00
|
|
|
})
|
|
|
|
# For testing non-form errors
|
|
|
|
self.response_nonform_errors = self.getResponse({
|
|
|
|
'form-TOTAL_FORMS': '2',
|
|
|
|
'form-INITIAL_FORMS': '2',
|
|
|
|
'form-0-text': 'Hello World',
|
|
|
|
'form-0-email': 'email@domain.com',
|
|
|
|
'form-0-value': 37,
|
|
|
|
'form-0-single': 'b',
|
2013-10-26 19:15:03 +00:00
|
|
|
'form-0-multi': ('b', 'c', 'e'),
|
2013-02-08 23:45:26 +00:00
|
|
|
'form-1-text': 'Hello World',
|
|
|
|
'form-1-email': 'email@domain.com',
|
|
|
|
'form-1-value': 37,
|
|
|
|
'form-1-single': 'b',
|
2013-10-26 19:15:03 +00:00
|
|
|
'form-1-multi': ('b', 'c', 'e'),
|
2013-02-08 23:45:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
def getResponse(self, post_data):
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post('/formset_view/', post_data)
|
2013-02-08 23:45:26 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertTemplateUsed(response, "Invalid POST Template")
|
|
|
|
return response
|
|
|
|
|
|
|
|
def test_unknown_formset(self):
|
|
|
|
"An assertion is raised if the formset name is unknown"
|
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2016-04-08 02:04:45 +00:00
|
|
|
msg = prefix + "The formset 'wrong_formset' was not used to render the response"
|
|
|
|
with self.assertRaisesMessage(AssertionError, msg):
|
|
|
|
self.assertFormsetError(
|
|
|
|
self.response_form_errors,
|
|
|
|
'wrong_formset', 0, 'Some_field', 'Some error.', **kwargs
|
|
|
|
)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
|
|
|
def test_unknown_field(self):
|
|
|
|
"An assertion is raised if the field name is unknown"
|
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2016-04-08 02:04:45 +00:00
|
|
|
msg = prefix + "The formset 'my_formset', form 0 in context 0 does not contain the field 'Some_field'"
|
|
|
|
with self.assertRaisesMessage(AssertionError, msg):
|
|
|
|
self.assertFormsetError(
|
|
|
|
self.response_form_errors,
|
|
|
|
'my_formset', 0, 'Some_field', 'Some error.', **kwargs
|
|
|
|
)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
|
|
|
def test_no_error_field(self):
|
|
|
|
"An assertion is raised if the field doesn't have any errors"
|
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2016-04-08 02:04:45 +00:00
|
|
|
msg = prefix + "The field 'value' on formset 'my_formset', form 1 in context 0 contains no errors"
|
|
|
|
with self.assertRaisesMessage(AssertionError, msg):
|
|
|
|
self.assertFormsetError(self.response_form_errors, 'my_formset', 1, 'value', 'Some error.', **kwargs)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
|
|
|
def test_unknown_error(self):
|
|
|
|
"An assertion is raised if the field doesn't contain the specified error"
|
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2017-01-20 14:24:05 +00:00
|
|
|
msg = prefix + (
|
|
|
|
"The field 'email' on formset 'my_formset', form 0 "
|
2016-04-08 02:04:45 +00:00
|
|
|
"in context 0 does not contain the error 'Some error.' "
|
2017-01-20 14:24:05 +00:00
|
|
|
"(actual errors: ['Enter a valid email address.'])"
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(AssertionError, msg):
|
|
|
|
self.assertFormsetError(self.response_form_errors, 'my_formset', 0, 'email', 'Some error.', **kwargs)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
|
|
|
def test_field_error(self):
|
|
|
|
"No assertion is raised if the field contains the provided error"
|
2016-04-08 02:04:45 +00:00
|
|
|
error_msg = ['Enter a valid email address.']
|
2013-02-08 23:45:26 +00:00
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertFormsetError(self.response_form_errors, 'my_formset', 0, 'email', error_msg, **kwargs)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
|
|
|
def test_no_nonfield_error(self):
|
|
|
|
"An assertion is raised if the formsets non-field errors doesn't contain any errors."
|
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2016-04-08 02:04:45 +00:00
|
|
|
msg = prefix + "The formset 'my_formset', form 1 in context 0 does not contain any non-field errors."
|
|
|
|
with self.assertRaisesMessage(AssertionError, msg):
|
|
|
|
self.assertFormsetError(self.response_form_errors, 'my_formset', 1, None, 'Some error.', **kwargs)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
|
|
|
def test_unknown_nonfield_error(self):
|
|
|
|
"An assertion is raised if the formsets non-field errors doesn't contain the provided error."
|
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2017-01-20 14:24:05 +00:00
|
|
|
msg = prefix + (
|
|
|
|
"The formset 'my_formset', form 0 in context 0 does not "
|
2013-02-08 23:45:26 +00:00
|
|
|
"contain the non-field error 'Some error.' (actual errors: "
|
2017-01-20 14:24:05 +00:00
|
|
|
"['Non-field error.'])"
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(AssertionError, msg):
|
|
|
|
self.assertFormsetError(self.response_form_errors, 'my_formset', 0, None, 'Some error.', **kwargs)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
|
|
|
def test_nonfield_error(self):
|
|
|
|
"No assertion is raised if the formsets non-field errors contains the provided error."
|
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertFormsetError(self.response_form_errors, 'my_formset', 0, None, 'Non-field error.', **kwargs)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
|
|
|
def test_no_nonform_error(self):
|
|
|
|
"An assertion is raised if the formsets non-form errors doesn't contain any errors."
|
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2016-04-08 02:04:45 +00:00
|
|
|
msg = prefix + "The formset 'my_formset' in context 0 does not contain any non-form errors."
|
|
|
|
with self.assertRaisesMessage(AssertionError, msg):
|
|
|
|
self.assertFormsetError(self.response_form_errors, 'my_formset', None, None, 'Some error.', **kwargs)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
|
|
|
def test_unknown_nonform_error(self):
|
|
|
|
"An assertion is raised if the formsets non-form errors doesn't contain the provided error."
|
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2017-01-20 14:24:05 +00:00
|
|
|
msg = prefix + (
|
2013-02-08 23:45:26 +00:00
|
|
|
"The formset 'my_formset' in context 0 does not contain the "
|
2017-01-20 14:24:05 +00:00
|
|
|
"non-form error 'Some error.' (actual errors: ['Forms in a set "
|
|
|
|
"must have distinct email addresses.'])"
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(AssertionError, msg):
|
|
|
|
self.assertFormsetError(
|
|
|
|
self.response_nonform_errors,
|
|
|
|
'my_formset', None, None, 'Some error.', **kwargs
|
|
|
|
)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
|
|
|
def test_nonform_error(self):
|
|
|
|
"No assertion is raised if the formsets non-form errors contains the provided error."
|
2016-04-08 02:04:45 +00:00
|
|
|
msg = 'Forms in a set must have distinct email addresses.'
|
2013-02-08 23:45:26 +00:00
|
|
|
for prefix, kwargs in self.msg_prefixes:
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertFormsetError(self.response_nonform_errors, 'my_formset', None, None, msg, **kwargs)
|
2013-02-08 23:45:26 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2016-02-05 20:56:52 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-02-23 00:53:57 +00:00
|
|
|
class LoginTests(TestDataMixin, TestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_login_different_client(self):
|
2016-10-27 07:53:39 +00:00
|
|
|
"Using a different test client doesn't violate authentication"
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
# Create a second client, and log in.
|
|
|
|
c = Client()
|
|
|
|
login = c.login(username='testclient', password='password')
|
|
|
|
self.assertTrue(login, 'Could not log in')
|
|
|
|
|
|
|
|
# Get a redirection page with the second client.
|
2014-01-14 15:43:27 +00:00
|
|
|
response = c.get("/login_protected_redirect_view/")
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
# At this points, the self.client isn't logged in.
|
2016-10-27 07:53:39 +00:00
|
|
|
# assertRedirects uses the original client, not the default client.
|
2015-03-13 22:40:14 +00:00
|
|
|
self.assertRedirects(response, "/get_view/")
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
|
2012-05-31 15:41:31 +00:00
|
|
|
@override_settings(
|
2014-04-05 06:04:46 +00:00
|
|
|
SESSION_ENGINE='test_client_regress.session',
|
|
|
|
ROOT_URLCONF='test_client_regress.urls',
|
2012-05-31 15:41:31 +00:00
|
|
|
)
|
2015-02-23 00:53:57 +00:00
|
|
|
class SessionEngineTests(TestDataMixin, TestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_login(self):
|
|
|
|
"A session engine that modifies the session key can be used to log in"
|
|
|
|
login = self.client.login(username='testclient', password='password')
|
|
|
|
self.assertTrue(login, 'Could not log in')
|
|
|
|
|
|
|
|
# Try to access a login protected page.
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get("/login_protected_view/")
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.context['user'].username, 'testclient')
|
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls',)
|
2015-04-17 21:38:20 +00:00
|
|
|
class URLEscapingTests(SimpleTestCase):
|
2014-01-14 15:43:27 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_simple_argument_get(self):
|
|
|
|
"Get a view that has a simple string argument"
|
|
|
|
response = self.client.get(reverse('arg_view', args=['Slartibartfast']))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'Howdy, Slartibartfast')
|
|
|
|
|
|
|
|
def test_argument_with_space_get(self):
|
|
|
|
"Get a view that has a string argument that requires escaping"
|
|
|
|
response = self.client.get(reverse('arg_view', args=['Arthur Dent']))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'Hi, Arthur')
|
|
|
|
|
|
|
|
def test_simple_argument_post(self):
|
|
|
|
"Post for a view that has a simple string argument"
|
|
|
|
response = self.client.post(reverse('arg_view', args=['Slartibartfast']))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'Howdy, Slartibartfast')
|
|
|
|
|
|
|
|
def test_argument_with_space_post(self):
|
|
|
|
"Post for a view that has a string argument that requires escaping"
|
|
|
|
response = self.client.post(reverse('arg_view', args=['Arthur Dent']))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'Hi, Arthur')
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2016-02-05 20:56:52 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-02-23 00:53:57 +00:00
|
|
|
class ExceptionTests(TestDataMixin, TestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_exception_cleared(self):
|
|
|
|
"#5836 - A stale user exception isn't re-raised by the test client."
|
|
|
|
|
2013-10-26 19:15:03 +00:00
|
|
|
login = self.client.login(username='testclient', password='password')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertTrue(login, 'Could not log in')
|
2016-06-28 15:21:26 +00:00
|
|
|
with self.assertRaises(CustomTestException):
|
2014-01-14 15:43:27 +00:00
|
|
|
self.client.get("/staff_only/")
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
# At this point, an exception has been raised, and should be cleared.
|
|
|
|
|
|
|
|
# This next operation should be successful; if it isn't we have a problem.
|
|
|
|
login = self.client.login(username='staff', password='password')
|
|
|
|
self.assertTrue(login, 'Could not log in')
|
2016-06-28 15:21:26 +00:00
|
|
|
self.client.get("/staff_only/")
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2012-05-31 15:41:31 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class TemplateExceptionTests(SimpleTestCase):
|
2014-01-14 15:43:27 +00:00
|
|
|
|
2014-12-17 21:51:42 +00:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
2017-01-20 13:01:02 +00:00
|
|
|
'DIRS': [os.path.join(os.path.dirname(__file__), 'bad_templates')],
|
2014-12-17 21:51:42 +00:00
|
|
|
}])
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_bad_404_template(self):
|
|
|
|
"Errors found when rendering 404 error templates are re-raised"
|
2016-06-28 15:21:26 +00:00
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2013-10-19 12:31:38 +00:00
|
|
|
self.client.get("/no_such_view/")
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
# We need two different tests to check URLconf substitution - one to check
|
|
|
|
# it was changed, and another one (without self.urls) to check it was reverted on
|
|
|
|
# teardown. This pair of tests relies upon the alphabetical ordering of test execution.
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class UrlconfSubstitutionTests(SimpleTestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_urlconf_was_changed(self):
|
|
|
|
"TestCase can enforce a custom URLconf on a per-test basis"
|
|
|
|
url = reverse('arg_view', args=['somename'])
|
|
|
|
self.assertEqual(url, '/arg_view/somename/')
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
# This test needs to run *after* UrlconfSubstitutionTests; the zz prefix in the
|
|
|
|
# name is to ensure alphabetical ordering.
|
2015-04-17 21:38:20 +00:00
|
|
|
class zzUrlconfSubstitutionTests(SimpleTestCase):
|
2014-01-14 15:43:27 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_urlconf_was_reverted(self):
|
2014-01-14 15:43:27 +00:00
|
|
|
"""URLconf is reverted to original value after modification in a TestCase
|
|
|
|
|
|
|
|
This will not find a match as the default ROOT_URLCONF is empty.
|
|
|
|
"""
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
reverse('arg_view', args=['somename'])
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2016-02-05 20:56:52 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-02-23 00:53:57 +00:00
|
|
|
class ContextTests(TestDataMixin, TestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_single_context(self):
|
|
|
|
"Context variables can be retrieved from a single context"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get("/request_data/", data={'foo': 'whiz'})
|
2015-12-22 18:50:16 +00:00
|
|
|
self.assertIsInstance(response.context, RequestContext)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn('get-foo', response.context)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['get-foo'], 'whiz')
|
|
|
|
self.assertEqual(response.context['data'], 'sausage')
|
|
|
|
|
2016-06-28 15:21:26 +00:00
|
|
|
with self.assertRaisesMessage(KeyError, 'does-not-exist'):
|
2012-05-31 15:21:13 +00:00
|
|
|
response.context['does-not-exist']
|
|
|
|
|
|
|
|
def test_inherited_context(self):
|
|
|
|
"Context variables can be retrieved from a list of contexts"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get("/request_data_extended/", data={'foo': 'whiz'})
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context.__class__, ContextList)
|
|
|
|
self.assertEqual(len(response.context), 2)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn('get-foo', response.context)
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['get-foo'], 'whiz')
|
|
|
|
self.assertEqual(response.context['data'], 'bacon')
|
|
|
|
|
2016-04-08 02:04:45 +00:00
|
|
|
with self.assertRaises(KeyError) as cm:
|
2012-05-31 15:21:13 +00:00
|
|
|
response.context['does-not-exist']
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(cm.exception.args[0], 'does-not-exist')
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2013-05-13 16:38:29 +00:00
|
|
|
def test_contextlist_keys(self):
|
|
|
|
c1 = Context()
|
|
|
|
c1.update({'hello': 'world', 'goodbye': 'john'})
|
|
|
|
c1.update({'hello': 'dolly', 'dolly': 'parton'})
|
|
|
|
c2 = Context()
|
|
|
|
c2.update({'goodbye': 'world', 'python': 'rocks'})
|
|
|
|
c2.update({'goodbye': 'dolly'})
|
|
|
|
|
2016-11-14 22:40:28 +00:00
|
|
|
k = ContextList([c1, c2])
|
2013-05-13 16:38:29 +00:00
|
|
|
# None, True and False are builtins of BaseContext, and present
|
|
|
|
# in every Context without needing to be added.
|
2016-11-14 22:40:28 +00:00
|
|
|
self.assertEqual({'None', 'True', 'False', 'hello', 'goodbye', 'python', 'dolly'}, k.keys())
|
2013-05-13 16:38:29 +00:00
|
|
|
|
2017-01-07 00:23:42 +00:00
|
|
|
def test_contextlist_get(self):
|
|
|
|
c1 = Context({'hello': 'world', 'goodbye': 'john'})
|
|
|
|
c2 = Context({'goodbye': 'world', 'python': 'rocks'})
|
|
|
|
k = ContextList([c1, c2])
|
|
|
|
self.assertEqual(k.get('hello'), 'world')
|
|
|
|
self.assertEqual(k.get('goodbye'), 'john')
|
|
|
|
self.assertEqual(k.get('python'), 'rocks')
|
|
|
|
self.assertEqual(k.get('nonexistent', 'default'), 'default')
|
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_15368(self):
|
2014-12-21 20:19:05 +00:00
|
|
|
# Need to insert a context processor that assumes certain things about
|
|
|
|
# the request instance. This triggers a bug caused by some ways of
|
|
|
|
# copying RequestContext.
|
|
|
|
with self.settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'APP_DIRS': True,
|
|
|
|
'OPTIONS': {
|
|
|
|
'context_processors': [
|
|
|
|
'test_client_regress.context_processors.special',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}]):
|
|
|
|
response = self.client.get("/request_context_view/")
|
|
|
|
self.assertContains(response, 'Path: /request_context_view/')
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2013-02-04 15:50:15 +00:00
|
|
|
def test_nested_requests(self):
|
|
|
|
"""
|
|
|
|
response.context is not lost when view call another view.
|
|
|
|
"""
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get("/nested_view/")
|
2015-12-22 18:50:16 +00:00
|
|
|
self.assertIsInstance(response.context, RequestContext)
|
2013-02-04 15:50:15 +00:00
|
|
|
self.assertEqual(response.context['nested'], 'yes')
|
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2016-02-05 20:56:52 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-02-23 00:53:57 +00:00
|
|
|
class SessionTests(TestDataMixin, TestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_session(self):
|
|
|
|
"The session isn't lost if a user logs in"
|
|
|
|
# The session doesn't exist to start.
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/check_session/')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'NO')
|
|
|
|
|
|
|
|
# This request sets a session variable.
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/set_session/')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'set_session')
|
|
|
|
|
2016-10-27 07:53:39 +00:00
|
|
|
# The session has been modified
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/check_session/')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'YES')
|
|
|
|
|
|
|
|
# Log in
|
2013-10-26 19:15:03 +00:00
|
|
|
login = self.client.login(username='testclient', password='password')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertTrue(login, 'Could not log in')
|
|
|
|
|
|
|
|
# Session should still contain the modified value
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/check_session/')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'YES')
|
|
|
|
|
2014-05-22 22:45:02 +00:00
|
|
|
def test_session_initiated(self):
|
|
|
|
session = self.client.session
|
|
|
|
session['session_var'] = 'foo'
|
|
|
|
session.save()
|
|
|
|
|
|
|
|
response = self.client.get('/check_session/')
|
|
|
|
self.assertEqual(response.content, b'foo')
|
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_logout(self):
|
|
|
|
"""Logout should work whether the user is logged in or not (#9978)."""
|
|
|
|
self.client.logout()
|
2013-10-26 19:15:03 +00:00
|
|
|
login = self.client.login(username='testclient', password='password')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertTrue(login, 'Could not log in')
|
|
|
|
self.client.logout()
|
|
|
|
self.client.logout()
|
|
|
|
|
2013-06-04 16:37:44 +00:00
|
|
|
def test_logout_with_user(self):
|
|
|
|
"""Logout should send user_logged_out signal if user was logged in."""
|
|
|
|
def listener(*args, **kwargs):
|
|
|
|
listener.executed = True
|
|
|
|
self.assertEqual(kwargs['sender'], User)
|
|
|
|
listener.executed = False
|
|
|
|
|
|
|
|
user_logged_out.connect(listener)
|
|
|
|
self.client.login(username='testclient', password='password')
|
|
|
|
self.client.logout()
|
|
|
|
user_logged_out.disconnect(listener)
|
|
|
|
self.assertTrue(listener.executed)
|
|
|
|
|
|
|
|
@override_settings(AUTH_USER_MODEL='test_client_regress.CustomUser')
|
|
|
|
def test_logout_with_custom_user(self):
|
|
|
|
"""Logout should send user_logged_out signal if custom user was logged in."""
|
|
|
|
def listener(*args, **kwargs):
|
|
|
|
self.assertEqual(kwargs['sender'], CustomUser)
|
2014-06-06 09:19:16 +00:00
|
|
|
listener.executed = True
|
|
|
|
listener.executed = False
|
|
|
|
u = CustomUser.custom_objects.create(email='test@test.com')
|
|
|
|
u.set_password('password')
|
|
|
|
u.save()
|
|
|
|
|
|
|
|
user_logged_out.connect(listener)
|
|
|
|
self.client.login(username='test@test.com', password='password')
|
|
|
|
self.client.logout()
|
|
|
|
user_logged_out.disconnect(listener)
|
|
|
|
self.assertTrue(listener.executed)
|
|
|
|
|
|
|
|
@override_settings(AUTHENTICATION_BACKENDS=(
|
|
|
|
'django.contrib.auth.backends.ModelBackend',
|
|
|
|
'test_client_regress.auth_backends.CustomUserBackend'))
|
|
|
|
def test_logout_with_custom_auth_backend(self):
|
|
|
|
"Request a logout after logging in with custom authentication backend"
|
|
|
|
def listener(*args, **kwargs):
|
|
|
|
self.assertEqual(kwargs['sender'], CustomUser)
|
2013-06-04 16:37:44 +00:00
|
|
|
listener.executed = True
|
|
|
|
listener.executed = False
|
|
|
|
u = CustomUser.custom_objects.create(email='test@test.com')
|
|
|
|
u.set_password('password')
|
|
|
|
u.save()
|
|
|
|
|
|
|
|
user_logged_out.connect(listener)
|
|
|
|
self.client.login(username='test@test.com', password='password')
|
|
|
|
self.client.logout()
|
|
|
|
user_logged_out.disconnect(listener)
|
|
|
|
self.assertTrue(listener.executed)
|
|
|
|
|
|
|
|
def test_logout_without_user(self):
|
|
|
|
"""Logout should send signal even if user not authenticated."""
|
|
|
|
def listener(user, *args, **kwargs):
|
|
|
|
listener.user = user
|
|
|
|
listener.executed = True
|
|
|
|
listener.executed = False
|
|
|
|
|
|
|
|
user_logged_out.connect(listener)
|
|
|
|
self.client.login(username='incorrect', password='password')
|
|
|
|
self.client.logout()
|
|
|
|
user_logged_out.disconnect(listener)
|
|
|
|
|
|
|
|
self.assertTrue(listener.executed)
|
|
|
|
self.assertIsNone(listener.user)
|
|
|
|
|
|
|
|
def test_login_with_user(self):
|
|
|
|
"""Login should send user_logged_in signal on successful login."""
|
|
|
|
def listener(*args, **kwargs):
|
|
|
|
listener.executed = True
|
|
|
|
listener.executed = False
|
|
|
|
|
|
|
|
user_logged_in.connect(listener)
|
|
|
|
self.client.login(username='testclient', password='password')
|
|
|
|
user_logged_out.disconnect(listener)
|
|
|
|
|
|
|
|
self.assertTrue(listener.executed)
|
|
|
|
|
|
|
|
def test_login_without_signal(self):
|
|
|
|
"""Login shouldn't send signal if user wasn't logged in"""
|
|
|
|
def listener(*args, **kwargs):
|
|
|
|
listener.executed = True
|
|
|
|
listener.executed = False
|
|
|
|
|
|
|
|
user_logged_in.connect(listener)
|
|
|
|
self.client.login(username='incorrect', password='password')
|
|
|
|
user_logged_in.disconnect(listener)
|
|
|
|
|
|
|
|
self.assertFalse(listener.executed)
|
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class RequestMethodTests(SimpleTestCase):
|
2014-01-14 15:43:27 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_get(self):
|
|
|
|
"Request a view via request method GET"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/request_methods/')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'request method: GET')
|
|
|
|
|
|
|
|
def test_post(self):
|
|
|
|
"Request a view via request method POST"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post('/request_methods/')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'request method: POST')
|
|
|
|
|
|
|
|
def test_head(self):
|
|
|
|
"Request a view via request method HEAD"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.head('/request_methods/')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
# A HEAD request doesn't return any content.
|
|
|
|
self.assertNotEqual(response.content, b'request method: HEAD')
|
|
|
|
self.assertEqual(response.content, b'')
|
|
|
|
|
|
|
|
def test_options(self):
|
|
|
|
"Request a view via request method OPTIONS"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.options('/request_methods/')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'request method: OPTIONS')
|
|
|
|
|
|
|
|
def test_put(self):
|
|
|
|
"Request a view via request method PUT"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.put('/request_methods/')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'request method: PUT')
|
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
"Request a view via request method DELETE"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.delete('/request_methods/')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'request method: DELETE')
|
|
|
|
|
2013-02-03 02:22:40 +00:00
|
|
|
def test_patch(self):
|
|
|
|
"Request a view via request method PATCH"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.patch('/request_methods/')
|
2013-02-03 02:22:40 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'request method: PATCH')
|
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class RequestMethodStringDataTests(SimpleTestCase):
|
2014-01-14 15:43:27 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_post(self):
|
|
|
|
"Request a view with string data via request method POST"
|
|
|
|
# Regression test for #11371
|
2012-06-07 16:08:47 +00:00
|
|
|
data = '{"test": "json"}'
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post('/request_methods/', data=data, content_type='application/json')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'request method: POST')
|
|
|
|
|
|
|
|
def test_put(self):
|
|
|
|
"Request a view with string data via request method PUT"
|
|
|
|
# Regression test for #11371
|
2012-06-07 16:08:47 +00:00
|
|
|
data = '{"test": "json"}'
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.put('/request_methods/', data=data, content_type='application/json')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'request method: PUT')
|
|
|
|
|
2013-02-03 02:22:40 +00:00
|
|
|
def test_patch(self):
|
|
|
|
"Request a view with string data via request method PATCH"
|
|
|
|
# Regression test for #17797
|
2013-02-03 04:57:38 +00:00
|
|
|
data = '{"test": "json"}'
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.patch('/request_methods/', data=data, content_type='application/json')
|
2013-02-03 02:22:40 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'request method: PATCH')
|
|
|
|
|
2014-10-20 20:32:43 +00:00
|
|
|
def test_empty_string_data(self):
|
|
|
|
"Request a view with empty string data via request method GET/POST/HEAD"
|
|
|
|
# Regression test for #21740
|
|
|
|
response = self.client.get('/body/', data='', content_type='application/json')
|
|
|
|
self.assertEqual(response.content, b'')
|
|
|
|
response = self.client.post('/body/', data='', content_type='application/json')
|
|
|
|
self.assertEqual(response.content, b'')
|
|
|
|
response = self.client.head('/body/', data='', content_type='application/json')
|
|
|
|
self.assertEqual(response.content, b'')
|
|
|
|
|
2015-05-09 05:33:26 +00:00
|
|
|
def test_json(self):
|
|
|
|
response = self.client.get('/json_response/')
|
|
|
|
self.assertEqual(response.json(), {'key': 'value'})
|
|
|
|
|
2016-12-29 13:32:15 +00:00
|
|
|
def test_json_vendor(self):
|
2017-03-07 15:44:15 +00:00
|
|
|
valid_types = (
|
|
|
|
'application/vnd.api+json',
|
|
|
|
'application/vnd.api.foo+json',
|
|
|
|
'application/json; charset=utf-8',
|
|
|
|
)
|
|
|
|
for content_type in valid_types:
|
2016-12-29 13:32:15 +00:00
|
|
|
response = self.client.get('/json_response/', {'content_type': content_type})
|
|
|
|
self.assertEqual(response['Content-Type'], content_type)
|
|
|
|
self.assertEqual(response.json(), {'key': 'value'})
|
|
|
|
|
2016-11-21 13:14:03 +00:00
|
|
|
def test_json_multiple_access(self):
|
|
|
|
response = self.client.get('/json_response/')
|
|
|
|
self.assertIs(response.json(), response.json())
|
|
|
|
|
2015-05-09 05:33:26 +00:00
|
|
|
def test_json_wrong_header(self):
|
|
|
|
response = self.client.get('/body/')
|
|
|
|
msg = 'Content-Type header is "text/html; charset=utf-8", not "application/json"'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
self.assertEqual(response.json(), {'key': 'value'})
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls',)
|
2015-04-17 21:38:20 +00:00
|
|
|
class QueryStringTests(SimpleTestCase):
|
2014-01-14 15:43:27 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_get_like_requests(self):
|
|
|
|
for method_name in ('get', 'head'):
|
2016-10-27 07:53:39 +00:00
|
|
|
# A GET-like request can pass a query string as data (#10571)
|
2012-05-31 15:21:13 +00:00
|
|
|
method = getattr(self.client, method_name)
|
2014-01-14 15:43:27 +00:00
|
|
|
response = method("/request_data/", data={'foo': 'whiz'})
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['get-foo'], 'whiz')
|
|
|
|
|
|
|
|
# A GET-like request can pass a query string as part of the URL
|
2014-01-14 15:43:27 +00:00
|
|
|
response = method("/request_data/?foo=whiz")
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['get-foo'], 'whiz')
|
|
|
|
|
|
|
|
# Data provided in the URL to a GET-like request is overridden by actual form data
|
2014-01-14 15:43:27 +00:00
|
|
|
response = method("/request_data/?foo=whiz", data={'foo': 'bang'})
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['get-foo'], 'bang')
|
|
|
|
|
2014-01-14 15:43:27 +00:00
|
|
|
response = method("/request_data/?foo=whiz", data={'bar': 'bang'})
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(response.context['get-foo'])
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['get-bar'], 'bang')
|
|
|
|
|
|
|
|
def test_post_like_requests(self):
|
|
|
|
# A POST-like request can pass a query string as data
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post("/request_data/", data={'foo': 'whiz'})
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(response.context['get-foo'])
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['post-foo'], 'whiz')
|
|
|
|
|
|
|
|
# A POST-like request can pass a query string as part of the URL
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post("/request_data/?foo=whiz")
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['get-foo'], 'whiz')
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(response.context['post-foo'])
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
# POST data provided in the URL augments actual form data
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post("/request_data/?foo=whiz", data={'foo': 'bang'})
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['get-foo'], 'whiz')
|
|
|
|
self.assertEqual(response.context['post-foo'], 'bang')
|
|
|
|
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post("/request_data/?foo=whiz", data={'bar': 'bang'})
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['get-foo'], 'whiz')
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(response.context['get-bar'])
|
|
|
|
self.assertIsNone(response.context['post-foo'])
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.context['post-bar'], 'bang')
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2017-03-08 19:07:09 +00:00
|
|
|
class PayloadEncodingTests(SimpleTestCase):
|
|
|
|
"""Regression tests for #10571."""
|
|
|
|
|
|
|
|
def test_simple_payload(self):
|
|
|
|
"""A simple ASCII-only text can be POSTed."""
|
|
|
|
text = 'English: mountain pass'
|
|
|
|
response = self.client.post('/parse_encoded_text/', text, content_type='text/plain')
|
|
|
|
self.assertEqual(response.content, text.encode())
|
|
|
|
|
|
|
|
def test_utf8_payload(self):
|
|
|
|
"""Non-ASCII data encoded as UTF-8 can be POSTed."""
|
|
|
|
text = 'dog: собака'
|
|
|
|
response = self.client.post('/parse_encoded_text/', text, content_type='text/plain; charset=utf-8')
|
|
|
|
self.assertEqual(response.content, text.encode())
|
|
|
|
|
|
|
|
def test_utf16_payload(self):
|
|
|
|
"""Non-ASCII data encoded as UTF-16 can be POSTed."""
|
|
|
|
text = 'dog: собака'
|
|
|
|
response = self.client.post('/parse_encoded_text/', text, content_type='text/plain; charset=utf-16')
|
|
|
|
self.assertEqual(response.content, text.encode('utf-16'))
|
|
|
|
|
|
|
|
def test_non_utf_payload(self):
|
|
|
|
"""Non-ASCII data as a non-UTF based encoding can be POSTed."""
|
|
|
|
text = 'dog: собака'
|
|
|
|
response = self.client.post('/parse_encoded_text/', text, content_type='text/plain; charset=koi8-r')
|
|
|
|
self.assertEqual(response.content, text.encode('koi8-r'))
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class DummyFile:
|
2012-05-31 15:21:13 +00:00
|
|
|
def __init__(self, filename):
|
|
|
|
self.name = filename
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2012-05-31 15:21:13 +00:00
|
|
|
def read(self):
|
2012-08-15 08:57:05 +00:00
|
|
|
return b'TEST_FILE_CONTENT'
|
2012-05-31 15:21:13 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class UploadedFileEncodingTest(SimpleTestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_file_encoding(self):
|
|
|
|
encoded_file = encode_file('TEST_BOUNDARY', 'TEST_KEY', DummyFile('test_name.bin'))
|
|
|
|
self.assertEqual(b'--TEST_BOUNDARY', encoded_file[0])
|
|
|
|
self.assertEqual(b'Content-Disposition: form-data; name="TEST_KEY"; filename="test_name.bin"', encoded_file[1])
|
|
|
|
self.assertEqual(b'TEST_FILE_CONTENT', encoded_file[-1])
|
|
|
|
|
|
|
|
def test_guesses_content_type_on_file_encoding(self):
|
|
|
|
self.assertEqual(b'Content-Type: application/octet-stream',
|
|
|
|
encode_file('IGNORE', 'IGNORE', DummyFile("file.bin"))[2])
|
|
|
|
self.assertEqual(b'Content-Type: text/plain',
|
|
|
|
encode_file('IGNORE', 'IGNORE', DummyFile("file.txt"))[2])
|
|
|
|
self.assertIn(encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2], (
|
2013-10-19 23:33:10 +00:00
|
|
|
b'Content-Type: application/x-compress',
|
|
|
|
b'Content-Type: application/x-zip',
|
|
|
|
b'Content-Type: application/x-zip-compressed',
|
|
|
|
b'Content-Type: application/zip',))
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(b'Content-Type: application/octet-stream',
|
|
|
|
encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2])
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls',)
|
2015-04-17 21:38:20 +00:00
|
|
|
class RequestHeadersTest(SimpleTestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
def test_client_headers(self):
|
|
|
|
"A test client can receive custom headers"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get("/check_headers/", HTTP_X_ARG_CHECK='Testing 123')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.content, b"HTTP_X_ARG_CHECK: Testing 123")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_client_headers_redirect(self):
|
|
|
|
"Test client headers are preserved through redirects"
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get("/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123')
|
2012-05-31 15:21:13 +00:00
|
|
|
self.assertEqual(response.content, b"HTTP_X_ARG_CHECK: Testing 123")
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertRedirects(response, '/check_headers/', status_code=302, target_status_code=200)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class ReadLimitedStreamTest(SimpleTestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
HttpRequest.body, HttpRequest.read(), and HttpRequest.read(BUFFER) have
|
|
|
|
proper LimitedStream behavior.
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
Refs #14753, #15785
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_body_from_empty_request(self):
|
|
|
|
"""HttpRequest.body on a test client GET request should return
|
|
|
|
the empty string."""
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertEqual(self.client.get("/body/").content, b'')
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_read_from_empty_request(self):
|
|
|
|
"""HttpRequest.read() on a test client GET request should return the
|
|
|
|
empty string."""
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertEqual(self.client.get("/read_all/").content, b'')
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_read_numbytes_from_empty_request(self):
|
|
|
|
"""HttpRequest.read(LARGE_BUFFER) on a test client GET request should
|
|
|
|
return the empty string."""
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertEqual(self.client.get("/read_buffer/").content, b'')
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_read_from_nonempty_request(self):
|
|
|
|
"""HttpRequest.read() on a test client PUT request with some payload
|
|
|
|
should return that payload."""
|
|
|
|
payload = b'foobar'
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(self.client.put("/read_all/", data=payload, content_type='text/plain').content, payload)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
def test_read_numbytes_from_nonempty_request(self):
|
|
|
|
"""HttpRequest.read(LARGE_BUFFER) on a test client PUT request with
|
|
|
|
some payload should return that payload."""
|
|
|
|
payload = b'foobar'
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(self.client.put("/read_buffer/", data=payload, content_type='text/plain').content, payload)
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class RequestFactoryStateTest(SimpleTestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
"""Regression tests for #15929."""
|
|
|
|
# These tests are checking that certain middleware don't change certain
|
|
|
|
# global state. Alternatively, from the point of view of a test, they are
|
|
|
|
# ensuring test isolation behavior. So, unusually, it doesn't make sense to
|
|
|
|
# run the tests individually, and if any are failing it is confusing to run
|
|
|
|
# them with any other set of tests.
|
|
|
|
|
|
|
|
def common_test_that_should_always_pass(self):
|
2012-05-31 15:41:31 +00:00
|
|
|
request = RequestFactory().get('/')
|
2012-05-31 15:21:13 +00:00
|
|
|
request.session = {}
|
|
|
|
self.assertFalse(hasattr(request, 'user'))
|
|
|
|
|
|
|
|
def test_request(self):
|
|
|
|
self.common_test_that_should_always_pass()
|
|
|
|
|
|
|
|
def test_request_after_client(self):
|
|
|
|
# apart from the next line the three tests are identical
|
|
|
|
self.client.get('/')
|
|
|
|
self.common_test_that_should_always_pass()
|
|
|
|
|
|
|
|
def test_request_after_client_2(self):
|
|
|
|
# This test is executed after the previous one
|
|
|
|
self.common_test_that_should_always_pass()
|
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='test_client_regress.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class RequestFactoryEnvironmentTests(SimpleTestCase):
|
2012-05-31 15:21:13 +00:00
|
|
|
"""
|
|
|
|
Regression tests for #8551 and #17067: ensure that environment variables
|
|
|
|
are set correctly in RequestFactory.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_should_set_correct_env_variables(self):
|
2012-05-31 15:41:31 +00:00
|
|
|
request = RequestFactory().get('/path/')
|
2012-05-31 15:21:13 +00:00
|
|
|
|
|
|
|
self.assertEqual(request.META.get('REMOTE_ADDR'), '127.0.0.1')
|
|
|
|
self.assertEqual(request.META.get('SERVER_NAME'), 'testserver')
|
|
|
|
self.assertEqual(request.META.get('SERVER_PORT'), '80')
|
|
|
|
self.assertEqual(request.META.get('SERVER_PROTOCOL'), 'HTTP/1.1')
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(request.META.get('SCRIPT_NAME') + request.META.get('PATH_INFO'), '/path/')
|