Skip to content

Commit d076ad8

Browse files
author
Hao Chen
committed
Updated Mock for subscriptionClient and added tests for RMProfile.
1 parent 02aade0 commit d076ad8

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
using Microsoft.Azure.Commands.Common.Authentication.Models;
2828
using Microsoft.Azure.Commands.Profile;
2929
using Microsoft.Azure.Commands.Profile.Models;
30+
using Microsoft.Azure.Subscriptions.Models;
31+
using Hyak.Common;
3032

3133
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
3234
{
@@ -67,6 +69,41 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
6769
return new RMProfileClient(profile);
6870
}
6971

72+
[Fact]
73+
[Trait(Category.AcceptanceType, Category.CheckIn)]
74+
public void SubscriptionIdNotInFirstTenant()
75+
{
76+
var tenants = new List<string> { DefaultTenant.ToString(), Guid.NewGuid().ToString() };
77+
var subscriptionInSecondTenant= Guid.NewGuid().ToString();
78+
var firstList = new List<string> { DefaultSubscription.ToString() };
79+
var secondList = new List<string> { Guid.NewGuid().ToString(), subscriptionInSecondTenant };
80+
var client = SetupTestEnvironment(tenants, firstList, secondList);
81+
82+
((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
83+
new MockAccessToken
84+
{
85+
UserId = "[email protected]",
86+
LoginType = LoginType.OrgId,
87+
AccessToken = "bbb",
88+
TenantId = DefaultTenant.ToString()
89+
};
90+
91+
var getAsyncResponses = new Queue<Func<GetSubscriptionResult>>();
92+
getAsyncResponses.Enqueue(() =>
93+
{
94+
throw new CloudException("InvalidAuthenticationTokenTenant: The access token is from the wrong issuer");
95+
});
96+
MockSubscriptionClientFactory.SetGetAsyncResponses(getAsyncResponses);
97+
98+
var azureRmProfile = client.Login(
99+
Context.Account,
100+
Context.Environment,
101+
null,
102+
subscriptionInSecondTenant,
103+
null,
104+
null);
105+
}
106+
70107
[Fact]
71108
[Trait(Category.AcceptanceType, Category.CheckIn)]
72109
public void TokenIdAndAccountIdMismatch()
@@ -78,7 +115,6 @@ public void TokenIdAndAccountIdMismatch()
78115
var thirdList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
79116
var fourthList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
80117
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);
81-
82118
var tokens = new Queue<MockAccessToken>();
83119
tokens.Enqueue(new MockAccessToken
84120
{

src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class MockSubscriptionClientFactory
2929
private IList<string> _tenants;
3030
private Queue<List<string>> _subscriptions;
3131
private HashSet<string> _subscriptionSet;
32+
private static Queue<Func<GetSubscriptionResult>> _getAsyncQueue;
3233
public MockSubscriptionClientFactory(List<string> tenants, Queue<List<string>> subscriptions)
3334
{
3435
_tenants = tenants;
@@ -49,6 +50,11 @@ public static string GetSubscriptionNameFromId(string id)
4950
return "Sub-" + id;
5051
}
5152

53+
public static void SetGetAsyncResponses(Queue<Func<GetSubscriptionResult>> responses)
54+
{
55+
_getAsyncQueue = responses;
56+
}
57+
5258
public SubscriptionClient GetSubscriptionClient()
5359
{
5460
var tenantMock = new Mock<ITenantOperations>();
@@ -66,6 +72,10 @@ public SubscriptionClient GetSubscriptionClient()
6672
s => s.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())).Returns(
6773
(string subId, CancellationToken token) =>
6874
{
75+
if (_getAsyncQueue != null && _getAsyncQueue.Any())
76+
{
77+
return Task.FromResult(_getAsyncQueue.Dequeue().Invoke());
78+
}
6979
GetSubscriptionResult result = new GetSubscriptionResult
7080
{
7181
RequestId = Guid.NewGuid().ToString(),

0 commit comments

Comments
 (0)