@@ -291,3 +291,146 @@ def custom_auth_request_handler():
291
291
with app .test_client () as c :
292
292
resp , jdata = post_json (c , '/auth' , {})
293
293
assert jdata == {'hello' : 'world' }
294
+
295
+
296
+ def test_role_required (app_with_role , user_with_role ):
297
+ with app_with_role .test_client () as c :
298
+ resp , jdata = post_json (
299
+ c , '/auth' , {'username' : user_with_role .username , 'password' : user_with_role .password })
300
+ token = jdata ['access_token' ]
301
+
302
+ # check if protected works with role set but not asked for this path
303
+ resp = c .get ('/protected' , headers = {'authorization' : 'JWT ' + token })
304
+ assert resp .status_code == 200
305
+ assert resp .data == b'success'
306
+
307
+ # check if protected works wit role set but not asked for this path
308
+ resp = c .get ('/role/protected/user' , headers = {'Authorization' : 'JWT ' + token })
309
+
310
+ assert resp .status_code == 200
311
+ assert resp .data == b'success'
312
+
313
+
314
+ def test_role_required_bad (app_with_role , user , user_with_role ):
315
+ with app_with_role .test_client () as c :
316
+
317
+ # test bad role
318
+ resp , jdata = post_json (
319
+ c , '/auth' , {'username' : user_with_role .username , 'password' : user_with_role .password })
320
+
321
+ token = jdata ['access_token' ]
322
+ resp = c .get ('/role/protected/admin' , headers = {'Authorization' : 'JWT ' + token })
323
+
324
+ assert resp .status_code == 401
325
+
326
+ # test no role
327
+ resp , jdata = post_json (
328
+ c , '/auth' , {'username' : user .username , 'password' : user .password })
329
+
330
+ token = jdata ['access_token' ]
331
+ resp = c .get ('/role/protected/admin' , headers = {'Authorization' : 'JWT ' + token })
332
+
333
+ assert resp .status_code == 401
334
+
335
+
336
+ def test_role_required_multi (app_with_role , user_with_roles ):
337
+ with app_with_role .test_client () as c :
338
+ resp , jdata = post_json (c , '/auth' , {'username' : user_with_roles .username ,
339
+ 'password' : user_with_roles .password })
340
+ token = jdata ['access_token' ]
341
+
342
+ # check if protected works with role set but not asked for this path
343
+ resp = c .get ('/protected' , headers = {'authorization' : 'JWT ' + token })
344
+ assert resp .status_code == 200
345
+ assert resp .data == b'success'
346
+
347
+ resp = c .get ('/role/protected/user' , headers = {'Authorization' : 'JWT ' + token })
348
+
349
+ assert resp .status_code == 200
350
+ assert resp .data == b'success'
351
+
352
+
353
+ def test_role_required_multi_bad (app_with_role , user_with_roles ):
354
+ with app_with_role .test_client () as c :
355
+ resp , jdata = post_json (c , '/auth' , {'username' : user_with_roles .username ,
356
+ 'password' : user_with_roles .password })
357
+
358
+ token = jdata ['access_token' ]
359
+ resp = c .get ('/role/protected/admin' , headers = {'Authorization' : 'JWT ' + token })
360
+
361
+ assert resp .status_code == 401
362
+
363
+
364
+ def test_multirole_required_multi (app_with_role , user , user_with_roles ):
365
+ with app_with_role .test_client () as c :
366
+ resp , jdata = post_json (c , '/auth' , {'username' : user_with_roles .username ,
367
+ 'password' : user_with_roles .password })
368
+ token = jdata ['access_token' ]
369
+
370
+ # check if protected works with role set but not asked for this path
371
+ resp = c .get ('/protected' , headers = {'authorization' : 'JWT ' + token })
372
+ assert resp .status_code == 200
373
+ assert resp .data == b'success'
374
+
375
+ resp = c .get ('/role/protected/multi' , headers = {'Authorization' : 'JWT ' + token })
376
+
377
+ assert resp .status_code == 200
378
+ assert resp .data == b'success'
379
+
380
+ # test no role
381
+ resp , jdata = post_json (
382
+ c , '/auth' , {'username' : user .username , 'password' : user .password })
383
+
384
+ token = jdata ['access_token' ]
385
+ resp = c .get ('/role/protected/multi' , headers = {'Authorization' : 'JWT ' + token })
386
+
387
+ assert resp .status_code == 401
388
+
389
+
390
+ def test_role_custom (app_with_role_trust_jwt , user , user_with_role , user_with_roles ):
391
+ with app_with_role_trust_jwt .test_client () as c :
392
+ resp , jdata = post_json (c , '/auth' , {'username' : user_with_role .username ,
393
+ 'password' : user_with_role .password })
394
+ token = jdata ['access_token' ]
395
+
396
+ # check if protected works with role set but not asked for this path
397
+ resp = c .get ('/protected' , headers = {'authorization' : 'JWT ' + token })
398
+ assert resp .status_code == 200
399
+ assert resp .data == b'success'
400
+
401
+ # check unauthorized role protection
402
+ resp = c .get ('/role/protected/admin' , headers = {'Authorization' : 'JWT ' + token })
403
+
404
+ assert resp .status_code == 401
405
+
406
+ resp = c .get ('/role/protected/multi' , headers = {'Authorization' : 'JWT ' + token })
407
+
408
+ assert resp .status_code == 200
409
+ assert resp .data == b'success'
410
+
411
+ resp = c .get ('/role/protected/user' , headers = {'Authorization' : 'JWT ' + token })
412
+
413
+ assert resp .status_code == 200
414
+ assert resp .data == b'success'
415
+
416
+ resp , jdata = post_json (c , '/auth' , {'username' : user_with_roles .username ,
417
+ 'password' : user_with_roles .password })
418
+ token = jdata ['access_token' ]
419
+
420
+ # check if protected works with role set but not asked for this path
421
+ resp = c .get ('/protected' , headers = {'authorization' : 'JWT ' + token })
422
+ assert resp .status_code == 200
423
+ assert resp .data == b'success'
424
+
425
+ resp = c .get ('/role/protected/multi' , headers = {'Authorization' : 'JWT ' + token })
426
+
427
+ assert resp .status_code == 200
428
+ assert resp .data == b'success'
429
+ # test no role
430
+ resp , jdata = post_json (
431
+ c , '/auth' , {'username' : user .username , 'password' : user .password })
432
+
433
+ token = jdata ['access_token' ]
434
+ resp = c .get ('/role/protected/multi' , headers = {'Authorization' : 'JWT ' + token })
435
+
436
+ assert resp .status_code == 401
0 commit comments