PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ServiceManagement/RemoteApp/Commands.RemoteApp.Test/SecurityPrincipals/RemoteAppSecurityPrincipals.cs

https://gitlab.com/jslee1/azure-powershell
C# | 433 lines | 324 code | 78 blank | 31 comment | 21 complexity | 9f503b34832e3a171adc3c1b2a4ac7c5 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. namespace Microsoft.WindowsAzure.Commands.RemoteApp.Test
  15. {
  16. using LocalModels;
  17. using Common;
  18. using Microsoft.WindowsAzure.Management.RemoteApp.Cmdlets;
  19. using Microsoft.WindowsAzure.Management.RemoteApp.Models;
  20. using System;
  21. using System.Collections.Generic;
  22. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  23. using Xunit;
  24. public class AzureRemoteAppServiceUser : RemoteAppClientTest
  25. {
  26. private string userName = "user1";
  27. [Fact]
  28. [Trait(Category.AcceptanceType, Category.CheckIn)]
  29. public void GetAllUsers()
  30. {
  31. int countOfExpectedUsers = 0;
  32. GetAzureRemoteAppUser MockCmdlet = SetUpTestCommon<GetAzureRemoteAppUser>();
  33. // Required parameters for this test
  34. MockCmdlet.CollectionName = collectionName;
  35. // Setup the environment for testing this cmdlet
  36. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  37. countOfExpectedUsers = MockObject.SetUpDefaultRemoteAppSecurityPrincipals(remoteAppManagementClientMock, collectionName, userName);
  38. MockCmdlet.ResetPipelines();
  39. Log("Calling Get-AzureRemoteAppUser which should have {0} users.", countOfExpectedUsers);
  40. MockCmdlet.ExecuteCmdlet();
  41. if (MockCmdlet.runTime().ErrorStream.Count != 0)
  42. {
  43. Assert.True(false,
  44. String.Format("Get-AzureRemoteAppUser returned the following error {0}.",
  45. MockCmdlet.runTime().ErrorStream[0].Exception.Message
  46. )
  47. );
  48. }
  49. List<ConsentStatusModel> users = MockObject.ConvertList<ConsentStatusModel>(MockCmdlet.runTime().OutputPipeline);
  50. Assert.NotNull(users);
  51. Assert.True(users.Count == countOfExpectedUsers,
  52. String.Format("The expected number of users returned {0} does not match the actual {1}.",
  53. countOfExpectedUsers,
  54. users.Count
  55. )
  56. );
  57. Assert.True(MockObject.ContainsExpectedServicePrincipalList(MockObject.mockUsersConsents, users),
  58. "The actual result does not match the expected"
  59. );
  60. Log("The test for Get-AzureRemoteAppUser with {0} users completed successfully.", countOfExpectedUsers);
  61. }
  62. [Fact]
  63. [Trait(Category.AcceptanceType, Category.CheckIn)]
  64. public void GetAllUsersForApp()
  65. {
  66. int countOfExpectedUsers = 0;
  67. GetAzureRemoteAppUser MockCmdlet = SetUpTestCommon<GetAzureRemoteAppUser>();
  68. // Required parameters for this test
  69. MockCmdlet.CollectionName = collectionName;
  70. MockCmdlet.Alias = appAlias;
  71. // Setup the environment for testing this cmdlet
  72. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  73. countOfExpectedUsers = MockObject.SetUpRemoteAppSecurityPrincipalsForApp(remoteAppManagementClientMock, collectionName, appAlias, userName);
  74. MockCmdlet.ResetPipelines();
  75. Log("Calling Get-AzureRemoteAppUser which should have {0} users.", countOfExpectedUsers);
  76. MockCmdlet.ExecuteCmdlet();
  77. if (MockCmdlet.runTime().ErrorStream.Count != 0)
  78. {
  79. Assert.True(false,
  80. String.Format("Get-AzureRemoteAppUser returned the following error {0}.",
  81. MockCmdlet.runTime().ErrorStream[0].Exception.Message
  82. )
  83. );
  84. }
  85. List<ConsentStatusModel> users = MockObject.ConvertList<ConsentStatusModel>(MockCmdlet.runTime().OutputPipeline);
  86. Assert.NotNull(users);
  87. Assert.True(users.Count == countOfExpectedUsers,
  88. String.Format("The expected number of users returned {0} does not match the actual {1}.",
  89. countOfExpectedUsers,
  90. users.Count
  91. )
  92. );
  93. Assert.True(MockObject.ContainsExpectedServicePrincipalList(MockObject.mockUsersConsents, users),
  94. "The actual result does not match the expected"
  95. );
  96. Log("The test for Get-AzureRemoteAppUser with {0} users completed successfully.", countOfExpectedUsers);
  97. }
  98. [Fact]
  99. [Trait(Category.AcceptanceType, Category.CheckIn)]
  100. public void GetUsersByName()
  101. {
  102. int countOfExpectedUsers = 1;
  103. GetAzureRemoteAppUser MockCmdlet = SetUpTestCommon<GetAzureRemoteAppUser>();
  104. // Required parameters for this test
  105. MockCmdlet.CollectionName = collectionName;
  106. MockCmdlet.UserUpn = userName;
  107. // Setup the environment for testing this cmdlet
  108. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  109. MockObject.SetUpDefaultRemoteAppSecurityPrincipals(remoteAppManagementClientMock, collectionName, userName);
  110. MockCmdlet.ResetPipelines();
  111. Log("Calling Get-AzureRemoteAppUser to get this user {0}.", MockCmdlet.UserUpn);
  112. MockCmdlet.ExecuteCmdlet();
  113. if (MockCmdlet.runTime().ErrorStream.Count != 0)
  114. {
  115. Assert.True(false,
  116. String.Format("Get-AzureRemoteAppUser returned the following error {0}.",
  117. MockCmdlet.runTime().ErrorStream[0].Exception.Message
  118. )
  119. );
  120. }
  121. List<ConsentStatusModel> users = MockObject.ConvertList<ConsentStatusModel>(MockCmdlet.runTime().OutputPipeline);
  122. Assert.NotNull(users);
  123. Assert.True(users.Count == countOfExpectedUsers,
  124. String.Format("The expected number of users returned {0} does not match the actual {1}.",
  125. countOfExpectedUsers,
  126. users.Count
  127. )
  128. );
  129. Assert.True(MockObject.ContainsExpectedServicePrincipalList(MockObject.mockUsersConsents, users),
  130. "The actual result does not match the expected"
  131. );
  132. Log("The test for Get-AzureRemoteAppUser with {0} users completed successfully.", countOfExpectedUsers);
  133. }
  134. [Fact]
  135. [Trait(Category.AcceptanceType, Category.CheckIn)]
  136. public void AddMSAUserThatDoesntExist()
  137. {
  138. int countOfExistingUsers = 0;
  139. int countOfNewUsers = 0;
  140. AddAzureRemoteAppUser MockCmdlet = SetUpTestCommon<AddAzureRemoteAppUser>();
  141. // Required parameters for this test
  142. MockCmdlet.CollectionName = collectionName;
  143. MockCmdlet.UserUpn = new string[]
  144. {
  145. "testUser1",
  146. "testUser2",
  147. };
  148. // Setup the environment for testing this cmdlet
  149. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  150. countOfExistingUsers = MockObject.SetUpDefaultRemoteAppSecurityPrincipals(remoteAppManagementClientMock, collectionName, userName);
  151. countOfNewUsers = MockObject.SetUpRemoteAppUserToAdd(remoteAppManagementClientMock, collectionName, PrincipalProviderType.MicrosoftAccount, MockCmdlet.UserUpn);
  152. MockCmdlet.ResetPipelines();
  153. Log("Calling Add-AzureRemoteAppMSAUser and adding {0} users.", countOfNewUsers);
  154. MockCmdlet.ExecuteCmdlet();
  155. if (MockCmdlet.runTime().ErrorStream.Count != 0)
  156. {
  157. Assert.True(false,
  158. String.Format("Add-AzureRemoteAppMSAUser returned the following error {0}.",
  159. MockCmdlet.runTime().ErrorStream[0].Exception.Message
  160. )
  161. );
  162. }
  163. List<SecurityPrincipalOperationsResult> status = MockObject.ConvertList<SecurityPrincipalOperationsResult>(MockCmdlet.runTime().OutputPipeline);
  164. Assert.NotNull(status);
  165. Assert.True(MockObject.HasExpectedResults<SecurityPrincipalOperationsResult>(status, MockObject.ContainsExpectedStatus),
  166. "The actual result does not match the expected."
  167. );
  168. Log("The test for Add-AzureRemoteAppMSAUser successfully added {0} users the new count is {1}.", countOfNewUsers, countOfExistingUsers + countOfNewUsers);
  169. }
  170. [Fact]
  171. [Trait(Category.AcceptanceType, Category.CheckIn)]
  172. public void AddOrgIDUserThatDoesntExist()
  173. {
  174. int countOfExistingUsers = 0;
  175. int countOfNewUsers = 0;
  176. AddAzureRemoteAppUser MockCmdlet = SetUpTestCommon<AddAzureRemoteAppUser>();
  177. // Required parameters for this test
  178. MockCmdlet.CollectionName = collectionName;
  179. MockCmdlet.Type = PrincipalProviderType.OrgId;
  180. MockCmdlet.UserUpn = new string[]
  181. {
  182. "testUser1",
  183. "testUser2",
  184. };
  185. // Setup the environment for testing this cmdlet
  186. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  187. countOfExistingUsers = MockObject.SetUpDefaultRemoteAppSecurityPrincipals(remoteAppManagementClientMock, collectionName, userName);
  188. countOfNewUsers = MockObject.SetUpRemoteAppUserToAdd(remoteAppManagementClientMock, collectionName, PrincipalProviderType.OrgId, MockCmdlet.UserUpn);
  189. MockCmdlet.ResetPipelines();
  190. Log("Calling Add-AzureRemoteAppOrgIDUser and adding {0} users.", countOfNewUsers);
  191. MockCmdlet.ExecuteCmdlet();
  192. if (MockCmdlet.runTime().ErrorStream.Count != 0)
  193. {
  194. Assert.True(false,
  195. String.Format("Add-AzureRemoteAppOrgIDUser returned the following error {0}.",
  196. MockCmdlet.runTime().ErrorStream[0].Exception.Message
  197. )
  198. );
  199. }
  200. List<SecurityPrincipalOperationsResult> status = MockObject.ConvertList<SecurityPrincipalOperationsResult>(MockCmdlet.runTime().OutputPipeline);
  201. Assert.NotNull(status);
  202. Assert.True(MockObject.HasExpectedResults<SecurityPrincipalOperationsResult>(status, MockObject.ContainsExpectedStatus),
  203. "The actual result does not match the expected."
  204. );
  205. Log("The test for Add-AzureRemoteAppOrgIDUser successfully added {0} users the new count is {1}.", countOfNewUsers, countOfExistingUsers + countOfNewUsers);
  206. }
  207. [Fact]
  208. [Trait(Category.AcceptanceType, Category.CheckIn)]
  209. public void AddUsersToApp()
  210. {
  211. int countOfExistingUsers = 0;
  212. int countOfNewUsers = 0;
  213. AddAzureRemoteAppUser MockCmdlet = SetUpTestCommon<AddAzureRemoteAppUser>();
  214. // Required parameters for this test
  215. MockCmdlet.CollectionName = collectionName;
  216. MockCmdlet.Alias = appAlias;
  217. MockCmdlet.Type = PrincipalProviderType.OrgId;
  218. MockCmdlet.UserUpn = new string[]
  219. {
  220. "testUser1",
  221. "testUser2",
  222. };
  223. // Setup the environment for testing this cmdlet
  224. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  225. countOfExistingUsers = MockObject.SetUpDefaultRemoteAppSecurityPrincipals(remoteAppManagementClientMock, collectionName, userName);
  226. countOfNewUsers = MockObject.SetUpRemoteAppUserToAddForApp(remoteAppManagementClientMock, collectionName, appAlias, PrincipalProviderType.OrgId, MockCmdlet.UserUpn);
  227. MockCmdlet.ResetPipelines();
  228. Log("Calling Add-AzureRemoteAppOrgIDUser and adding {0} users.", countOfNewUsers);
  229. MockCmdlet.ExecuteCmdlet();
  230. if (MockCmdlet.runTime().ErrorStream.Count != 0)
  231. {
  232. Assert.True(false,
  233. String.Format("Add-AzureRemoteAppOrgIDUser returned the following error {0}.",
  234. MockCmdlet.runTime().ErrorStream[0].Exception.Message
  235. )
  236. );
  237. }
  238. List<SecurityPrincipalOperationsResult> status = MockObject.ConvertList<SecurityPrincipalOperationsResult>(MockCmdlet.runTime().OutputPipeline);
  239. Assert.NotNull(status);
  240. Assert.True(MockObject.HasExpectedResults<SecurityPrincipalOperationsResult>(status, MockObject.ContainsExpectedStatus),
  241. "The actual result does not match the expected."
  242. );
  243. Log("The test for Add-AzureRemoteAppOrgIDUser successfully added {0} users the new count is {1}.", countOfNewUsers, countOfExistingUsers + countOfNewUsers);
  244. }
  245. [Fact]
  246. [Trait(Category.AcceptanceType, Category.CheckIn)]
  247. public void RemoveUserThatExistsFromApp()
  248. {
  249. int countOfExistingUsers = 0;
  250. int countOfDeletedUsers = 0;
  251. RemoveAzureRemoteAppUser MockCmdlet = SetUpTestCommon<RemoveAzureRemoteAppUser>();
  252. // Required parameters for this test
  253. MockCmdlet.CollectionName = collectionName;
  254. MockCmdlet.Alias = appAlias;
  255. MockCmdlet.Type = PrincipalProviderType.OrgId;
  256. MockCmdlet.UserUpn = new string[]
  257. {
  258. userName
  259. };
  260. // Setup the environment for testing this cmdlet
  261. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  262. countOfExistingUsers = MockObject.SetUpDefaultRemoteAppSecurityPrincipals(remoteAppManagementClientMock, collectionName, userName);
  263. countOfDeletedUsers = MockObject.SetUpRemoteAppUserToRemoveFromApp(remoteAppManagementClientMock, collectionName, appAlias, PrincipalProviderType.OrgId, MockCmdlet.UserUpn);
  264. MockCmdlet.ResetPipelines();
  265. Log("Calling Remove-AzureRemoteAppOrgIdUser and removing {0} users.", countOfDeletedUsers);
  266. MockCmdlet.ExecuteCmdlet();
  267. if (MockCmdlet.runTime().ErrorStream.Count != 0)
  268. {
  269. Assert.True(false,
  270. String.Format("Remove-AzureRemoteAppMSAUser returned the following error {0}.",
  271. MockCmdlet.runTime().ErrorStream[0].Exception.Message
  272. )
  273. );
  274. }
  275. List<SecurityPrincipalOperationsResult> status = MockObject.ConvertList<SecurityPrincipalOperationsResult>(MockCmdlet.runTime().OutputPipeline);
  276. Assert.NotNull(status);
  277. Assert.True(MockObject.HasExpectedResults<SecurityPrincipalOperationsResult>(status, MockObject.ContainsExpectedStatus),
  278. "The actual result does not match the expected."
  279. );
  280. Log("The test for Remove-AzureRemoteAppOrgIdUser successfully removed {0} users the new count is {1}.", countOfDeletedUsers, countOfExistingUsers - countOfDeletedUsers);
  281. }
  282. [Fact]
  283. [Trait(Category.AcceptanceType, Category.CheckIn)]
  284. public void RemoveMSAUserThatExists()
  285. {
  286. int countOfExistingUsers = 0;
  287. int countOfDeletedUsers = 0;
  288. RemoveAzureRemoteAppUser MockCmdlet = SetUpTestCommon<RemoveAzureRemoteAppUser>();
  289. // Required parameters for this test
  290. MockCmdlet.CollectionName = collectionName;
  291. MockCmdlet.Type = PrincipalProviderType.MicrosoftAccount;
  292. MockCmdlet.UserUpn = new string[]
  293. {
  294. userName
  295. };
  296. // Setup the environment for testing this cmdlet
  297. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  298. countOfExistingUsers = MockObject.SetUpDefaultRemoteAppSecurityPrincipals(remoteAppManagementClientMock, collectionName, userName);
  299. countOfDeletedUsers = MockObject.SetUpDefaultRemoteAppUserToRemove(remoteAppManagementClientMock, collectionName, PrincipalProviderType.MicrosoftAccount, MockCmdlet.UserUpn);
  300. MockCmdlet.ResetPipelines();
  301. Log("Calling Remove-AzureRemoteAppMSAUser and removing {0} users.", countOfDeletedUsers);
  302. MockCmdlet.ExecuteCmdlet();
  303. if (MockCmdlet.runTime().ErrorStream.Count != 0)
  304. {
  305. Assert.True(false,
  306. String.Format("Remove-AzureRemoteAppMSAUser returned the following error {0}.",
  307. MockCmdlet.runTime().ErrorStream[0].Exception.Message
  308. )
  309. );
  310. }
  311. List<SecurityPrincipalOperationsResult> status = MockObject.ConvertList<SecurityPrincipalOperationsResult>(MockCmdlet.runTime().OutputPipeline);
  312. Assert.NotNull(status);
  313. Assert.True(MockObject.HasExpectedResults<SecurityPrincipalOperationsResult>(status, MockObject.ContainsExpectedStatus),
  314. "The actual result does not match the expected."
  315. );
  316. Log("The test for Remove-AzureRemoteAppMSAUser successfully removed {0} users the new count is {1}.", countOfDeletedUsers, countOfExistingUsers - countOfDeletedUsers);
  317. }
  318. [Fact]
  319. [Trait(Category.AcceptanceType, Category.CheckIn)]
  320. public void RemoveOrgIDUserThatExists()
  321. {
  322. int countOfExistingUsers = 0;
  323. int countOfDeletedUsers = 0;
  324. RemoveAzureRemoteAppUser MockCmdlet = SetUpTestCommon<RemoveAzureRemoteAppUser>();
  325. // Required parameters for this test
  326. MockCmdlet.CollectionName = collectionName;
  327. MockCmdlet.Type = PrincipalProviderType.OrgId;
  328. MockCmdlet.UserUpn = new string[]
  329. {
  330. userName
  331. };
  332. // Setup the environment for testing this cmdlet
  333. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  334. countOfExistingUsers = MockObject.SetUpDefaultRemoteAppSecurityPrincipals(remoteAppManagementClientMock, collectionName, userName);
  335. countOfDeletedUsers = MockObject.SetUpDefaultRemoteAppUserToRemove(remoteAppManagementClientMock, collectionName, PrincipalProviderType.OrgId, MockCmdlet.UserUpn);
  336. MockCmdlet.ResetPipelines();
  337. Log("Calling Remove-AzureRemoteAppOrgIdUser and removing {0} users.", countOfDeletedUsers);
  338. MockCmdlet.ExecuteCmdlet();
  339. if (MockCmdlet.runTime().ErrorStream.Count != 0)
  340. {
  341. Assert.True(false,
  342. String.Format("Remove-AzureRemoteAppMSAUser returned the following error {0}.",
  343. MockCmdlet.runTime().ErrorStream[0].Exception.Message
  344. )
  345. );
  346. }
  347. List<SecurityPrincipalOperationsResult> status = MockObject.ConvertList<SecurityPrincipalOperationsResult>(MockCmdlet.runTime().OutputPipeline);
  348. Assert.NotNull(status);
  349. Assert.True(MockObject.HasExpectedResults<SecurityPrincipalOperationsResult>(status, MockObject.ContainsExpectedStatus),
  350. "The actual result does not match the expected."
  351. );
  352. Log("The test for Remove-AzureRemoteAppOrgIdUser successfully removed {0} users the new count is {1}.", countOfDeletedUsers, countOfExistingUsers - countOfDeletedUsers);
  353. }
  354. }
  355. }