From d076ad8d84638830a6788265c2fce86f174ba263 Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Mon, 14 Mar 2016 16:05:12 -0700 Subject: [PATCH 1/4] Updated Mock for subscriptionClient and added tests for RMProfile. --- .../AzureRMProfileTests.cs | 38 ++++++++++++++++++- .../MockSubscriptionClientFactory.cs | 10 +++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs index 2c8397f4f707..6303ffa9be8d 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs @@ -27,6 +27,8 @@ 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; namespace Microsoft.Azure.Commands.ResourceManager.Common.Test { @@ -67,6 +69,41 @@ private static RMProfileClient SetupTestEnvironment(List tenants, params return new RMProfileClient(profile); } + [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 TokenIdAndAccountIdMismatch() @@ -78,7 +115,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..ce88e7ebd1df 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs @@ -29,6 +29,7 @@ public class MockSubscriptionClientFactory private IList _tenants; private Queue> _subscriptions; private HashSet _subscriptionSet; + private static Queue> _getAsyncQueue; public MockSubscriptionClientFactory(List tenants, Queue> subscriptions) { _tenants = tenants; @@ -49,6 +50,11 @@ public static string GetSubscriptionNameFromId(string id) return "Sub-" + id; } + public static void SetGetAsyncResponses(Queue> responses) + { + _getAsyncQueue = responses; + } + public SubscriptionClient GetSubscriptionClient() { var tenantMock = new Mock(); @@ -66,6 +72,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(), From 18c5d8e1b6fa2e519e99c70e11e2de6ab4bbad7f Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Mon, 14 Mar 2016 16:56:03 -0700 Subject: [PATCH 2/4] Added test for login with subscriptionName specified only. --- .../AzureRMProfileTests.cs | 45 +++++++++++++++++++ .../MockSubscriptionClientFactory.cs | 11 +++++ 2 files changed, 56 insertions(+) diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs index 6303ffa9be8d..6b62cff78a74 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs @@ -104,6 +104,51 @@ public void SubscriptionIdNotInFirstTenant() 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 sub = new Subscription + { + Id = DefaultSubscription.ToString(), + SubscriptionId = DefaultSubscription.ToString(), + DisplayName = DefaultSubscriptionName, + State = "enabled", + }; + return new SubscriptionListResult + { + Subscriptions = new List { sub } + }; + }); + MockSubscriptionClientFactory.SetListAsyncResponses(listAsyncResponses); + + var azureRmProfile = client.Login( + Context.Account, + Context.Environment, + null, + null, + "sub name", + null); + } + [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TokenIdAndAccountIdMismatch() diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs b/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs index ce88e7ebd1df..77cb2fdad0dc 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs @@ -30,6 +30,8 @@ public class MockSubscriptionClientFactory private Queue> _subscriptions; private HashSet _subscriptionSet; private static Queue> _getAsyncQueue; + private static Queue> _listAsyncQueue; + public MockSubscriptionClientFactory(List tenants, Queue> subscriptions) { _tenants = tenants; @@ -54,6 +56,10 @@ public static void SetGetAsyncResponses(Queue> respo { _getAsyncQueue = responses; } + public static void SetListAsyncResponses(Queue> responses) + { + _listAsyncQueue = responses; + } public SubscriptionClient GetSubscriptionClient() { @@ -101,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) { From 4f5b030b7f352294a18d36b92da5ec9d3ac3cea1 Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Tue, 15 Mar 2016 15:45:31 -0700 Subject: [PATCH 3/4] Fixed RMProfileClient login bug and added more unit tests for RMProfileClient. --- .../AzureRMProfileTests.cs | 103 +++++++++++++++++- .../MockSubscriptionClientFactory.cs | 2 +- .../Models/RMProfileClient.cs | 12 +- 3 files changed, 110 insertions(+), 7 deletions(-) diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs index 6b62cff78a74..ceb99b710f9d 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs @@ -29,6 +29,7 @@ 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 { @@ -69,6 +70,93 @@ 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() @@ -126,16 +214,23 @@ public void SubscriptionNameNotInFirstTenant() var listAsyncResponses = new Queue>(); listAsyncResponses.Enqueue(() => { - var sub = new Subscription + var sub1 = new Subscription { Id = DefaultSubscription.ToString(), SubscriptionId = DefaultSubscription.ToString(), DisplayName = DefaultSubscriptionName, - State = "enabled", + State = "enabled" + }; + var sub2 = new Subscription + { + Id = subscriptionInSecondTenant, + SubscriptionId = subscriptionInSecondTenant, + DisplayName = MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant), + State = "enabled" }; return new SubscriptionListResult { - Subscriptions = new List { sub } + Subscriptions = new List { sub1, sub2 } }; }); MockSubscriptionClientFactory.SetListAsyncResponses(listAsyncResponses); @@ -145,7 +240,7 @@ public void SubscriptionNameNotInFirstTenant() Context.Environment, null, null, - "sub name", + MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant), null); } diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs b/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs index 77cb2fdad0dc..0e97c6b80b5f 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs @@ -129,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..7af09c87345b 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs @@ -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()) + { + // No subscription found for the given token/tenant. + // Discard tempTenant value unless current token/tenant is the last one. + } + else + { + newTenant = tempTenant; + newSubscription = tempSubscription; + } } } } From 5377f622e68cb18b8b60cba7fdeec114e9442fae Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Wed, 16 Mar 2016 10:11:25 -0700 Subject: [PATCH 4/4] minor change. --- .../Profile/Commands.Profile/Models/RMProfileClient.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs index 7af09c87345b..54a50a9c2ad6 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs @@ -121,12 +121,9 @@ public AzureRMProfile Login( newTenant == null && TryGetTenantSubscription(token, account, environment, tenant, subscriptionId, subscriptionName, out tempSubscription, out tempTenant)) { - if (tempSubscription == null && i + 1 < tenants.Count()) - { - // No subscription found for the given token/tenant. - // Discard tempTenant value unless current token/tenant is the last one. - } - else + // 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;