Refs #32843 -- Moved _get_GET_csrf_cookie_request() to CsrfViewMiddlewareTestMixin.

This commit is contained in:
Chris Jerdonek 2021-06-23 10:16:40 -04:00 committed by Mariusz Felisiak
parent 4397d2bd6b
commit 6bccb64347
1 changed files with 11 additions and 13 deletions

View File

@ -90,6 +90,9 @@ class CsrfViewMiddlewareTestMixin:
_csrf_id_cookie = MASKED_TEST_SECRET1 _csrf_id_cookie = MASKED_TEST_SECRET1
_csrf_id_token = MASKED_TEST_SECRET2 _csrf_id_token = MASKED_TEST_SECRET2
def _set_csrf_cookie(self, req, cookie):
raise NotImplementedError('This method must be implemented by a subclass.')
def _get_request(self): def _get_request(self):
req = TestingHttpRequest() req = TestingHttpRequest()
req.method = 'GET' req.method = 'GET'
@ -99,7 +102,12 @@ class CsrfViewMiddlewareTestMixin:
return self._get_request() return self._get_request()
def _get_GET_csrf_cookie_request(self, cookie=None): def _get_GET_csrf_cookie_request(self, cookie=None):
raise NotImplementedError('This method must be implemented by a subclass.') """The cookie argument defaults to the valid test cookie."""
if cookie is None:
cookie = self._csrf_id_cookie
req = self._get_request()
self._set_csrf_cookie(req, cookie)
return req
def _get_POST_csrf_cookie_request( def _get_POST_csrf_cookie_request(
self, cookie=None, post_token=None, meta_token=None, token_header=None, self, cookie=None, post_token=None, meta_token=None, token_header=None,
@ -858,13 +866,8 @@ class CsrfViewMiddlewareTestMixin:
class CsrfViewMiddlewareTests(CsrfViewMiddlewareTestMixin, SimpleTestCase): class CsrfViewMiddlewareTests(CsrfViewMiddlewareTestMixin, SimpleTestCase):
def _get_GET_csrf_cookie_request(self, cookie=None): def _set_csrf_cookie(self, req, cookie):
"""The cookie argument defaults to the valid test cookie."""
if cookie is None:
cookie = self._csrf_id_cookie
req = self._get_request()
req.COOKIES[settings.CSRF_COOKIE_NAME] = cookie req.COOKIES[settings.CSRF_COOKIE_NAME] = cookie
return req
def test_ensures_csrf_cookie_no_middleware(self): def test_ensures_csrf_cookie_no_middleware(self):
""" """
@ -1068,13 +1071,8 @@ class CsrfViewMiddlewareUseSessionsTests(CsrfViewMiddlewareTestMixin, SimpleTest
CSRF tests with CSRF_USE_SESSIONS=True. CSRF tests with CSRF_USE_SESSIONS=True.
""" """
def _get_GET_csrf_cookie_request(self, cookie=None): def _set_csrf_cookie(self, req, cookie):
"""The cookie argument defaults to the valid test cookie."""
if cookie is None:
cookie = self._csrf_id_cookie
req = self._get_request()
req.session[CSRF_SESSION_KEY] = cookie req.session[CSRF_SESSION_KEY] = cookie
return req
def test_no_session_on_request(self): def test_no_session_on_request(self):
msg = ( msg = (