Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Commands.Profile;
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.Azure.Subscriptions.Models;
using Hyak.Common;
using System.Management.Automation;

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

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SpecifyTenantAndSubscriptionIdSucceed()
{
var tenants = new List<string> { DefaultTenant.ToString() };
var firstList = new List<string> { DefaultSubscription.ToString(), Guid.NewGuid().ToString() };
var secondList = new List<string> { Guid.NewGuid().ToString() };
var client = SetupTestEnvironment(tenants, firstList, secondList);

((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
new MockAccessToken
{
UserId = "aaa@contoso.com",
LoginType = LoginType.OrgId,
AccessToken = "bbb",
TenantId = DefaultTenant.ToString()
};

var azureRmProfile = client.Login(
Context.Account,
Context.Environment,
DefaultTenant.ToString(),
DefaultSubscription.ToString(),
null,
null);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SubscriptionIdNotExist()
{
var tenants = new List<string> { DefaultTenant.ToString() };
var firstList = new List<string> { Guid.NewGuid().ToString() };
var client = SetupTestEnvironment(tenants, firstList);

((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
new MockAccessToken
{
UserId = "aaa@contoso.com",
LoginType = LoginType.OrgId,
AccessToken = "bbb",
TenantId = DefaultTenant.ToString()
};

var getAsyncResponses = new Queue<Func<GetSubscriptionResult>>();
getAsyncResponses.Enqueue(() =>
{
throw new CloudException("InvalidAuthenticationTokenTenant: The access token is from the wrong issuer");
});
MockSubscriptionClientFactory.SetGetAsyncResponses(getAsyncResponses);

Assert.Throws<PSInvalidOperationException>( () => client.Login(
Context.Account,
Context.Environment,
null,
DefaultSubscription.ToString(),
null,
null));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SpecifyTenantAndNotExistingSubscriptionId()
{
var tenants = new List<string> { DefaultTenant.ToString() };
var firstList = new List<string> { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() };
var secondList = new List<string> { Guid.NewGuid().ToString() };
var client = SetupTestEnvironment(tenants, firstList, secondList);

((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
new MockAccessToken
{
UserId = "aaa@contoso.com",
LoginType = LoginType.OrgId,
AccessToken = "bbb",
TenantId = DefaultTenant.ToString()
};

Assert.Throws<PSInvalidOperationException>( () => client.Login(
Context.Account,
Context.Environment,
DefaultTenant.ToString(),
DefaultSubscription.ToString(),
null,
null));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SubscriptionIdNotInFirstTenant()
{
var tenants = new List<string> { DefaultTenant.ToString(), Guid.NewGuid().ToString() };
var subscriptionInSecondTenant= Guid.NewGuid().ToString();
var firstList = new List<string> { DefaultSubscription.ToString() };
var secondList = new List<string> { Guid.NewGuid().ToString(), subscriptionInSecondTenant };
var client = SetupTestEnvironment(tenants, firstList, secondList);

((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
new MockAccessToken
{
UserId = "aaa@contoso.com",
LoginType = LoginType.OrgId,
AccessToken = "bbb",
TenantId = DefaultTenant.ToString()
};

var getAsyncResponses = new Queue<Func<GetSubscriptionResult>>();
getAsyncResponses.Enqueue(() =>
{
throw new CloudException("InvalidAuthenticationTokenTenant: The access token is from the wrong issuer");
});
MockSubscriptionClientFactory.SetGetAsyncResponses(getAsyncResponses);

var azureRmProfile = client.Login(
Context.Account,
Context.Environment,
null,
subscriptionInSecondTenant,
null,
null);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SubscriptionNameNotInFirstTenant()
{
var tenants = new List<string> { DefaultTenant.ToString(), Guid.NewGuid().ToString() };
var subscriptionInSecondTenant= Guid.NewGuid().ToString();
var firstList = new List<string> { DefaultSubscription.ToString() };
var secondList = new List<string> { Guid.NewGuid().ToString(), subscriptionInSecondTenant };
var client = SetupTestEnvironment(tenants, firstList, secondList);

((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
new MockAccessToken
{
UserId = "aaa@contoso.com",
LoginType = LoginType.OrgId,
AccessToken = "bbb",
TenantId = DefaultTenant.ToString()
};

var listAsyncResponses = new Queue<Func<SubscriptionListResult>>();
listAsyncResponses.Enqueue(() =>
{
var sub1 = new Subscription
{
Id = DefaultSubscription.ToString(),
SubscriptionId = DefaultSubscription.ToString(),
DisplayName = DefaultSubscriptionName,
State = "enabled"
};
var sub2 = new Subscription
{
Id = subscriptionInSecondTenant,
SubscriptionId = subscriptionInSecondTenant,
DisplayName = MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
State = "enabled"
};
return new SubscriptionListResult
{
Subscriptions = new List<Subscription> { sub1, sub2 }
};
});
MockSubscriptionClientFactory.SetListAsyncResponses(listAsyncResponses);

var azureRmProfile = client.Login(
Context.Account,
Context.Environment,
null,
null,
MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
null);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TokenIdAndAccountIdMismatch()
Expand All @@ -78,7 +255,6 @@ public void TokenIdAndAccountIdMismatch()
var thirdList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
var fourthList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);

var tokens = new Queue<MockAccessToken>();
tokens.Enqueue(new MockAccessToken
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class MockSubscriptionClientFactory
private IList<string> _tenants;
private Queue<List<string>> _subscriptions;
private HashSet<string> _subscriptionSet;
private static Queue<Func<GetSubscriptionResult>> _getAsyncQueue;
private static Queue<Func<SubscriptionListResult>> _listAsyncQueue;

public MockSubscriptionClientFactory(List<string> tenants, Queue<List<string>> subscriptions)
{
_tenants = tenants;
Expand All @@ -49,6 +52,15 @@ public static string GetSubscriptionNameFromId(string id)
return "Sub-" + id;
}

public static void SetGetAsyncResponses(Queue<Func<GetSubscriptionResult>> responses)
{
_getAsyncQueue = responses;
}
public static void SetListAsyncResponses(Queue<Func<SubscriptionListResult>> responses)
{
_listAsyncQueue = responses;
}

public SubscriptionClient GetSubscriptionClient()
{
var tenantMock = new Mock<ITenantOperations>();
Expand All @@ -66,6 +78,10 @@ public SubscriptionClient GetSubscriptionClient()
s => s.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())).Returns(
(string subId, CancellationToken token) =>
{
if (_getAsyncQueue != null && _getAsyncQueue.Any())
{
return Task.FromResult(_getAsyncQueue.Dequeue().Invoke());
}
GetSubscriptionResult result = new GetSubscriptionResult
{
RequestId = Guid.NewGuid().ToString(),
Expand All @@ -91,6 +107,11 @@ public SubscriptionClient GetSubscriptionClient()
(s) => s.ListAsync(It.IsAny<CancellationToken>())).Returns(
(CancellationToken token) =>
{
if (_listAsyncQueue != null && _listAsyncQueue.Any())
{
return Task.FromResult(_listAsyncQueue.Dequeue().Invoke());
}

SubscriptionListResult result = null;
if (_subscriptions.Count > 0)
{
Expand All @@ -108,7 +129,7 @@ public SubscriptionClient GetSubscriptionClient()
{
DisplayName = GetSubscriptionNameFromId(sub),
Id = sub,
State = "Active",
State = "enabled",
SubscriptionId = sub
}))
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,16 @@ public AzureRMProfile Login(
newTenant == null &&
TryGetTenantSubscription(token, account, environment, tenant, subscriptionId, subscriptionName, out tempSubscription, out tempTenant))
{
newTenant = tempTenant;
newSubscription = tempSubscription;
if (tempSubscription == null && i + 1 < tenants.Count())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the empty bock. You can invert the if to work for one case. Doesn't this work?

If( tempSubscription != null || i == (tenants.Count() -1) ) 

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, this will change the logic of selecting the tenants. Now if tenant does not have any subscription - Last tenant will be selected. Somewhere in the code we say that "First subscription will be selected"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hovsep (@hovsepm)

  • yes.
  • To keep the tenant selecting order, how about iterating tenants in a reverse fashion? Personally, I don't think "first tenant/subscription" makes much sense here if tenants are not sorted(or sorted alphabetically).

{
// No subscription found for the given token/tenant.
// Discard tempTenant value unless current token/tenant is the last one.
}
else
{
newTenant = tempTenant;
newSubscription = tempSubscription;
}
}
}
}
Expand Down