Refs #32817 -- Combined the bad-or-missing CSRF token tests.

This commit is contained in:
Chris Jerdonek 2021-06-10 10:14:14 -07:00 committed by Mariusz Felisiak
parent 1bbb98d9a4
commit 999402f142
1 changed files with 11 additions and 24 deletions

View File

@ -128,33 +128,20 @@ class CsrfViewMiddlewareTestMixin:
self.assertEqual(403, resp.status_code) self.assertEqual(403, resp.status_code)
self.assertEqual(cm.records[0].getMessage(), 'Forbidden (%s): ' % expected) self.assertEqual(cm.records[0].getMessage(), 'Forbidden (%s): ' % expected)
def test_csrf_cookie_no_token(self): def test_csrf_cookie_bad_or_missing_token(self):
""" """
If a CSRF cookie is present but with no token, the middleware rejects If a CSRF cookie is present but the token is missing or invalid, the
the incoming request.
"""
self._check_bad_or_missing_token(None, REASON_CSRF_TOKEN_MISSING)
def test_csrf_cookie_bad_token_characters(self):
"""
If a CSRF cookie is present but the token has invalid characters, the
middleware rejects the incoming request. middleware rejects the incoming request.
""" """
self._check_bad_or_missing_token(64 * '*', 'CSRF token has invalid characters.') cases = [
(None, REASON_CSRF_TOKEN_MISSING),
def test_csrf_cookie_bad_token_length(self): (64 * '*', 'CSRF token has invalid characters.'),
""" (16 * 'a', 'CSRF token has incorrect length.'),
If a CSRF cookie is present but the token has an incorrect length, the (64 * 'a', 'CSRF token incorrect.'),
middleware rejects the incoming request. ]
""" for token, expected in cases:
self._check_bad_or_missing_token(16 * 'a', 'CSRF token has incorrect length.') with self.subTest(token=token):
self._check_bad_or_missing_token(expected, token)
def test_csrf_cookie_incorrect_token(self):
"""
If a CSRF cookie is present but the correctly formatted token is
incorrect, the middleware rejects the incoming request.
"""
self._check_bad_or_missing_token(64 * 'a', 'CSRF token incorrect.')
def test_process_request_csrf_cookie_and_token(self): def test_process_request_csrf_cookie_and_token(self):
""" """