Skip to content

Merge pull request #473 from Azure/dev #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from all 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,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;
}
}
}
}
Expand Down