@@ -791,16 +791,15 @@ class WorldWideRegionalEndpointTestCase(LabBasedTestCase):
791791 region = "westus"
792792 timeout = 2 # Short timeout makes this test case responsive on non-VM
793793
794- def test_acquire_token_for_client_should_hit_regional_endpoint (self ):
794+ def _test_acquire_token_for_client (self , configured_region , expected_region ):
795795 """This is the only grant supported by regional endpoint, for now"""
796796 self .app = get_lab_app ( # Regional endpoint only supports confidential client
797797
798798 ## FWIW, the MSAL<1.12 versions could use this to achieve similar result
799799 #authority="https://westus.login.microsoft.com/microsoft.onmicrosoft.com",
800800 #validate_authority=False,
801801 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 ,
804803 timeout = 2 , # Short timeout makes this test case responsive on non-VM
805804 )
806805 scopes = ["https://graph.microsoft.com/.default" ]
@@ -809,9 +808,11 @@ def test_acquire_token_for_client_should_hit_regional_endpoint(self):
809808 self .app .http_client , "post" , return_value = MinimalResponse (
810809 status_code = 400 , text = '{"error": "mock"}' )) as mocked_method :
811810 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'
812813 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 ),
815816 params = ANY , data = ANY , headers = ANY )
816817 result = self .app .acquire_token_for_client (
817818 scopes ,
@@ -820,6 +821,29 @@ def test_acquire_token_for_client_should_hit_regional_endpoint(self):
820821 self .assertIn ('access_token' , result )
821822 self .assertCacheWorksForApp (result , scopes )
822823
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+
823847 @unittest .skipUnless (
824848 os .getenv ("LAB_OBO_CLIENT_SECRET" ),
825849 "Need LAB_OBO_CLIENT_SECRET from https://aka.ms/GetLabSecret?Secret=TodoListServiceV2-OBO" )
0 commit comments