Skip to content

Commit b3b8962

Browse files
author
IndominusByte
committed
complete all validation cookie jwt
1 parent 8f508d5 commit b3b8962

File tree

1 file changed

+116
-3
lines changed

1 file changed

+116
-3
lines changed

tests/test_cookies.py

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def authjwt_exception_handler(request: Request, exc: AuthJWTException):
1818

1919
@app.get('/all-token')
2020
def all_token(Authorize: AuthJWT = Depends()):
21-
access_token = Authorize.create_access_token(subject=1)
21+
access_token = Authorize.create_access_token(subject=1,fresh=True)
2222
refresh_token = Authorize.create_refresh_token(subject=1)
2323
Authorize.set_access_cookies(access_token)
2424
Authorize.set_refresh_cookies(refresh_token)
@@ -44,18 +44,31 @@ def unset_all_token(Authorize: AuthJWT = Depends()):
4444
@app.get('/unset-access-token')
4545
def unset_access_token(Authorize: AuthJWT = Depends()):
4646
Authorize.unset_access_cookies()
47-
return {"msg":"unset access token"}
4847

4948
@app.get('/unset-refresh-token')
5049
def unset_refresh_token(Authorize: AuthJWT = Depends()):
5150
Authorize.unset_refresh_cookies()
52-
return {"msg":"unset refresh token"}
5351

5452
@app.post('/jwt-optional')
5553
def jwt_optional(Authorize: AuthJWT = Depends()):
5654
Authorize.jwt_optional()
5755
return {"hello": Authorize.get_jwt_subject()}
5856

57+
@app.post('/jwt-required')
58+
def jwt_required(Authorize: AuthJWT = Depends()):
59+
Authorize.jwt_required()
60+
return {"hello": Authorize.get_jwt_subject()}
61+
62+
@app.post('/jwt-refresh')
63+
def jwt_refresh(Authorize: AuthJWT = Depends()):
64+
Authorize.jwt_refresh_token_required()
65+
return {"hello": Authorize.get_jwt_subject()}
66+
67+
@app.post('/jwt-fresh')
68+
def jwt_fresh(Authorize: AuthJWT = Depends()):
69+
Authorize.fresh_jwt_required()
70+
return {"hello": Authorize.get_jwt_subject()}
71+
5972
client = TestClient(app)
6073
return client
6174

@@ -251,6 +264,106 @@ def custom_header_name_cookie_key():
251264
res = client.get('/access-token')
252265
csrf_token = res.cookies.get("csrf_access_token")
253266

267+
# valid request
254268
response = client.post(url,headers={"X-CSRF": csrf_token})
255269
assert response.status_code == 200
256270
assert response.json() == {'hello': 1}
271+
272+
@pytest.mark.parametrize("url",["/jwt-required","/jwt-refresh","/jwt-fresh"])
273+
def test_cookie_protected(url,client):
274+
# custom csrf header name and cookie key
275+
@AuthJWT.load_config
276+
def custom_header_name_cookie_key():
277+
return [
278+
("authjwt_token_location",{'cookies'}),
279+
("authjwt_secret_key","secret"),
280+
("authjwt_access_cookie_key","access_cookie"),
281+
("authjwt_access_csrf_header_name","X-CSRF-Access"),
282+
("authjwt_refresh_cookie_key","refresh_cookie"),
283+
("authjwt_refresh_csrf_header_name","X-CSRF-Refresh")
284+
]
285+
286+
res = client.get('/all-token')
287+
csrf_access = res.cookies.get("csrf_access_token")
288+
csrf_refresh = res.cookies.get("csrf_refresh_token")
289+
290+
if url != "/jwt-refresh":
291+
response = client.post(url,headers={"X-CSRF-Access": csrf_access})
292+
assert response.status_code == 200
293+
assert response.json() == {'hello': 1}
294+
else:
295+
response = client.post(url,headers={"X-CSRF-Refresh": csrf_refresh})
296+
assert response.status_code == 200
297+
assert response.json() == {'hello': 1}
298+
299+
# missing csrf token
300+
response = client.post(url)
301+
assert response.status_code == 401
302+
assert response.json() == {'detail': 'Missing CSRF Token'}
303+
304+
# missing cookie
305+
client.get('/unset-all-token')
306+
response = client.post(url)
307+
assert response.status_code == 401
308+
if url != "/jwt-refresh":
309+
assert response.json() == {'detail': 'Missing cookie access_cookie'}
310+
else:
311+
assert response.json() == {'detail': 'Missing cookie refresh_cookie'}
312+
313+
# change csrf protect to False not check csrf token
314+
@AuthJWT.load_config
315+
def change_request_csrf_protect_to_false():
316+
return [
317+
("authjwt_token_location",{'cookies'}),
318+
("authjwt_secret_key","secret"),
319+
("authjwt_cookie_csrf_protect",False)
320+
]
321+
322+
client.get('/all-token')
323+
response = client.post(url)
324+
assert response.status_code == 200
325+
assert response.json() == {'hello': 1}
326+
327+
# change request methods and not check csrf token
328+
@AuthJWT.load_config
329+
def change_request_methods():
330+
return [
331+
("authjwt_csrf_methods",{"GET"}),
332+
("authjwt_token_location",{'cookies'}),
333+
("authjwt_secret_key","secret"),
334+
("authjwt_cookie_csrf_protect",True)
335+
]
336+
337+
response = client.post(url)
338+
assert response.status_code == 200
339+
assert response.json() == {'hello': 1}
340+
341+
# missing claim csrf in token
342+
@AuthJWT.load_config
343+
def change_request_methods_to_default():
344+
return [
345+
("authjwt_csrf_methods",{'POST','PUT','PATCH','DELETE'}),
346+
("authjwt_token_location",{'cookies'}),
347+
("authjwt_secret_key","secret"),
348+
]
349+
350+
response = client.post(url,headers={"X-CSRF-Token":"invalid"})
351+
assert response.status_code == 422
352+
assert response.json() == {'detail': 'Missing claim: csrf'}
353+
354+
# csrf token do not match
355+
res = client.get('/all-token')
356+
csrf_access = res.cookies.get("csrf_access_token")
357+
csrf_refresh = res.cookies.get("csrf_refresh_token")
358+
359+
response = client.post(url,headers={"X-CSRF-Token":"invalid"})
360+
assert response.status_code == 401
361+
assert response.json() == {'detail': 'CSRF double submit tokens do not match'}
362+
363+
# valid request
364+
if url != "/jwt-refresh":
365+
response = client.post(url,headers={"X-CSRF-Token": csrf_access})
366+
else:
367+
response = client.post(url,headers={"X-CSRF-Token": csrf_refresh})
368+
assert response.status_code == 200
369+
assert response.json() == {'hello': 1}

0 commit comments

Comments
 (0)