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

/src/ServiceManagement/Services/Commands.Test/Profile/ProfileCmdltsTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 1007 lines | 790 code | 125 blank | 92 comment | 3 complexity | d1f5423e22631f7c28a8e4bb3027f28a 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 System.Collections;
  15. using System.Security;
  16. using System.Text;
  17. using Hyak.Common;
  18. using Microsoft.Azure.Commands.Common.Authentication;
  19. using Microsoft.Azure.Commands.Common.Authentication.Models;
  20. using Microsoft.IdentityModel.Clients.ActiveDirectory;
  21. using Microsoft.ServiceBus.Management;
  22. using Microsoft.WindowsAzure.Commands.Common;
  23. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  24. using Microsoft.WindowsAzure.Commands.Profile;
  25. using Microsoft.WindowsAzure.Commands.Profile.Models;
  26. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Linq;
  31. using System.Management.Automation;
  32. using System.Reflection;
  33. using System.Security.Cryptography.X509Certificates;
  34. using Microsoft.Azure.Commands.Common.Authentication.Factories;
  35. using Microsoft.Azure.ServiceManagemenet.Common;
  36. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  37. using Xunit;
  38. namespace Microsoft.WindowsAzure.Commands.Test.Profile
  39. {
  40. public class ProfileCmdltsTests
  41. {
  42. private AzureSubscription azureSubscription1;
  43. private AzureSubscription azureSubscription2;
  44. private AzureEnvironment azureEnvironment;
  45. private AzureAccount azureAccount;
  46. private MockCommandRuntime commandRuntimeMock;
  47. private X509Certificate2 SampleCertificate = new X509Certificate2(Convert.FromBase64String(@"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"), string.Empty);
  48. private MemoryDataStore dataStore;
  49. public ProfileCmdltsTests()
  50. : base()
  51. {
  52. dataStore = new MemoryDataStore();
  53. AzureSession.DataStore = dataStore;
  54. commandRuntimeMock = new MockCommandRuntime();
  55. SetMockData();
  56. AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory();
  57. }
  58. [Fact]
  59. [Trait(Category.AcceptanceType, Category.CheckIn)]
  60. public void ClearAzureProfileClearsDefaultProfile()
  61. {
  62. ClearAzureProfileCommand cmdlt = new ClearAzureProfileCommand();
  63. // Setup
  64. var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  65. AzureSMCmdlet.CurrentProfile = profile;
  66. ProfileClient client = new ProfileClient(profile);
  67. client.AddOrSetAccount(azureAccount);
  68. client.AddOrSetEnvironment(azureEnvironment);
  69. client.AddOrSetSubscription(azureSubscription1);
  70. client.Profile.Save();
  71. cmdlt.CommandRuntime = commandRuntimeMock;
  72. cmdlt.Force = new SwitchParameter(true);
  73. // Act
  74. cmdlt.InvokeBeginProcessing();
  75. cmdlt.ExecuteCmdlet();
  76. cmdlt.InvokeEndProcessing();
  77. // Verify
  78. client = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
  79. Assert.Equal(0, client.Profile.Subscriptions.Count);
  80. Assert.Equal(0, client.Profile.Accounts.Count);
  81. Assert.Equal(4, client.Profile.Environments.Count); //only default environments
  82. }
  83. [Fact]
  84. [Trait(Category.AcceptanceType, Category.CheckIn)]
  85. public void ClearAzureProfileClearsCustomProfile()
  86. {
  87. string subscriptionDataFile = Path.GetTempFileName();
  88. ClearAzureProfileCommand cmdlt = new ClearAzureProfileCommand();
  89. // Setup
  90. ProfileClient client = new ProfileClient(new AzureSMProfile(subscriptionDataFile));
  91. client.AddOrSetAccount(azureAccount);
  92. client.AddOrSetEnvironment(azureEnvironment);
  93. client.AddOrSetSubscription(azureSubscription1);
  94. client.Profile.Save();
  95. cmdlt.CommandRuntime = commandRuntimeMock;
  96. cmdlt.Force = new SwitchParameter(true);
  97. cmdlt.Profile = new AzureSMProfile(subscriptionDataFile);
  98. // Act
  99. cmdlt.InvokeBeginProcessing();
  100. cmdlt.ExecuteCmdlet();
  101. cmdlt.InvokeEndProcessing();
  102. // Verify
  103. client = new ProfileClient(new AzureSMProfile(subscriptionDataFile));
  104. Assert.Equal(0, client.Profile.Subscriptions.Count);
  105. Assert.Equal(0, client.Profile.Accounts.Count);
  106. Assert.Equal(4, client.Profile.Environments.Count); //only default environments
  107. }
  108. [Fact]
  109. [Trait(Category.AcceptanceType, Category.CheckIn)]
  110. public void ClearAzureProfileClearsTokenCache()
  111. {
  112. var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  113. AzureSMCmdlet.CurrentProfile = profile;
  114. ClearAzureProfileCommand cmdlt = new ClearAzureProfileCommand();
  115. AzureSession.DataStore = new MemoryDataStore();
  116. AzureSession.TokenCache = new ProtectedFileTokenCache(Path.Combine(AzureSession.ProfileDirectory, AzureSession.TokenCacheFile));
  117. cmdlt.CommandRuntime = commandRuntimeMock;
  118. cmdlt.Force = new SwitchParameter(true);
  119. // Act
  120. cmdlt.InvokeBeginProcessing();
  121. var tokenCache = AzureSession.TokenCache as ProtectedFileTokenCache;
  122. tokenCache.HasStateChanged = true;
  123. // HACK: Do not look at this code
  124. TokenCacheNotificationArgs args = new TokenCacheNotificationArgs();
  125. typeof(TokenCacheNotificationArgs).GetProperty("ClientId").SetValue(args, "123");
  126. typeof(TokenCacheNotificationArgs).GetProperty("Resource").SetValue(args, "123");
  127. typeof(TokenCacheNotificationArgs).GetProperty("TokenCache").SetValue(args, tokenCache);
  128. typeof(TokenCacheNotificationArgs).GetProperty("UniqueId").SetValue(args, "test@live.com");
  129. typeof(TokenCacheNotificationArgs).GetProperty("DisplayableId").SetValue(args, "test@live.com");
  130. AuthenticationResult authenticationResult =
  131. typeof(AuthenticationResult).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
  132. null, new Type[] { typeof(string), typeof(string), typeof(string), typeof(DateTimeOffset) }, null).Invoke(new object[]
  133. {
  134. "foo", "123", "123",
  135. new DateTimeOffset(DateTime.Now.AddDays(1))
  136. }) as AuthenticationResult;
  137. var storeToCache = typeof(TokenCache).GetMethod("StoreToCache", BindingFlags.Instance | BindingFlags.NonPublic);
  138. storeToCache.Invoke(tokenCache,
  139. new object[] { authenticationResult, "Common", "123", "123", 0, null});
  140. tokenCache.AfterAccess.Invoke(args);
  141. Assert.Equal(1, tokenCache.ReadItems().Count());
  142. cmdlt.ExecuteCmdlet();
  143. cmdlt.InvokeEndProcessing();
  144. // Verify
  145. Assert.Equal(0, tokenCache.ReadItems().Count());
  146. }
  147. [Fact]
  148. [Trait(Category.AcceptanceType, Category.CheckIn)]
  149. public void DeleteCorruptedTokenCache()
  150. {
  151. //setup
  152. string testFileName = @"c:\foobar\TokenCache.dat";
  153. AzureSession.DataStore.WriteFile(testFileName, new byte[] { 0, 1 });
  154. //Act
  155. ProtectedFileTokenCache tokenCache = new ProtectedFileTokenCache(testFileName);
  156. //Assert
  157. Assert.False(AzureSession.DataStore.FileExists(testFileName));
  158. }
  159. [Fact]
  160. [Trait(Category.AcceptanceType, Category.CheckIn)]
  161. public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
  162. {
  163. RunMockedCmdletTest(() =>
  164. {
  165. var profile =
  166. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  167. ProfileClient client = new ProfileClient(profile);
  168. AzureSMProfileProvider.Instance.Profile = profile;
  169. SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
  170. // Setup
  171. cmdlt.CommandRuntime = commandRuntimeMock;
  172. cmdlt.SubscriptionId = Guid.NewGuid().ToString();
  173. cmdlt.SubscriptionName = "NewSubscriptionName";
  174. cmdlt.CurrentStorageAccountName = "NewCloudStorage";
  175. cmdlt.Certificate = SampleCertificate;
  176. // Act
  177. cmdlt.InvokeBeginProcessing();
  178. cmdlt.ExecuteCmdlet();
  179. cmdlt.InvokeEndProcessing();
  180. // Verify
  181. var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
  182. var newAccount = client.Profile.Accounts[SampleCertificate.Thumbprint];
  183. Assert.Equal(cmdlt.SubscriptionName, newSubscription.Name);
  184. Assert.Equal(EnvironmentName.AzureCloud, newSubscription.Environment);
  185. Assert.True(
  186. newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)
  187. .Contains(string.Format("AccountName={0}", cmdlt.CurrentStorageAccountName)));
  188. Assert.Equal(newAccount.Id, newSubscription.Account);
  189. Assert.Equal(AzureAccount.AccountType.Certificate, newAccount.Type);
  190. Assert.Equal(SampleCertificate.Thumbprint, newAccount.Id);
  191. Assert.Equal(cmdlt.SubscriptionId,
  192. newAccount.GetProperty(AzureAccount.Property.Subscriptions));
  193. });
  194. }
  195. [Fact]
  196. [Trait(Category.AcceptanceType, Category.CheckIn)]
  197. public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnAdd()
  198. {
  199. RunMockedCmdletTest(() =>
  200. {
  201. // Setup
  202. var profile =
  203. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  204. AzureSMCmdlet.CurrentProfile = profile;
  205. ProfileClient client = new ProfileClient(profile);
  206. client.AddOrSetEnvironment(azureEnvironment);
  207. client.Profile.Save();
  208. SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
  209. cmdlt.CommandRuntime = commandRuntimeMock;
  210. cmdlt.SubscriptionId = Guid.NewGuid().ToString();
  211. cmdlt.SubscriptionName = "NewSubscriptionName";
  212. cmdlt.CurrentStorageAccountName = "NewCloudStorage";
  213. cmdlt.Environment = azureEnvironment.Name;
  214. cmdlt.Certificate = SampleCertificate;
  215. // Act
  216. cmdlt.InvokeBeginProcessing();
  217. cmdlt.ExecuteCmdlet();
  218. cmdlt.InvokeEndProcessing();
  219. // Verify
  220. client =
  221. new ProfileClient(
  222. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory,
  223. AzureSession.ProfileFile)));
  224. var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
  225. Assert.Equal(cmdlt.SubscriptionName, newSubscription.Name);
  226. Assert.Equal(cmdlt.Environment, newSubscription.Environment);
  227. Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
  228. newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
  229. });
  230. }
  231. [Fact]
  232. [Trait(Category.AcceptanceType, Category.CheckIn)]
  233. public void SetAzureSubscriptionThrowsExceptionWithoutCertificateOnAdd()
  234. {
  235. SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
  236. // Setup
  237. var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  238. AzureSMCmdlet.CurrentProfile = profile;
  239. ProfileClient client = new ProfileClient(profile);
  240. client.AddOrSetEnvironment(azureEnvironment);
  241. client.Profile.Save();
  242. cmdlt.CommandRuntime = commandRuntimeMock;
  243. cmdlt.SubscriptionId = Guid.NewGuid().ToString();
  244. cmdlt.SubscriptionName = "NewSubscriptionName";
  245. cmdlt.CurrentStorageAccountName = "NewCloudStorage";
  246. cmdlt.Environment = azureEnvironment.Name;
  247. // Verify
  248. cmdlt.InvokeBeginProcessing();
  249. Assert.Throws<ArgumentException>(() => cmdlt.ExecuteCmdlet());
  250. }
  251. [Fact]
  252. [Trait(Category.AcceptanceType, Category.CheckIn)]
  253. public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnSet()
  254. {
  255. RunMockedCmdletTest(() =>
  256. {
  257. // Setup
  258. var profile =
  259. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  260. AzureSMCmdlet.CurrentProfile = profile;
  261. ProfileClient client = new ProfileClient(profile);
  262. client.AddOrSetAccount(azureAccount);
  263. client.AddOrSetEnvironment(azureEnvironment);
  264. client.AddOrSetSubscription(azureSubscription1);
  265. client.Profile.Save();
  266. SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
  267. cmdlt.CommandRuntime = commandRuntimeMock;
  268. cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
  269. cmdlt.CurrentStorageAccountName = "NewCloudStorage";
  270. cmdlt.Environment = azureEnvironment.Name;
  271. // Act
  272. cmdlt.InvokeBeginProcessing();
  273. cmdlt.ExecuteCmdlet();
  274. cmdlt.InvokeEndProcessing();
  275. // Verify
  276. client =
  277. new ProfileClient(
  278. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory,
  279. AzureSession.ProfileFile)));
  280. var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
  281. Assert.Equal(cmdlt.Environment, newSubscription.Environment);
  282. Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
  283. newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
  284. });
  285. }
  286. [Fact]
  287. [Trait(Category.AcceptanceType, Category.CheckIn)]
  288. public void SetAzureSubscriptionDerivesEnvironmentFromServiceEndpointParameterOnSet()
  289. {
  290. RunMockedCmdletTest(() =>
  291. {
  292. // Setup
  293. var profile =
  294. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  295. AzureSMCmdlet.CurrentProfile = profile;
  296. ProfileClient client = new ProfileClient(profile);
  297. client.AddOrSetAccount(azureAccount);
  298. client.AddOrSetEnvironment(azureEnvironment);
  299. client.AddOrSetSubscription(azureSubscription1);
  300. client.Profile.Save();
  301. SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
  302. cmdlt.CommandRuntime = commandRuntimeMock;
  303. cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
  304. cmdlt.CurrentStorageAccountName = "NewCloudStorage";
  305. cmdlt.ServiceEndpoint =
  306. azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement);
  307. // Act
  308. cmdlt.InvokeBeginProcessing();
  309. cmdlt.ExecuteCmdlet();
  310. cmdlt.InvokeEndProcessing();
  311. // Verify
  312. client =
  313. new ProfileClient(
  314. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory,
  315. AzureSession.ProfileFile)));
  316. var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
  317. Assert.Equal(cmdlt.Environment, newSubscription.Environment);
  318. Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
  319. newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
  320. });
  321. }
  322. [Fact]
  323. [Trait(Category.AcceptanceType, Category.CheckIn)]
  324. public void SetAzureSubscriptionDerivesEnvironmentFromResourcesEndpointParameterOnSet()
  325. {
  326. RunMockedCmdletTest(() =>
  327. {
  328. // Setup
  329. var profile =
  330. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  331. ProfileClient client = new ProfileClient(profile);
  332. AzureSMCmdlet.CurrentProfile = profile;
  333. client.AddOrSetAccount(azureAccount);
  334. client.AddOrSetEnvironment(azureEnvironment);
  335. client.AddOrSetSubscription(azureSubscription1);
  336. client.Profile.Save();
  337. SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
  338. cmdlt.CommandRuntime = commandRuntimeMock;
  339. cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
  340. cmdlt.CurrentStorageAccountName = "NewCloudStorage";
  341. cmdlt.ResourceManagerEndpoint =
  342. azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager);
  343. // Act
  344. cmdlt.InvokeBeginProcessing();
  345. cmdlt.ExecuteCmdlet();
  346. cmdlt.InvokeEndProcessing();
  347. // Verify
  348. client = new ProfileClient(profile);
  349. var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
  350. Assert.Equal(cmdlt.Environment, newSubscription.Environment);
  351. Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
  352. newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
  353. });
  354. }
  355. [Fact]
  356. [Trait(Category.AcceptanceType, Category.CheckIn)]
  357. public void SetAzureSubscriptionDerivesEnvironmentFromBothEndpointParameters()
  358. {
  359. RunMockedCmdletTest(() =>
  360. {
  361. // Setup
  362. var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  363. AzureSMCmdlet.CurrentProfile = profile;
  364. ProfileClient client = new ProfileClient(profile);
  365. client.AddOrSetAccount(azureAccount);
  366. client.AddOrSetEnvironment(azureEnvironment);
  367. client.AddOrSetSubscription(azureSubscription1);
  368. client.Profile.Save();
  369. SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
  370. cmdlt.CommandRuntime = commandRuntimeMock;
  371. cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
  372. cmdlt.CurrentStorageAccountName = "NewCloudStorage";
  373. cmdlt.ServiceEndpoint = azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement);
  374. cmdlt.ResourceManagerEndpoint = azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager);
  375. // Act
  376. cmdlt.InvokeBeginProcessing();
  377. cmdlt.ExecuteCmdlet();
  378. cmdlt.InvokeEndProcessing();
  379. // Verify
  380. client =
  381. new ProfileClient(
  382. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
  383. var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
  384. Assert.Equal(cmdlt.Environment, newSubscription.Environment);
  385. Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
  386. newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
  387. });
  388. }
  389. [Fact]
  390. [Trait(Category.AcceptanceType, Category.CheckIn)]
  391. public void SetAzureSubscriptionUpdatesSubscriptionWithCertificate()
  392. {
  393. RunMockedCmdletTest(() =>
  394. {
  395. // Setup
  396. var profile =
  397. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  398. AzureSMCmdlet.CurrentProfile = profile;
  399. ProfileClient client = new ProfileClient(profile);
  400. client.AddOrSetAccount(azureAccount);
  401. client.AddOrSetEnvironment(azureEnvironment);
  402. client.AddOrSetSubscription(azureSubscription1);
  403. client.Profile.Save();
  404. SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
  405. cmdlt.CommandRuntime = commandRuntimeMock;
  406. cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
  407. cmdlt.CurrentStorageAccountName = "NewCloudStorage";
  408. cmdlt.Certificate = SampleCertificate;
  409. // Act
  410. cmdlt.InvokeBeginProcessing();
  411. cmdlt.ExecuteCmdlet();
  412. cmdlt.InvokeEndProcessing();
  413. // Verify
  414. client =
  415. new ProfileClient(
  416. new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory,
  417. AzureSession.ProfileFile)));
  418. var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
  419. var newAccount = client.Profile.Accounts[SampleCertificate.Thumbprint];
  420. var existingAccount = client.Profile.Accounts[azureAccount.Id];
  421. Assert.Equal(azureEnvironment.Name, newSubscription.Environment);
  422. Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
  423. newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
  424. Assert.Equal(newAccount.Id, newSubscription.Account);
  425. Assert.Equal(AzureAccount.AccountType.Certificate, newAccount.Type);
  426. Assert.Equal(SampleCertificate.Thumbprint, newAccount.Id);
  427. Assert.Equal(cmdlt.SubscriptionId,
  428. newAccount.GetProperty(AzureAccount.Property.Subscriptions));
  429. Assert.Equal(azureAccount.Id, existingAccount.Id);
  430. Assert.Equal(AzureAccount.AccountType.User, existingAccount.Type);
  431. Assert.True(
  432. existingAccount.GetPropertyAsArray(AzureAccount.Property.Subscriptions)
  433. .Contains(cmdlt.SubscriptionId));
  434. });
  435. }
  436. [Fact]
  437. [Trait(Category.AcceptanceType, Category.CheckIn)]
  438. public void ImportPublishSettingsFileSelectsCorrectEnvironment()
  439. {
  440. ImportAzurePublishSettingsCommand cmdlt = new ImportAzurePublishSettingsCommand();
  441. // Setup
  442. AzureSession.DataStore.WriteFile("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings",
  443. Commands.Common.Test.Properties.Resources.ValidProfileChina);
  444. var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  445. AzureSMCmdlet.CurrentProfile = profile;
  446. ProfileClient client = new ProfileClient(profile);
  447. var oldDataStore = FileUtilities.DataStore;
  448. FileUtilities.DataStore = AzureSession.DataStore;
  449. var expectedEnv = "AzureChinaCloud";
  450. var expected = client.ImportPublishSettings("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings", null);
  451. cmdlt.CommandRuntime = commandRuntimeMock;
  452. cmdlt.ProfileClient = new ProfileClient(profile);
  453. cmdlt.PublishSettingsFile = "ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings";
  454. try
  455. {
  456. // Act
  457. cmdlt.InvokeBeginProcessing();
  458. AzureSession.DataStore = FileUtilities.DataStore;
  459. cmdlt.ExecuteCmdlet();
  460. cmdlt.InvokeEndProcessing();
  461. // Verify
  462. foreach (var subscription in expected)
  463. {
  464. Assert.Equal(cmdlt.ProfileClient.GetSubscription(subscription.Id).Environment, expectedEnv);
  465. }
  466. Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
  467. }
  468. finally
  469. {
  470. // Cleanup
  471. FileUtilities.DataStore = oldDataStore;
  472. }
  473. }
  474. [Fact]
  475. [Trait(Category.AcceptanceType, Category.CheckIn)]
  476. public void ImportPublishSettingsFileOverwritesEnvironment()
  477. {
  478. ImportAzurePublishSettingsCommand cmdlt = new ImportAzurePublishSettingsCommand();
  479. var oldAzureDataStore = AzureSession.DataStore;
  480. AzureSession.DataStore = new MemoryDataStore();
  481. // Setup
  482. AzureSession.DataStore.WriteFile("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings",
  483. Commands.Common.Test.Properties.Resources.ValidProfileChina);
  484. var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  485. AzureSMCmdlet.CurrentProfile = profile;
  486. ProfileClient client = new ProfileClient(profile);
  487. var oldDataStore = FileUtilities.DataStore;
  488. FileUtilities.DataStore = AzureSession.DataStore;
  489. var expectedEnv = "AzureCloud";
  490. var expected = client.ImportPublishSettings("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings", expectedEnv);
  491. cmdlt.CommandRuntime = commandRuntimeMock;
  492. cmdlt.ProfileClient = client;
  493. cmdlt.PublishSettingsFile = "ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings";
  494. cmdlt.Environment = expectedEnv;
  495. try
  496. {
  497. // Act
  498. cmdlt.InvokeBeginProcessing();
  499. cmdlt.ExecuteCmdlet();
  500. cmdlt.InvokeEndProcessing();
  501. // Verify
  502. foreach (var subscription in expected)
  503. {
  504. Assert.Equal(cmdlt.ProfileClient.GetSubscription(subscription.Id).Environment, expectedEnv);
  505. }
  506. Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
  507. }
  508. finally
  509. {
  510. // Cleanup
  511. FileUtilities.DataStore = oldDataStore;
  512. AzureSession.DataStore = oldAzureDataStore;
  513. }
  514. }
  515. [Fact]
  516. [Trait(Category.AcceptanceType, Category.CheckIn)]
  517. public void ImportPublishSettingsWorksForCustomProfile()
  518. {
  519. ImportAzurePublishSettingsCommand cmdlt = new ImportAzurePublishSettingsCommand();
  520. var oldAzureDataStore = AzureSession.DataStore;
  521. AzureSession.DataStore = new MemoryDataStore();
  522. // Setup
  523. AzureSession.DataStore.WriteFile("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings",
  524. Commands.Common.Test.Properties.Resources.ValidProfileChina);
  525. var oldProfile = new AzureSMProfile();
  526. AzureSMCmdlet.CurrentProfile = oldProfile;
  527. var profile = new AzureSMProfile();
  528. ProfileClient client = new ProfileClient(profile);
  529. var oldDataStore = FileUtilities.DataStore;
  530. FileUtilities.DataStore = AzureSession.DataStore;
  531. var expectedEnv = "AzureCloud";
  532. var expected = client.ImportPublishSettings("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings", expectedEnv);
  533. AzureSMCmdlet.CurrentProfile = new AzureSMProfile();
  534. cmdlt.Profile = profile;
  535. cmdlt.CommandRuntime = commandRuntimeMock;
  536. cmdlt.ProfileClient = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
  537. cmdlt.PublishSettingsFile = "ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings";
  538. cmdlt.Environment = expectedEnv;
  539. try
  540. {
  541. // Act
  542. cmdlt.InvokeBeginProcessing();
  543. cmdlt.ExecuteCmdlet();
  544. cmdlt.InvokeEndProcessing();
  545. // Verify
  546. foreach (var subscription in expected)
  547. {
  548. Assert.Equal(client.GetSubscription(subscription.Id).Environment, expectedEnv);
  549. }
  550. Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
  551. Assert.Equal(oldProfile.Subscriptions.Count, 0);
  552. Assert.Equal(oldProfile.Accounts.Count, 0);
  553. }
  554. finally
  555. {
  556. // Cleanup
  557. FileUtilities.DataStore = oldDataStore;
  558. AzureSession.DataStore = oldAzureDataStore;
  559. }
  560. }
  561. [Fact]
  562. [Trait(Category.AcceptanceType, Category.CheckIn)]
  563. public void SelectDefaultAzureSubscriptionByNameUpdatesProfile()
  564. {
  565. var client = SetupDefaultProfile();
  566. SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
  567. // Setup
  568. cmdlt.CommandRuntime = commandRuntimeMock;
  569. cmdlt.SetParameterSet("SelectDefaultSubscriptionByNameParameterSet");
  570. cmdlt.SubscriptionName = azureSubscription2.Name;
  571. cmdlt.Default = new SwitchParameter(true);
  572. Assert.NotEqual(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
  573. // Act
  574. cmdlt.InvokeBeginProcessing();
  575. cmdlt.ExecuteCmdlet();
  576. cmdlt.InvokeEndProcessing();
  577. // Verify
  578. client = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
  579. Assert.NotNull(client.Profile.DefaultSubscription);
  580. Assert.Equal(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
  581. }
  582. [Fact]
  583. [Trait(Category.AcceptanceType, Category.CheckIn)]
  584. public void SelectAzureSubscriptionByNameUpdatesProfile()
  585. {
  586. SetupDefaultProfile();
  587. SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
  588. // Setup
  589. cmdlt.CommandRuntime = commandRuntimeMock;
  590. cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
  591. cmdlt.SubscriptionName = azureSubscription2.Name;
  592. // Act
  593. cmdlt.InvokeBeginProcessing();
  594. cmdlt.ExecuteCmdlet();
  595. cmdlt.InvokeEndProcessing();
  596. // Verify
  597. Assert.NotNull(cmdlt.Profile.Context.Subscription);
  598. Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
  599. }
  600. [Fact]
  601. [Trait(Category.AcceptanceType, Category.CheckIn)]
  602. public void SelectAzureSubscriptionByNameUpdatesCustomProfile()
  603. {
  604. var client = SetupDefaultProfile();
  605. var profile = SetupCustomProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
  606. SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
  607. // Setup
  608. cmdlt.CommandRuntime = commandRuntimeMock;
  609. cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
  610. cmdlt.SubscriptionName = azureSubscription2.Name;
  611. cmdlt.Profile = profile;
  612. // Act
  613. cmdlt.InvokeBeginProcessing();
  614. cmdlt.ExecuteCmdlet();
  615. cmdlt.InvokeEndProcessing();
  616. // Verify
  617. Assert.NotNull(cmdlt.Profile.Context.Subscription);
  618. Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
  619. // current profile unchanged
  620. Assert.Equal(azureSubscription1.Id, client.Profile.Context.Subscription.Id);
  621. }
  622. [Fact]
  623. [Trait(Category.AcceptanceType, Category.CheckIn)]
  624. public void SelectAzureSubscriptionByNameWithoutAccountPreservesTheAccount()
  625. {
  626. SetupDefaultProfile();
  627. SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
  628. // Setup
  629. cmdlt.CommandRuntime = commandRuntimeMock;
  630. cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
  631. cmdlt.SubscriptionName = azureSubscription2.Name;
  632. // Act
  633. cmdlt.InvokeBeginProcessing();
  634. cmdlt.ExecuteCmdlet();
  635. cmdlt.InvokeEndProcessing();
  636. // Verify
  637. Assert.NotNull(cmdlt.Profile.Context.Subscription);
  638. Assert.Equal(azureSubscription2.Account, cmdlt.Profile.Context.Subscription.Account);
  639. Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
  640. }
  641. [Fact]
  642. [Trait(Category.AcceptanceType, Category.CheckIn)]
  643. public void SelectAzureSubscriptionByIdWithoutAccountPreservesTheAccount()
  644. {
  645. SetupDefaultProfile();
  646. SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
  647. // Setup
  648. cmdlt.CommandRuntime = commandRuntimeMock;
  649. cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
  650. cmdlt.SubscriptionId = azureSubscription2.Id.ToString();
  651. // Act
  652. cmdlt.InvokeBeginProcessing();
  653. cmdlt.ExecuteCmdlet();
  654. cmdlt.InvokeEndProcessing();
  655. // Verify
  656. Assert.NotNull(cmdlt.Profile.Context.Subscription);
  657. Assert.Equal(azureSubscription2.Account, cmdlt.Profile.Context.Subscription.Account);
  658. Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
  659. }
  660. [Fact]
  661. [Trait(Category.AcceptanceType, Category.CheckIn)]
  662. public void SelectAzureSubscriptionWithoutPassthroughDoesNotPrint()
  663. {
  664. SetupDefaultProfile();
  665. SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
  666. // Setup
  667. cmdlt.CommandRuntime = commandRuntimeMock;
  668. cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
  669. cmdlt.SubscriptionName = azureSubscription2.Name;
  670. // Act
  671. cmdlt.InvokeBeginProcessing();
  672. cmdlt.ExecuteCmdlet();
  673. cmdlt.InvokeEndProcessing();
  674. // Verify
  675. Assert.Equal(0, commandRuntimeMock.OutputPipeline.Count);
  676. }
  677. [Fact]
  678. [Trait(Category.AcceptanceType, Category.CheckIn)]
  679. public void SelectAzureSubscriptionWithPassthroughPrintsSubscription()
  680. {
  681. SetupDefaultProfile();
  682. SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
  683. // Setup
  684. cmdlt.CommandRuntime = commandRuntimeMock;
  685. cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
  686. cmdlt.SubscriptionName = azureSubscription2.Name;
  687. cmdlt.PassThru = new SwitchParameter(true);
  688. // Act
  689. cmdlt.InvokeBeginProcessing();
  690. cmdlt.ExecuteCmdlet();
  691. cmdlt.InvokeEndProcessing();
  692. // Verify
  693. Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
  694. Assert.True(commandRuntimeMock.OutputPipeline[0] is PSAzureSubscription);
  695. }
  696. [Fact]
  697. [Trait(Category.AcceptanceType, Category.CheckIn)]
  698. public void SelectDefaultAzureSubscriptionByIdAndNoDefaultUpdatesProfile()
  699. {
  700. var client = SetupDefaultProfile();
  701. SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
  702. // Setup
  703. cmdlt.CommandRuntime = commandRuntimeMock;
  704. cmdlt.SetParameterSet("SelectDefaultSubscriptionByIdParameterSet");
  705. cmdlt.SubscriptionId = azureSubscription2.Id.ToString();
  706. cmdlt.Default = new SwitchParameter(true);
  707. Assert.NotEqual(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
  708. // Act
  709. cmdlt.InvokeBeginProcessing();
  710. cmdlt.ExecuteCmdlet();
  711. cmdlt.InvokeEndProcessing();
  712. // Verify
  713. client = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
  714. Assert.NotNull(client.Profile.DefaultSubscription);
  715. Assert.Equal(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
  716. cmdlt = new SelectAzureSubscriptionCommand();
  717. // Setup
  718. cmdlt.CommandRuntime = commandRuntimeMock;
  719. cmdlt.SetParameterSet("NoDefaultSubscriptionParameterSet");
  720. cmdlt.NoDefault = new SwitchParameter(true);
  721. // Act
  722. cmdlt.InvokeBeginProcessing();
  723. cmdlt.ExecuteCmdlet();
  724. cmdlt.InvokeEndProcessing();
  725. // Verify
  726. client = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
  727. Assert.Null(client.Profile.DefaultSubscription);
  728. }
  729. [Fact]
  730. [Trait(Category.AcceptanceType, Category.CheckIn)]
  731. public void SelectAzureSubscriptionByInvalidIdThrowsException()
  732. {
  733. SetupDefaultProfile();
  734. SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
  735. // Setup
  736. cmdlt.CommandRuntime = commandRuntimeMock;
  737. cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
  738. string invalidGuid = Guid.NewGuid().ToString();
  739. cmdlt.SubscriptionId = invalidGuid;
  740. // Act
  741. cmdlt.InvokeBeginProcessing();
  742. try
  743. {
  744. cmdlt.ExecuteCmdlet();
  745. Assert.True(false);
  746. }
  747. catch (ArgumentException ex)
  748. {
  749. Assert.Contains(string.Format("The subscription id {0} doesn't exist.\r\nParameter name: id", invalidGuid), ex.Message);
  750. }
  751. }
  752. [Fact]
  753. [Trait(Category.AcceptanceType, Category.CheckIn)]
  754. public void SelectAzureSubscriptionByInvalidGuidThrowsException()
  755. {
  756. SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
  757. SetupDefaultProfile();
  758. // Setup
  759. cmdlt.CommandRuntime = commandRuntimeMock;
  760. cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
  761. string invalidGuid = "foo";
  762. cmdlt.SubscriptionId = invalidGuid;
  763. // Act
  764. cmdlt.InvokeBeginProcessing();
  765. try
  766. {
  767. cmdlt.ExecuteCmdlet();
  768. Assert.True(false);
  769. }
  770. catch (ArgumentException ex)
  771. {
  772. Assert.Contains(string.Format(Microsoft.WindowsAzure.Commands.Common.Properties.Resources.InvalidGuid, invalidGuid), ex.Message);
  773. }
  774. }
  775. [Fact]
  776. [Trait(Category.AcceptanceType, Category.CheckIn)]
  777. public void CanCreateProfileCertificateAuth()
  778. {
  779. RunCreateProfileTestForParams(
  780. (cmdlet) =>
  781. {
  782. cmdlet.Certificate = SampleCertificate;
  783. },
  784. NewAzureProfileCommand.CertificateParameterSet, ValidateCertificate);
  785. }
  786. [Fact]
  787. [Trait(Category.AcceptanceType, Category.CheckIn)]
  788. public void CanCreateProfileFromHashCertificateAuth()
  789. {
  790. RunCreateProfileTestForHashTable(
  791. (cmdlet) =>
  792. {
  793. cmdlet.Properties.Add(NewAzureProfileCommand.CertificateKey, SampleCertificate);
  794. }, ValidateCertificate);
  795. }
  796. [Fact]
  797. [Trait(Category.AcceptanceType, Category.CheckIn)]
  798. public void CanCreateProfileWithADAuth()
  799. {
  800. var credential = GenerateCredential();
  801. RunCreateProfileTestForParams(
  802. (cmdlet) =>
  803. {
  804. cmdlet.Credential = credential;
  805. }, NewAzureProfileCommand.CredentialsParameterSet,
  806. (profile) => ValidateCredential(credential, profile, AzureAccount.AccountType.User));
  807. }
  808. [Fact]
  809. [Trait(Category.AcceptanceType, Category.CheckIn)]
  810. public void CanCreateProfileFromHashWithADAuth()
  811. {
  812. var password = GeneratePassword();
  813. var credential = GenerateCredential(password);
  814. RunCreateProfileTestForHashTable(
  815. (cmdlet) =>
  816. {
  817. cmdlet.Properties[NewAzureProfileCommand.UsernameKey] = credential.UserName;
  818. cmdlet.Properties[NewAzureProfileCommand.PasswordKey] = password;
  819. }, (profile) => ValidateCredential(credential, profile, AzureAccount.AccountType.User));
  820. }
  821. [Fact]
  822. [Trait(Category.AcceptanceType, Category.CheckIn)]
  823. public void CanAddAccountToCustomProfile()
  824. {
  825. var cmdlet = new AddAzureAccount();
  826. var csmSubscription = Guid.NewGuid();
  827. var rdfeSubscription = Guid.NewGuid();
  828. var credential = GenerateCredential("mySillyPassword");
  829. var profile = new AzureSMProfile();
  830. var client = new ProfileClient(profile);
  831. cmdlet.Credential = credential;
  832. cmdlet.Profile = profile;
  833. cmdlet.SetParameterSet("User");
  834. AzureSession.ClientFactory =
  835. new MockClientFactory(
  836. new List<object>
  837. {
  838. ProfileClientHelper.CreateRdfeSubscriptionClient(rdfeSubscription, rdfeSubscription.ToString()),
  839. ProfileClientHelper.CreateCsmSubscriptionClient(new List<string>{csmSubscription.ToString()},
  840. new List<string>{csmSubscription.ToString(), rdfeSubscription.ToString()})
  841. }, true);
  842. AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory(credential.UserName,
  843. Guid.NewGuid().ToString());
  844. cmdlet.CommandRuntime = commandRuntimeMock;
  845. cmdlet.InvokeBeginProcessing();
  846. cmdlet.ExecuteCmdlet();
  847. cmdlet.InvokeEndProcessing();
  848. Assert.NotNull(profile.Subscriptions);
  849. Assert.NotNull(profile.Accounts);
  850. Assert.NotNull(profile.Environments);
  851. Assert.NotNull(profile.Context);
  852. Assert.Equal(profile.Subscriptions.Values.Count((s) => s.Id == csmSubscription), 0);
  853. Assert.Equal(profile.Subscriptions.Values.Count((s) => s.Id == rdfeSubscription), 1);
  854. Assert.Equal(profile.Accounts.Values.Count((s) => s.Id == credential.UserName), 1);
  855. Assert.Contains(rdfeSubscription.ToString(), profile.Accounts.First().Value.GetProperty(AzureAccount.Property.Subscriptions));
  856. Assert.Equal(profile.Context.Account.Id, credential.UserName);
  857. Assert.Equal(profile.Context.Subscription.Id, rdfeSubscription);
  858. }
  859. [Fact]
  860. [Trait(Category.AcceptanceType, Category.CheckIn)]
  861. public void AddAzureAccountThrowsForEmptySubscriptions()
  862. {
  863. var cmdlet = new AddAzureAccount();
  864. var csmSubscription = Guid.NewGuid();
  865. var rdfeSubscription = Guid.NewGuid();
  866. var credential = GenerateCredential("mySillyPassword");
  867. var profile = new AzureSMProfile();
  868. var client = new ProfileClient(profile);
  869. cmdlet.Credential = credential;
  870. cmdlet.Profile = profile;
  871. cmdlet.SetParameterSet("User");
  872. AzureSession.ClientFactory =
  873. new MockClientFactory(
  874. new List<object>
  875. {
  876. ProfileClientHelper.CreateRdfeSubscriptionClient(rdfeSubscription),
  877. ProfileClientHelper.CreateCsmSubscriptionClient(new List<string>(),
  878. new List<string>{csmSubscription.ToString(), rdfeSubscription.ToString()})
  879. }, true);
  880. AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory(credential.UserName,
  881. Guid.NewGuid().ToString());
  882. cmdlet.CommandRuntime = commandRuntimeMock;