@@ -73,9 +73,9 @@ def test_json_access(app):
73
73
@pytest .mark .parametrize ("options" , [
74
74
(['cookies' , 'headers' ], ('Missing JWT in cookies or headers (Missing cookie '
75
75
'"access_token_cookie"; Missing Authorization Header)' )),
76
- (['json' , 'query_string' ], ('Missing JWT in json or query_string (Missing "jwt" '
77
- 'query paramater; Invalid content-type. Must be '
78
- 'application/json. )' )),
76
+ (['json' , 'query_string' ], ('Missing JWT in json or query_string (Invalid '
77
+ 'content-type. Must be application/json.; '
78
+ 'Missing "jwt" query paramater )' )),
79
79
])
80
80
def test_no_jwt_in_request (app , options ):
81
81
token_locations , expected_err = options
@@ -84,3 +84,48 @@ def test_no_jwt_in_request(app, options):
84
84
response = test_client .get ('/protected' )
85
85
assert response .status_code == 401
86
86
assert response .get_json () == {'msg' : expected_err }
87
+
88
+
89
+ @pytest .mark .parametrize ("options" , [
90
+ (['cookies' , 'headers' ], 200 , None , {'foo' : 'bar' }),
91
+ (['headers' , 'cookies' ], 200 , None , {'foo' : 'bar' }),
92
+ ])
93
+ def test_order_of_jwt_locations_in_request (app , options ):
94
+ """ test order doesn't matter if at least one valid token is set"""
95
+ token_locations , status_code , expected_err , expected_dict = options
96
+ app .config ['JWT_TOKEN_LOCATION' ] = token_locations
97
+ test_client = app .test_client ()
98
+ test_client .get ('/cookie_login' )
99
+ response = test_client .get ('/protected' )
100
+
101
+ assert response .status_code == status_code
102
+ if expected_dict :
103
+ assert response .get_json () == expected_dict
104
+ else :
105
+ assert response .get_json () == {'msg' : expected_err }
106
+
107
+
108
+ @pytest .mark .parametrize ("options" , [
109
+ (['cookies' , 'headers' ], 200 , None , {'foo' : 'bar' }),
110
+ (['headers' , 'cookies' ], 422 , ('Invalid header padding' ), None ),
111
+ ])
112
+ def test_order_of_jwt_locations_with_one_invalid_token_in_request (app , options ):
113
+ """ test order doesn't matter if at least one valid token is set"""
114
+ token_locations , status_code , expected_err , expected_dict = options
115
+ app .config ['JWT_TOKEN_LOCATION' ] = token_locations
116
+ test_client = app .test_client ()
117
+
118
+ with app .test_request_context ():
119
+ access_token = create_access_token ('username' )
120
+ # invalidate the token, to check token location precedence
121
+ access_token = "000000{}" .format (access_token [5 :])
122
+ access_headers = {'Authorization' : 'Bearer {}' .format (access_token )}
123
+ # set valid cookies
124
+ test_client .get ('/cookie_login' )
125
+ response = test_client .get ('/protected' , headers = access_headers )
126
+
127
+ assert response .status_code == status_code
128
+ if expected_dict :
129
+ assert response .get_json () == expected_dict
130
+ else :
131
+ assert response .get_json () == {'msg' : expected_err }
0 commit comments