Skip to content

Commit 96140b0

Browse files
committed
Regional endpoint test cases do not rely on env var REGION_NAME
1 parent 71802d0 commit 96140b0

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

tests/test_e2e.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,15 @@ def assertCacheWorksForApp(self, result_from_wire, scope):
137137
def _test_username_password(self,
138138
authority=None, client_id=None, username=None, password=None, scope=None,
139139
client_secret=None, # Since MSAL 1.11, confidential client has ROPC too
140+
azure_region=None,
141+
http_client=None,
140142
**ignored):
141143
assert authority and client_id and username and password and scope
142144
self.app = msal.ClientApplication(
143-
client_id, authority=authority, http_client=MinimalHttpClient(),
145+
client_id, authority=authority,
146+
http_client=http_client or MinimalHttpClient(),
147+
azure_region=azure_region, # Regional endpoint does not support ROPC.
148+
# Here we just use it to test a regional app won't break ROPC.
144149
client_credential=client_secret)
145150
result = self.app.acquire_token_by_username_password(
146151
username, password, scopes=scope)
@@ -541,11 +546,16 @@ def _test_acquire_token_by_auth_code_flow(
541546
error_description=result.get("error_description")))
542547
self.assertCacheWorksForUser(result, scope, username=None)
543548

544-
def _test_acquire_token_obo(self, config_pca, config_cca):
549+
def _test_acquire_token_obo(self, config_pca, config_cca,
550+
azure_region=None, # Regional endpoint does not really support OBO.
551+
# Here we just test regional apps won't adversely break OBO
552+
http_client=None,
553+
):
545554
# 1. An app obtains a token representing a user, for our mid-tier service
546555
pca = msal.PublicClientApplication(
547556
config_pca["client_id"], authority=config_pca["authority"],
548-
http_client=MinimalHttpClient())
557+
azure_region=azure_region,
558+
http_client=http_client or MinimalHttpClient())
549559
pca_result = pca.acquire_token_by_username_password(
550560
config_pca["username"],
551561
config_pca["password"],
@@ -560,7 +570,8 @@ def _test_acquire_token_obo(self, config_pca, config_cca):
560570
config_cca["client_id"],
561571
client_credential=config_cca["client_secret"],
562572
authority=config_cca["authority"],
563-
http_client=MinimalHttpClient(),
573+
azure_region=azure_region,
574+
http_client=http_client or MinimalHttpClient(),
564575
# token_cache= ..., # Default token cache is all-tokens-store-in-memory.
565576
# That's fine if OBO app uses short-lived msal instance per session.
566577
# Otherwise, the OBO app need to implement a one-cache-per-user setup.
@@ -778,6 +789,7 @@ def test_b2c_acquire_token_by_ropc(self):
778789

779790
class WorldWideRegionalEndpointTestCase(LabBasedTestCase):
780791
region = "westus"
792+
timeout = 2 # Short timeout makes this test case responsive on non-VM
781793

782794
def test_acquire_token_for_client_should_hit_regional_endpoint(self):
783795
"""This is the only grant supported by regional endpoint, for now"""
@@ -808,15 +820,6 @@ def test_acquire_token_for_client_should_hit_regional_endpoint(self):
808820
self.assertIn('access_token', result)
809821
self.assertCacheWorksForApp(result, scopes)
810822

811-
812-
class RegionalEndpointViaEnvVarTestCase(WorldWideRegionalEndpointTestCase):
813-
814-
def setUp(self):
815-
os.environ["REGION_NAME"] = "eastus"
816-
817-
def tearDown(self):
818-
del os.environ["REGION_NAME"]
819-
820823
@unittest.skipUnless(
821824
os.getenv("LAB_OBO_CLIENT_SECRET"),
822825
"Need LAB_OBO_CLIENT_SECRET from https://aka.ms/GetLabSecret?Secret=TodoListServiceV2-OBO")
@@ -842,7 +845,11 @@ def test_cca_obo_should_bypass_regional_endpoint_therefore_still_work(self):
842845
config_pca["password"] = self.get_lab_user_secret(config_pca["lab_name"])
843846
config_pca["scope"] = ["api://%s/read" % config_cca["client_id"]]
844847

845-
self._test_acquire_token_obo(config_pca, config_cca)
848+
self._test_acquire_token_obo(
849+
config_pca, config_cca,
850+
azure_region=self.region,
851+
http_client=MinimalHttpClient(timeout=self.timeout),
852+
)
846853

847854
@unittest.skipUnless(
848855
os.getenv("LAB_OBO_CLIENT_SECRET"),
@@ -859,7 +866,10 @@ def test_cca_ropc_should_bypass_regional_endpoint_therefore_still_work(self):
859866
config["client_id"] = os.getenv("LAB_OBO_CONFIDENTIAL_CLIENT_ID")
860867
config["scope"] = ["https://graph.microsoft.com/.default"]
861868
config["client_secret"] = os.getenv("LAB_OBO_CLIENT_SECRET")
862-
self._test_username_password(**config)
869+
self._test_username_password(
870+
azure_region=self.region,
871+
http_client=MinimalHttpClient(timeout=self.timeout),
872+
**config)
863873

864874

865875
class ArlingtonCloudTestCase(LabBasedTestCase):

0 commit comments

Comments
 (0)