From f3ad5a51f2d5dfce690b68de26c01ab8a6a99c0d Mon Sep 17 00:00:00 2001 From: "Mingzhe Huang (from Dev Box)" Date: Wed, 13 Nov 2024 17:04:58 +0800 Subject: [PATCH] test(resourcemanager): mock test for cae support add a mock test case to verify mgmt SDKs should support CAE resolve #47137 --- .../tests/Unit/HttpPipelineTests.cs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 sdk/resourcemanager/Azure.ResourceManager/tests/Unit/HttpPipelineTests.cs diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Unit/HttpPipelineTests.cs b/sdk/resourcemanager/Azure.ResourceManager/tests/Unit/HttpPipelineTests.cs new file mode 100644 index 0000000000000..6842cdfd8b721 --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager/tests/Unit/HttpPipelineTests.cs @@ -0,0 +1,52 @@ +using Azure.Core.TestFramework; +using NUnit.Framework; + +namespace Azure.ResourceManager.Tests.Unit +{ + [Parallelizable] + public class HttpPipelineTests + { + [Test] + public void CaeSupport() + { + int callCount = 0; + var option = new ArmClientOptions() + { + // we mock a CAE challenge before the actual response is returned + Transport = new MockTransport((r) => + { + callCount++; + + if (callCount == 1) + { + return new MockResponse(401).WithHeader("WWW-Authenticate", CaeChallenge); + } + var response = new MockResponse(200); + response.SetContent(SubscriptionData); + return response; + }) + }; + var client = new ArmClient(new MockCredential(), "83aa47df-e3e9-49ff-877b-94304bf3d3ad", option); + var subscription = client.GetDefaultSubscription(); + Assert.AreEqual(2, callCount); + Assert.AreEqual("83aa47df-e3e9-49ff-877b-94304bf3d3ad", subscription.Data.Id.SubscriptionId); + Assert.AreEqual("Subscription2", subscription.Data.DisplayName); + Assert.IsEmpty(subscription.Data.Tags); + } + + private const string CaeChallenge = """PoP realm="", authorization_uri="https://login.microsoftonline.com/common/oauth2/authorize", client_id="00000003-0000-0000-c000-000000000000", nonce="ey==", Bearer realm="", authorization_uri="https://login.microsoftonline.com/common/oauth2/authorize", client_id="00000003-0000-0000-c000-000000000000", error_description="Continuous access evaluation resulted in challenge with result: InteractionRequired and code: TokenIssuedBeforeRevocationTimestamp", error="insufficient_claims", claims="eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTcyNjI1ODEyMiJ9fX0=" """; + + private const string SubscriptionData = @"{ + ""id"": ""/subscriptions/83aa47df-e3e9-49ff-877b-94304bf3d3ad"", + ""authorizationSource"": ""Legacy"", + ""subscriptionId"": ""83aa47df-e3e9-49ff-877b-94304bf3d3ad"", + ""displayName"": ""Subscription2"", + ""state"": ""Enabled"", + ""subscriptionPolicies"": { + ""locationPlacementId"": ""Internal_2014-09-01"", + ""quotaId"": ""Internal_2014-09-01"", + ""spendingLimit"": ""Off"" + } +}"; + } +}