@@ -139,7 +139,7 @@ def test_default_access_csrf_protection(app, options):
139139 # Test you cannot post without the additional csrf protection
140140 response = test_client .post (post_url )
141141 assert response .status_code == 401
142- assert response .get_json () == {'msg' : 'Missing CSRF token in headers ' }
142+ assert response .get_json () == {'msg' : 'Missing CSRF token' }
143143
144144 # Test that you can post with the csrf double submit value
145145 csrf_headers = {'X-CSRF-TOKEN' : csrf_token }
@@ -201,6 +201,48 @@ def test_csrf_with_custom_header_names(app, options):
201201 assert response .get_json () == {'foo' : 'bar' }
202202
203203
204+ @pytest .mark .parametrize ("options" , [
205+ ('/refresh_token' , 'csrf_refresh_token' , '/post_refresh_protected' ),
206+ ('/access_token' , 'csrf_access_token' , '/post_protected' )
207+ ])
208+ def test_csrf_with_default_form_field (app , options ):
209+ app .config ['JWT_CSRF_CHECK_FORM' ] = True
210+ test_client = app .test_client ()
211+ auth_url , csrf_cookie_name , post_url = options
212+
213+ # Get the jwt cookies and csrf double submit tokens
214+ response = test_client .get (auth_url )
215+ csrf_token = _get_cookie_from_response (response , csrf_cookie_name )[csrf_cookie_name ]
216+
217+ # Test that you can post with the csrf double submit value
218+ csrf_data = {'csrf_token' : csrf_token }
219+ response = test_client .post (post_url , data = csrf_data )
220+ assert response .status_code == 200
221+ assert response .get_json () == {'foo' : 'bar' }
222+
223+
224+ @pytest .mark .parametrize ("options" , [
225+ ('/refresh_token' , 'csrf_refresh_token' , '/post_refresh_protected' ),
226+ ('/access_token' , 'csrf_access_token' , '/post_protected' )
227+ ])
228+ def test_csrf_with_custom_form_field (app , options ):
229+ app .config ['JWT_CSRF_CHECK_FORM' ] = True
230+ app .config ['JWT_ACCESS_CSRF_FIELD_NAME' ] = 'FOO'
231+ app .config ['JWT_REFRESH_CSRF_FIELD_NAME' ] = 'FOO'
232+ test_client = app .test_client ()
233+ auth_url , csrf_cookie_name , post_url = options
234+
235+ # Get the jwt cookies and csrf double submit tokens
236+ response = test_client .get (auth_url )
237+ csrf_token = _get_cookie_from_response (response , csrf_cookie_name )[csrf_cookie_name ]
238+
239+ # Test that you can post with the csrf double submit value
240+ csrf_data = {'FOO' : csrf_token }
241+ response = test_client .post (post_url , data = csrf_data )
242+ assert response .status_code == 200
243+ assert response .get_json () == {'foo' : 'bar' }
244+
245+
204246@pytest .mark .parametrize ("options" , [
205247 ('/refresh_token' , 'csrf_refresh_token' , '/refresh_protected' , '/post_refresh_protected' ), # nopep8
206248 ('/access_token' , 'csrf_access_token' , '/protected' , '/post_protected' )
@@ -222,7 +264,7 @@ def test_custom_csrf_methods(app, options):
222264 # Insure GET requests now fail without csrf
223265 response = test_client .get (get_url )
224266 assert response .status_code == 401
225- assert response .get_json () == {'msg' : 'Missing CSRF token in headers ' }
267+ assert response .get_json () == {'msg' : 'Missing CSRF token' }
226268
227269 # Insure GET requests now succeed with csrf
228270 csrf_headers = {'X-CSRF-TOKEN' : csrf_token }
@@ -430,4 +472,4 @@ def test_jwt_optional_with_csrf_enabled(app):
430472 csrf_token = csrf_cookie ['csrf_access_token' ]
431473 response = test_client .post ('/optional_post_protected' )
432474 assert response .status_code == 401
433- assert response .get_json () == {'msg' : 'Missing CSRF token in headers ' }
475+ assert response .get_json () == {'msg' : 'Missing CSRF token' }
0 commit comments