|
| 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 | +} |
0 commit comments