11from tests import unittest
22import msal
3- import logging
43import sys
54
6- if not sys .platform .startswith ("win" ):
7- raise unittest .SkipTest ("Currently, our broker supports Windows" )
85
9- SCOPE_ARM = "https://management.azure.com/.default"
6+ if sys .platform not in ("win32" , "darwin" ):
7+ raise unittest .SkipTest (f"Our broker does not support { sys .platform } " )
8+
9+ SCOPES = ["https://management.azure.com/.default" ]
1010_AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
1111pca = msal .PublicClientApplication (
1212 _AZURE_CLI ,
1313 authority = "https://login.microsoftonline.com/organizations" ,
1414 enable_broker_on_mac = True ,
15- enable_broker_on_windows = True )
15+ enable_broker_on_windows = True ,
16+ )
17+
1618
1719class ForceRefreshTestCase (unittest .TestCase ):
18- def test_silent_with_force_refresh (self ):
19- print ("Testing silent flow with force_refresh=True" )
20- result = pca .acquire_token_interactive (scopes = [SCOPE_ARM ], prompt = "select_account" , parent_window_handle = pca .CONSOLE_WINDOW_HANDLE , enable_msa_passthrough = True )
20+ def test_silent_with_force_refresh_should_return_a_new_token (self ):
21+ result = pca .acquire_token_interactive (
22+ scopes = SCOPES ,
23+ prompt = "select_account" ,
24+ parent_window_handle = pca .CONSOLE_WINDOW_HANDLE ,
25+ enable_msa_passthrough = True ,
26+ )
2127 accounts = pca .get_accounts ()
28+ self .assertNotEqual (
29+ [], accounts ,
30+ "Interactive flow should have established a logged-in account" )
2231 account = accounts [0 ]
23- assert account , "The logged in account should have been established by interactive flow"
24- oldToken = result .get ("access_token" )
25-
26-
27- result = pca .acquire_token_silent (
28- scopes = [SCOPE_ARM ],
29- account = account ,
30- force_refresh = False )
31-
32- # This token should have been recieved from cache
33- assert result .get ("access_token" ) == oldToken , "Token should not be refreshed"
34-
35-
36- result = pca .acquire_token_silent (
37- scopes = [SCOPE_ARM ],
38- account = account ,
39- force_refresh = True )
40-
41- # Token will be different proving it is not token from cache and was renewed
42- assert result .get ("access_token" ) != oldToken , "Token should be refreshed"
32+ old_token = result .get ("access_token" )
33+
34+ result = pca .acquire_token_silent (SCOPES , account )
35+ assertion = "This token should have been received from cache"
36+ self .assertEqual (result .get ("access_token" ), old_token , assertion )
37+ self .assertEqual (result .get ("token_source" ), "cache" , assertion )
38+
39+ result = pca .acquire_token_silent (SCOPES , account , force_refresh = True )
40+ assertion = "A new token should have been received from broker"
41+ self .assertNotEqual (result .get ("access_token" ), old_token , assertion )
42+ self .assertEqual (result .get ("token_source" ), "broker" , assertion )
43+
0 commit comments