@@ -791,16 +791,15 @@ class WorldWideRegionalEndpointTestCase(LabBasedTestCase):
791
791
region = "westus"
792
792
timeout = 2 # Short timeout makes this test case responsive on non-VM
793
793
794
- def test_acquire_token_for_client_should_hit_regional_endpoint (self ):
794
+ def _test_acquire_token_for_client (self , configured_region , expected_region ):
795
795
"""This is the only grant supported by regional endpoint, for now"""
796
796
self .app = get_lab_app ( # Regional endpoint only supports confidential client
797
797
798
798
## FWIW, the MSAL<1.12 versions could use this to achieve similar result
799
799
#authority="https://westus.login.microsoft.com/microsoft.onmicrosoft.com",
800
800
#validate_authority=False,
801
801
authority = "https://login.microsoftonline.com/microsoft.onmicrosoft.com" ,
802
- azure_region = self .region , # Explicitly use this region, regardless of detection
803
-
802
+ azure_region = configured_region ,
804
803
timeout = 2 , # Short timeout makes this test case responsive on non-VM
805
804
)
806
805
scopes = ["https://graph.microsoft.com/.default" ]
@@ -809,9 +808,11 @@ def test_acquire_token_for_client_should_hit_regional_endpoint(self):
809
808
self .app .http_client , "post" , return_value = MinimalResponse (
810
809
status_code = 400 , text = '{"error": "mock"}' )) as mocked_method :
811
810
self .app .acquire_token_for_client (scopes )
811
+ expected_host = '{}.r.login.microsoftonline.com' .format (
812
+ expected_region ) if expected_region else 'login.microsoftonline.com'
812
813
mocked_method .assert_called_with (
813
- 'https://westus.r.login.microsoftonline.com /{}/oauth2/v2.0/token' .format (
814
- self .app .authority .tenant ),
814
+ 'https://{} /{}/oauth2/v2.0/token' .format (
815
+ expected_host , self .app .authority .tenant ),
815
816
params = ANY , data = ANY , headers = ANY )
816
817
result = self .app .acquire_token_for_client (
817
818
scopes ,
@@ -820,6 +821,29 @@ def test_acquire_token_for_client_should_hit_regional_endpoint(self):
820
821
self .assertIn ('access_token' , result )
821
822
self .assertCacheWorksForApp (result , scopes )
822
823
824
+ def test_acquire_token_for_client_should_hit_global_endpoint_by_default (self ):
825
+ self ._test_acquire_token_for_client (None , None )
826
+
827
+ def test_acquire_token_for_client_should_ignore_env_var_by_default (self ):
828
+ os .environ ["REGION_NAME" ] = "eastus"
829
+ self ._test_acquire_token_for_client (None , None )
830
+ del os .environ ["REGION_NAME" ]
831
+
832
+ def test_acquire_token_for_client_should_use_a_specified_region (self ):
833
+ self ._test_acquire_token_for_client ("westus" , "westus" )
834
+
835
+ def test_acquire_token_for_client_should_use_an_env_var_with_short_region_name (self ):
836
+ os .environ ["REGION_NAME" ] = "eastus"
837
+ self ._test_acquire_token_for_client (
838
+ msal .ConfidentialClientApplication .ATTEMPT_REGION_DISCOVERY , "eastus" )
839
+ del os .environ ["REGION_NAME" ]
840
+
841
+ def test_acquire_token_for_client_should_use_an_env_var_with_long_region_name (self ):
842
+ os .environ ["REGION_NAME" ] = "East Us 2"
843
+ self ._test_acquire_token_for_client (
844
+ msal .ConfidentialClientApplication .ATTEMPT_REGION_DISCOVERY , "eastus2" )
845
+ del os .environ ["REGION_NAME" ]
846
+
823
847
@unittest .skipUnless (
824
848
os .getenv ("LAB_OBO_CLIENT_SECRET" ),
825
849
"Need LAB_OBO_CLIENT_SECRET from https://aka.ms/GetLabSecret?Secret=TodoListServiceV2-OBO" )
0 commit comments