Skip to content

Commit 92de911

Browse files
committed
Fix cookie unit tests (refs #110)
1 parent 116a7c0 commit 92de911

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed

tests/test_cookies.py

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import pytest
22
from flask import Flask, jsonify, json
3-
from werkzeug.http import parse_cookie
3+
try:
4+
from http.cookies import SimpleCookie
5+
except ImportError:
6+
from Cookie import SimpleCookie
47

58
from flask_jwt_extended import (
69
jwt_required, JWTManager, jwt_refresh_token_required, create_access_token,
@@ -10,11 +13,12 @@
1013

1114

1215
def _get_cookie_from_response(response, cookie_name):
13-
cookies = response.headers.getlist('Set-Cookie')
14-
for cookie in cookies:
15-
parsed_cookie = parse_cookie(cookie)
16-
if cookie_name in parsed_cookie:
17-
return parsed_cookie
16+
cookie_headers = response.headers.getlist('Set-Cookie')
17+
for header in cookie_headers:
18+
cookie = SimpleCookie()
19+
cookie.load(header)
20+
if cookie_name in cookie:
21+
return cookie[cookie_name]
1822
return None
1923

2024

@@ -107,8 +111,7 @@ def test_default_access_csrf_protection(app, options):
107111

108112
# Get the jwt cookies and csrf double submit tokens
109113
response = test_client.get(auth_url)
110-
csrf_cookie = _get_cookie_from_response(response, csrf_cookie_name)
111-
csrf_token = csrf_cookie[csrf_cookie_name]
114+
csrf_token = _get_cookie_from_response(response, csrf_cookie_name).value
112115

113116
# Test you cannot post without the additional csrf protection
114117
response = test_client.post(post_url)
@@ -170,8 +173,7 @@ def test_csrf_with_custom_header_names(app, options):
170173

171174
# Get the jwt cookies and csrf double submit tokens
172175
response = test_client.get(auth_url)
173-
csrf_cookie = _get_cookie_from_response(response, csrf_cookie_name)
174-
csrf_token = csrf_cookie[csrf_cookie_name]
176+
csrf_token = _get_cookie_from_response(response, csrf_cookie_name).value
175177

176178
# Test that you can post with the csrf double submit value
177179
csrf_headers = {'FOO': csrf_token}
@@ -192,8 +194,7 @@ def test_custom_csrf_methods(app, options):
192194

193195
# Get the jwt cookies and csrf double submit tokens
194196
response = test_client.get(auth_url)
195-
csrf_cookie = _get_cookie_from_response(response, csrf_cookie_name)
196-
csrf_token = csrf_cookie[csrf_cookie_name]
197+
csrf_token = _get_cookie_from_response(response, csrf_cookie_name).value
197198

198199
# Insure we can now do posts without csrf
199200
response = test_client.post(post_url)
@@ -234,22 +235,31 @@ def test_default_cookie_options(app):
234235
response = test_client.get('/access_token')
235236
cookies = response.headers.getlist('Set-Cookie')
236237
assert len(cookies) == 2 # JWT and CSRF value
238+
237239
access_cookie = _get_cookie_from_response(response, 'access_token_cookie')
240+
assert access_cookie is not None
241+
assert access_cookie['path'] == '/'
242+
assert access_cookie['httponly'] is True
243+
238244
access_csrf_cookie = _get_cookie_from_response(response, 'csrf_access_token')
239-
assert 'access_token_cookie' in access_cookie
240-
assert access_cookie['HttpOnly; Path'] == '/'
241-
assert 'csrf_access_token' in access_csrf_cookie
245+
assert access_csrf_cookie is not None
246+
assert access_csrf_cookie['path'] == '/'
247+
assert access_csrf_cookie['httponly'] == ''
242248

243249
# Test the default refresh cookies
244250
response = test_client.get('/refresh_token')
245251
cookies = response.headers.getlist('Set-Cookie')
246252
assert len(cookies) == 2 # JWT and CSRF value
253+
247254
refresh_cookie = _get_cookie_from_response(response, 'refresh_token_cookie')
255+
assert refresh_cookie is not None
256+
assert refresh_cookie['path'] == '/'
257+
assert refresh_cookie['httponly'] is True
258+
248259
refresh_csrf_cookie = _get_cookie_from_response(response, 'csrf_refresh_token')
249-
assert 'refresh_token_cookie' in refresh_cookie
250-
assert 'HttpOnly; Path' in refresh_cookie
251-
assert refresh_cookie['HttpOnly; Path'] == '/'
252-
assert 'csrf_refresh_token' in refresh_csrf_cookie
260+
assert refresh_csrf_cookie is not None
261+
assert refresh_csrf_cookie['path'] == '/'
262+
assert refresh_csrf_cookie['httponly'] == ''
253263

254264

255265
def test_custom_cookie_options(app):
@@ -265,41 +275,39 @@ def test_custom_cookie_options(app):
265275
assert len(cookies) == 2 # JWT and CSRF value
266276

267277
access_cookie = _get_cookie_from_response(response, 'access_token_cookie')
268-
assert 'access_token_cookie' in access_cookie
269-
assert 'Domain' in access_cookie
270-
assert 'Expires=' in str(cookies[0]) # Ignored by parse_cookie :(
271-
assert 'Secure; HttpOnly; Path' in access_cookie
272-
assert access_cookie['Domain'] == 'test.com'
273-
assert access_cookie['Secure; HttpOnly; Path'] == '/'
278+
assert access_cookie is not None
279+
assert access_cookie['domain'] == 'test.com'
280+
assert access_cookie['path'] == '/'
281+
assert access_cookie['expires'] != ''
282+
assert access_cookie['httponly'] is True
283+
assert access_cookie['secure'] is True
274284

275285
access_csrf_cookie = _get_cookie_from_response(response, 'csrf_access_token')
276-
assert 'csrf_access_token' in access_csrf_cookie
277-
assert 'Domain' in access_csrf_cookie
278-
assert 'Expires=' in str(cookies[1]) # Ignored by parse_cookie :(
279-
assert 'Secure; Path' in access_csrf_cookie
280-
assert access_csrf_cookie['Domain'] == 'test.com'
281-
assert access_csrf_cookie['Secure; Path'] == '/'
286+
assert access_csrf_cookie is not None
287+
assert access_csrf_cookie['path'] == '/'
288+
assert access_csrf_cookie['secure'] is True
289+
assert access_csrf_cookie['domain'] == 'test.com'
290+
assert access_csrf_cookie['expires'] != ''
282291

283292
# Test refresh cookies with changed options
284293
response = test_client.get('/refresh_token')
285294
cookies = response.headers.getlist('Set-Cookie')
286295
assert len(cookies) == 2 # JWT and CSRF value
287296

288297
refresh_cookie = _get_cookie_from_response(response, 'refresh_token_cookie')
289-
assert 'refresh_token_cookie' in refresh_cookie
290-
assert 'Domain' in refresh_cookie
291-
assert 'Expires=' in str(cookies[0]) # Ignored by parse_cookie :(
292-
assert 'Secure; HttpOnly; Path' in refresh_cookie
293-
assert refresh_cookie['Domain'] == 'test.com'
294-
assert refresh_cookie['Secure; HttpOnly; Path'] == '/'
298+
assert refresh_cookie is not None
299+
assert refresh_cookie['domain'] == 'test.com'
300+
assert refresh_cookie['path'] == '/'
301+
assert refresh_cookie['httponly'] is True
302+
assert refresh_cookie['secure'] is True
303+
assert refresh_cookie['expires'] != ''
295304

296305
refresh_csrf_cookie = _get_cookie_from_response(response, 'csrf_refresh_token')
297-
assert 'csrf_refresh_token' in refresh_csrf_cookie
298-
assert 'Domain' in refresh_csrf_cookie
299-
assert 'Expires=' in str(cookies[1]) # Ignored by parse_cookie :(
300-
assert 'Secure; Path' in refresh_csrf_cookie
301-
assert refresh_csrf_cookie['Domain'] == 'test.com'
302-
assert refresh_csrf_cookie['Secure; Path'] == '/'
306+
assert refresh_csrf_cookie is not None
307+
assert refresh_csrf_cookie['path'] == '/'
308+
assert refresh_csrf_cookie['secure'] is True
309+
assert refresh_csrf_cookie['domain'] == 'test.com'
310+
assert refresh_csrf_cookie['expires'] != ''
303311

304312

305313
def test_custom_cookie_names_and_paths(app):
@@ -321,14 +329,10 @@ def test_custom_cookie_names_and_paths(app):
321329

322330
access_cookie = _get_cookie_from_response(response, 'access_foo')
323331
access_csrf_cookie = _get_cookie_from_response(response, 'access_foo_csrf')
324-
assert 'access_foo' in access_cookie
325-
assert 'access_foo_csrf' in access_csrf_cookie
326-
327-
# The parse cookie library ignores 'Path' cookies, and we don't know which
328-
# cookie in the list is the csrf cookie and which is the jwt cookie. So
329-
# we have to resort to doing string comparisons on both of them.
330-
assert 'Path=/protected' in cookies[0]
331-
assert 'Path=/protected' in cookies[1]
332+
assert access_cookie is not None
333+
assert access_csrf_cookie is not None
334+
assert access_cookie['path'] == '/protected'
335+
assert access_csrf_cookie['path'] == '/protected'
332336

333337
# Test the default refresh cookies
334338
response = test_client.get('/refresh_token')
@@ -337,14 +341,10 @@ def test_custom_cookie_names_and_paths(app):
337341

338342
refresh_cookie = _get_cookie_from_response(response, 'refresh_foo')
339343
refresh_csrf_cookie = _get_cookie_from_response(response, 'refresh_foo_csrf')
340-
assert 'refresh_foo' in refresh_cookie
341-
assert 'refresh_foo_csrf' in refresh_csrf_cookie
342-
343-
# The parse cookie library ignores 'Path' cookies, and we don't know which
344-
# cookie in the list is the csrf cookie and which is the jwt cookie. So
345-
# we have to resort to doing string comparisons on both of them.
346-
assert 'Path=/refresh_protected' in cookies[0]
347-
assert 'Path=/refresh_protected' in cookies[1]
344+
assert refresh_cookie is not None
345+
assert refresh_csrf_cookie is not None
346+
assert refresh_cookie['path'] == '/refresh_protected'
347+
assert refresh_csrf_cookie['path'] == '/refresh_protected'
348348

349349

350350
def test_csrf_token_not_in_cookie(app):
@@ -357,14 +357,14 @@ def test_csrf_token_not_in_cookie(app):
357357
cookies = response.headers.getlist('Set-Cookie')
358358
assert len(cookies) == 1
359359
access_cookie = _get_cookie_from_response(response, 'access_token_cookie')
360-
assert 'access_token_cookie' in access_cookie
360+
assert access_cookie is not None
361361

362362
# Test the default refresh cookies
363363
response = test_client.get('/refresh_token')
364364
cookies = response.headers.getlist('Set-Cookie')
365365
assert len(cookies) == 1
366366
refresh_cookie = _get_cookie_from_response(response, 'refresh_token_cookie')
367-
assert 'refresh_token_cookie' in refresh_cookie
367+
assert refresh_cookie is not None
368368

369369

370370
def test_cookies_without_csrf(app):
@@ -377,11 +377,11 @@ def test_cookies_without_csrf(app):
377377
cookies = response.headers.getlist('Set-Cookie')
378378
assert len(cookies) == 1
379379
access_cookie = _get_cookie_from_response(response, 'access_token_cookie')
380-
assert 'access_token_cookie' in access_cookie
380+
assert access_cookie is not None
381381

382382
# Test the default refresh cookies
383383
response = test_client.get('/refresh_token')
384384
cookies = response.headers.getlist('Set-Cookie')
385385
assert len(cookies) == 1
386386
refresh_cookie = _get_cookie_from_response(response, 'refresh_token_cookie')
387-
assert 'refresh_token_cookie' in refresh_cookie
387+
assert refresh_cookie is not None

0 commit comments

Comments
 (0)