-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(resourcemanager): mock test for cae support (#47138)
* add a mock test case to verify mgmt SDKs should support CAE * add invalid cae response case resolve #47137 --------- Co-authored-by: Mingzhe Huang (from Dev Box) <[email protected]>
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
sdk/resourcemanager/Azure.ResourceManager/tests/Unit/HttpPipelineTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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); | ||
} | ||
|
||
[Test] | ||
public void InvalidCaeResponse() | ||
{ | ||
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", InvalidCaeChallenge); | ||
} | ||
var response = new MockResponse(200); | ||
response.SetContent(SubscriptionData); | ||
return response; | ||
}) | ||
}; | ||
var client = new ArmClient(new MockCredential(), "83aa47df-e3e9-49ff-877b-94304bf3d3ad", option); | ||
Assert.Throws<RequestFailedException>(() => client.GetDefaultSubscription()); | ||
Assert.AreEqual(1, callCount); | ||
} | ||
|
||
private const string CaeChallenge = """Bearer realm="", error_description="Continuous access evaluation resulted in challenge", error="insufficient_claims", claims="eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTcyNjI1ODEyMiJ9fX0=" """; | ||
|
||
private const string InvalidCaeChallenge = """Bearer realm="", error_description="", error="insufficient_claims", claims="" """; | ||
|
||
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"" | ||
} | ||
}"; | ||
} | ||
} |