Skip to content

Commit a9601ac

Browse files
committed
Added tests for token and user based new profile cmdlet
1 parent 8678554 commit a9601ac

File tree

9 files changed

+128
-7
lines changed

9 files changed

+128
-7
lines changed

src/Common/Commands.Common.Test/Common/Testing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static void AssertThrows<T>(Action action, string expectedMessage)
7575
catch (Exception ex)
7676
{
7777
Assert.IsInstanceOfType(ex, typeof(T));
78-
Assert.AreEqual(expectedMessage, ex.Message);
78+
Assert.AreEqual(expectedMessage, ex.Message, ex.ToString());
7979
}
8080
}
8181

src/Common/Commands.Common.Test/Mocks/MockTokenAuthenticationFactory.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,19 @@ public IAccessToken Authenticate(AzureAccount account, AzureEnvironment environm
6161
account.Id = "test";
6262
}
6363

64-
return TokenProvider(account, environment, tenant);
64+
if (TokenProvider == null)
65+
{
66+
return new MockAccessToken()
67+
{
68+
AccessToken = account.Id,
69+
LoginType = LoginType.OrgId,
70+
UserId = account.Id
71+
};
72+
}
73+
else
74+
{
75+
return TokenProvider(account, environment, tenant);
76+
}
6577
}
6678

6779
public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(AzureContext context)

src/Common/Commands.Profile/Profile/NewAzureProfile.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,45 @@ public class NewAzureProfileCommand : AzurePSCmdlet
3030
{
3131
private const string CertificateParameterSet = "Certificate";
3232
private const string CredentialsParameterSet = "Credentials";
33+
private const string ServicePrincipalParameterSet = "ServicePrincipal";
3334
private const string AccessTokenParameterSet = "Token";
3435
private const string FileParameterSet = "File";
3536

3637
[Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = CertificateParameterSet)]
38+
[Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = ServicePrincipalParameterSet)]
39+
[Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = AccessTokenParameterSet)]
3740
[Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = CredentialsParameterSet)]
3841
public AzureEnvironment Environment { get; set; }
3942

4043
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = CertificateParameterSet)]
44+
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = ServicePrincipalParameterSet)]
45+
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = AccessTokenParameterSet)]
4146
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = CredentialsParameterSet)]
4247
public string SubscriptionId { get; set; }
4348

4449
[Parameter(Mandatory = false, Position = 2, ValueFromPipelineByPropertyName = true, ParameterSetName = CertificateParameterSet)]
50+
[Parameter(Mandatory = false, Position = 2, ValueFromPipelineByPropertyName = true, ParameterSetName = ServicePrincipalParameterSet)]
51+
[Parameter(Mandatory = false, Position = 2, ValueFromPipelineByPropertyName = true, ParameterSetName = AccessTokenParameterSet)]
4552
[Parameter(Mandatory = false, Position = 2, ValueFromPipelineByPropertyName = true, ParameterSetName = CredentialsParameterSet)]
4653
public string StorageAccount { get; set; }
4754

4855
[Parameter(Mandatory = true, Position = 3, ValueFromPipelineByPropertyName = true, ParameterSetName = CertificateParameterSet)]
4956
public X509Certificate2 Certificate { get; set; }
5057

5158
[Parameter(Mandatory = true, Position = 3, ValueFromPipelineByPropertyName = true, ParameterSetName = CredentialsParameterSet)]
59+
[Parameter(Mandatory = true, Position = 3, ValueFromPipelineByPropertyName = true, ParameterSetName = ServicePrincipalParameterSet)]
5260
public PSCredential Credential { get; set; }
5361

5462
[Parameter(Mandatory = true, Position = 4, ValueFromPipelineByPropertyName = true, ParameterSetName = CredentialsParameterSet)]
63+
[Parameter(Mandatory = true, Position = 4, ValueFromPipelineByPropertyName = true, ParameterSetName = ServicePrincipalParameterSet)]
5564
public string Tenant { get; set; }
5665

66+
[Parameter(Mandatory = true, Position = 5, ValueFromPipelineByPropertyName = true, ParameterSetName = ServicePrincipalParameterSet)]
67+
public SwitchParameter ServicePrincipal { get; set; }
68+
5769
[Parameter(Mandatory = true, Position = 3, ValueFromPipelineByPropertyName = true, ParameterSetName = AccessTokenParameterSet)]
5870
public string AccessToken { get; set; }
5971

60-
[Parameter(Mandatory = true, Position = 4, ValueFromPipelineByPropertyName = true, ParameterSetName = AccessTokenParameterSet)]
61-
public string AccountId { get; set; }
62-
6372
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = FileParameterSet)]
6473
public string Path { get; set; }
6574

@@ -82,11 +91,30 @@ public override void ExecuteCmdlet()
8291
case CredentialsParameterSet:
8392
AzureAccount userAccount = new AzureAccount
8493
{
85-
Id = Credential.UserName
94+
Id = Credential.UserName,
95+
Type = AzureAccount.AccountType.User
8696
};
8797
profileClient.InitializeProfile(Environment, new Guid(SubscriptionId), userAccount,
8898
Credential.Password, StorageAccount);
8999
break;
100+
case AccessTokenParameterSet:
101+
AzureAccount tokenAccount = new AzureAccount
102+
{
103+
Id = AccessToken,
104+
Type = AzureAccount.AccountType.AccessToken
105+
};
106+
profileClient.InitializeProfile(Environment, new Guid(SubscriptionId), tokenAccount,
107+
null, StorageAccount);
108+
break;
109+
case ServicePrincipalParameterSet:
110+
AzureAccount servicePrincipalAccount = new AzureAccount
111+
{
112+
Id = Credential.UserName,
113+
Type = AzureAccount.AccountType.ServicePrincipal
114+
};
115+
profileClient.InitializeProfile(Environment, new Guid(SubscriptionId), servicePrincipalAccount,
116+
Credential.Password, StorageAccount);
117+
break;
90118
}
91119

92120
WriteObject(azureProfile);

src/ServiceManagement/Services/Commands.Test/Commands.Test.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@
354354
<None Include="Profile\NewAzureProfileTests.ps1">
355355
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
356356
</None>
357+
<None Include="SessionRecords\Microsoft.Azure.Commands.Test.Profile.NewAzureProfileTests\TestCreatesNewAzureProfileWithAccessToken.json">
358+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
359+
</None>
360+
<None Include="SessionRecords\Microsoft.Azure.Commands.Test.Profile.NewAzureProfileTests\TestCreatesNewAzureProfileWithUserCredential.json">
361+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
362+
</None>
357363
<None Include="SessionRecords\Microsoft.Azure.Commands.Test.Profile.NewAzureProfileTests\TestCreatesNewAzureProfileWithCertificate.json">
358364
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
359365
</None>

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
using System.Collections.Generic;
1517
using Microsoft.Azure.Commands.Test.Profile;
18+
using Microsoft.Azure.Common.Authentication;
19+
using Microsoft.Azure.Common.Authentication.Models;
20+
using Microsoft.Azure.Internal.Subscriptions.Csm.Models;
21+
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
1622
using Microsoft.WindowsAzure.Commands.ScenarioTest;
1723
using Xunit;
24+
using CSMSubscription = Microsoft.Azure.Internal.Subscriptions.Csm.Models.Subscription;
25+
using RDFESubscription = Microsoft.Azure.Internal.Subscriptions.Rdfe.Models.Subscription;
1826

1927
namespace Microsoft.Azure.Commands.Test.Profile
2028
{
@@ -26,5 +34,19 @@ public void TestCreatesNewAzureProfileWithCertificate()
2634
{
2735
ProfileTestController.NewInstance.RunPsTest("Test-CreatesNewAzureProfileWithCertificate");
2836
}
37+
38+
[Fact(Skip = "Need support from mocking framework")]
39+
[Trait(Category.AcceptanceType, Category.CheckIn)]
40+
public void TestCreatesNewAzureProfileWithUserCredentials()
41+
{
42+
ProfileTestController.NewInstance.RunPsTest("Test-CreatesNewAzureProfileWithUserCredentials");
43+
}
44+
45+
[Fact]
46+
[Trait(Category.AcceptanceType, Category.CheckIn)]
47+
public void TestCreatesNewAzureProfileWithAccessToken()
48+
{
49+
ProfileTestController.NewInstance.RunPsTest("Test-CreatesNewAzureProfileWithAccessToken");
50+
}
2951
}
3052
}

src/ServiceManagement/Services/Commands.Test/Profile/NewAzureProfileTests.ps1

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ $testCertData = [Convert]::FromBase64String("MIIKJAIBAzCCCeQGCSqGSIb3DQEHAaCCCdU
1717
$testCert = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2
1818
$testCert.Import($testCertData)
1919

20+
$secPasswd = ConvertTo-SecureString "TestPassw0rd" -AsPlainText -Force
21+
$testCreds = New-Object System.Management.Automation.PSCredential ("[email protected]", $secPasswd)
22+
2023
<#
2124
.SYNOPSIS
2225
Tests creating new azure profile with certificate
@@ -29,5 +32,37 @@ function Test-CreatesNewAzureProfileWithCertificate
2932
# Assert
3033
Assert-AreEqual "058de55e-28e0-49e7-8cf2-6701d4a88ef5" $actual.Context.Subscription.Id
3134
Assert-AreEqual "AzureCloud" $actual.Context.Environment.Name
35+
Assert-AreEqual "Certificate" $actual.Context.Account.Type
36+
}
37+
38+
<#
39+
.SYNOPSIS
40+
Tests creating new azure profile with user creds
41+
#>
42+
function Test-CreatesNewAzureProfileWithUserCredentials
43+
{
44+
# Test
45+
$actual = New-AzureProfile -SubscriptionId "058de55e-28e0-49e7-8cf2-6701d4a88ef5" -StorageAccount myStorage -Credentials $testCreds -Tenant "testTenant"
46+
47+
# Assert
48+
Assert-AreEqual "058de55e-28e0-49e7-8cf2-6701d4a88ef5" $actual.Context.Subscription.Id
49+
Assert-AreEqual "AzureCloud" $actual.Context.Environment.Name
50+
Assert-AreEqual "[email protected]" $actual.Context.Account.Id
51+
Assert-AreEqual "User" $actual.Context.Account.Type
52+
}
53+
54+
<#
55+
.SYNOPSIS
56+
Tests creating new azure profile with access token
57+
#>
58+
function Test-CreatesNewAzureProfileWithAccessToken
59+
{
60+
# Test
61+
$actual = New-AzureProfile -SubscriptionId "058de55e-28e0-49e7-8cf2-6701d4a88ef5" -StorageAccount myStorage -AccessToken "123456"
3262

63+
# Assert
64+
Assert-AreEqual "058de55e-28e0-49e7-8cf2-6701d4a88ef5" $actual.Context.Subscription.Id
65+
Assert-AreEqual "AzureCloud" $actual.Context.Environment.Name
66+
Assert-AreEqual "123456" $actual.Context.Account.Id
67+
Assert-AreEqual "AccessToken" $actual.Context.Account.Type
3368
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void RunPsTestWorkflow(
106106

107107
private void SetupManagementClients()
108108
{
109-
helper.SetupManagementClients();
109+
helper.SetupSomeOfManagementClients();
110110
}
111111

112112
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Entries": [
3+
],
4+
"Names": {
5+
},
6+
"Variables": {
7+
"SubscriptionId": "db1ab6f0-4769-4b27-930e-01e2ef9c123c"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Entries": [
3+
],
4+
"Names": {
5+
},
6+
"Variables": {
7+
"SubscriptionId": "db1ab6f0-4769-4b27-930e-01e2ef9c123c"
8+
}
9+
}

0 commit comments

Comments
 (0)