Skip to content

Commit f924008

Browse files
author
Hovsep Mkrtchyan
committed
Fixed "KeyNotFoundException when running Add-AzureAccount with no RDFE subscriptions" Azure#1654
1 parent 487a2bd commit f924008

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/ProfileCmdletTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2424
using System;
2525
using System.Linq;
26+
using System.Management.Automation;
2627
using Xunit;
2728
using Xunit.Abstractions;
2829

@@ -76,6 +77,21 @@ public void SelectAzureProfileNull()
7677
cmdlt.InvokeEndProcessing();
7778
}
7879

80+
81+
[Fact]
82+
[Trait(Category.AcceptanceType, Category.CheckIn)]
83+
public void SelectAzureProfileBadPath()
84+
{
85+
SelectAzureRMProfileCommand cmdlt = new SelectAzureRMProfileCommand();
86+
cmdlt.Path = "z:\non-existent-path\non-existent-file.ext";
87+
// Setup
88+
cmdlt.CommandRuntime = commandRuntimeMock;
89+
90+
// Act
91+
cmdlt.InvokeBeginProcessing();
92+
Assert.Throws<PSArgumentException>(() => cmdlt.ExecuteCmdlet());
93+
}
94+
7995
[Fact]
8096
[Trait(Category.AcceptanceType, Category.CheckIn)]
8197
public void SelectAzureProfileFromDisk()

src/ServiceManagement/Common/Commands.ServiceManagement.Common/ProfileClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,13 @@ public AzureAccount AddAccountAndLoadSubscriptions(AzureAccount account, AzureEn
322322
password,
323323
password == null ? ShowDialog.Always : ShowDialog.Never).ToList();
324324

325+
if (subscriptionsFromServer == null ||
326+
subscriptionsFromServer.Count ==0 )
327+
{
328+
throw new ArgumentException("No subscriptions are associated with the logged in account in " +
329+
"Azure Service Management (RDFE). This means that the logged in user is not an administrator " +
330+
"or co-administrator for any account." + Environment.NewLine + "Did you mean to execute Login-AzureRmAccount?");
331+
}
325332
// If account id is null the login failed
326333
if (account.Id != null)
327334
{

src/ServiceManagement/Services/Commands.Test/Profile/ProfileCmdltsTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,34 @@ public void CanAddAccountToCustomProfile()
979979
Assert.Equal(profile.Context.Subscription.Id, rdfeSubscription);
980980
}
981981

982+
[Fact]
983+
[Trait(Category.AcceptanceType, Category.CheckIn)]
984+
public void AddAzureAccountThrowsForEmptySubscriptions()
985+
{
986+
var cmdlet = new AddAzureAccount();
987+
var csmSubscription = Guid.NewGuid();
988+
var rdfeSubscription = Guid.NewGuid();
989+
var credential = GenerateCredential("mySillyPassword");
990+
var profile = new AzureSMProfile();
991+
var client = new ProfileClient(profile);
992+
cmdlet.Credential = credential;
993+
cmdlet.Profile = profile;
994+
cmdlet.SetParameterSet("User");
995+
996+
AzureSession.ClientFactory =
997+
new MockClientFactory(
998+
new List<object>
999+
{
1000+
ProfileClientHelper.CreateRdfeSubscriptionClient(rdfeSubscription),
1001+
ProfileClientHelper.CreateCsmSubscriptionClient(new List<string>(),
1002+
new List<string>{csmSubscription.ToString(), rdfeSubscription.ToString()})
1003+
}, true);
1004+
AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory(credential.UserName,
1005+
Guid.NewGuid().ToString());
1006+
cmdlet.CommandRuntime = commandRuntimeMock;
1007+
cmdlet.InvokeBeginProcessing();
1008+
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
1009+
}
9821010
[Fact]
9831011
[Trait(Category.AcceptanceType, Category.CheckIn)]
9841012
public void CanCreateProfieWithSPAuth()

0 commit comments

Comments
 (0)