Skip to content

Commit 4f5b030

Browse files
author
Hao Chen
committed
Fixed RMProfileClient login bug and added more unit tests for RMProfileClient.
1 parent 18c5d8e commit 4f5b030

File tree

3 files changed

+110
-7
lines changed

3 files changed

+110
-7
lines changed

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

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using Microsoft.Azure.Commands.Profile.Models;
3030
using Microsoft.Azure.Subscriptions.Models;
3131
using Hyak.Common;
32+
using System.Management.Automation;
3233

3334
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
3435
{
@@ -69,6 +70,93 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
6970
return new RMProfileClient(profile);
7071
}
7172

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+
72160
[Fact]
73161
[Trait(Category.AcceptanceType, Category.CheckIn)]
74162
public void SubscriptionIdNotInFirstTenant()
@@ -126,16 +214,23 @@ public void SubscriptionNameNotInFirstTenant()
126214
var listAsyncResponses = new Queue<Func<SubscriptionListResult>>();
127215
listAsyncResponses.Enqueue(() =>
128216
{
129-
var sub = new Subscription
217+
var sub1 = new Subscription
130218
{
131219
Id = DefaultSubscription.ToString(),
132220
SubscriptionId = DefaultSubscription.ToString(),
133221
DisplayName = DefaultSubscriptionName,
134-
State = "enabled",
222+
State = "enabled"
223+
};
224+
var sub2 = new Subscription
225+
{
226+
Id = subscriptionInSecondTenant,
227+
SubscriptionId = subscriptionInSecondTenant,
228+
DisplayName = MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
229+
State = "enabled"
135230
};
136231
return new SubscriptionListResult
137232
{
138-
Subscriptions = new List<Subscription> { sub }
233+
Subscriptions = new List<Subscription> { sub1, sub2 }
139234
};
140235
});
141236
MockSubscriptionClientFactory.SetListAsyncResponses(listAsyncResponses);
@@ -145,7 +240,7 @@ public void SubscriptionNameNotInFirstTenant()
145240
Context.Environment,
146241
null,
147242
null,
148-
"sub name",
243+
MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
149244
null);
150245
}
151246

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public SubscriptionClient GetSubscriptionClient()
129129
{
130130
DisplayName = GetSubscriptionNameFromId(sub),
131131
Id = sub,
132-
State = "Active",
132+
State = "enabled",
133133
SubscriptionId = sub
134134
}))
135135
};

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,16 @@ 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 (tempSubscription == null && i + 1 < tenants.Count())
125+
{
126+
// No subscription found for the given token/tenant.
127+
// Discard tempTenant value unless current token/tenant is the last one.
128+
}
129+
else
130+
{
131+
newTenant = tempTenant;
132+
newSubscription = tempSubscription;
133+
}
126134
}
127135
}
128136
}

0 commit comments

Comments
 (0)