forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Azure#1827 from markcowl/authtests
Adding authentication tests to repo
- Loading branch information
Showing
38 changed files
with
1,942 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/Common/Commands.Common.Authentication.Test/AuthenticationFactoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
230
src/Common/Commands.Common.Authentication.Test/AzureRMProfileTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.