1
- from typing import List , Any , Dict , Union
1
+ from typing import List , Any , Dict , Union , Optional
2
2
from unittest .mock import patch
3
3
4
4
from fastapi import FastAPI , Depends
@@ -99,13 +99,14 @@ def should_refetch(self, payload: JSONObject, user_context: Dict[str, Any]):
99
99
100
100
101
101
class AlwaysInvalidValidator (SessionClaimValidator ):
102
- def __init__ (self ):
102
+ def __init__ (self , reason : Optional [ Dict [ str , Any ]] ):
103
103
super ().__init__ ("always-invalid-validator" , TrueClaim )
104
+ self .reason = reason
104
105
105
106
async def validate (
106
107
self , payload : JSONObject , user_context : Union [Dict [str , Any ], None ] = None
107
108
) -> ClaimValidationResult :
108
- return ClaimValidationResult (is_valid = False , reason = { "message" : "foo" } )
109
+ return ClaimValidationResult (is_valid = False , reason = self . reason )
109
110
110
111
def should_refetch (self , payload : JSONObject , user_context : Dict [str , Any ]) -> bool :
111
112
return True
@@ -290,10 +291,28 @@ async def test_should_allow_with_custom_validator_returning_true(
290
291
assert "-" in res .json ()["handle" ]
291
292
292
293
293
- async def test_should_reject_with_custom_validator_returning_false (
294
+ async def test_should_reject_with_custom_validator_returning_false_without_reason (
294
295
fastapi_client : TestClient ,
295
296
):
296
- custom_validator = AlwaysInvalidValidator ()
297
+ custom_validator = AlwaysInvalidValidator (reason = None )
298
+
299
+ st_init_args = st_init_generator_with_claim_validator (custom_validator )
300
+ init (** st_init_args ) # type: ignore
301
+ start_st ()
302
+
303
+ create_session (fastapi_client )
304
+ response = fastapi_client .get ("/default-claims" )
305
+ assert response .status_code == 403
306
+ assert response .json () == {
307
+ "message" : "invalid claim" ,
308
+ "claimValidationErrors" : [{"id" : "always-invalid-validator" }],
309
+ }
310
+
311
+
312
+ async def test_should_reject_with_custom_validator_returning_false_with_reason (
313
+ fastapi_client : TestClient ,
314
+ ):
315
+ custom_validator = AlwaysInvalidValidator (reason = {"message" : "foo" })
297
316
298
317
st_init_args = st_init_generator_with_claim_validator (custom_validator )
299
318
init (** st_init_args ) # type: ignore
@@ -310,9 +329,6 @@ async def test_should_reject_with_custom_validator_returning_false(
310
329
}
311
330
312
331
313
- # should reject with validator returning false with reason (Leaving this. It's exactly same as prev.)
314
-
315
-
316
332
async def test_should_reject_if_assert_claims_returns_an_error (
317
333
fastapi_client : TestClient ,
318
334
):
@@ -440,7 +456,9 @@ async def test_should_reject_with_custom_claim_returning_false(
440
456
):
441
457
# This gets overriden by override_global_claim_validators passed to verify_session()
442
458
# in "/refetched-claim-isvalid-false" api
443
- cv = AlwaysInvalidValidator ()
459
+ cv = AlwaysInvalidValidator (
460
+ reason = {"message" : "does not matter because of override" }
461
+ )
444
462
st_init_args = st_init_generator_with_claim_validator (cv )
445
463
init (** st_init_args ) # type: ignore
446
464
start_st ()
0 commit comments