|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -#if !ANDROID && !iOS |
5 | 4 | using System; |
6 | 5 | using System.IO; |
7 | 6 | using System.Net; |
@@ -304,6 +303,99 @@ public async Task RegionFallbackToGlobalAsync() |
304 | 303 | } |
305 | 304 | } |
306 | 305 |
|
| 306 | + [TestMethod] |
| 307 | + public async Task MsalForceRegionIsSet_RegionIsUsed() |
| 308 | + { |
| 309 | + using (new EnvVariableContext()) |
| 310 | + using (var httpManager = new MockHttpManager()) |
| 311 | + { |
| 312 | + Environment.SetEnvironmentVariable( |
| 313 | + ConfidentialClientApplicationBuilder.ForceRegionEnvVariable, TestConstants.Region); |
| 314 | + |
| 315 | + httpManager.AddRegionDiscoveryMockHandler(TestConstants.Region); |
| 316 | + httpManager.AddMockHandler(CreateTokenResponseHttpHandler(expectRegional: true)); |
| 317 | + |
| 318 | + var cca = ConfidentialClientApplicationBuilder |
| 319 | + .Create(TestConstants.ClientId) |
| 320 | + .WithHttpManager(httpManager) |
| 321 | + .WithClientSecret(TestConstants.ClientSecret) |
| 322 | + .Build(); |
| 323 | + |
| 324 | + AuthenticationResult result = await cca |
| 325 | + .AcquireTokenForClient(TestConstants.s_scope) |
| 326 | + .ExecuteAsync() |
| 327 | + .ConfigureAwait(false); |
| 328 | + |
| 329 | + Assert.AreEqual(TestConstants.Region, result.ApiEvent.RegionUsed); |
| 330 | + Assert.AreEqual(RegionAutodetectionSource.Imds, result.ApiEvent.RegionAutodetectionSource); |
| 331 | + |
| 332 | + Assert.AreEqual(TestConstants.Region, result.AuthenticationResultMetadata.RegionDetails.RegionUsed); |
| 333 | + Assert.AreEqual(RegionOutcome.UserProvidedValid, result.AuthenticationResultMetadata.RegionDetails.RegionOutcome); |
| 334 | + Assert.IsNull(result.AuthenticationResultMetadata.RegionDetails.AutoDetectionError); |
| 335 | + } |
| 336 | + } |
| 337 | + |
| 338 | + [TestMethod] |
| 339 | + public async Task MsalForceRegionIsSet_WithRegionIsSet_WithRegionWins() |
| 340 | + { |
| 341 | + using (new EnvVariableContext()) |
| 342 | + using (var httpManager = new MockHttpManager()) |
| 343 | + { |
| 344 | + // this will be ignored, in favor of TestConstants.Region |
| 345 | + Environment.SetEnvironmentVariable( |
| 346 | + ConfidentialClientApplicationBuilder.ForceRegionEnvVariable, "someOtherRegion"); |
| 347 | + |
| 348 | + httpManager.AddRegionDiscoveryMockHandler(TestConstants.Region); |
| 349 | + httpManager.AddMockHandler(CreateTokenResponseHttpHandler(expectRegional: true)); |
| 350 | + |
| 351 | + var cca = ConfidentialClientApplicationBuilder |
| 352 | + .Create(TestConstants.ClientId) |
| 353 | + .WithHttpManager(httpManager) |
| 354 | + .WithAzureRegion(TestConstants.Region) |
| 355 | + .WithClientSecret(TestConstants.ClientSecret) |
| 356 | + .Build(); |
| 357 | + |
| 358 | + AuthenticationResult result = await cca |
| 359 | + .AcquireTokenForClient(TestConstants.s_scope) |
| 360 | + .ExecuteAsync() |
| 361 | + .ConfigureAwait(false); |
| 362 | + |
| 363 | + Assert.AreEqual(TestConstants.Region, result.ApiEvent.RegionUsed); |
| 364 | + Assert.AreEqual(RegionAutodetectionSource.Imds, result.ApiEvent.RegionAutodetectionSource); |
| 365 | + |
| 366 | + Assert.AreEqual(TestConstants.Region, result.AuthenticationResultMetadata.RegionDetails.RegionUsed); |
| 367 | + Assert.AreEqual(RegionOutcome.UserProvidedValid, result.AuthenticationResultMetadata.RegionDetails.RegionOutcome); |
| 368 | + Assert.IsNull(result.AuthenticationResultMetadata.RegionDetails.AutoDetectionError); |
| 369 | + } |
| 370 | + } |
| 371 | + |
| 372 | + [TestMethod] |
| 373 | + public async Task MsalForceRegionIsSet_WithRegionIsSetToOptOut_NoRegionIsUsed() |
| 374 | + { |
| 375 | + using (new EnvVariableContext()) |
| 376 | + using (var httpManager = new MockHttpManager()) |
| 377 | + { |
| 378 | + Environment.SetEnvironmentVariable( |
| 379 | + ConfidentialClientApplicationBuilder.ForceRegionEnvVariable, TestConstants.Region); |
| 380 | + |
| 381 | + httpManager.AddInstanceDiscoveryMockHandler(); |
| 382 | + httpManager.AddMockHandler(CreateTokenResponseHttpHandler(expectRegional: false)); |
| 383 | + |
| 384 | + var cca = ConfidentialClientApplicationBuilder |
| 385 | + .Create(TestConstants.ClientId) |
| 386 | + .WithHttpManager(httpManager) |
| 387 | + .WithAzureRegion(ConfidentialClientApplicationBuilder.DisableForceRegion) |
| 388 | + .WithClientSecret(TestConstants.ClientSecret) |
| 389 | + .Build(); |
| 390 | + |
| 391 | + AuthenticationResult result = await cca |
| 392 | + .AcquireTokenForClient(TestConstants.s_scope) |
| 393 | + .ExecuteAsync() |
| 394 | + .ConfigureAwait(false); |
| 395 | + |
| 396 | + Assert.IsNull(result.ApiEvent.RegionUsed); |
| 397 | + } |
| 398 | + } |
307 | 399 | [TestMethod] |
308 | 400 | public void WithAzureRegionThrowsOnNullArg() |
309 | 401 | { |
@@ -724,4 +816,3 @@ private static HttpResponseMessage CreateResponse(bool clientCredentialFlow) |
724 | 816 |
|
725 | 817 | } |
726 | 818 | } |
727 | | -#endif |
|
0 commit comments