/// <summary>
 /// Adds a list of principals to the given collection.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.RemoteApp.IPrincipalOperations.
 /// </param>
 /// <param name='collectionName'>
 /// Required. The RemoteApp collection name.
 /// </param>
 /// <param name='securityPrincipalList'>
 /// Required. A list of RemoteApp principals to add.
 /// </param>
 /// <returns>
 /// The response for the collection user operation.
 /// </returns>
 public static SecurityPrincipalOperationsResult Add(this IPrincipalOperations operations, string collectionName, SecurityPrincipalList securityPrincipalList)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IPrincipalOperations)s).AddAsync(collectionName, securityPrincipalList);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
Exemplo n.º 2
0
        public static int SetUpRemoteAppUserToAdd(Mock<IRemoteAppManagementClient> clientMock, string collectionName, PrincipalProviderType userIdType, string[] userNames)
        {
            SecurityPrincipalOperationsResult response = new SecurityPrincipalOperationsResult()
            {
                RequestId = "122-13342",
                TrackingId = "2334-323456",
                StatusCode = System.Net.HttpStatusCode.Accepted,
                Errors = null,
            };

            mockSecurityPrincipalResult = new List<SecurityPrincipalOperationsResult>()
            {
                new SecurityPrincipalOperationsResult()
                {
                    RequestId = response.RequestId,
                    TrackingId = response.TrackingId,
                    StatusCode = response.StatusCode,
                    Errors = response.Errors
                },
            };

            SecurityPrincipalList spAdd = new SecurityPrincipalList();

            foreach (string userName in userNames)
            {
                SecurityPrincipal mockUser = new SecurityPrincipal()
                {
                    Name = userName,
                    SecurityPrincipalType = PrincipalType.User,
                    UserIdType = userIdType,
                };
                spAdd.SecurityPrincipals.Add(mockUser);
            }

            ISetup<IRemoteAppManagementClient, Task<SecurityPrincipalOperationsResult>> setup = clientMock.Setup(c => c.Principals.AddAsync(collectionName, It.IsAny<SecurityPrincipalList>(), It.IsAny<CancellationToken>()));
            setup.Returns(Task.Factory.StartNew(() => response));

            mockUsers = spAdd.SecurityPrincipals;

            return mockUsers.Count;
        }
 /// <summary>
 /// Adds a list of principals to the given collection.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.RemoteApp.IPrincipalOperations.
 /// </param>
 /// <param name='collectionName'>
 /// Required. The RemoteApp collection name.
 /// </param>
 /// <param name='securityPrincipalList'>
 /// Required. A list of RemoteApp principals to add.
 /// </param>
 /// <returns>
 /// The response for the collection user operation.
 /// </returns>
 public static Task<SecurityPrincipalOperationsResult> AddAsync(this IPrincipalOperations operations, string collectionName, SecurityPrincipalList securityPrincipalList)
 {
     return operations.AddAsync(collectionName, securityPrincipalList, CancellationToken.None);
 }
        public void CanNotAddInvalidPrincipalToCollection()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                RemoteAppManagementClient client = GetRemoteAppManagementClient();

                string collectionName = "asquick";

                SecurityPrincipal user = new SecurityPrincipal("johndoe_test");
                user.SecurityPrincipalType = PrincipalType.User;
                user.UserIdType = PrincipalProviderType.MicrosoftAccount;

                SecurityPrincipalList principals = new SecurityPrincipalList();
                principals.SecurityPrincipals.Add(user);

                SecurityPrincipalOperationsResult result = client.Principals.Add(collectionName, principals);

                Assert.NotNull(result);
                Assert.NotNull(result.Errors);
                Assert.NotEmpty(result.Errors);
            }
        }
        public void CanAddAndRemovePrincipalToCollection()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string collectionName = "simple";

                RemoteAppManagementClient client = GetRemoteAppManagementClient();

                // verifying the added principals
                SecurityPrincipalInfoListResult principalList = client.Principals.List(collectionName);

                int numberOfUsersBeforeAdd = principalList.SecurityPrincipalInfoList.Count;

                SecurityPrincipalList principals = new SecurityPrincipalList();

                // adding the principals to the collection

                SecurityPrincipal user = new SecurityPrincipal("*****@*****.**");
                user.SecurityPrincipalType = PrincipalType.User;
                user.UserIdType = PrincipalProviderType.MicrosoftAccount;

                principals.SecurityPrincipals.Add(user);

                SecurityPrincipalOperationsResult result = client.Principals.Add(collectionName, principals);

                Assert.NotNull(result);
                Assert.True(result.StatusCode == System.Net.HttpStatusCode.OK || result.StatusCode == System.Net.HttpStatusCode.Accepted, "Failed to add security principal. Status code: " + result.StatusCode + ".");
                Assert.NotNull(result.Errors);
                Assert.Empty(result.Errors);

                // verifying the added principals
                principalList = client.Principals.List(collectionName);

                Assert.NotNull(principalList);

                // verify that all the requested users are added
                Assert.True(principalList.SecurityPrincipalInfoList.Count == (numberOfUsersBeforeAdd + principals.SecurityPrincipals.Count), "Add users did not add the requested users to the collection.");

                List<SecurityPrincipal> matchedPrincipals = new List<SecurityPrincipal>();

                foreach (var principal in principalList.SecurityPrincipalInfoList)
                {
                    foreach (SecurityPrincipal p in principals.SecurityPrincipals)
                    {
                        if (String.Equals(principal.SecurityPrincipal.Name, p.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            matchedPrincipals.Add(principal.SecurityPrincipal);
                        }
                    }
                }

                Assert.True(matchedPrincipals.Count() == 1);
                Assert.Equal(matchedPrincipals.First().Name.ToLowerInvariant(), user.Name.ToLowerInvariant());

                // now remove the added security principals here
                result = client.Principals.Delete(collectionName, principals);

                Assert.NotNull(result);
                Assert.True(result.StatusCode == System.Net.HttpStatusCode.OK || result.StatusCode == System.Net.HttpStatusCode.Accepted, "Failed to remove security principal. Status code: " + result.StatusCode + ".");

                // verifying the deletion of the principals
                principalList = client.Principals.List(collectionName);

                Assert.NotNull(principalList);

                // verify that all the requested users are added
                Assert.Equal(principalList.SecurityPrincipalInfoList.Count, numberOfUsersBeforeAdd);

                matchedPrincipals.Clear();
                Assert.Empty(matchedPrincipals);

                foreach (var principal in principalList.SecurityPrincipalInfoList)
                {
                    foreach (SecurityPrincipal p in principals.SecurityPrincipals)
                    {
                        if (String.Equals(principal.SecurityPrincipal.Name, p.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            matchedPrincipals.Add(principal.SecurityPrincipal);
                        }
                    }
                }

                Assert.Empty(matchedPrincipals);
            }
        }