Skip to content
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

Add test coverage for Get-AzureRmSubscription list call #8392

Merged
merged 1 commit into from
Jan 29, 2019
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
73 changes: 73 additions & 0 deletions src/Accounts/Accounts.Test/AzureRMProfileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,79 @@ public void GetAzureRmSubscriptionByNameMultiplePages()
Assert.Equal(tenants[1], resultSubscription.TenantId);
}

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

var firstList = new List<string> { firstTenantSubscriptions[0], firstTenantSubscriptions[1] };
var secondList = new List<string> { firstTenantSubscriptions[2], firstTenantSubscriptions[3] };

var thirdList = new List<string> { secondTenantSubscriptions[0], secondTenantSubscriptions[1] };
var fourthList = new List<string> { secondTenantSubscriptions[2], secondTenantSubscriptions[3] };

var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);

// TEST WITH USER TYPE
var dataStore = new MemoryDataStore();
AzureSession.Instance.DataStore = dataStore;
var commandRuntimeMock = new MockCommandRuntime();
AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
var profile = new AzureRmProfile();
profile.EnvironmentTable.Add("foo", new AzureEnvironment(AzureEnvironment.PublicEnvironments.Values.FirstOrDefault()));
profile.DefaultContext = Context;
profile.DefaultContext.Account = new AzureAccount();
profile.DefaultContext.Tenant.Id = DefaultTenant.ToString();

profile.DefaultContext.Account.Type = "User";
var cmdlt = new GetAzureRMSubscriptionCommand();
// Setup
cmdlt.DefaultProfile = profile;
cmdlt.CommandRuntime = commandRuntimeMock;
Assert.Null(cmdlt.TenantId);
// Act
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();
Assert.Null(cmdlt.TenantId);
Assert.True(commandRuntimeMock.OutputPipeline.Count == 8);

// TEST WITH MANAGEDSERVICE
client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);

dataStore = new MemoryDataStore();
AzureSession.Instance.DataStore = dataStore;
commandRuntimeMock = new MockCommandRuntime();
AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
profile = new AzureRmProfile();
profile.EnvironmentTable.Add("foo", new AzureEnvironment(AzureEnvironment.PublicEnvironments.Values.FirstOrDefault()));
profile.DefaultContext = Context;
profile.DefaultContext.Account = new AzureAccount();
profile.DefaultContext.Tenant.Id = DefaultTenant.ToString();

profile.DefaultContext.Account.Type = "ManagedService";
cmdlt = new GetAzureRMSubscriptionCommand();
// Setup
cmdlt.DefaultProfile = profile;
cmdlt.CommandRuntime = commandRuntimeMock;
Assert.Null(cmdlt.TenantId);
// Act
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();
Assert.NotNull(cmdlt.TenantId);
Assert.True(commandRuntimeMock.OutputPipeline.Count == 4);
}

#if NETSTANDARD
[Fact(Skip = "ConcurrentDictionary is not marked as Serializable")]
[Trait(Category.RunType, Category.DesktopOnly)]
Expand Down
21 changes: 10 additions & 11 deletions src/Accounts/Accounts/Subscription/GetAzureRMSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ protected override void BeginProcessing()

public override void ExecuteCmdlet()
{
var tenant = TenantId;
if (!string.IsNullOrWhiteSpace(this.SubscriptionName))
{
IAzureSubscription result;
try
{
if (!this._client.TryGetSubscriptionByName(tenant, this.SubscriptionName, out result))
if (!this._client.TryGetSubscriptionByName(TenantId, this.SubscriptionName, out result))
{
ThrowSubscriptionNotFoundError(this.TenantId, this.SubscriptionName);
}
Expand All @@ -78,7 +77,7 @@ public override void ExecuteCmdlet()
}
catch (AadAuthenticationException exception)
{
ThrowTenantAuthenticationError(tenant, exception);
ThrowTenantAuthenticationError(TenantId, exception);
throw;
}

Expand All @@ -88,7 +87,7 @@ public override void ExecuteCmdlet()
IAzureSubscription result;
try
{
if (!this._client.TryGetSubscriptionById(tenant, this.SubscriptionId, out result))
if (!this._client.TryGetSubscriptionById(TenantId, this.SubscriptionId, out result))
{
ThrowSubscriptionNotFoundError(this.TenantId, this.SubscriptionId);
}
Expand All @@ -97,7 +96,7 @@ public override void ExecuteCmdlet()
}
catch (AadAuthenticationException exception)
{
ThrowTenantAuthenticationError(tenant, exception);
ThrowTenantAuthenticationError(TenantId, exception);
throw;
}

Expand All @@ -108,26 +107,26 @@ public override void ExecuteCmdlet()
{
if (DefaultContext.Account.Type.Equals("ManagedService"))
{
if (tenant == null)
if (TenantId == null)
{
tenant = DefaultContext.Tenant.Id;
TenantId = DefaultContext.Tenant.Id;
}

if (tenant.Equals(DefaultContext.Tenant.Id))
if (TenantId.Equals(DefaultContext.Tenant.Id))
{
var subscriptions = _client.ListSubscriptions(tenant);
var subscriptions = _client.ListSubscriptions(TenantId);
WriteObject(subscriptions.Select((s) => new PSAzureSubscription(s)), enumerateCollection: true);
}
}
else
{
var subscriptions = _client.ListSubscriptions(tenant);
var subscriptions = _client.ListSubscriptions(TenantId);
WriteObject(subscriptions.Select((s) => new PSAzureSubscription(s)), enumerateCollection: true);
}
}
catch (AadAuthenticationException exception)
{
ThrowTenantAuthenticationError(tenant, exception);
ThrowTenantAuthenticationError(TenantId, exception);
throw;
}
}
Expand Down