Skip to content

Adding authentication tests to repo #1827

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 5 commits into from
Feb 18, 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
3 changes: 2 additions & 1 deletion AzurePowershell.Test.targets
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
<XUnitTests Include=".\src\ResourceManager\ApiManagement\Commands.ApiManagement.Test\bin\Debug\Microsoft.Azure.Commands.ApiManagement.Test.dll"/>
<XUnitTests Include=".\src\ResourceManager\Profile\Commands.Profile.Test\bin\Debug\Microsoft.Azure.Commands.Profile.Test.dll"/>
<XUnitTests Include=".\src\ResourceManager\AzureBackup\Commands.AzureBackup.Test\bin\Debug\Microsoft.Azure.Commands.AzureBackup.Test.dll"/>
<XUnitTests Include=".\src\ResourceManager\NotificationHubs\Commands.NotificationHubs.Test\bin\Debug\Microsoft.Azure.Commands.NotificationHubs.Test.dll"/>
<XUnitTests Include=".\src\ResourceManager\NotificationHubs\Commands.NotificationHubs.Test\bin\Debug\Microsoft.Azure.Commands.NotificationHubs.Test.dll"/>
<XUnitTests Include=".\src\Common\Commands.Common.Authentication.Test\bin\Debug\Microsoft.Azure.Commands.Common.Authentication.Test.dll"/>
<XUnitTests Include="@(AsmXUnitTests)"/>
</ItemGroup>
<ItemGroup Condition=" '$(scope)' == 'ServiceManagement' ">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Factories;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using System;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Common.Authentication.Test
{
public class AuthenticationFactoryTests
{
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void VerifySubscriptionTokenCacheRemove()
{
var authFactory = new AuthenticationFactory
{
TokenProvider = new MockAccessTokenProvider("testtoken", "testuser")
};

var subscriptionId = Guid.NewGuid();

var credential = authFactory.GetSubscriptionCloudCredentials(new AzureContext
(
new AzureSubscription
{
Id = subscriptionId,
Properties = new Dictionary<AzureSubscription.Property, string>
{
{ AzureSubscription.Property.Tenants, "123"}
}
},
new AzureAccount
{
Id = "testuser",
Type = AzureAccount.AccountType.User,
Properties = new Dictionary<AzureAccount.Property, string>
{
{ AzureAccount.Property.Tenants, "123" }
}
},
AzureEnvironment.PublicEnvironments["AzureCloud"]
));

Assert.True(credential is AccessTokenCredential);
Assert.Equal(subscriptionId, new Guid(((AccessTokenCredential)credential).SubscriptionId));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void VerifyValidateAuthorityFalseForOnPremise()
{
var authFactory = new AuthenticationFactory
{
TokenProvider = new MockAccessTokenProvider("testtoken", "testuser")
};

var subscriptionId = Guid.NewGuid();
var context = new AzureContext
(
new AzureSubscription
{
Id = subscriptionId,
Properties = new Dictionary<AzureSubscription.Property, string>
{
{ AzureSubscription.Property.Tenants, "123"}
}
},
new AzureAccount
{
Id = "testuser",
Type = AzureAccount.AccountType.User,
Properties = new Dictionary<AzureAccount.Property, string>
{
{ AzureAccount.Property.Tenants, "123" }
}
},
new AzureEnvironment
{
Name = "Katal",
OnPremise = true,
Endpoints = new Dictionary<AzureEnvironment.Endpoint, string>
{
{ AzureEnvironment.Endpoint.ActiveDirectory, "http://ad.com" },
{ AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId, "http://adresource.com" }
}
}
);

var credential = authFactory.Authenticate(context.Account, context.Environment, "common", null, ShowDialog.Always);

Assert.False(((MockAccessTokenProvider)authFactory.TokenProvider).AdalConfiguration.ValidateAuthority);
}
}
}
230 changes: 230 additions & 0 deletions src/Common/Commands.Common.Authentication.Test/AzureRMProfileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Common.Authentication.Test
{
public class AzureRMProfileTests
{
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ProfileSerializeDeserializeWorks()
{
var dataStore = new MockDataStore();
AzureSession.DataStore = dataStore;
var profilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile);
var currentProfile = new AzureRMProfile(profilePath);
var tenantId = Guid.NewGuid().ToString();
var environment = new AzureEnvironment
{
Name = "testCloud",
Endpoints = { { AzureEnvironment.Endpoint.ActiveDirectory, "http://contoso.com" } }
};
var account = new AzureAccount
{
Id = "[email protected]",
Type = AzureAccount.AccountType.User,
Properties = { { AzureAccount.Property.Tenants, tenantId } }
};
var sub = new AzureSubscription
{
Account = account.Id,
Environment = environment.Name,
Id = new Guid(),
Name = "Contoso Test Subscription",
Properties = { { AzureSubscription.Property.Tenants, tenantId } }
};
var tenant = new AzureTenant
{
Id = new Guid(tenantId),
Domain = "contoso.com"
};

currentProfile.Context = new AzureContext(sub, account, environment, tenant);
currentProfile.Environments[environment.Name] = environment;
currentProfile.Context.TokenCache = new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 };

AzureRMProfile deserializedProfile;
// Round-trip the exception: Serialize and de-serialize with a BinaryFormatter
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
// "Save" object state
bf.Serialize(ms, currentProfile);

// Re-use the same stream for de-serialization
ms.Seek(0, 0);

// Replace the original exception with de-serialized one
deserializedProfile = (AzureRMProfile)bf.Deserialize(ms);
}
Assert.NotNull(deserializedProfile);
var jCurrentProfile = currentProfile.ToString();
var jDeserializedProfile = deserializedProfile.ToString();
Assert.Equal(jCurrentProfile, jDeserializedProfile);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SavingProfileWorks()
{
string expected = @"{
""Environments"": {
""testCloud"": {
""Name"": ""testCloud"",
""OnPremise"": false,
""Endpoints"": {
""ActiveDirectory"": ""http://contoso.com""
}
}
},
""Context"": {
""Account"": {
""Id"": ""[email protected]"",
""Type"": 1,
""Properties"": {
""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
}
},
""Subscription"": {
""Id"": ""00000000-0000-0000-0000-000000000000"",
""Name"": ""Contoso Test Subscription"",
""Environment"": ""testCloud"",
""Account"": ""[email protected]"",
""State"": ""Enabled"",
""Properties"": {
""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
}
},
""Environment"": {
""Name"": ""testCloud"",
""OnPremise"": false,
""Endpoints"": {
""ActiveDirectory"": ""http://contoso.com""
}
},
""Tenant"": {
""Id"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"",
""Domain"": ""contoso.com""
},
""TokenCache"": ""AQIDBAUGCAkA""
}
}";
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile);
var dataStore = new MockDataStore();
AzureSession.DataStore = dataStore;
AzureRMProfile profile = new AzureRMProfile(path);
var tenantId = new Guid("3c0ff8a7-e8bb-40e8-ae66-271343379af6");
var environment = new AzureEnvironment
{
Name = "testCloud",
Endpoints = { { AzureEnvironment.Endpoint.ActiveDirectory, "http://contoso.com" } }
};
var account = new AzureAccount
{
Id = "[email protected]",
Type = AzureAccount.AccountType.User,
Properties = { { AzureAccount.Property.Tenants, tenantId.ToString() } }
};
var sub = new AzureSubscription
{
Account = account.Id,
Environment = environment.Name,
Id = new Guid(),
Name = "Contoso Test Subscription",
State = "Enabled",
Properties = { { AzureSubscription.Property.Tenants, tenantId.ToString() } }
};
var tenant = new AzureTenant
{
Id = tenantId,
Domain = "contoso.com"
};
profile.Context = new AzureContext(sub, account, environment, tenant);
profile.Environments[environment.Name] = environment;
profile.Context.TokenCache = new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 };
profile.Save();
string actual = dataStore.ReadFileAsText(path);
Assert.Equal(expected, actual);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void LoadingProfileWorks()
{
string contents = @"{
""Environments"": {
""testCloud"": {
""Name"": ""testCloud"",
""OnPremise"": false,
""Endpoints"": {
""ActiveDirectory"": ""http://contoso.com""
}
}
},
""Context"": {
""TokenCache"": ""AQIDBAUGCAkA"",
""Account"": {
""Id"": ""[email protected]"",
""Type"": 1,
""Properties"": {
""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
}
},
""Subscription"": {
""Id"": ""00000000-0000-0000-0000-000000000000"",
""Name"": ""Contoso Test Subscription"",
""Environment"": ""testCloud"",
""Account"": ""[email protected]"",
""Properties"": {
""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
}
},
""Environment"": {
""Name"": ""testCloud"",
""OnPremise"": false,
""Endpoints"": {
""ActiveDirectory"": ""http://contoso.com""
}
},
""Tenant"": {
""Id"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"",
""Domain"": ""contoso.com""
}
}
}";
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile);
var dataStore = new MockDataStore();
AzureSession.DataStore = dataStore;
dataStore.WriteFile(path, contents);
var profile = new AzureRMProfile(path);
Assert.Equal(4, profile.Environments.Count);
Assert.Equal("3c0ff8a7-e8bb-40e8-ae66-271343379af6", profile.Context.Tenant.Id.ToString());
Assert.Equal("contoso.com", profile.Context.Tenant.Domain);
Assert.Equal("00000000-0000-0000-0000-000000000000", profile.Context.Subscription.Id.ToString());
Assert.Equal("testCloud", profile.Context.Environment.Name);
Assert.Equal("[email protected]", profile.Context.Account.Id);
Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 }, profile.Context.TokenCache);
Assert.Equal(path, profile.ProfilePath);
}
}
}
Loading