Skip to content

Commit 858fdaf

Browse files
committed
Merge pull request #178 from Azure/dev
.
2 parents 2d49009 + f330472 commit 858fdaf

File tree

3 files changed

+206
-4
lines changed

3 files changed

+206
-4
lines changed

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

Lines changed: 177 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
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;
32+
using System.Management.Automation;
3033

3134
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
3235
{
@@ -67,6 +70,180 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
6770
return new RMProfileClient(profile);
6871
}
6972

73+
[Fact]
74+
[Trait(Category.AcceptanceType, Category.CheckIn)]
75+
public void SpecifyTenantAndSubscriptionIdSucceed()
76+
{
77+
var tenants = new List<string> { DefaultTenant.ToString() };
78+
var firstList = new List<string> { DefaultSubscription.ToString(), Guid.NewGuid().ToString() };
79+
var secondList = new List<string> { Guid.NewGuid().ToString() };
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 azureRmProfile = client.Login(
92+
Context.Account,
93+
Context.Environment,
94+
DefaultTenant.ToString(),
95+
DefaultSubscription.ToString(),
96+
null,
97+
null);
98+
}
99+
100+
[Fact]
101+
[Trait(Category.AcceptanceType, Category.CheckIn)]
102+
public void SubscriptionIdNotExist()
103+
{
104+
var tenants = new List<string> { DefaultTenant.ToString() };
105+
var firstList = new List<string> { Guid.NewGuid().ToString() };
106+
var client = SetupTestEnvironment(tenants, firstList);
107+
108+
((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
109+
new MockAccessToken
110+
{
111+
UserId = "[email protected]",
112+
LoginType = LoginType.OrgId,
113+
AccessToken = "bbb",
114+
TenantId = DefaultTenant.ToString()
115+
};
116+
117+
var getAsyncResponses = new Queue<Func<GetSubscriptionResult>>();
118+
getAsyncResponses.Enqueue(() =>
119+
{
120+
throw new CloudException("InvalidAuthenticationTokenTenant: The access token is from the wrong issuer");
121+
});
122+
MockSubscriptionClientFactory.SetGetAsyncResponses(getAsyncResponses);
123+
124+
Assert.Throws<PSInvalidOperationException>( () => client.Login(
125+
Context.Account,
126+
Context.Environment,
127+
null,
128+
DefaultSubscription.ToString(),
129+
null,
130+
null));
131+
}
132+
133+
[Fact]
134+
[Trait(Category.AcceptanceType, Category.CheckIn)]
135+
public void SpecifyTenantAndNotExistingSubscriptionId()
136+
{
137+
var tenants = new List<string> { DefaultTenant.ToString() };
138+
var firstList = new List<string> { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() };
139+
var secondList = new List<string> { Guid.NewGuid().ToString() };
140+
var client = SetupTestEnvironment(tenants, firstList, secondList);
141+
142+
((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
143+
new MockAccessToken
144+
{
145+
UserId = "[email protected]",
146+
LoginType = LoginType.OrgId,
147+
AccessToken = "bbb",
148+
TenantId = DefaultTenant.ToString()
149+
};
150+
151+
Assert.Throws<PSInvalidOperationException>( () => client.Login(
152+
Context.Account,
153+
Context.Environment,
154+
DefaultTenant.ToString(),
155+
DefaultSubscription.ToString(),
156+
null,
157+
null));
158+
}
159+
160+
[Fact]
161+
[Trait(Category.AcceptanceType, Category.CheckIn)]
162+
public void SubscriptionIdNotInFirstTenant()
163+
{
164+
var tenants = new List<string> { DefaultTenant.ToString(), Guid.NewGuid().ToString() };
165+
var subscriptionInSecondTenant= Guid.NewGuid().ToString();
166+
var firstList = new List<string> { DefaultSubscription.ToString() };
167+
var secondList = new List<string> { Guid.NewGuid().ToString(), subscriptionInSecondTenant };
168+
var client = SetupTestEnvironment(tenants, firstList, secondList);
169+
170+
((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
171+
new MockAccessToken
172+
{
173+
UserId = "[email protected]",
174+
LoginType = LoginType.OrgId,
175+
AccessToken = "bbb",
176+
TenantId = DefaultTenant.ToString()
177+
};
178+
179+
var getAsyncResponses = new Queue<Func<GetSubscriptionResult>>();
180+
getAsyncResponses.Enqueue(() =>
181+
{
182+
throw new CloudException("InvalidAuthenticationTokenTenant: The access token is from the wrong issuer");
183+
});
184+
MockSubscriptionClientFactory.SetGetAsyncResponses(getAsyncResponses);
185+
186+
var azureRmProfile = client.Login(
187+
Context.Account,
188+
Context.Environment,
189+
null,
190+
subscriptionInSecondTenant,
191+
null,
192+
null);
193+
}
194+
195+
[Fact]
196+
[Trait(Category.AcceptanceType, Category.CheckIn)]
197+
public void SubscriptionNameNotInFirstTenant()
198+
{
199+
var tenants = new List<string> { DefaultTenant.ToString(), Guid.NewGuid().ToString() };
200+
var subscriptionInSecondTenant= Guid.NewGuid().ToString();
201+
var firstList = new List<string> { DefaultSubscription.ToString() };
202+
var secondList = new List<string> { Guid.NewGuid().ToString(), subscriptionInSecondTenant };
203+
var client = SetupTestEnvironment(tenants, firstList, secondList);
204+
205+
((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
206+
new MockAccessToken
207+
{
208+
UserId = "[email protected]",
209+
LoginType = LoginType.OrgId,
210+
AccessToken = "bbb",
211+
TenantId = DefaultTenant.ToString()
212+
};
213+
214+
var listAsyncResponses = new Queue<Func<SubscriptionListResult>>();
215+
listAsyncResponses.Enqueue(() =>
216+
{
217+
var sub1 = new Subscription
218+
{
219+
Id = DefaultSubscription.ToString(),
220+
SubscriptionId = DefaultSubscription.ToString(),
221+
DisplayName = DefaultSubscriptionName,
222+
State = "enabled"
223+
};
224+
var sub2 = new Subscription
225+
{
226+
Id = subscriptionInSecondTenant,
227+
SubscriptionId = subscriptionInSecondTenant,
228+
DisplayName = MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
229+
State = "enabled"
230+
};
231+
return new SubscriptionListResult
232+
{
233+
Subscriptions = new List<Subscription> { sub1, sub2 }
234+
};
235+
});
236+
MockSubscriptionClientFactory.SetListAsyncResponses(listAsyncResponses);
237+
238+
var azureRmProfile = client.Login(
239+
Context.Account,
240+
Context.Environment,
241+
null,
242+
null,
243+
MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
244+
null);
245+
}
246+
70247
[Fact]
71248
[Trait(Category.AcceptanceType, Category.CheckIn)]
72249
public void TokenIdAndAccountIdMismatch()
@@ -78,7 +255,6 @@ public void TokenIdAndAccountIdMismatch()
78255
var thirdList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
79256
var fourthList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
80257
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);
81-
82258
var tokens = new Queue<MockAccessToken>();
83259
tokens.Enqueue(new MockAccessToken
84260
{

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ 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;
33+
private static Queue<Func<SubscriptionListResult>> _listAsyncQueue;
34+
3235
public MockSubscriptionClientFactory(List<string> tenants, Queue<List<string>> subscriptions)
3336
{
3437
_tenants = tenants;
@@ -49,6 +52,15 @@ public static string GetSubscriptionNameFromId(string id)
4952
return "Sub-" + id;
5053
}
5154

55+
public static void SetGetAsyncResponses(Queue<Func<GetSubscriptionResult>> responses)
56+
{
57+
_getAsyncQueue = responses;
58+
}
59+
public static void SetListAsyncResponses(Queue<Func<SubscriptionListResult>> responses)
60+
{
61+
_listAsyncQueue = responses;
62+
}
63+
5264
public SubscriptionClient GetSubscriptionClient()
5365
{
5466
var tenantMock = new Mock<ITenantOperations>();
@@ -66,6 +78,10 @@ public SubscriptionClient GetSubscriptionClient()
6678
s => s.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())).Returns(
6779
(string subId, CancellationToken token) =>
6880
{
81+
if (_getAsyncQueue != null && _getAsyncQueue.Any())
82+
{
83+
return Task.FromResult(_getAsyncQueue.Dequeue().Invoke());
84+
}
6985
GetSubscriptionResult result = new GetSubscriptionResult
7086
{
7187
RequestId = Guid.NewGuid().ToString(),
@@ -91,6 +107,11 @@ public SubscriptionClient GetSubscriptionClient()
91107
(s) => s.ListAsync(It.IsAny<CancellationToken>())).Returns(
92108
(CancellationToken token) =>
93109
{
110+
if (_listAsyncQueue != null && _listAsyncQueue.Any())
111+
{
112+
return Task.FromResult(_listAsyncQueue.Dequeue().Invoke());
113+
}
114+
94115
SubscriptionListResult result = null;
95116
if (_subscriptions.Count > 0)
96117
{
@@ -108,7 +129,7 @@ public SubscriptionClient GetSubscriptionClient()
108129
{
109130
DisplayName = GetSubscriptionNameFromId(sub),
110131
Id = sub,
111-
State = "Active",
132+
State = "enabled",
112133
SubscriptionId = sub
113134
}))
114135
};

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ public AzureRMProfile Login(
121121
newTenant == null &&
122122
TryGetTenantSubscription(token, account, environment, tenant, subscriptionId, subscriptionName, out tempSubscription, out tempTenant))
123123
{
124-
newTenant = tempTenant;
125-
newSubscription = tempSubscription;
124+
// If no subscription found for the given token/tenant
125+
// discard tempTenant value unless current token/tenant is the last one.
126+
if (tempSubscription != null || i == (tenants.Count() -1))
127+
{
128+
newTenant = tempTenant;
129+
newSubscription = tempSubscription;
130+
}
126131
}
127132
}
128133
}

0 commit comments

Comments
 (0)