Skip to content

Commit a8eb9fe

Browse files
committed
Additions to profile test
1 parent f0556a6 commit a8eb9fe

25 files changed

+8827
-1
lines changed

src/CLU/CLUCoreCLR.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Azure.Commands.Co
1111
EndProject
1212
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Azure.Commands.Profile", "Microsoft.Azure.Commands.Profile\Microsoft.Azure.Commands.Profile.xproj", "{45B05B68-516F-4D74-897F-56D12894946C}"
1313
EndProject
14+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Azure.Commands.Profile.Test", "Microsoft.Azure.Commands.Profile.Test\Microsoft.Azure.Commands.Profile.Test.xproj", "{E267C25B-98EA-4872-9753-4F6B22F7CA24}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{45B05B68-516F-4D74-897F-56D12894946C}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{45B05B68-516F-4D74-897F-56D12894946C}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{45B05B68-516F-4D74-897F-56D12894946C}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{E267C25B-98EA-4872-9753-4F6B22F7CA24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{E267C25B-98EA-4872-9753-4F6B22F7CA24}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{E267C25B-98EA-4872-9753-4F6B22F7CA24}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{E267C25B-98EA-4872-9753-4F6B22F7CA24}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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.Common.Authentication;
16+
using Microsoft.Azure.Common.Authentication.Models;
17+
using System.Linq;
18+
using Xunit;
19+
using System;
20+
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
21+
using System.Collections.Generic;
22+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
23+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
24+
25+
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
26+
{
27+
28+
public class AzureRMProfileTests
29+
{
30+
private const string DefaultAccount = "[email protected]";
31+
private static Guid DefaultSubscription = Guid.NewGuid();
32+
private static string DefaultSubscriptionName = "Contoso Subscription";
33+
private static string DefaultDomain = "contoso.com";
34+
private static Guid DefaultTenant = Guid.NewGuid();
35+
36+
private static RMProfileClient SetupTestEnvironment(List<string> tenants, params List<string>[] subscriptionLists)
37+
{
38+
var authFactory = new MockTokenAuthenticationFactory(DefaultAccount,
39+
Guid.NewGuid().ToString(), DefaultTenant.ToString());
40+
var subscriptionList = new Queue<List<string>>(subscriptionLists);
41+
var clientFactory = new MockSubscriptionClientFactory(tenants, subscriptionList);
42+
var mock = new MockClientFactory(new List<object>
43+
{
44+
clientFactory.GetSubscriptionClient()
45+
}, true);
46+
mock.MoqClients = true;
47+
AzureSession.ClientFactory = mock;
48+
var context = new AzureContext(new AzureSubscription()
49+
{
50+
Account = DefaultAccount,
51+
Environment = EnvironmentName.AzureCloud,
52+
Id = DefaultSubscription,
53+
Name = DefaultSubscriptionName
54+
},
55+
new AzureAccount() { Id = DefaultAccount, Type = AzureAccount.AccountType.User },
56+
AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
57+
new AzureTenant() { Domain = DefaultDomain, Id = DefaultTenant });
58+
var profile = new AzureRMProfile();
59+
profile.Context = context;
60+
return new RMProfileClient(authFactory, mock, profile);
61+
}
62+
63+
[Fact]
64+
[Trait(Category.AcceptanceType, Category.CheckIn)]
65+
public void MultipleTenantsAndSubscriptionsSucceed()
66+
{
67+
var tenants = new List<string> {Guid.NewGuid().ToString(), DefaultTenant.ToString()};
68+
var secondsubscriptionInTheFirstTenant = Guid.NewGuid().ToString();
69+
var firstList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant};
70+
var secondList = new List<string> { Guid.NewGuid().ToString()};
71+
var thirdList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
72+
var fourthList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
73+
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);
74+
var subResults = new List<AzureSubscription>(client.ListSubscriptions());
75+
Assert.Equal(3, subResults.Count);
76+
var tenantResults = client.ListTenants();
77+
Assert.Equal(2, tenantResults.Count());
78+
tenantResults = client.ListTenants(DefaultTenant.ToString());
79+
Assert.Equal(1, tenantResults.Count());
80+
AzureSubscription subValue;
81+
Assert.True(client.TryGetSubscriptionById(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
82+
Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString());
83+
Assert.True(client.TryGetSubscriptionByName(DefaultTenant.ToString(),
84+
MockSubscriptionClientFactory.GetSubscriptionNameFromId(DefaultSubscription.ToString()),
85+
out subValue));
86+
Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString());
87+
}
88+
89+
[Fact]
90+
[Trait(Category.AcceptanceType, Category.CheckIn)]
91+
public void SingleTenantAndSubscriptionSucceeds()
92+
{
93+
var tenants = new List<string> {DefaultTenant.ToString()};
94+
var firstList = new List<string> {DefaultSubscription.ToString()};
95+
var secondList = firstList;
96+
var thirdList = firstList;
97+
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList);
98+
var subResults = new List<AzureSubscription>(client.ListSubscriptions());
99+
Assert.Equal(1, subResults.Count);
100+
var tenantResults = client.ListTenants();
101+
Assert.Equal(1, tenantResults.Count());
102+
tenantResults = client.ListTenants(DefaultTenant.ToString());
103+
Assert.Equal(1, tenantResults.Count());
104+
AzureSubscription subValue;
105+
Assert.True(client.TryGetSubscriptionById(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
106+
Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString());
107+
Assert.True(client.TryGetSubscriptionByName(DefaultTenant.ToString(),
108+
MockSubscriptionClientFactory.GetSubscriptionNameFromId(DefaultSubscription.ToString()),
109+
out subValue));
110+
Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString());
111+
}
112+
113+
[Fact]
114+
[Trait(Category.AcceptanceType, Category.CheckIn)]
115+
public void SubscriptionNotFoundDoesNotThrow()
116+
{
117+
var tenants = new List<string> { DefaultTenant.ToString() };
118+
string randomSubscriptionId = Guid.NewGuid().ToString();
119+
var firstList = new List<string> { randomSubscriptionId };
120+
var secondList = firstList;
121+
var client = SetupTestEnvironment(tenants, firstList, secondList);
122+
var subResults = new List<AzureSubscription>(client.ListSubscriptions());
123+
Assert.Equal(1, subResults.Count);
124+
AzureSubscription subValue;
125+
Assert.False(client.TryGetSubscriptionById(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
126+
Assert.False(client.TryGetSubscriptionByName("random-tenant", "random-subscription", out subValue));
127+
}
128+
129+
[Fact]
130+
[Trait(Category.AcceptanceType, Category.CheckIn)]
131+
public void NoTenantsDoesNotThrow()
132+
{
133+
var tenants = new List<string> { };
134+
var subscriptions = new List<string> { Guid.NewGuid().ToString() };
135+
var client = SetupTestEnvironment(tenants, subscriptions);
136+
Assert.Equal(0, client.ListSubscriptions().Count());
137+
Assert.Equal(0, client.ListTenants().Count());
138+
}
139+
140+
[Fact]
141+
[Trait(Category.AcceptanceType, Category.CheckIn)]
142+
public void NoSubscriptionsInListDoesNotThrow()
143+
{
144+
var tenants = new List<string> { DefaultTenant.ToString() };
145+
var subscriptions = new List<string> () ;
146+
var client = SetupTestEnvironment(tenants, subscriptions, subscriptions);
147+
Assert.Equal(0, client.ListSubscriptions().Count());
148+
AzureSubscription subValue;
149+
Assert.False(client.TryGetSubscriptionById(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
150+
Assert.False(client.TryGetSubscriptionByName(DefaultTenant.ToString(), "random-name", out subValue));
151+
}
152+
153+
[Fact]
154+
[Trait(Category.AcceptanceType, Category.CheckIn)]
155+
public void SetContextPreservesTokenCache()
156+
{
157+
AzureRMProfile profile = null;
158+
AzureContext context = new AzureContext(null, null, null, null);
159+
Assert.Throws<ArgumentNullException>(() =>profile.SetContextWithCache(context));
160+
profile = new AzureRMProfile();
161+
Assert.Throws<ArgumentNullException>(() => profile.SetContextWithCache(null));
162+
profile.SetContextWithCache(context);
163+
Assert.Equal(TokenCache.DefaultShared.Serialize(), profile.Context.TokenCache);
164+
}
165+
}
166+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 System.Collections.Generic;
16+
using Xunit;
17+
using System;
18+
using System.Net.Http.Headers;
19+
using Hyak.Common;
20+
using Microsoft.Azure;
21+
using Microsoft.Azure.Common.Authentication.Factories;
22+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
23+
using Microsoft.Azure.Common.Authentication;
24+
using Microsoft.Azure.Common.Authentication.Models;
25+
using Microsoft.Rest.TransientFaultHandling;
26+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
27+
28+
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
29+
{
30+
public class ClientFactoryTests
31+
{
32+
[Fact]
33+
[Trait(Category.AcceptanceType, Category.CheckIn)]
34+
public void VerifyUserAgentValuesAreTransmitted()
35+
{
36+
var storedClientFactory = AzureSession.ClientFactory;
37+
var storedAuthFactory = AzureSession.AuthenticationFactory;
38+
try
39+
{
40+
var authFactory = new AuthenticationFactory();
41+
authFactory.TokenProvider = new MockAccessTokenProvider(Guid.NewGuid().ToString(), "[email protected]");
42+
AzureSession.AuthenticationFactory = authFactory;
43+
var factory = new ClientFactory();
44+
AzureSession.ClientFactory = factory;
45+
factory.UserAgents.Clear();
46+
factory.AddUserAgent("agent1");
47+
factory.AddUserAgent("agent1", "1.0.0");
48+
factory.AddUserAgent("agent1", "1.0.0");
49+
factory.AddUserAgent("agent1", "1.9.8");
50+
factory.AddUserAgent("agent2");
51+
Assert.Equal(4, factory.UserAgents.Count);
52+
var client = factory.CreateClient<NullClient>(new AzureContext(
53+
new AzureSubscription
54+
{
55+
Id = Guid.NewGuid(),
56+
Properties = new Dictionary<AzureSubscription.Property, string>
57+
{
58+
{AzureSubscription.Property.Tenants, "123"}
59+
}
60+
},
61+
new AzureAccount
62+
{
63+
64+
Type = AzureAccount.AccountType.User,
65+
Properties = new Dictionary<AzureAccount.Property, string>
66+
{
67+
{AzureAccount.Property.Tenants, "123"}
68+
}
69+
},
70+
AzureEnvironment.PublicEnvironments["AzureCloud"]
71+
72+
), AzureEnvironment.Endpoint.ResourceManager);
73+
Assert.Equal(5, client.HttpClient.DefaultRequestHeaders.UserAgent.Count);
74+
Assert.True(client.HttpClient.DefaultRequestHeaders.UserAgent.Contains(new ProductInfoHeaderValue("agent1", "")));
75+
Assert.True(client.HttpClient.DefaultRequestHeaders.UserAgent.Contains(new ProductInfoHeaderValue("agent1", "1.0.0")));
76+
Assert.True(client.HttpClient.DefaultRequestHeaders.UserAgent.Contains(new ProductInfoHeaderValue("agent1", "1.9.8")));
77+
Assert.True(client.HttpClient.DefaultRequestHeaders.UserAgent.Contains(new ProductInfoHeaderValue("agent2", "")));
78+
}
79+
finally
80+
{
81+
AzureSession.ClientFactory = storedClientFactory;
82+
AzureSession.AuthenticationFactory = storedAuthFactory;
83+
}
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)