diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs index 2c8397f4f707..ceb99b710f9d 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs @@ -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 { @@ -67,6 +70,180 @@ private static RMProfileClient SetupTestEnvironment(List tenants, params return new RMProfileClient(profile); } + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void SpecifyTenantAndSubscriptionIdSucceed() + { + var tenants = new List { DefaultTenant.ToString() }; + var firstList = new List { DefaultSubscription.ToString(), Guid.NewGuid().ToString() }; + var secondList = new List { 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 { DefaultTenant.ToString() }; + var firstList = new List { 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>(); + getAsyncResponses.Enqueue(() => + { + throw new CloudException("InvalidAuthenticationTokenTenant: The access token is from the wrong issuer"); + }); + MockSubscriptionClientFactory.SetGetAsyncResponses(getAsyncResponses); + + Assert.Throws( () => client.Login( + Context.Account, + Context.Environment, + null, + DefaultSubscription.ToString(), + null, + null)); + } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void SpecifyTenantAndNotExistingSubscriptionId() + { + var tenants = new List { DefaultTenant.ToString() }; + var firstList = new List { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }; + var secondList = new List { 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( () => 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 { DefaultTenant.ToString(), Guid.NewGuid().ToString() }; + var subscriptionInSecondTenant= Guid.NewGuid().ToString(); + var firstList = new List { DefaultSubscription.ToString() }; + var secondList = new List { 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>(); + 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 { DefaultTenant.ToString(), Guid.NewGuid().ToString() }; + var subscriptionInSecondTenant= Guid.NewGuid().ToString(); + var firstList = new List { DefaultSubscription.ToString() }; + var secondList = new List { 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>(); + 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 { 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() @@ -78,7 +255,6 @@ public void TokenIdAndAccountIdMismatch() var thirdList = new List { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant }; var fourthList = new List { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant }; var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList); - var tokens = new Queue(); tokens.Enqueue(new MockAccessToken { diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs b/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs index cbf8efcdab0e..0e97c6b80b5f 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs @@ -29,6 +29,9 @@ public class MockSubscriptionClientFactory private IList _tenants; private Queue> _subscriptions; private HashSet _subscriptionSet; + private static Queue> _getAsyncQueue; + private static Queue> _listAsyncQueue; + public MockSubscriptionClientFactory(List tenants, Queue> subscriptions) { _tenants = tenants; @@ -49,6 +52,15 @@ public static string GetSubscriptionNameFromId(string id) return "Sub-" + id; } + public static void SetGetAsyncResponses(Queue> responses) + { + _getAsyncQueue = responses; + } + public static void SetListAsyncResponses(Queue> responses) + { + _listAsyncQueue = responses; + } + public SubscriptionClient GetSubscriptionClient() { var tenantMock = new Mock(); @@ -66,6 +78,10 @@ public SubscriptionClient GetSubscriptionClient() s => s.GetAsync(It.IsAny(), It.IsAny())).Returns( (string subId, CancellationToken token) => { + if (_getAsyncQueue != null && _getAsyncQueue.Any()) + { + return Task.FromResult(_getAsyncQueue.Dequeue().Invoke()); + } GetSubscriptionResult result = new GetSubscriptionResult { RequestId = Guid.NewGuid().ToString(), @@ -91,6 +107,11 @@ public SubscriptionClient GetSubscriptionClient() (s) => s.ListAsync(It.IsAny())).Returns( (CancellationToken token) => { + if (_listAsyncQueue != null && _listAsyncQueue.Any()) + { + return Task.FromResult(_listAsyncQueue.Dequeue().Invoke()); + } + SubscriptionListResult result = null; if (_subscriptions.Count > 0) { @@ -108,7 +129,7 @@ public SubscriptionClient GetSubscriptionClient() { DisplayName = GetSubscriptionNameFromId(sub), Id = sub, - State = "Active", + State = "enabled", SubscriptionId = sub })) }; diff --git a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs index 9330d4fba078..54a50a9c2ad6 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs @@ -121,8 +121,13 @@ public AzureRMProfile Login( newTenant == null && TryGetTenantSubscription(token, account, environment, tenant, subscriptionId, subscriptionName, out tempSubscription, out tempTenant)) { - newTenant = tempTenant; - newSubscription = tempSubscription; + // If no subscription found for the given token/tenant + // discard tempTenant value unless current token/tenant is the last one. + if (tempSubscription != null || i == (tenants.Count() -1)) + { + newTenant = tempTenant; + newSubscription = tempSubscription; + } } } }