PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/ServiceManagement/Common/Commands.Common.Test/Common/ProfileClientTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 784 lines | 659 code | 88 blank | 37 comment | 18 complexity | eaf850e8b2eb76dd4524f9eca0682bc6 MD5 | raw file
  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. using Microsoft.Azure.Commands.Common.Authentication;
  15. using Microsoft.Azure.Commands.Common.Authentication.Models;
  16. using Microsoft.Azure.Subscriptions.Models;
  17. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Security.Cryptography.X509Certificates;
  23. using Microsoft.Azure.ServiceManagemenet.Common;
  24. using Xunit;
  25. using CSMSubscription = Microsoft.Azure.Subscriptions.Models.Subscription;
  26. using RDFESubscription = Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionListOperationResponse.Subscription;
  27. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  28. using Xunit.Abstractions;
  29. using Microsoft.WindowsAzure.ServiceManagemenet.Common.Models;
  30. namespace Common.Authentication.Test
  31. {
  32. public class ProfileClientTests
  33. {
  34. private string oldProfileData;
  35. private string oldProfileDataBadSubscription;
  36. private string oldProfileDataCorruptedFile;
  37. private string oldProfileDataPath;
  38. private string oldProfileDataPathError;
  39. private string newProfileDataPath;
  40. private string defaultSubscription = "06E3F6FD-A3AA-439A-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67KaLF825pRm+6Nl3mx0jaPDgK2ToydGfuVBA+9TSpnuV26imsd+K2yL2nwrdvBJPE/t2lPzVIR0hnf4AJ8/9BR0vTGmxiWwy8VMxrS3PyouLPZMXAgdT6ddRVwmewNjTe5g/tciGazIW+nROgg6fsgyObMp7keONMvtFMrJQLa2oKarGkwNzAfMAcGBSsOAwIaBBQXFDnqplMX7OuyknHK7B+HA/N8tAQUsL21+IY37DPL968vhVzqz09W/so=";
  42. private RDFESubscription rdfeSubscription1;
  43. private RDFESubscription rdfeSubscription2;
  44. private CSMSubscription csmSubscription1;
  45. private CSMSubscription csmSubscription1withDuplicateId;
  46. private CSMSubscription csmSubscription2;
  47. private AzureSubscription azureSubscription1;
  48. private AzureSubscription azureSubscription2;
  49. private AzureSubscription azureSubscription3withoutUser;
  50. private AzureEnvironment azureEnvironment;
  51. private AzureAccount azureAccount;
  52. private TenantIdDescription commonTenant;
  53. private TenantIdDescription guestTenant;
  54. private RDFESubscription guestRdfeSubscription;
  55. private CSMSubscription guestCsmSubscription;
  56. private AzureSMProfile currentProfile;
  57. public ProfileClientTests(ITestOutputHelper output)
  58. {
  59. XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
  60. SetMockData();
  61. currentProfile = new AzureSMProfile();
  62. }
  63. [Fact]
  64. public void ProfileMigratesOldData()
  65. {
  66. MemoryDataStore dataStore = new MemoryDataStore();
  67. dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
  68. AzureSession.DataStore = dataStore;
  69. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  70. ProfileClient client = new ProfileClient(currentProfile);
  71. Assert.False(dataStore.FileExists(oldProfileDataPath));
  72. Assert.True(dataStore.FileExists(newProfileDataPath));
  73. }
  74. [Fact]
  75. public void NewProfileFromCertificateWithNullsThrowsArgumentNullException()
  76. {
  77. MemoryDataStore dataStore = new MemoryDataStore();
  78. AzureSession.DataStore = dataStore;
  79. AzureSMProfile newProfile = new AzureSMProfile();
  80. ProfileClient client1 = new ProfileClient(newProfile);
  81. Assert.Throws<ArgumentNullException>(() =>
  82. client1.InitializeProfile(null, Guid.NewGuid(), new X509Certificate2(), "foo"));
  83. Assert.Throws<ArgumentNullException>(() =>
  84. client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"], Guid.NewGuid(), null, "foo"));
  85. }
  86. [Fact]
  87. public void NewProfileFromCertificateReturnsProfile()
  88. {
  89. MemoryDataStore dataStore = new MemoryDataStore();
  90. AzureSession.DataStore = dataStore;
  91. AzureSMProfile newProfile = new AzureSMProfile();
  92. ProfileClient client1 = new ProfileClient(newProfile);
  93. var subscriptionId = Guid.NewGuid();
  94. var certificate = new X509Certificate2(Convert.FromBase64String(dummyCertificate));
  95. client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"],
  96. subscriptionId, certificate, null);
  97. Assert.Equal("AzureCloud", newProfile.DefaultSubscription.Environment);
  98. Assert.Equal(subscriptionId, newProfile.DefaultSubscription.Id);
  99. Assert.Equal(certificate.Thumbprint, newProfile.DefaultSubscription.Account);
  100. Assert.False(newProfile.DefaultSubscription.Properties.ContainsKey(AzureSubscription.Property.StorageAccount));
  101. }
  102. [Fact]
  103. public void NewProfileFromAdCredentialsWithNullsThrowsArgumentNullException()
  104. {
  105. MemoryDataStore dataStore = new MemoryDataStore();
  106. AzureSession.DataStore = dataStore;
  107. AzureSMProfile newProfile = new AzureSMProfile();
  108. ProfileClient client1 = new ProfileClient(newProfile);
  109. Assert.Throws<ArgumentNullException>(() =>
  110. client1.InitializeProfile(null, Guid.NewGuid(), new AzureAccount(), null, "foo"));
  111. Assert.Throws<ArgumentNullException>(() =>
  112. client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"], Guid.NewGuid(), (AzureAccount)null, null, "foo"));
  113. }
  114. [Fact]
  115. public void NewProfileFromADReturnsProfile()
  116. {
  117. SetMocks(new[] { rdfeSubscription1, rdfeSubscription2 }.ToList(), new List<CSMSubscription>());
  118. rdfeSubscription2.ActiveDirectoryTenantId = "123";
  119. MemoryDataStore dataStore = new MemoryDataStore();
  120. AzureSession.DataStore = dataStore;
  121. AzureSMProfile newProfile = new AzureSMProfile();
  122. ProfileClient client1 = new ProfileClient(newProfile);
  123. var newAccount = new AzureAccount { Id = "foo" };
  124. newAccount.Properties[AzureAccount.Property.Tenants] = "123";
  125. client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"],
  126. new Guid(rdfeSubscription2.SubscriptionId), newAccount, null, null);
  127. Assert.Equal("AzureCloud", newProfile.DefaultSubscription.Environment);
  128. Assert.Equal(new Guid(rdfeSubscription2.SubscriptionId), newProfile.DefaultSubscription.Id);
  129. Assert.Equal(newAccount.Id, newProfile.DefaultSubscription.Account);
  130. Assert.False(newProfile.DefaultSubscription.Properties.ContainsKey(AzureSubscription.Property.StorageAccount));
  131. }
  132. [Fact]
  133. public void NewProfileWithAccessTokenReturnsProfile()
  134. {
  135. //SetMocks(new[] { rdfeSubscription1, rdfeSubscription2 }.ToList(), new[] { csmSubscription1 }.ToList());
  136. MemoryDataStore dataStore = new MemoryDataStore();
  137. AzureSession.DataStore = dataStore;
  138. AzureSMProfile newProfile = new AzureSMProfile();
  139. ProfileClient client1 = new ProfileClient(newProfile);
  140. client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"],
  141. new Guid(csmSubscription1.SubscriptionId), "accessToken", "accountId", null);
  142. Assert.Equal("AzureCloud", newProfile.DefaultSubscription.Environment);
  143. Assert.Equal(new Guid(csmSubscription1.SubscriptionId), newProfile.DefaultSubscription.Id);
  144. Assert.Equal("accountId", newProfile.DefaultSubscription.Account);
  145. Assert.Equal(AzureAccount.AccountType.AccessToken, newProfile.Context.Account.Type);
  146. Assert.Equal("accessToken", newProfile.Context.Account.Properties[AzureAccount.Property.AccessToken]);
  147. Assert.False(newProfile.DefaultSubscription.Properties.ContainsKey(AzureSubscription.Property.StorageAccount));
  148. }
  149. [Fact]
  150. public void NewProfileFromADWithMismatchSubscriptionThrows()
  151. {
  152. SetMocks(new[] { rdfeSubscription1, rdfeSubscription2 }.ToList(), new[] { csmSubscription1 }.ToList());
  153. MemoryDataStore dataStore = new MemoryDataStore();
  154. AzureSession.DataStore = dataStore;
  155. AzureSMProfile newProfile = new AzureSMProfile();
  156. ProfileClient client1 = new ProfileClient(newProfile);
  157. var newAccount = new AzureAccount { Id = "foo" };
  158. newAccount.Properties[AzureAccount.Property.Tenants] = "123";
  159. Assert.Throws<ArgumentException>(() => client1.InitializeProfile(AzureEnvironment.PublicEnvironments["AzureCloud"],
  160. Guid.NewGuid(), newAccount, null, null));
  161. }
  162. [Fact]
  163. public void ProfileMigratesOldDataOnce()
  164. {
  165. MemoryDataStore dataStore = new MemoryDataStore();
  166. dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
  167. AzureSession.DataStore = dataStore;
  168. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  169. ProfileClient client1 = new ProfileClient(currentProfile);
  170. Assert.False(dataStore.FileExists(oldProfileDataPath));
  171. Assert.True(dataStore.FileExists(newProfileDataPath));
  172. ProfileClient client2 = new ProfileClient(currentProfile);
  173. Assert.False(dataStore.FileExists(oldProfileDataPath));
  174. Assert.True(dataStore.FileExists(newProfileDataPath));
  175. }
  176. [Fact]
  177. [Trait(Category.AcceptanceType, Category.CheckIn)]
  178. public void ProfileMigratesAccountsAndDefaultSubscriptions()
  179. {
  180. MemoryDataStore dataStore = new MemoryDataStore();
  181. dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
  182. AzureSession.DataStore = dataStore;
  183. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  184. ProfileClient client = new ProfileClient(currentProfile);
  185. // Verify Environment migration
  186. Assert.Equal(6, client.Profile.Environments.Count);
  187. Assert.Equal("Current", client.Profile.Environments["Current"].Name);
  188. Assert.Equal("Dogfood", client.Profile.Environments["Dogfood"].Name);
  189. Assert.Equal("https://login.windows-ppe.net/", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.AdTenant]);
  190. Assert.Equal("https://management.core.windows.net/", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId]);
  191. Assert.Equal("https://df.gallery.azure-test.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.Gallery]);
  192. Assert.Equal("https://windows.azure-test.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ManagementPortalUrl]);
  193. Assert.Equal("https://auxnext.windows.azure-test.net/publishsettings/index", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.PublishSettingsFileUrl]);
  194. Assert.Equal("https://api-dogfood.resources.windows-int.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ResourceManager]);
  195. Assert.Equal("https://management-preview.core.windows-int.net/", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ServiceManagement]);
  196. Assert.Equal(".database.windows.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix]);
  197. // Verify subscriptions
  198. Assert.Equal(3, client.Profile.Subscriptions.Count);
  199. Assert.False(client.Profile.Subscriptions.ContainsKey(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E")));
  200. Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")));
  201. Assert.Equal("Test 2", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Name);
  202. Assert.True(client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].IsPropertySet(AzureSubscription.Property.Default));
  203. Assert.Equal("test@mail.com", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Account);
  204. Assert.Equal("Dogfood", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Environment);
  205. Assert.Equal("123", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Properties[AzureSubscription.Property.Tenants]);
  206. Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")));
  207. Assert.Equal("Test 3", client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Name);
  208. Assert.False(client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].IsPropertySet(AzureSubscription.Property.Default));
  209. Assert.Equal("test@mail.com", client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Account);
  210. Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Properties[AzureSubscription.Property.Tenants]);
  211. Assert.Equal(EnvironmentName.AzureCloud, client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Environment);
  212. Assert.Equal(EnvironmentName.AzureChinaCloud, client.Profile.Subscriptions[new Guid("c14d7dc5-ed4d-4346-a02f-9f1bcf78fb66")].Environment);
  213. // Verify accounts
  214. Assert.Equal(2, client.Profile.Accounts.Count);
  215. Assert.Equal("test@mail.com", client.Profile.Accounts["test@mail.com"].Id);
  216. Assert.Equal(AzureAccount.AccountType.User, client.Profile.Accounts["test@mail.com"].Type);
  217. Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
  218. .Contains(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F").ToString()));
  219. Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
  220. .Contains(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f").ToString()));
  221. Assert.True(client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E09"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
  222. .Contains(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f").ToString()));
  223. Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Tenants)
  224. .Contains("72f988bf-86f1-41af-91ab-2d7cd011db47"));
  225. Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Tenants)
  226. .Contains("123"));
  227. Assert.Equal("3AF24D48B97730E5C4C9CCB12397B5E046F79E09", client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E09"].Id);
  228. Assert.Equal(AzureAccount.AccountType.Certificate, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E09"].Type);
  229. Assert.Equal(0, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E09"].GetPropertyAsArray(AzureAccount.Property.Tenants).Length);
  230. Assert.Equal(2, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E09"].GetPropertyAsArray(AzureAccount.Property.Subscriptions).Length);
  231. }
  232. [Fact]
  233. public void ProfileMigratesAccountsSkipsBadOnesAndBacksUpFile()
  234. {
  235. MemoryDataStore dataStore = new MemoryDataStore();
  236. dataStore.VirtualStore[oldProfileDataPath] = oldProfileDataBadSubscription;
  237. AzureSession.DataStore = dataStore;
  238. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  239. ProfileClient client = new ProfileClient(currentProfile);
  240. // Verify Environment migration
  241. Assert.Equal(4, client.Profile.Environments.Count);
  242. // Verify subscriptions
  243. Assert.Equal(3, client.Profile.Subscriptions.Count);
  244. Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")));
  245. Assert.Equal("Test Bad Management Endpoint", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Name);
  246. Assert.Equal(EnvironmentName.AzureCloud, client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")].Environment);
  247. Assert.Equal("Test Null Management Endpoint", client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2ADFF")].Name);
  248. Assert.Equal(EnvironmentName.AzureCloud, client.Profile.Subscriptions[new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2ADFF")].Environment);
  249. Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")));
  250. Assert.Equal("Test Bad Cert", client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Name);
  251. // Verify accounts
  252. Assert.Equal(2, client.Profile.Accounts.Count);
  253. Assert.Equal("test@mail.com", client.Profile.Accounts["test@mail.com"].Id);
  254. Assert.Equal(AzureAccount.AccountType.User, client.Profile.Accounts["test@mail.com"].Type);
  255. Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
  256. .Contains(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F").ToString()));
  257. Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
  258. .Contains(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f").ToString()));
  259. Assert.True(client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E99"].GetPropertyAsArray(AzureAccount.Property.Subscriptions)
  260. .Contains(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f").ToString()));
  261. Assert.True(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Tenants)
  262. .Contains("72f988bf-86f1-41af-91ab-2d7cd011db47"));
  263. Assert.False(client.Profile.Accounts["test@mail.com"].GetPropertyAsArray(AzureAccount.Property.Tenants)
  264. .Contains("123"));
  265. Assert.Equal("3AF24D48B97730E5C4C9CCB12397B5E046F79E99", client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E99"].Id);
  266. Assert.Equal(AzureAccount.AccountType.Certificate, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E99"].Type);
  267. Assert.Equal(0, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E99"].GetPropertyAsArray(AzureAccount.Property.Tenants).Length);
  268. Assert.Equal(1, client.Profile.Accounts["3AF24D48B97730E5C4C9CCB12397B5E046F79E99"].GetPropertyAsArray(AzureAccount.Property.Subscriptions).Length);
  269. // Verify backup file
  270. Assert.True(dataStore.FileExists(oldProfileDataPathError));
  271. Assert.False(dataStore.FileExists(oldProfileDataPath));
  272. Assert.Equal(oldProfileDataBadSubscription, dataStore.ReadFileAsText(oldProfileDataPathError));
  273. }
  274. [Fact]
  275. public void ProfileMigratesCorruptedFileAndCreatedBackup()
  276. {
  277. MemoryDataStore dataStore = new MemoryDataStore();
  278. dataStore.VirtualStore[oldProfileDataPath] = oldProfileDataCorruptedFile;
  279. AzureSession.DataStore = dataStore;
  280. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  281. ProfileClient client = new ProfileClient(currentProfile);
  282. // Verify Environment migration
  283. Assert.Equal(4, client.Profile.Environments.Count);
  284. // Verify subscriptions
  285. Assert.Equal(0, client.Profile.Subscriptions.Count);
  286. // Verify accounts
  287. Assert.Equal(0, client.Profile.Accounts.Count);
  288. // Verify backup file
  289. Assert.True(dataStore.FileExists(oldProfileDataPathError));
  290. Assert.False(dataStore.FileExists(oldProfileDataPath));
  291. Assert.Equal(oldProfileDataCorruptedFile, dataStore.ReadFileAsText(oldProfileDataPathError));
  292. }
  293. [Fact]
  294. public void AddAzureAccountReturnsAccountWithAllSubscriptionsInRdfeMode()
  295. {
  296. SetMocks(new[] { rdfeSubscription1, rdfeSubscription2 }.ToList(), new[] { csmSubscription1 }.ToList());
  297. MemoryDataStore dataStore = new MemoryDataStore();
  298. dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
  299. AzureSession.DataStore = dataStore;
  300. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  301. ProfileClient client = new ProfileClient(currentProfile);
  302. var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "test", Type = AzureAccount.AccountType.User }, AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], null);
  303. Assert.Equal("test", account.Id);
  304. Assert.Equal(2, account.GetSubscriptions(client.Profile).Count);
  305. Assert.True(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(rdfeSubscription1.SubscriptionId)));
  306. Assert.True(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(rdfeSubscription2.SubscriptionId)));
  307. Assert.False(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(csmSubscription1.SubscriptionId)));
  308. }
  309. [Fact]
  310. public void AddAzureAccountFiltersEmptyAdClientsInRdfeMode()
  311. {
  312. var emptyTenantIdrdfeSubscription = new RDFESubscription
  313. {
  314. SubscriptionId = "16E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E",
  315. SubscriptionName = "RdfeSub1",
  316. SubscriptionStatus = Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionStatus.Active,
  317. ActiveDirectoryTenantId = ""
  318. };
  319. var disabledTenantIdrdfeSubscription = new RDFESubscription
  320. {
  321. SubscriptionId = "16E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E",
  322. SubscriptionName = "RdfeSub1",
  323. SubscriptionStatus = Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionStatus.Disabled,
  324. ActiveDirectoryTenantId = "B59BE059-5E3F-463B-8C1A-831A29819B52"
  325. };
  326. var deletedTenantIdrdfeSubscription = new RDFESubscription
  327. {
  328. SubscriptionId = "16E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E",
  329. SubscriptionName = "RdfeSub1",
  330. SubscriptionStatus = Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionStatus.Deleted,
  331. ActiveDirectoryTenantId = "B59BE059-5E3F-463B-8C1A-831A29819B52"
  332. };
  333. var deletingTenantIdrdfeSubscription = new RDFESubscription
  334. {
  335. SubscriptionId = "16E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E",
  336. SubscriptionName = "RdfeSub1",
  337. SubscriptionStatus = Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionStatus.Deleting,
  338. ActiveDirectoryTenantId = "B59BE059-5E3F-463B-8C1A-831A29819B52"
  339. };
  340. SetMocks(
  341. new[] { rdfeSubscription1, emptyTenantIdrdfeSubscription, disabledTenantIdrdfeSubscription, deletedTenantIdrdfeSubscription, deletingTenantIdrdfeSubscription }.ToList(),
  342. new[] { csmSubscription1 }.ToList());
  343. MemoryDataStore dataStore = new MemoryDataStore();
  344. dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
  345. AzureSession.DataStore = dataStore;
  346. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  347. ProfileClient client = new ProfileClient(currentProfile);
  348. var account = client.AddAccountAndLoadSubscriptions(
  349. new AzureAccount { Id = "test", Type = AzureAccount.AccountType.User },
  350. AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
  351. null);
  352. Assert.Equal("test", account.Id);
  353. Assert.Equal(1, account.GetSubscriptions(client.Profile).Count);
  354. Assert.True(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(rdfeSubscription1.SubscriptionId)));
  355. Assert.False(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(csmSubscription1.SubscriptionId)));
  356. }
  357. [Fact]
  358. public void AddAzureAccountReturnsAccountWithAllSubscriptionsInCsmMode()
  359. {
  360. SetMocks(new[] { rdfeSubscription1, rdfeSubscription2 }.ToList(), new[] { csmSubscription1 }.ToList());
  361. MemoryDataStore dataStore = new MemoryDataStore();
  362. dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
  363. AzureSession.DataStore = dataStore;
  364. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  365. ProfileClient client = new ProfileClient(currentProfile);
  366. var account = client.AddAccountAndLoadSubscriptions(
  367. new AzureAccount
  368. {
  369. Id = "test",
  370. Type = AzureAccount.AccountType.User
  371. },
  372. AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
  373. null);
  374. Assert.Equal("test", account.Id);
  375. Assert.Equal(2, account.GetSubscriptions(client.Profile).Count);
  376. Assert.True(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(rdfeSubscription1.SubscriptionId)));
  377. Assert.True(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(rdfeSubscription2.SubscriptionId)));
  378. Assert.False(account.GetSubscriptions(client.Profile).Any(s => s.Id == new Guid(csmSubscription1.SubscriptionId)));
  379. }
  380. /// <summary>
  381. /// Verify that if a user has a different identity in one tenantId, the identity is not added if it has no
  382. /// access to subscriptions
  383. /// </summary>
  384. [Fact]
  385. public void AddAzureAccountWithImpersonatedGuestWithNoSubscriptions()
  386. {
  387. SetMocks(new[] { rdfeSubscription1 }.ToList(),
  388. new List<Microsoft.Azure.Subscriptions.Models.Subscription>(),
  389. new[] { commonTenant, guestTenant }.ToList(),
  390. (userAccount, environment, tenant) =>
  391. {
  392. var token = new MockAccessToken
  393. {
  394. UserId = tenant == commonTenant.TenantId ? userAccount.Id : "UserB",
  395. AccessToken = "def",
  396. LoginType = LoginType.OrgId
  397. };
  398. userAccount.Id = token.UserId;
  399. return token;
  400. });
  401. MemoryDataStore dataStore = new MemoryDataStore();
  402. dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
  403. AzureSession.DataStore = dataStore;
  404. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  405. ProfileClient client = new ProfileClient(currentProfile);
  406. var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "UserA", Type = AzureAccount.AccountType.User }, AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], null);
  407. Assert.Equal("UserA", account.Id);
  408. Assert.Equal(1, account.GetSubscriptions(client.Profile).Count);
  409. var subrdfe1 = account.GetSubscriptions(client.Profile).FirstOrDefault(s => s.Id == new Guid(rdfeSubscription1.SubscriptionId));
  410. var userA = client.GetAccount("UserA");
  411. Assert.Throws<ArgumentException>(() => client.GetAccount("UserB"));
  412. Assert.NotNull(userA);
  413. Assert.Contains<string>(rdfeSubscription1.SubscriptionId, userA.GetPropertyAsArray(AzureAccount.Property.Subscriptions), StringComparer.OrdinalIgnoreCase);
  414. Assert.NotNull(subrdfe1);
  415. Assert.Equal("UserA", subrdfe1.Account);
  416. }
  417. /// <summary>
  418. /// Verify that multiple accounts can be added if a user has different identities in different domains, linked to the same login
  419. /// Verify that subscriptions with admin access for all accounts are added
  420. /// </summary>
  421. [Fact]
  422. public void AddAzureAccountWithImpersonatedGuestWithSubscriptions()
  423. {
  424. SetMocks(new[] { rdfeSubscription1, guestRdfeSubscription }.ToList(),
  425. new List<CSMSubscription>(),
  426. new[] { commonTenant, guestTenant }.ToList(),
  427. (userAccount, environment, tenant) =>
  428. {
  429. var token = new MockAccessToken
  430. {
  431. UserId = tenant == commonTenant.TenantId ? userAccount.Id : "UserB",
  432. AccessToken = "def",
  433. LoginType = LoginType.OrgId
  434. };
  435. userAccount.Id = token.UserId;
  436. return token;
  437. });
  438. MemoryDataStore dataStore = new MemoryDataStore();
  439. dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
  440. AzureSession.DataStore = dataStore;
  441. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  442. ProfileClient client = new ProfileClient(currentProfile);
  443. var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "UserA", Type = AzureAccount.AccountType.User },
  444. AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], null);
  445. Assert.Equal("UserA", account.Id);
  446. Assert.Equal(2, account.GetSubscriptions(client.Profile).Count);
  447. var subrdfe1 = account.GetSubscriptions(client.Profile).FirstOrDefault(s => s.Id == new Guid(rdfeSubscription1.SubscriptionId));
  448. var userA = client.GetAccount("UserA");
  449. var userB = client.GetAccount("UserB");
  450. var subGuest = userB.GetSubscriptions(client.Profile).FirstOrDefault(s => s.Id == new Guid(guestRdfeSubscription.SubscriptionId));
  451. Assert.NotNull(userA);
  452. Assert.NotNull(userB);
  453. Assert.Contains<string>(rdfeSubscription1.SubscriptionId, userA.GetPropertyAsArray(AzureAccount.Property.Subscriptions), StringComparer.OrdinalIgnoreCase);
  454. Assert.Contains<string>(guestRdfeSubscription.SubscriptionId, userB.GetPropertyAsArray(AzureAccount.Property.Subscriptions), StringComparer.OrdinalIgnoreCase);
  455. Assert.NotNull(subrdfe1);
  456. Assert.NotNull(subGuest);
  457. Assert.Equal("UserA", subrdfe1.Account);
  458. Assert.Equal("UserB", subGuest.Account);
  459. }
  460. /// <summary>
  461. /// Test that when accountId is added more than once with different capitalization, only a single accountId is added
  462. /// and that accounts can be retrieved case-insensitively
  463. /// </summary>
  464. [Fact]
  465. public void AddAzureAccountIsCaseInsensitive()
  466. {
  467. SetMocks(new[] { rdfeSubscription1, guestRdfeSubscription }.ToList(),
  468. new List<Microsoft.Azure.Subscriptions.Models.Subscription>(),
  469. new[] { commonTenant, guestTenant }.ToList(),
  470. (userAccount, environment, tenant) =>
  471. {
  472. var token = new MockAccessToken
  473. {
  474. UserId = tenant == commonTenant.TenantId ? userAccount.Id : "USERA",
  475. AccessToken = "def",
  476. LoginType = LoginType.OrgId
  477. };
  478. userAccount.Id = token.UserId;
  479. return token;
  480. });
  481. MemoryDataStore dataStore = new MemoryDataStore();
  482. dataStore.VirtualStore[oldProfileDataPath] = oldProfileData;
  483. AzureSession.DataStore = dataStore;
  484. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  485. ProfileClient client = new ProfileClient(currentProfile);
  486. var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "UserA", Type = AzureAccount.AccountType.User },
  487. AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], null);
  488. var userA = client.GetAccount("UserA");
  489. var secondUserA = client.GetAccount("USERA");
  490. Assert.NotNull(userA);
  491. Assert.NotNull(secondUserA);
  492. Assert.Equal(userA.Id, secondUserA.Id);
  493. }
  494. [Fact]
  495. public void GetAzureAccountReturnsAccountWithSubscriptions()
  496. {
  497. MemoryDataStore dataStore = new MemoryDataStore();
  498. AzureSession.DataStore = dataStore;
  499. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  500. ProfileClient client = new ProfileClient(currentProfile);
  501. client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
  502. client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
  503. client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
  504. client.Profile.Accounts[azureAccount.Id] = azureAccount;
  505. client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
  506. var account = client.ListAccounts("test").ToList();
  507. Assert.Equal(1, account.Count);
  508. Assert.Equal("test", account[0].Id);
  509. Assert.Equal(2, account[0].GetSubscriptions(client.Profile).Count);
  510. Assert.True(account[0].GetSubscriptions(client.Profile).Any(s => s.Id == azureSubscription1.Id));
  511. Assert.True(account[0].GetSubscriptions(client.Profile).Any(s => s.Id == azureSubscription2.Id));
  512. }
  513. [Fact]
  514. public void GetAzureAccountWithoutEnvironmentReturnsAccount()
  515. {
  516. MemoryDataStore dataStore = new MemoryDataStore();
  517. AzureSession.DataStore = dataStore;
  518. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  519. ProfileClient client = new ProfileClient(currentProfile);
  520. client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
  521. client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
  522. client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
  523. client.Profile.Accounts[azureAccount.Id] = azureAccount;
  524. client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
  525. var account = client.ListAccounts("test").ToList();
  526. Assert.Equal(1, account.Count);
  527. Assert.Equal("test", account[0].Id);
  528. Assert.Equal(2, account[0].GetSubscriptions(client.Profile).Count);
  529. Assert.True(account[0].GetSubscriptions(client.Profile).Any(s => s.Id == azureSubscription1.Id));
  530. Assert.True(account[0].GetSubscriptions(client.Profile).Any(s => s.Id == azureSubscription2.Id));
  531. }
  532. [Fact]
  533. public void GetAzureAccountReturnsEmptyEnumerationForNonExistingUser()
  534. {
  535. MemoryDataStore dataStore = new MemoryDataStore();
  536. AzureSession.DataStore = dataStore;
  537. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  538. ProfileClient client = new ProfileClient(currentProfile);
  539. client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
  540. client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
  541. client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
  542. client.Profile.Accounts[azureAccount.Id] = azureAccount;
  543. client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
  544. var account = client.ListAccounts("test2").ToList();
  545. Assert.Equal(0, account.Count);
  546. }
  547. [Fact]
  548. public void GetAzureAccountReturnsAllAccountsWithNullUser()
  549. {
  550. MemoryDataStore dataStore = new MemoryDataStore();
  551. AzureSession.DataStore = dataStore;
  552. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  553. ProfileClient client = new ProfileClient(currentProfile);
  554. client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
  555. client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
  556. client.Profile.Accounts[azureAccount.Id] = azureAccount;
  557. azureSubscription3withoutUser.Account = "test2";
  558. client.Profile.Accounts["test2"] = new AzureAccount
  559. {
  560. Id = "test2",
  561. Type = AzureAccount.AccountType.User,
  562. Properties = new Dictionary<AzureAccount.Property, string>
  563. {
  564. {AzureAccount.Property.Subscriptions, azureSubscription3withoutUser.Id.ToString()}
  565. }
  566. };
  567. client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
  568. client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
  569. var account = client.ListAccounts(null).ToList();
  570. Assert.Equal(2, account.Count);
  571. }
  572. [Fact]
  573. public void RemoveAzureAccountRemovesSubscriptions()
  574. {
  575. MemoryDataStore dataStore = new MemoryDataStore();
  576. AzureSession.DataStore = dataStore;
  577. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  578. ProfileClient client = new ProfileClient(currentProfile);
  579. client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
  580. client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
  581. client.Profile.Accounts[azureAccount.Id] = azureAccount;
  582. azureSubscription3withoutUser.Account = "test2";
  583. client.Profile.Accounts["test2"] = new AzureAccount
  584. {
  585. Id = "test2",
  586. Type = AzureAccount.AccountType.User,
  587. Properties = new Dictionary<AzureAccount.Property, string>
  588. {
  589. {AzureAccount.Property.Subscriptions, azureSubscription3withoutUser.Id.ToString()}
  590. }
  591. };
  592. client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
  593. client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
  594. List<string> log = new List<string>();
  595. client.WarningLog = log.Add;
  596. Assert.Equal(3, client.Profile.Subscriptions.Count);
  597. client.RemoveAccount("test2");
  598. Assert.Equal(2, client.Profile.Subscriptions.Count);
  599. Assert.Equal(0, log.Count);
  600. }
  601. [Fact]
  602. public void RemoveAzureAccountRemovesDefaultSubscriptionAndWritesWarning()
  603. {
  604. MemoryDataStore dataStore = new MemoryDataStore();
  605. AzureSession.DataStore = dataStore;
  606. ProfileClient client = new ProfileClient(currentProfile);
  607. client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
  608. client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
  609. client.Profile.Accounts[azureAccount.Id] = azureAccount;
  610. azureSubscription3withoutUser.Account = "test2";
  611. client.Profile.Accounts["test2"] = new AzureAccount
  612. {
  613. Id = "test2",
  614. Type = AzureAccount.AccountType.User,
  615. Properties = new Dictionary<AzureAccount.Property, string>
  616. {
  617. {AzureAccount.Property.Subscriptions, azureSubscription3withoutUser.Id.ToString()}
  618. }
  619. };
  620. client.Profile.Subscriptions[azureSubscription3withoutUser.Id] = azureSubscription3withoutUser;
  621. client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
  622. List<string> log = new List<string>();
  623. client.WarningLog = log.Add;
  624. Assert.Equal(3, client.Profile.Subscriptions.Count);
  625. var account = client.RemoveAccount("test");
  626. Assert.Equal(1, client.Profile.Subscriptions.Count);
  627. Assert.Equal("test", account.Id);
  628. Assert.Equal(2, account.GetPropertyAsArray(AzureAccount.Property.Subscriptions).Length);
  629. Assert.Equal(1, log.Count);
  630. Assert.Equal(
  631. "The default subscription is being removed. Use Select-AzureSubscription -Default <subscriptionName> to select a new default subscription.",
  632. log[0]);
  633. }
  634. [Fact]
  635. public void RemoveAzureAccountRemovesDefaultAccountFromSubscription()
  636. {
  637. MemoryDataStore dataStore = new MemoryDataStore();
  638. AzureSession.DataStore = dataStore;
  639. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  640. ProfileClient client = new ProfileClient(currentProfile);
  641. client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
  642. client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
  643. client.Profile.Accounts[azureAccount.Id] = azureAccount;
  644. azureSubscription3withoutUser.Account = "test2";
  645. client.Profile.Accounts["test2"] = new AzureAccount
  646. {
  647. Id = "test2",
  648. Type = AzureAccount.AccountType.User,
  649. Properties = new Dictionary<AzureAccount.Property, string>
  650. {
  651. {AzureAccount.Property.Subscriptions, azureSubscription1.Id.ToString()}
  652. }
  653. };
  654. client.Profile.Subscriptions[azureSubscription1.Id].Account = azureAccount.Id;
  655. client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
  656. var account = client.RemoveAccount(azureAccount.Id);
  657. Assert.Equal("test2", client.Profile.Subscriptions[azureSubscription1.Id].Account);
  658. }
  659. [Fact]
  660. public void RemoveAzureAccountRemovesInMemoryAccount()
  661. {
  662. MemoryDataStore dataStore = new MemoryDataStore();
  663. AzureSession.DataStore = dataStore;
  664. ProfileClient client = new ProfileClient(currentProfile);
  665. client.Profile.Subscriptions[azureSubscription1.Id] = azureSubscription1;
  666. client.Profile.Subscriptions[azureSubscription2.Id] = azureSubscription2;
  667. client.Profile.Accounts[azureAccount.Id] = azureAccount;
  668. azureSubscription3withoutUser.Account = "test2";
  669. client.Profile.Accounts["test2"] = new AzureAccount
  670. {
  671. Id = "test2",
  672. Type = AzureAccount.AccountType.User,
  673. Properties = new Dictionary<AzureAccount.Property, string>
  674. {
  675. {AzureAccount.Property.Subscriptions, azureSubscription1.Id.ToString()}
  676. }
  677. };
  678. client.Profile.Subscriptions[azureSubscription1.Id].Account = azureAccount.Id;
  679. client.Profile.Environments[azureEnvironment.Name] = azureEnvironment;
  680. currentProfile.DefaultSubscription = azureSubscription1;
  681. client.RemoveAccount(azureAccount.Id);
  682. Assert.Equal("test2", currentProfile.Context.Account.Id);
  683. Assert.Equal("test2", currentProfile.Context.Subscription.Account);
  684. Assert.Equal(azureSubscription1.Id, currentProfile.Context.Subscription.Id);
  685. client.RemoveAccount("test2");
  686. Assert.Null(currentProfile.Context.Account);
  687. Assert.Null(currentProfile.Context.Subscription);
  688. Assert.Null(currentProfile.Context.Environment);
  689. }
  690. [Fact]
  691. public void AddAzureEnvironmentAddsEnvironment()
  692. {
  693. MemoryDataStore dataStore = new MemoryDataStore();
  694. AzureSession.DataStore = dataStore;
  695. currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  696. ProfileClient client = new ProfileClient(currentProfile);