Skip to content

Commit 50a76db

Browse files
committed
Merge pull request Azure#412 from Azure/dev
HPF PR: vmss <- Azure:dev
2 parents d55d26c + 6d2af53 commit 50a76db

File tree

40 files changed

+1993
-57
lines changed

40 files changed

+1993
-57
lines changed

AzurePowershell.Test.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
<XUnitTests Include=".\src\ResourceManager\ApiManagement\Commands.ApiManagement.Test\bin\Debug\Microsoft.Azure.Commands.ApiManagement.Test.dll"/>
8585
<XUnitTests Include=".\src\ResourceManager\Profile\Commands.Profile.Test\bin\Debug\Microsoft.Azure.Commands.Profile.Test.dll"/>
8686
<XUnitTests Include=".\src\ResourceManager\AzureBackup\Commands.AzureBackup.Test\bin\Debug\Microsoft.Azure.Commands.AzureBackup.Test.dll"/>
87-
<XUnitTests Include=".\src\ResourceManager\NotificationHubs\Commands.NotificationHubs.Test\bin\Debug\Microsoft.Azure.Commands.NotificationHubs.Test.dll"/>
87+
<XUnitTests Include=".\src\ResourceManager\NotificationHubs\Commands.NotificationHubs.Test\bin\Debug\Microsoft.Azure.Commands.NotificationHubs.Test.dll"/>
88+
<XUnitTests Include=".\src\Common\Commands.Common.Authentication.Test\bin\Debug\Microsoft.Azure.Commands.Common.Authentication.Test.dll"/>
8889
<XUnitTests Include="@(AsmXUnitTests)"/>
8990
</ItemGroup>
9091
<ItemGroup Condition=" '$(scope)' == 'ServiceManagement' ">
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Common.Authentication;
16+
using Microsoft.Azure.Commands.Common.Authentication.Factories;
17+
using Microsoft.Azure.Commands.Common.Authentication.Models;
18+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
19+
using System;
20+
using System.Collections.Generic;
21+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
22+
using Xunit;
23+
24+
namespace Common.Authentication.Test
25+
{
26+
public class AuthenticationFactoryTests
27+
{
28+
[Fact]
29+
[Trait(Category.AcceptanceType, Category.CheckIn)]
30+
public void VerifySubscriptionTokenCacheRemove()
31+
{
32+
var authFactory = new AuthenticationFactory
33+
{
34+
TokenProvider = new MockAccessTokenProvider("testtoken", "testuser")
35+
};
36+
37+
var subscriptionId = Guid.NewGuid();
38+
39+
var credential = authFactory.GetSubscriptionCloudCredentials(new AzureContext
40+
(
41+
new AzureSubscription
42+
{
43+
Id = subscriptionId,
44+
Properties = new Dictionary<AzureSubscription.Property, string>
45+
{
46+
{ AzureSubscription.Property.Tenants, "123"}
47+
}
48+
},
49+
new AzureAccount
50+
{
51+
Id = "testuser",
52+
Type = AzureAccount.AccountType.User,
53+
Properties = new Dictionary<AzureAccount.Property, string>
54+
{
55+
{ AzureAccount.Property.Tenants, "123" }
56+
}
57+
},
58+
AzureEnvironment.PublicEnvironments["AzureCloud"]
59+
));
60+
61+
Assert.True(credential is AccessTokenCredential);
62+
Assert.Equal(subscriptionId, new Guid(((AccessTokenCredential)credential).SubscriptionId));
63+
}
64+
65+
[Fact]
66+
[Trait(Category.AcceptanceType, Category.CheckIn)]
67+
public void VerifyValidateAuthorityFalseForOnPremise()
68+
{
69+
var authFactory = new AuthenticationFactory
70+
{
71+
TokenProvider = new MockAccessTokenProvider("testtoken", "testuser")
72+
};
73+
74+
var subscriptionId = Guid.NewGuid();
75+
var context = new AzureContext
76+
(
77+
new AzureSubscription
78+
{
79+
Id = subscriptionId,
80+
Properties = new Dictionary<AzureSubscription.Property, string>
81+
{
82+
{ AzureSubscription.Property.Tenants, "123"}
83+
}
84+
},
85+
new AzureAccount
86+
{
87+
Id = "testuser",
88+
Type = AzureAccount.AccountType.User,
89+
Properties = new Dictionary<AzureAccount.Property, string>
90+
{
91+
{ AzureAccount.Property.Tenants, "123" }
92+
}
93+
},
94+
new AzureEnvironment
95+
{
96+
Name = "Katal",
97+
OnPremise = true,
98+
Endpoints = new Dictionary<AzureEnvironment.Endpoint, string>
99+
{
100+
{ AzureEnvironment.Endpoint.ActiveDirectory, "http://ad.com" },
101+
{ AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId, "http://adresource.com" }
102+
}
103+
}
104+
);
105+
106+
var credential = authFactory.Authenticate(context.Account, context.Environment, "common", null, ShowDialog.Always);
107+
108+
Assert.False(((MockAccessTokenProvider)authFactory.TokenProvider).AdalConfiguration.ValidateAuthority);
109+
}
110+
}
111+
}
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Common.Authentication;
16+
using Microsoft.Azure.Commands.Common.Authentication.Models;
17+
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
18+
using System;
19+
using System.IO;
20+
using System.Runtime.Serialization.Formatters.Binary;
21+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
22+
using Xunit;
23+
24+
namespace Common.Authentication.Test
25+
{
26+
public class AzureRMProfileTests
27+
{
28+
[Fact]
29+
[Trait(Category.AcceptanceType, Category.CheckIn)]
30+
public void ProfileSerializeDeserializeWorks()
31+
{
32+
var dataStore = new MockDataStore();
33+
AzureSession.DataStore = dataStore;
34+
var profilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile);
35+
var currentProfile = new AzureRMProfile(profilePath);
36+
var tenantId = Guid.NewGuid().ToString();
37+
var environment = new AzureEnvironment
38+
{
39+
Name = "testCloud",
40+
Endpoints = { { AzureEnvironment.Endpoint.ActiveDirectory, "http://contoso.com" } }
41+
};
42+
var account = new AzureAccount
43+
{
44+
45+
Type = AzureAccount.AccountType.User,
46+
Properties = { { AzureAccount.Property.Tenants, tenantId } }
47+
};
48+
var sub = new AzureSubscription
49+
{
50+
Account = account.Id,
51+
Environment = environment.Name,
52+
Id = new Guid(),
53+
Name = "Contoso Test Subscription",
54+
Properties = { { AzureSubscription.Property.Tenants, tenantId } }
55+
};
56+
var tenant = new AzureTenant
57+
{
58+
Id = new Guid(tenantId),
59+
Domain = "contoso.com"
60+
};
61+
62+
currentProfile.Context = new AzureContext(sub, account, environment, tenant);
63+
currentProfile.Environments[environment.Name] = environment;
64+
currentProfile.Context.TokenCache = new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 };
65+
66+
AzureRMProfile deserializedProfile;
67+
// Round-trip the exception: Serialize and de-serialize with a BinaryFormatter
68+
BinaryFormatter bf = new BinaryFormatter();
69+
using (MemoryStream ms = new MemoryStream())
70+
{
71+
// "Save" object state
72+
bf.Serialize(ms, currentProfile);
73+
74+
// Re-use the same stream for de-serialization
75+
ms.Seek(0, 0);
76+
77+
// Replace the original exception with de-serialized one
78+
deserializedProfile = (AzureRMProfile)bf.Deserialize(ms);
79+
}
80+
Assert.NotNull(deserializedProfile);
81+
var jCurrentProfile = currentProfile.ToString();
82+
var jDeserializedProfile = deserializedProfile.ToString();
83+
Assert.Equal(jCurrentProfile, jDeserializedProfile);
84+
}
85+
86+
[Fact]
87+
[Trait(Category.AcceptanceType, Category.CheckIn)]
88+
public void SavingProfileWorks()
89+
{
90+
string expected = @"{
91+
""Environments"": {
92+
""testCloud"": {
93+
""Name"": ""testCloud"",
94+
""OnPremise"": false,
95+
""Endpoints"": {
96+
""ActiveDirectory"": ""http://contoso.com""
97+
}
98+
}
99+
},
100+
""Context"": {
101+
""Account"": {
102+
""Id"": ""[email protected]"",
103+
""Type"": 1,
104+
""Properties"": {
105+
""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
106+
}
107+
},
108+
""Subscription"": {
109+
""Id"": ""00000000-0000-0000-0000-000000000000"",
110+
""Name"": ""Contoso Test Subscription"",
111+
""Environment"": ""testCloud"",
112+
""Account"": ""[email protected]"",
113+
""State"": ""Enabled"",
114+
""Properties"": {
115+
""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
116+
}
117+
},
118+
""Environment"": {
119+
""Name"": ""testCloud"",
120+
""OnPremise"": false,
121+
""Endpoints"": {
122+
""ActiveDirectory"": ""http://contoso.com""
123+
}
124+
},
125+
""Tenant"": {
126+
""Id"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"",
127+
""Domain"": ""contoso.com""
128+
},
129+
""TokenCache"": ""AQIDBAUGCAkA""
130+
}
131+
}";
132+
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile);
133+
var dataStore = new MockDataStore();
134+
AzureSession.DataStore = dataStore;
135+
AzureRMProfile profile = new AzureRMProfile(path);
136+
var tenantId = new Guid("3c0ff8a7-e8bb-40e8-ae66-271343379af6");
137+
var environment = new AzureEnvironment
138+
{
139+
Name = "testCloud",
140+
Endpoints = { { AzureEnvironment.Endpoint.ActiveDirectory, "http://contoso.com" } }
141+
};
142+
var account = new AzureAccount
143+
{
144+
145+
Type = AzureAccount.AccountType.User,
146+
Properties = { { AzureAccount.Property.Tenants, tenantId.ToString() } }
147+
};
148+
var sub = new AzureSubscription
149+
{
150+
Account = account.Id,
151+
Environment = environment.Name,
152+
Id = new Guid(),
153+
Name = "Contoso Test Subscription",
154+
State = "Enabled",
155+
Properties = { { AzureSubscription.Property.Tenants, tenantId.ToString() } }
156+
};
157+
var tenant = new AzureTenant
158+
{
159+
Id = tenantId,
160+
Domain = "contoso.com"
161+
};
162+
profile.Context = new AzureContext(sub, account, environment, tenant);
163+
profile.Environments[environment.Name] = environment;
164+
profile.Context.TokenCache = new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 };
165+
profile.Save();
166+
string actual = dataStore.ReadFileAsText(path);
167+
Assert.Equal(expected, actual);
168+
}
169+
170+
[Fact]
171+
[Trait(Category.AcceptanceType, Category.CheckIn)]
172+
public void LoadingProfileWorks()
173+
{
174+
string contents = @"{
175+
""Environments"": {
176+
""testCloud"": {
177+
""Name"": ""testCloud"",
178+
""OnPremise"": false,
179+
""Endpoints"": {
180+
""ActiveDirectory"": ""http://contoso.com""
181+
}
182+
}
183+
},
184+
""Context"": {
185+
""TokenCache"": ""AQIDBAUGCAkA"",
186+
""Account"": {
187+
""Id"": ""[email protected]"",
188+
""Type"": 1,
189+
""Properties"": {
190+
""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
191+
}
192+
},
193+
""Subscription"": {
194+
""Id"": ""00000000-0000-0000-0000-000000000000"",
195+
""Name"": ""Contoso Test Subscription"",
196+
""Environment"": ""testCloud"",
197+
""Account"": ""[email protected]"",
198+
""Properties"": {
199+
""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
200+
}
201+
},
202+
""Environment"": {
203+
""Name"": ""testCloud"",
204+
""OnPremise"": false,
205+
""Endpoints"": {
206+
""ActiveDirectory"": ""http://contoso.com""
207+
}
208+
},
209+
""Tenant"": {
210+
""Id"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"",
211+
""Domain"": ""contoso.com""
212+
}
213+
}
214+
}";
215+
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile);
216+
var dataStore = new MockDataStore();
217+
AzureSession.DataStore = dataStore;
218+
dataStore.WriteFile(path, contents);
219+
var profile = new AzureRMProfile(path);
220+
Assert.Equal(4, profile.Environments.Count);
221+
Assert.Equal("3c0ff8a7-e8bb-40e8-ae66-271343379af6", profile.Context.Tenant.Id.ToString());
222+
Assert.Equal("contoso.com", profile.Context.Tenant.Domain);
223+
Assert.Equal("00000000-0000-0000-0000-000000000000", profile.Context.Subscription.Id.ToString());
224+
Assert.Equal("testCloud", profile.Context.Environment.Name);
225+
Assert.Equal("[email protected]", profile.Context.Account.Id);
226+
Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 }, profile.Context.TokenCache);
227+
Assert.Equal(path, profile.ProfilePath);
228+
}
229+
}
230+
}

0 commit comments

Comments
 (0)