2020logger = logging .getLogger (__name__ )
2121logging .basicConfig (level = logging .DEBUG )
2222
23+ _OIDC_DISCOVERY = "msal.authority.tenant_discovery"
24+ _OIDC_DISCOVERY_MOCK = Mock (return_value = {
25+ "authorization_endpoint" : "https://contoso.com/placeholder" ,
26+ "token_endpoint" : "https://contoso.com/placeholder" ,
27+ })
28+
2329
2430class TestHelperExtractCerts (unittest .TestCase ): # It is used by SNI scenario
2531
@@ -58,10 +64,9 @@ def test_bytes_to_bytes(self):
5864
5965class TestClientApplicationAcquireTokenSilentErrorBehaviors (unittest .TestCase ):
6066
67+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
6168 def setUp (self ):
6269 self .authority_url = "https://login.microsoftonline.com/common"
63- self .authority = msal .authority .Authority (
64- self .authority_url , MinimalHttpClient ())
6570 self .scopes = ["s1" , "s2" ]
6671 self .uid = "my_uid"
6772 self .utid = "my_utid"
@@ -116,12 +121,11 @@ def tester(url, **kwargs):
116121 self .assertEqual ("" , result .get ("classification" ))
117122
118123
124+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
119125class TestClientApplicationAcquireTokenSilentFociBehaviors (unittest .TestCase ):
120126
121127 def setUp (self ):
122128 self .authority_url = "https://login.microsoftonline.com/common"
123- self .authority = msal .authority .Authority (
124- self .authority_url , MinimalHttpClient ())
125129 self .scopes = ["s1" , "s2" ]
126130 self .uid = "my_uid"
127131 self .utid = "my_utid"
@@ -148,7 +152,7 @@ def tester(url, data=None, **kwargs):
148152 self .assertEqual (self .frt , data .get ("refresh_token" ), "Should attempt the FRT" )
149153 return MinimalResponse (status_code = 400 , text = error_response )
150154 app ._acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family (
151- self .authority , self .scopes , self .account , post = tester )
155+ app .authority , self .scopes , self .account , post = tester )
152156 self .assertNotEqual ([], app .token_cache .find (
153157 msal .TokenCache .CredentialType .REFRESH_TOKEN , query = {"secret" : self .frt }),
154158 "The FRT should not be removed from the cache" )
@@ -168,7 +172,7 @@ def tester(url, data=None, **kwargs):
168172 self .assertEqual (rt , data .get ("refresh_token" ), "Should attempt the RT" )
169173 return MinimalResponse (status_code = 200 , text = '{}' )
170174 app ._acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family (
171- self .authority , self .scopes , self .account , post = tester )
175+ app .authority , self .scopes , self .account , post = tester )
172176
173177 def test_unknown_family_app_will_attempt_frt_and_join_family (self ):
174178 def tester (url , data = None , ** kwargs ):
@@ -180,7 +184,7 @@ def tester(url, data=None, **kwargs):
180184 app = ClientApplication (
181185 "unknown_family_app" , authority = self .authority_url , token_cache = self .cache )
182186 at = app ._acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family (
183- self .authority , self .scopes , self .account , post = tester )
187+ app .authority , self .scopes , self .account , post = tester )
184188 logger .debug ("%s.cache = %s" , self .id (), self .cache .serialize ())
185189 self .assertEqual ("at" , at .get ("access_token" ), "New app should get a new AT" )
186190 app_metadata = app .token_cache .find (
@@ -202,7 +206,7 @@ def tester(url, data=None, **kwargs):
202206 app = ClientApplication (
203207 "preexisting_family_app" , authority = self .authority_url , token_cache = self .cache )
204208 resp = app ._acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family (
205- self .authority , self .scopes , self .account , post = tester )
209+ app .authority , self .scopes , self .account , post = tester )
206210 logger .debug ("%s.cache = %s" , self .id (), self .cache .serialize ())
207211 self .assertEqual (json .loads (error_response ), resp , "Error raised will be returned" )
208212
@@ -237,7 +241,7 @@ def test_family_app_remove_account(self):
237241
238242class TestClientApplicationForAuthorityMigration (unittest .TestCase ):
239243
240- @ classmethod
244+ # Chose to not mock oidc discovery, because AuthorityMigration might rely on real data
241245 def setUp (self ):
242246 self .environment_in_cache = "sts.windows.net"
243247 self .authority_url_in_app = "https://login.microsoftonline.com/common"
@@ -444,6 +448,7 @@ def mock_post(url, headers=None, *args, **kwargs):
444448 self .assertRefreshOn (result , new_refresh_in )
445449
446450
451+ # TODO Patching oidc discovery ends up failing. But we plan to remove offline telemetry anyway.
447452class TestTelemetryMaintainingOfflineState (unittest .TestCase ):
448453 authority_url = "https://login.microsoftonline.com/common"
449454 scopes = ["s1" , "s2" ]
@@ -524,6 +529,7 @@ def mock_post(url, headers=None, *args, **kwargs):
524529
525530class TestTelemetryOnClientApplication (unittest .TestCase ):
526531 @classmethod
532+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
527533 def setUpClass (cls ): # Initialization at runtime, not interpret-time
528534 cls .app = ClientApplication (
529535 "client_id" , authority = "https://login.microsoftonline.com/common" )
@@ -552,6 +558,7 @@ def mock_post(url, headers=None, *args, **kwargs):
552558
553559class TestTelemetryOnPublicClientApplication (unittest .TestCase ):
554560 @classmethod
561+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
555562 def setUpClass (cls ): # Initialization at runtime, not interpret-time
556563 cls .app = PublicClientApplication (
557564 "client_id" , authority = "https://login.microsoftonline.com/common" )
@@ -581,6 +588,7 @@ def mock_post(url, headers=None, *args, **kwargs):
581588
582589class TestTelemetryOnConfidentialClientApplication (unittest .TestCase ):
583590 @classmethod
591+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
584592 def setUpClass (cls ): # Initialization at runtime, not interpret-time
585593 cls .app = ConfidentialClientApplication (
586594 "client_id" , client_credential = "secret" ,
@@ -626,6 +634,7 @@ def mock_post(url, headers=None, *args, **kwargs):
626634 self .assertEqual (at , result .get ("access_token" ))
627635
628636
637+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
629638class TestClientApplicationWillGroupAccounts (unittest .TestCase ):
630639 def test_get_accounts (self ):
631640 client_id = "my_app"
@@ -678,15 +687,24 @@ def mock_post(url, headers=None, *args, **kwargs):
678687 with self .assertWarns (DeprecationWarning ):
679688 app .acquire_token_for_client (["scope" ], post = mock_post )
680689
690+ @patch (_OIDC_DISCOVERY , new = Mock (return_value = {
691+ "authorization_endpoint" : "https://contoso.com/common" ,
692+ "token_endpoint" : "https://contoso.com/common" ,
693+ }))
681694 def test_common_authority_should_emit_warning (self ):
682695 self ._test_certain_authority_should_emit_warning (
683696 authority = "https://login.microsoftonline.com/common" )
684697
698+ @patch (_OIDC_DISCOVERY , new = Mock (return_value = {
699+ "authorization_endpoint" : "https://contoso.com/organizations" ,
700+ "token_endpoint" : "https://contoso.com/organizations" ,
701+ }))
685702 def test_organizations_authority_should_emit_warning (self ):
686703 self ._test_certain_authority_should_emit_warning (
687704 authority = "https://login.microsoftonline.com/organizations" )
688705
689706
707+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
690708class TestRemoveTokensForClient (unittest .TestCase ):
691709 def test_remove_tokens_for_client_should_remove_client_tokens_only (self ):
692710 at_for_user = "AT for user"
@@ -716,6 +734,7 @@ def test_remove_tokens_for_client_should_remove_client_tokens_only(self):
716734 self .assertEqual (at_for_user , remaining_tokens [0 ].get ("secret" ))
717735
718736
737+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
719738class TestScopeDecoration (unittest .TestCase ):
720739 def _test_client_id_should_be_a_valid_scope (self , client_id , other_scopes ):
721740 # B2C needs this https://learn.microsoft.com/en-us/azure/active-directory-b2c/access-tokens#openid-connect-scopes
@@ -855,4 +874,3 @@ def test_app_did_not_register_redirect_uri_should_error_out(self):
855874 parent_window_handle = app .CONSOLE_WINDOW_HANDLE ,
856875 )
857876 self .assertEqual (result .get ("error" ), "broker_error" )
858-
0 commit comments