/src/ServiceManagement/Common/Commands.Common.Test/Common/ProfileClientTests.cs
C# | 784 lines | 659 code | 88 blank | 37 comment | 18 complexity | eaf850e8b2eb76dd4524f9eca0682bc6 MD5 | raw file
- // ----------------------------------------------------------------------------------
- //
- // 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.Azure.Subscriptions.Models;
- using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography.X509Certificates;
- using Microsoft.Azure.ServiceManagemenet.Common;
- using Xunit;
- using CSMSubscription = Microsoft.Azure.Subscriptions.Models.Subscription;
- using RDFESubscription = Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionListOperationResponse.Subscription;
- using Microsoft.WindowsAzure.Commands.ScenarioTest;
- using Xunit.Abstractions;
- using Microsoft.WindowsAzure.ServiceManagemenet.Common.Models;
-
- namespace Common.Authentication.Test
- {
- public class ProfileClientTests
- {
- private string oldProfileData;
- private string oldProfileDataBadSubscription;
- private string oldProfileDataCorruptedFile;
- private string oldProfileDataPath;
- private string oldProfileDataPathError;
- private string newProfileDataPath;
- private string defaultSubscription = "06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F";
- private string dummyCertificate = "MIIKJAIBAzCCCeQGCSqGSIb3DQEHAaCCCdUEggnRMIIJzTCCBe4GCSqGSIb3DQEHAaCCBd8EggXbMIIF1zCCBdMGCyqGSIb3DQEMCgECoIIE7jCCBOowHAY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keONMvtFMrJQLa2oKarGkwNzAfMAcGBSsOAwIaBBQXFDnqplMX7OuyknHK7B+HA/N8tAQUsL21+IY37DPL968vhVzqz09W/so=";
- private RDFESubscription rdfeSubscription1;
- private RDFESubscription rdfeSubscription2;
- private CSMSubscription csmSubscription1;
- private CSMSubscription csmSubscription1withDuplicateId;
- private CSMSubscription csmSubscription2;
- private AzureSubscription azureSubscription1;
- private AzureSubscription azureSubscription2;
- private AzureSubscription azureSubscription3withoutUser;
- private AzureEnvironment azureEnvironment;
- private AzureAccount azureAccount;
- private TenantIdDescription commonTenant;
- private TenantIdDescription guestTenant;
- private RDFESubscription guestRdfeSubscription;
- private CSMSubscription guestCsmSubscription;
- private AzureSMProfile currentProfile;
-
- public ProfileClientTests(ITestOutputHelper output)
- {
- XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
- SetMockData();
- currentProfile = new AzureSMProfile();
- }
-
- [Fact]
- public void ProfileMigratesOldData()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
-
- Assert.False(dataStore.FileExists(oldProfileDataPath));
- Assert.True(dataStore.FileExists(newProfileDataPath));
- }
-
- [Fact]
- public void NewProfileFromCertificateWithNullsThrowsArgumentNullException()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- AzureSMProfile newProfile = new AzureSMProfile();
- ProfileClient client1 = new ProfileClient(newProfile);
- Assert.Throws<ArgumentNullException>(() =>
- client1.InitializeProfile(null, Guid.NewGuid(), new X509Certificate2(), "foo"));
- Assert.Throws<ArgumentNullException>(() =>
- client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"], Guid.NewGuid(), null, "foo"));
- }
-
- [Fact]
- public void NewProfileFromCertificateReturnsProfile()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- AzureSMProfile newProfile = new AzureSMProfile();
- ProfileClient client1 = new ProfileClient(newProfile);
- var subscriptionId = Guid.NewGuid();
- var certificate = new X509Certificate2(Convert.FromBase64String(dummyCertificate));
-
- client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"],
- subscriptionId, certificate, null);
-
- Assert.Equal("AzureCloud", newProfile.DefaultSubscription.Environment);
- Assert.Equal(subscriptionId, newProfile.DefaultSubscription.Id);
- Assert.Equal(certificate.Thumbprint, newProfile.DefaultSubscription.Account);
- Assert.False(newProfile.DefaultSubscription.Properties.ContainsKey(AzureSubscription.Property.StorageAccount));
- }
-
- [Fact]
- public void NewProfileFromAdCredentialsWithNullsThrowsArgumentNullException()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- AzureSMProfile newProfile = new AzureSMProfile();
- ProfileClient client1 = new ProfileClient(newProfile);
- Assert.Throws<ArgumentNullException>(() =>
- client1.InitializeProfile(null, Guid.NewGuid(), new AzureAccount(), null, "foo"));
- Assert.Throws<ArgumentNullException>(() =>
- client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"], Guid.NewGuid(), (AzureAccount)null, null, "foo"));
- }
-
- [Fact]
- public void NewProfileFromADReturnsProfile()
- {
- SetMocks(new[] { rdfeSubscription1, rdfeSubscription2 }.ToList(), new List<CSMSubscription>());
- rdfeSubscription2.ActiveDirectoryTenantId = "123";
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- AzureSMProfile newProfile = new AzureSMProfile();
- ProfileClient client1 = new ProfileClient(newProfile);
- var newAccount = new AzureAccount { Id = "foo" };
- newAccount.Properties[AzureAccount.Property.Tenants] = "123";
-
- client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"],
- new Guid(rdfeSubscription2.SubscriptionId), newAccount, null, null);
-
- Assert.Equal("AzureCloud", newProfile.DefaultSubscription.Environment);
- Assert.Equal(new Guid(rdfeSubscription2.SubscriptionId), newProfile.DefaultSubscription.Id);
- Assert.Equal(newAccount.Id, newProfile.DefaultSubscription.Account);
- Assert.False(newProfile.DefaultSubscription.Properties.ContainsKey(AzureSubscription.Property.StorageAccount));
- }
-
- [Fact]
- public void NewProfileWithAccessTokenReturnsProfile()
- {
- //SetMocks(new[] { rdfeSubscription1, rdfeSubscription2 }.ToList(), new[] { csmSubscription1 }.ToList());
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- AzureSMProfile newProfile = new AzureSMProfile();
- ProfileClient client1 = new ProfileClient(newProfile);
-
- client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"],
- new Guid(csmSubscription1.SubscriptionId), "accessToken", "accountId", null);
-
- Assert.Equal("AzureCloud", newProfile.DefaultSubscription.Environment);
- Assert.Equal(new Guid(csmSubscription1.SubscriptionId), newProfile.DefaultSubscription.Id);
- Assert.Equal("accountId", newProfile.DefaultSubscription.Account);
- Assert.Equal(AzureAccount.AccountType.AccessToken, newProfile.Context.Account.Type);
- Assert.Equal("accessToken", newProfile.Context.Account.Properties[AzureAccount.Property.AccessToken]);
- Assert.False(newProfile.DefaultSubscription.Properties.ContainsKey(AzureSubscription.Property.StorageAccount));
- }
-
- [Fact]
- public void NewProfileFromADWithMismatchSubscriptionThrows()
- {
- SetMocks(new[] { rdfeSubscription1, rdfeSubscription2 }.ToList(), new[] { csmSubscription1 }.ToList());
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- AzureSMProfile newProfile = new AzureSMProfile();
- ProfileClient client1 = new ProfileClient(newProfile);
- var newAccount = new AzureAccount { Id = "foo" };
- newAccount.Properties[AzureAccount.Property.Tenants] = "123";
-
- Assert.Throws<ArgumentException>(() => client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"],
- Guid.NewGuid(), newAccount, null, null));
- }
-
- [Fact]
- public void ProfileMigratesOldDataOnce()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client1 = new ProfileClient(currentProfile);
-
- Assert.False(dataStore.FileExists(oldProfileDataPath));
- Assert.True(dataStore.FileExists(newProfileDataPath));
-
- ProfileClient client2 = new ProfileClient(currentProfile);
-
- Assert.False(dataStore.FileExists(oldProfileDataPath));
- Assert.True(dataStore.FileExists(newProfileDataPath));
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void ProfileMigratesAccountsAndDefaultSubscriptions()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
-
- // Verify Environment migration
- Assert.Equal(6, client.Profile.Environments.Count);
- Assert.Equal("Current", client.Profile.Environments["Current"].Name);
- Assert.Equal("Dogfood", client.Profile.Environments["Dogfood"].Name);
- Assert.Equal("https://login.windows-ppe.net/", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.AdTenant]);
- Assert.Equal("https://management.core.windows.net/", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId]);
- Assert.Equal("https://df.gallery.azure-test.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.Gallery]);
- Assert.Equal("https://windows.azure-test.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ManagementPortalUrl]);
- Assert.Equal("https://auxnext.windows.azure-test.net/publishsettings/index", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.PublishSettingsFileUrl]);
- Assert.Equal("https://api-dogfood.resources.windows-int.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ResourceManager]);
- Assert.Equal("https://management-preview.core.windows-int.net/", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ServiceManagement]);
- Assert.Equal(".database.windows.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix]);
-
- // Verify subscriptions
- Assert.Equal(3, client.Profile.Subscriptions.Count);
- Assert.False(client.Profile.Subscriptions.ContainsKey(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E")));
- Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")));
- Assert.Equal("Test 2", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Name);
- Assert.True(client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].IsPropertySet(AzureSubscription.Property.Default));
- Assert.Equal("test@mail.com", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Account);
- Assert.Equal("Dogfood", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Environment);
- Assert.Equal("123", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Properties[AzureSubscription.Property.Tenants]);
- Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")));
- Assert.Equal("Test 3", client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Name);
- Assert.False(client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].IsPropertySet(AzureSubscription.Property.Default));
- Assert.Equal("test@mail.com", client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Account);
- Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Properties[AzureSubscription.Property.Tenants]);
- Assert.Equal(EnvironmentName.AzureCloud, client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Environment);
- Assert.Equal(EnvironmentName.AzureChinaCloud, client.Profile.Subscriptions[new Guid("c14d7dc5-ed4d-4346-a02f-9f1bcf78fb66")].Environment);
-
- // Verify accounts
- Assert.Equal(2, client.Profile.Accounts.Count);
- Assert.Equal("test@mail.com", client.Profile.Accounts["test@mail.com"].Id);
- Assert.Equal(AzureAccount.AccountType.User, client.Profile.Accounts["test@mail.com"].Type);
- Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
- .Contains(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F").ToString()));
- Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
- .Contains(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f").ToString()));
- Assert.True(client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E09"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
- .Contains(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f").ToString()));
- Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Tenants)
- .Contains("72f988bf-86f1-41af-91ab-2d7cd011db47"));
- Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Tenants)
- .Contains("123"));
- Assert.Equal("3AF24D48B97730E5C4C9CCB12397B5E046F79E09", client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E09"].Id);
- Assert.Equal(AzureAccount.AccountType.Certificate, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E09"].Type);
- Assert.Equal(0, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E09"].GetPropertyAsArray(AzureAccount.Property.Tenants).Length);
- Assert.Equal(2, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E09"].GetPropertyAsArray(AzureAccount.Property.Subscriptions).Length);
- }
-
- [Fact]
- public void ProfileMigratesAccountsSkipsBadOnesAndBacksUpFile()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileDataBadSubscription;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
-
- // Verify Environment migration
- Assert.Equal(4, client.Profile.Environments.Count);
-
- // Verify subscriptions
- Assert.Equal(3, client.Profile.Subscriptions.Count);
- Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")));
- Assert.Equal("Test Bad Management Endpoint", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Name);
- Assert.Equal(EnvironmentName.AzureCloud, client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Environment);
- Assert.Equal("Test Null Management Endpoint", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2ADFF")].Name);
- Assert.Equal(EnvironmentName.AzureCloud, client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2ADFF")].Environment);
-
- Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")));
- Assert.Equal("Test Bad Cert", client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Name);
-
- // Verify accounts
- Assert.Equal(2, client.Profile.Accounts.Count);
- Assert.Equal("test@mail.com", client.Profile.Accounts["test@mail.com"].Id);
- Assert.Equal(AzureAccount.AccountType.User, client.Profile.Accounts["test@mail.com"].Type);
- Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
- .Contains(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F").ToString()));
- Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
- .Contains(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f").ToString()));
- Assert.True(client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E99"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
- .Contains(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f").ToString()));
- Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Tenants)
- .Contains("72f988bf-86f1-41af-91ab-2d7cd011db47"));
- Assert.False(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Tenants)
- .Contains("123"));
- Assert.Equal("3AF24D48B97730E5C4C9CCB12397B5E046F79E99", client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E99"].Id);
- Assert.Equal(AzureAccount.AccountType.Certificate, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E99"].Type);
- Assert.Equal(0, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E99"].GetPropertyAsArray(AzureAccount.Property.Tenants).Length);
- Assert.Equal(1, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E99"].GetPropertyAsArray(AzureAccount.Property.Subscriptions).Length);
-
- // Verify backup file
- Assert.True(dataStore.FileExists(oldProfileDataPathError));
- Assert.False(dataStore.FileExists(oldProfileDataPath));
- Assert.Equal(oldProfileDataBadSubscription, dataStore.ReadFileAsText(oldProfileDataPathError));
- }
-
- [Fact]
- public void ProfileMigratesCorruptedFileAndCreatedBackup()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileDataCorruptedFile;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
-
- // Verify Environment migration
- Assert.Equal(4, client.Profile.Environments.Count);
-
- // Verify subscriptions
- Assert.Equal(0, client.Profile.Subscriptions.Count);
-
- // Verify accounts
- Assert.Equal(0, client.Profile.Accounts.Count);
-
- // Verify backup file
- Assert.True(dataStore.FileExists(oldProfileDataPathError));
- Assert.False(dataStore.FileExists(oldProfileDataPath));
- Assert.Equal(oldProfileDataCorruptedFile, dataStore.ReadFileAsText(oldProfileDataPathError));
- }
-
- [Fact]
- public void AddAzureAccountReturnsAccountWithAllSubscriptionsInRdfeMode()
- {
- SetMocks(new[] { rdfeSubscription1, rdfeSubscription2 }.ToList(), new[] { csmSubscription1 }.ToList());
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
-
- var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "test", Type = AzureAccount.AccountType.User }, AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], null);
-
- Assert.Equal("test", account.Id);
- Assert.Equal(2, account.GetSubscriptions(client.Profile).Count);
- Assert.True(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(rdfeSubscription1.SubscriptionId)));
- Assert.True(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(rdfeSubscription2.SubscriptionId)));
- Assert.False(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(csmSubscription1.SubscriptionId)));
- }
-
- [Fact]
- public void AddAzureAccountFiltersEmptyAdClientsInRdfeMode()
- {
- var emptyTenantIdrdfeSubscription = new RDFESubscription
- {
- SubscriptionId = "16E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E",
- SubscriptionName = "RdfeSub1",
- SubscriptionStatus = Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionStatus.Active,
- ActiveDirectoryTenantId = ""
- };
-
- var disabledTenantIdrdfeSubscription = new RDFESubscription
- {
- SubscriptionId = "16E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E",
- SubscriptionName = "RdfeSub1",
- SubscriptionStatus = Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionStatus.Disabled,
- ActiveDirectoryTenantId = "B59BE059-5E3F-463B-8C1A-831A29819B52"
- };
-
- var deletedTenantIdrdfeSubscription = new RDFESubscription
- {
- SubscriptionId = "16E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E",
- SubscriptionName = "RdfeSub1",
- SubscriptionStatus = Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionStatus.Deleted,
- ActiveDirectoryTenantId = "B59BE059-5E3F-463B-8C1A-831A29819B52"
- };
-
- var deletingTenantIdrdfeSubscription = new RDFESubscription
- {
- SubscriptionId = "16E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E",
- SubscriptionName = "RdfeSub1",
- SubscriptionStatus = Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionStatus.Deleting,
- ActiveDirectoryTenantId = "B59BE059-5E3F-463B-8C1A-831A29819B52"
- };
-
- SetMocks(
- new[] { rdfeSubscription1, emptyTenantIdrdfeSubscription, disabledTenantIdrdfeSubscription, deletedTenantIdrdfeSubscription, deletingTenantIdrdfeSubscription }.ToList(),
- new[] { csmSubscription1 }.ToList());
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
-
- var account = client.AddAccountAndLoadSubscriptions(
- new AzureAccount { Id = "test", Type = AzureAccount.AccountType.User },
- AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
- null);
-
- Assert.Equal("test", account.Id);
- Assert.Equal(1, account.GetSubscriptions(client.Profile).Count);
- Assert.True(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(rdfeSubscription1.SubscriptionId)));
- Assert.False(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(csmSubscription1.SubscriptionId)));
- }
-
- [Fact]
- public void AddAzureAccountReturnsAccountWithAllSubscriptionsInCsmMode()
- {
- SetMocks(new[] { rdfeSubscription1, rdfeSubscription2 }.ToList(), new[] { csmSubscription1 }.ToList());
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
-
- var account = client.AddAccountAndLoadSubscriptions(
- new AzureAccount
- {
- Id = "test",
- Type = AzureAccount.AccountType.User
- },
- AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
- null);
-
- Assert.Equal("test", account.Id);
- Assert.Equal(2, account.GetSubscriptions(client.Profile).Count);
- Assert.True(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(rdfeSubscription1.SubscriptionId)));
- Assert.True(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(rdfeSubscription2.SubscriptionId)));
- Assert.False(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(csmSubscription1.SubscriptionId)));
- }
-
- /// <summary>
- /// Verify that if a user has a different identity in one tenantId, the identity is not added if it has no
- /// access to subscriptions
- /// </summary>
- [Fact]
- public void AddAzureAccountWithImpersonatedGuestWithNoSubscriptions()
- {
- SetMocks(new[] { rdfeSubscription1 }.ToList(),
- new List<Microsoft.Azure.Subscriptions.Models.Subscription>(),
- new[] { commonTenant, guestTenant }.ToList(),
- (userAccount, environment, tenant) =>
- {
- var token = new MockAccessToken
- {
- UserId = tenant == commonTenant.TenantId ? userAccount.Id : "UserB",
- AccessToken = "def",
- LoginType = LoginType.OrgId
- };
- userAccount.Id = token.UserId;
- return token;
- });
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
-
- var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "UserA", Type = AzureAccount.AccountType.User }, AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], null);
-
- Assert.Equal("UserA", account.Id);
- Assert.Equal(1, account.GetSubscriptions(client.Profile).Count);
- var subrdfe1 = account.GetSubscriptions(client.Profile).FirstOrDefault(s => s.Id == new Guid(rdfeSubscription1.SubscriptionId));
- var userA = client.GetAccount("UserA");
- Assert.Throws<ArgumentException>(() => client.GetAccount("UserB"));
- Assert.NotNull(userA);
- Assert.Contains<string>(rdfeSubscription1.SubscriptionId, userA.GetPropertyAsArray(AzureAccount.Property.Subscriptions), StringComparer.OrdinalIgnoreCase);
- Assert.NotNull(subrdfe1);
- Assert.Equal("UserA", subrdfe1.Account);
- }
-
- /// <summary>
- /// Verify that multiple accounts can be added if a user has different identities in different domains, linked to the same login
- /// Verify that subscriptions with admin access for all accounts are added
- /// </summary>
- [Fact]
- public void AddAzureAccountWithImpersonatedGuestWithSubscriptions()
- {
- SetMocks(new[] { rdfeSubscription1, guestRdfeSubscription }.ToList(),
- new List<CSMSubscription>(),
- new[] { commonTenant, guestTenant }.ToList(),
- (userAccount, environment, tenant) =>
- {
- var token = new MockAccessToken
- {
- UserId = tenant == commonTenant.TenantId ? userAccount.Id : "UserB",
- AccessToken = "def",
- LoginType = LoginType.OrgId
- };
- userAccount.Id = token.UserId;
- return token;
- });
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
-
- var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "UserA", Type = AzureAccount.AccountType.User },
- AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], null);
-
- Assert.Equal("UserA", account.Id);
- Assert.Equal(2, account.GetSubscriptions(client.Profile).Count);
- var subrdfe1 = account.GetSubscriptions(client.Profile).FirstOrDefault(s => s.Id == new Guid(rdfeSubscription1.SubscriptionId));
- var userA = client.GetAccount("UserA");
- var userB = client.GetAccount("UserB");
- var subGuest = userB.GetSubscriptions(client.Profile).FirstOrDefault(s => s.Id == new Guid(guestRdfeSubscription.SubscriptionId));
- Assert.NotNull(userA);
- Assert.NotNull(userB);
- Assert.Contains<string>(rdfeSubscription1.SubscriptionId, userA.GetPropertyAsArray(AzureAccount.Property.Subscriptions), StringComparer.OrdinalIgnoreCase);
- Assert.Contains<string>(guestRdfeSubscription.SubscriptionId, userB.GetPropertyAsArray(AzureAccount.Property.Subscriptions), StringComparer.OrdinalIgnoreCase);
- Assert.NotNull(subrdfe1);
- Assert.NotNull(subGuest);
- Assert.Equal("UserA", subrdfe1.Account);
- Assert.Equal("UserB", subGuest.Account);
- }
- /// <summary>
- /// Test that when accountId is added more than once with different capitalization, only a single accountId is added
- /// and that accounts can be retrieved case-insensitively
- /// </summary>
- [Fact]
- public void AddAzureAccountIsCaseInsensitive()
- {
- SetMocks(new[] { rdfeSubscription1, guestRdfeSubscription }.ToList(),
- new List<Microsoft.Azure.Subscriptions.Models.Subscription>(),
- new[] { commonTenant, guestTenant }.ToList(),
- (userAccount, environment, tenant) =>
- {
- var token = new MockAccessToken
- {
- UserId = tenant == commonTenant.TenantId ? userAccount.Id : "USERA",
- AccessToken = "def",
- LoginType = LoginType.OrgId
- };
- userAccount.Id = token.UserId;
- return token;
- });
- MemoryDataStore dataStore = new MemoryDataStore();
- dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
-
- var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "UserA", Type = AzureAccount.AccountType.User },
- AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], null);
-
- var userA = client.GetAccount("UserA");
- var secondUserA = client.GetAccount("USERA");
- Assert.NotNull(userA);
- Assert.NotNull(secondUserA);
- Assert.Equal(userA.Id, secondUserA.Id);
- }
-
- [Fact]
- public void GetAzureAccountReturnsAccountWithSubscriptions()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
- client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
- client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
- client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
- client.Profile.Accounts[azureAccount.Id] = azureAccount;
- client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
-
- var account = client.ListAccounts("test").ToList();
-
- Assert.Equal(1, account.Count);
- Assert.Equal("test", account[0].Id);
- Assert.Equal(2, account[0].GetSubscriptions(client.Profile).Count);
- Assert.True(account[0].GetSubscriptions(client.Profile).Any(s => s.Id == azureSubscription1.Id));
- Assert.True(account[0].GetSubscriptions(client.Profile).Any(s => s.Id == azureSubscription2.Id));
- }
-
- [Fact]
- public void GetAzureAccountWithoutEnvironmentReturnsAccount()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
- client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
- client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
- client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
- client.Profile.Accounts[azureAccount.Id] = azureAccount;
- client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
-
- var account = client.ListAccounts("test").ToList();
-
- Assert.Equal(1, account.Count);
- Assert.Equal("test", account[0].Id);
- Assert.Equal(2, account[0].GetSubscriptions(client.Profile).Count);
- Assert.True(account[0].GetSubscriptions(client.Profile).Any(s => s.Id == azureSubscription1.Id));
- Assert.True(account[0].GetSubscriptions(client.Profile).Any(s => s.Id == azureSubscription2.Id));
- }
-
- [Fact]
- public void GetAzureAccountReturnsEmptyEnumerationForNonExistingUser()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
- client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
- client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
- client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
- client.Profile.Accounts[azureAccount.Id] = azureAccount;
- client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
-
- var account = client.ListAccounts("test2").ToList();
-
- Assert.Equal(0, account.Count);
- }
-
- [Fact]
- public void GetAzureAccountReturnsAllAccountsWithNullUser()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
- client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
- client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
- client.Profile.Accounts[azureAccount.Id] = azureAccount;
- azureSubscription3withoutUser.Account = "test2";
- client.Profile.Accounts["test2"] = new AzureAccount
- {
- Id = "test2",
- Type = AzureAccount.AccountType.User,
- Properties = new Dictionary<AzureAccount.Property, string>
- {
- {AzureAccount.Property.Subscriptions, azureSubscription3withoutUser.Id.ToString()}
- }
- };
- client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
- client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
-
- var account = client.ListAccounts(null).ToList();
-
- Assert.Equal(2, account.Count);
- }
-
- [Fact]
- public void RemoveAzureAccountRemovesSubscriptions()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
- client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
- client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
- client.Profile.Accounts[azureAccount.Id] = azureAccount;
- azureSubscription3withoutUser.Account = "test2";
- client.Profile.Accounts["test2"] = new AzureAccount
- {
- Id = "test2",
- Type = AzureAccount.AccountType.User,
- Properties = new Dictionary<AzureAccount.Property, string>
- {
- {AzureAccount.Property.Subscriptions, azureSubscription3withoutUser.Id.ToString()}
- }
- };
- client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
- client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
- List<string> log = new List<string>();
- client.WarningLog = log.Add;
-
- Assert.Equal(3, client.Profile.Subscriptions.Count);
-
- client.RemoveAccount("test2");
-
- Assert.Equal(2, client.Profile.Subscriptions.Count);
- Assert.Equal(0, log.Count);
- }
-
- [Fact]
- public void RemoveAzureAccountRemovesDefaultSubscriptionAndWritesWarning()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- ProfileClient client = new ProfileClient(currentProfile);
-
- client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
- client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
- client.Profile.Accounts[azureAccount.Id] = azureAccount;
- azureSubscription3withoutUser.Account = "test2";
- client.Profile.Accounts["test2"] = new AzureAccount
- {
- Id = "test2",
- Type = AzureAccount.AccountType.User,
- Properties = new Dictionary<AzureAccount.Property, string>
- {
- {AzureAccount.Property.Subscriptions, azureSubscription3withoutUser.Id.ToString()}
- }
- };
- client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
- client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
- List<string> log = new List<string>();
- client.WarningLog = log.Add;
-
- Assert.Equal(3, client.Profile.Subscriptions.Count);
-
- var account = client.RemoveAccount("test");
-
- Assert.Equal(1, client.Profile.Subscriptions.Count);
- Assert.Equal("test", account.Id);
- Assert.Equal(2, account.GetPropertyAsArray(AzureAccount.Property.Subscriptions).Length);
- Assert.Equal(1, log.Count);
- Assert.Equal(
- "The default subscription is being removed. Use Select-AzureSubscription -Default <subscriptionName> to select a new default subscription.",
- log[0]);
- }
-
- [Fact]
- public void RemoveAzureAccountRemovesDefaultAccountFromSubscription()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);
- client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
- client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
- client.Profile.Accounts[azureAccount.Id] = azureAccount;
- azureSubscription3withoutUser.Account = "test2";
- client.Profile.Accounts["test2"] = new AzureAccount
- {
- Id = "test2",
- Type = AzureAccount.AccountType.User,
- Properties = new Dictionary<AzureAccount.Property, string>
- {
- {AzureAccount.Property.Subscriptions, azureSubscription1.Id.ToString()}
- }
- };
- client.Profile.Subscriptions[azureSubscription1.Id].Account = azureAccount.Id;
- client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
-
- var account = client.RemoveAccount(azureAccount.Id);
-
- Assert.Equal("test2", client.Profile.Subscriptions[azureSubscription1.Id].Account);
- }
-
- [Fact]
- public void RemoveAzureAccountRemovesInMemoryAccount()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- ProfileClient client = new ProfileClient(currentProfile);
- client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
- client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
- client.Profile.Accounts[azureAccount.Id] = azureAccount;
- azureSubscription3withoutUser.Account = "test2";
- client.Profile.Accounts["test2"] = new AzureAccount
- {
- Id = "test2",
- Type = AzureAccount.AccountType.User,
- Properties = new Dictionary<AzureAccount.Property, string>
- {
- {AzureAccount.Property.Subscriptions, azureSubscription1.Id.ToString()}
- }
- };
- client.Profile.Subscriptions[azureSubscription1.Id].Account = azureAccount.Id;
- client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
- currentProfile.DefaultSubscription = azureSubscription1;
-
- client.RemoveAccount(azureAccount.Id);
-
- Assert.Equal("test2", currentProfile.Context.Account.Id);
- Assert.Equal("test2", currentProfile.Context.Subscription.Account);
- Assert.Equal(azureSubscription1.Id, currentProfile.Context.Subscription.Id);
-
- client.RemoveAccount("test2");
-
- Assert.Null(currentProfile.Context.Account);
- Assert.Null(currentProfile.Context.Subscription);
- Assert.Null(currentProfile.Context.Environment);
- }
-
- [Fact]
- public void AddAzureEnvironmentAddsEnvironment()
- {
- MemoryDataStore dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(currentProfile);