20
20
logger = logging .getLogger (__name__ )
21
21
logging .basicConfig (level = logging .DEBUG )
22
22
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
+
23
29
24
30
class TestHelperExtractCerts (unittest .TestCase ): # It is used by SNI scenario
25
31
@@ -58,10 +64,9 @@ def test_bytes_to_bytes(self):
58
64
59
65
class TestClientApplicationAcquireTokenSilentErrorBehaviors (unittest .TestCase ):
60
66
67
+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
61
68
def setUp (self ):
62
69
self .authority_url = "https://login.microsoftonline.com/common"
63
- self .authority = msal .authority .Authority (
64
- self .authority_url , MinimalHttpClient ())
65
70
self .scopes = ["s1" , "s2" ]
66
71
self .uid = "my_uid"
67
72
self .utid = "my_utid"
@@ -116,12 +121,11 @@ def tester(url, **kwargs):
116
121
self .assertEqual ("" , result .get ("classification" ))
117
122
118
123
124
+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
119
125
class TestClientApplicationAcquireTokenSilentFociBehaviors (unittest .TestCase ):
120
126
121
127
def setUp (self ):
122
128
self .authority_url = "https://login.microsoftonline.com/common"
123
- self .authority = msal .authority .Authority (
124
- self .authority_url , MinimalHttpClient ())
125
129
self .scopes = ["s1" , "s2" ]
126
130
self .uid = "my_uid"
127
131
self .utid = "my_utid"
@@ -148,7 +152,7 @@ def tester(url, data=None, **kwargs):
148
152
self .assertEqual (self .frt , data .get ("refresh_token" ), "Should attempt the FRT" )
149
153
return MinimalResponse (status_code = 400 , text = error_response )
150
154
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 )
152
156
self .assertNotEqual ([], app .token_cache .find (
153
157
msal .TokenCache .CredentialType .REFRESH_TOKEN , query = {"secret" : self .frt }),
154
158
"The FRT should not be removed from the cache" )
@@ -168,7 +172,7 @@ def tester(url, data=None, **kwargs):
168
172
self .assertEqual (rt , data .get ("refresh_token" ), "Should attempt the RT" )
169
173
return MinimalResponse (status_code = 200 , text = '{}' )
170
174
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 )
172
176
173
177
def test_unknown_family_app_will_attempt_frt_and_join_family (self ):
174
178
def tester (url , data = None , ** kwargs ):
@@ -180,7 +184,7 @@ def tester(url, data=None, **kwargs):
180
184
app = ClientApplication (
181
185
"unknown_family_app" , authority = self .authority_url , token_cache = self .cache )
182
186
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 )
184
188
logger .debug ("%s.cache = %s" , self .id (), self .cache .serialize ())
185
189
self .assertEqual ("at" , at .get ("access_token" ), "New app should get a new AT" )
186
190
app_metadata = app .token_cache .find (
@@ -202,7 +206,7 @@ def tester(url, data=None, **kwargs):
202
206
app = ClientApplication (
203
207
"preexisting_family_app" , authority = self .authority_url , token_cache = self .cache )
204
208
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 )
206
210
logger .debug ("%s.cache = %s" , self .id (), self .cache .serialize ())
207
211
self .assertEqual (json .loads (error_response ), resp , "Error raised will be returned" )
208
212
@@ -237,7 +241,7 @@ def test_family_app_remove_account(self):
237
241
238
242
class TestClientApplicationForAuthorityMigration (unittest .TestCase ):
239
243
240
- @ classmethod
244
+ # Chose to not mock oidc discovery, because AuthorityMigration might rely on real data
241
245
def setUp (self ):
242
246
self .environment_in_cache = "sts.windows.net"
243
247
self .authority_url_in_app = "https://login.microsoftonline.com/common"
@@ -444,6 +448,7 @@ def mock_post(url, headers=None, *args, **kwargs):
444
448
self .assertRefreshOn (result , new_refresh_in )
445
449
446
450
451
+ # TODO Patching oidc discovery ends up failing. But we plan to remove offline telemetry anyway.
447
452
class TestTelemetryMaintainingOfflineState (unittest .TestCase ):
448
453
authority_url = "https://login.microsoftonline.com/common"
449
454
scopes = ["s1" , "s2" ]
@@ -524,6 +529,7 @@ def mock_post(url, headers=None, *args, **kwargs):
524
529
525
530
class TestTelemetryOnClientApplication (unittest .TestCase ):
526
531
@classmethod
532
+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
527
533
def setUpClass (cls ): # Initialization at runtime, not interpret-time
528
534
cls .app = ClientApplication (
529
535
"client_id" , authority = "https://login.microsoftonline.com/common" )
@@ -552,6 +558,7 @@ def mock_post(url, headers=None, *args, **kwargs):
552
558
553
559
class TestTelemetryOnPublicClientApplication (unittest .TestCase ):
554
560
@classmethod
561
+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
555
562
def setUpClass (cls ): # Initialization at runtime, not interpret-time
556
563
cls .app = PublicClientApplication (
557
564
"client_id" , authority = "https://login.microsoftonline.com/common" )
@@ -581,6 +588,7 @@ def mock_post(url, headers=None, *args, **kwargs):
581
588
582
589
class TestTelemetryOnConfidentialClientApplication (unittest .TestCase ):
583
590
@classmethod
591
+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
584
592
def setUpClass (cls ): # Initialization at runtime, not interpret-time
585
593
cls .app = ConfidentialClientApplication (
586
594
"client_id" , client_credential = "secret" ,
@@ -626,6 +634,7 @@ def mock_post(url, headers=None, *args, **kwargs):
626
634
self .assertEqual (at , result .get ("access_token" ))
627
635
628
636
637
+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
629
638
class TestClientApplicationWillGroupAccounts (unittest .TestCase ):
630
639
def test_get_accounts (self ):
631
640
client_id = "my_app"
@@ -678,15 +687,24 @@ def mock_post(url, headers=None, *args, **kwargs):
678
687
with self .assertWarns (DeprecationWarning ):
679
688
app .acquire_token_for_client (["scope" ], post = mock_post )
680
689
690
+ @patch (_OIDC_DISCOVERY , new = Mock (return_value = {
691
+ "authorization_endpoint" : "https://contoso.com/common" ,
692
+ "token_endpoint" : "https://contoso.com/common" ,
693
+ }))
681
694
def test_common_authority_should_emit_warning (self ):
682
695
self ._test_certain_authority_should_emit_warning (
683
696
authority = "https://login.microsoftonline.com/common" )
684
697
698
+ @patch (_OIDC_DISCOVERY , new = Mock (return_value = {
699
+ "authorization_endpoint" : "https://contoso.com/organizations" ,
700
+ "token_endpoint" : "https://contoso.com/organizations" ,
701
+ }))
685
702
def test_organizations_authority_should_emit_warning (self ):
686
703
self ._test_certain_authority_should_emit_warning (
687
704
authority = "https://login.microsoftonline.com/organizations" )
688
705
689
706
707
+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
690
708
class TestRemoveTokensForClient (unittest .TestCase ):
691
709
def test_remove_tokens_for_client_should_remove_client_tokens_only (self ):
692
710
at_for_user = "AT for user"
@@ -716,6 +734,7 @@ def test_remove_tokens_for_client_should_remove_client_tokens_only(self):
716
734
self .assertEqual (at_for_user , remaining_tokens [0 ].get ("secret" ))
717
735
718
736
737
+ @patch (_OIDC_DISCOVERY , new = _OIDC_DISCOVERY_MOCK )
719
738
class TestScopeDecoration (unittest .TestCase ):
720
739
def _test_client_id_should_be_a_valid_scope (self , client_id , other_scopes ):
721
740
# 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):
855
874
parent_window_handle = app .CONSOLE_WINDOW_HANDLE ,
856
875
)
857
876
self .assertEqual (result .get ("error" ), "broker_error" )
858
-
0 commit comments