@@ -15,32 +15,28 @@ def temp_location():
1515 yield os .path .join (test_folder , 'token_cache.bin' )
1616 shutil .rmtree (test_folder , ignore_errors = True )
1717
18-
19- def _test_token_cache_roundtrip (cache ):
18+ def _test_token_cache_roundtrip (persistence ):
2019 client_id = os .getenv ('AZURE_CLIENT_ID' )
2120 client_secret = os .getenv ('AZURE_CLIENT_SECRET' )
2221 if not (client_id and client_secret ):
2322 pytest .skip ('no credentials present to test TokenCache round-trip with.' )
2423
25- app = msal .ConfidentialClientApplication (
26- client_id = client_id ,
27- client_credential = client_secret ,
28- token_cache = cache )
2924 desired_scopes = ['https://graph.microsoft.com/.default' ]
30- token1 = app .acquire_token_for_client (scopes = desired_scopes )
31- os .utime ( # Mock having another process update the cache
32- cache ._persistence .get_location (), None )
33- token2 = app .acquire_token_silent (scopes = desired_scopes , account = None )
34- assert token1 ['access_token' ] == token2 ['access_token' ]
35-
36- def test_file_token_cache_roundtrip (temp_location ):
37- _test_token_cache_roundtrip (PersistedTokenCache (FilePersistence (temp_location )))
38-
39- def test_current_platform_cache_roundtrip_with_persistence_builder (temp_location ):
40- _test_token_cache_roundtrip (PersistedTokenCache (build_encrypted_persistence (temp_location )))
41-
42- def test_persisted_token_cache (temp_location ):
43- _test_token_cache_roundtrip (PersistedTokenCache (FilePersistence (temp_location )))
25+ apps = [ # Multiple apps sharing same persistence
26+ msal .ConfidentialClientApplication (
27+ client_id , client_credential = client_secret ,
28+ token_cache = PersistedTokenCache (persistence )) for i in range (2 )]
29+ token1 = apps [0 ].acquire_token_for_client (scopes = desired_scopes )
30+ assert token1 ["token_source" ] == "identity_provider" , "Initial token should come from IdP"
31+ token2 = apps [1 ].acquire_token_for_client (scopes = desired_scopes ) # Hit token cache in MSAL 1.23+
32+ assert token2 ["token_source" ] == "cache" , "App2 should hit cache written by app1"
33+ assert token1 ['access_token' ] == token2 ['access_token' ], "Cache should hit"
34+
35+ def test_token_cache_roundtrip_with_persistence_biulder (temp_location ):
36+ _test_token_cache_roundtrip (build_encrypted_persistence (temp_location ))
37+
38+ def test_token_cache_roundtrip_with_file_persistence (temp_location ):
39+ _test_token_cache_roundtrip (FilePersistence (temp_location ))
4440
4541def test_file_not_found_error_is_not_raised ():
4642 persistence = FilePersistence ('non_existing_file' )
0 commit comments