/// <summary>
 /// Creates new user.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.ApiManagement.IUsersOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of the Api Management service.
 /// </param>
 /// <param name='uid'>
 /// Required. Identifier of the user.
 /// </param>
 /// <param name='parameters'>
 /// Required. Create or update parameters.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static AzureOperationResponse Create(this IUsersOperations operations, string resourceGroupName, string serviceName, string uid, UserCreateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IUsersOperations)s).CreateAsync(resourceGroupName, serviceName, uid, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public void GroupUsersListAddRemove()
        {
            TestUtilities.StartTest("SmapiFunctionalTests", "GroupUsersListAddRemove");

            try
            {
                // create new group
                string gid = TestUtilities.GenerateName("groupId");

                try
                {
                    var createGroupParams = new GroupCreateParameters
                    {
                        Name = TestUtilities.GenerateName("groupName"),
                        Type = GroupTypeContract.Custom,
                    };

                    ApiManagementClient.Groups.Create(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        gid,
                        createGroupParams);

                    // get the group
                    var getGroupResponse = ApiManagementClient.Groups.Get(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        gid);

                    var group = getGroupResponse.Value;

                    // list all group users
                    var listGroupUsersResponse = ApiManagementClient.GroupUsers.List(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        group.Id,
                        null);

                    Assert.NotNull(listGroupUsersResponse);
                    Assert.NotNull(listGroupUsersResponse.Result);
                    Assert.NotNull(listGroupUsersResponse.Result.Values);

                    // there should be no users in the group
                    Assert.Equal(0, listGroupUsersResponse.Result.TotalCount);
                    Assert.Equal(0, listGroupUsersResponse.Result.Values.Count);

                    // create new user and add it to the group
                    string userId = TestUtilities.GenerateName("userId");
                    try
                    {
                        var creteUserParameters = new UserCreateParameters
                        {
                            FirstName = TestUtilities.GenerateName("Ivan"),
                            LastName = TestUtilities.GenerateName("Ivanov"),
                            Email = TestUtilities.GenerateName("ivan.ivanov") + "@contoso.com",
                            Password = TestUtilities.GenerateName("pwd"),
                            State = UserStateContract.Active,
                            Note = TestUtilities.GenerateName("note")
                        };

                        ApiManagementClient.Users.Create(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            creteUserParameters);

                        // add the user to the group
                        var addResponse = ApiManagementClient.GroupUsers.Add(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            group.Id,
                            userId);

                        Assert.NotNull(addResponse);

                        // list group users
                        listGroupUsersResponse = ApiManagementClient.GroupUsers.List(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            group.Id,
                            null);

                        Assert.NotNull(listGroupUsersResponse);
                        Assert.NotNull(listGroupUsersResponse.Result);
                        Assert.NotNull(listGroupUsersResponse.Result.Values);

                        // there should be one user
                        Assert.Equal(1, listGroupUsersResponse.Result.TotalCount);
                        Assert.Equal(1, listGroupUsersResponse.Result.Values.Count);

                        var user = listGroupUsersResponse.Result.Values.Single();
                        Assert.Equal(userId, user.Id);
                        Assert.Equal(creteUserParameters.FirstName, user.FirstName);
                        Assert.Equal(creteUserParameters.LastName, user.LastName);
                        Assert.Equal(creteUserParameters.Email, user.Email);
                        Assert.Equal(creteUserParameters.Note, user.Note);
                        Assert.Equal(creteUserParameters.State, user.State);

                        // remove the user from the group
                        var removeReponse = ApiManagementClient.GroupUsers.Remove(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            group.Id,
                            userId);

                        Assert.NotNull(removeReponse);

                        // list to make sure it was removed
                        listGroupUsersResponse = ApiManagementClient.GroupUsers.List(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            group.Id,
                            null);

                        Assert.NotNull(listGroupUsersResponse);
                        Assert.NotNull(listGroupUsersResponse.Result);
                        Assert.NotNull(listGroupUsersResponse.Result.Values);

                        // there should be no users
                        Assert.Equal(0, listGroupUsersResponse.Result.TotalCount);
                        Assert.Equal(0, listGroupUsersResponse.Result.Values.Count);
                    }
                    finally
                    {
                        // delete the user
                        ApiManagementClient.Users.Delete(ResourceGroupName, ApiManagementServiceName, userId, "*", true);
                    }
                }
                finally
                {
                    // delete the group
                    ApiManagementClient.Groups.Delete(ResourceGroupName, ApiManagementServiceName, gid, "*");
                }
            }
            finally
            {
                TestUtilities.EndTest();
            }
        }
        public void UserGroupsListAddRemove()
        {
            TestUtilities.StartTest("SmapiFunctionalTests", "UserGroupsListAddRemove");

            try
            {
                // create new group
                string gid = TestUtilities.GenerateName("groupId");

                try
                {
                    var createGroupParams = new GroupCreateParameters
                    {
                        Name = TestUtilities.GenerateName("groupName"),
                        Type = GroupTypeContract.Custom,
                    };

                    ApiManagementClient.Groups.Create(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        gid,
                        createGroupParams);

                    // get the group
                    var getGroupResponse = ApiManagementClient.Groups.Get(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        gid);

                    var group = getGroupResponse.Value;

                    // create new user and add it to the group
                    string userId = TestUtilities.GenerateName("userId");
                    try
                    {
                        var creteUserParameters = new UserCreateParameters
                        {
                            FirstName = TestUtilities.GenerateName("Ivan"),
                            LastName = TestUtilities.GenerateName("Ivanov"),
                            Email = TestUtilities.GenerateName("ivan.ivanov") + "@contoso.com",
                            Password = TestUtilities.GenerateName("pwd"),
                            State = UserStateContract.Active,
                            Note = TestUtilities.GenerateName("note")
                        };

                        ApiManagementClient.Users.Create(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            creteUserParameters);

                        // list user groups
                        var listUserGroupsResponse = ApiManagementClient.UserGroups.List(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            null);

                        Assert.NotNull(listUserGroupsResponse);
                        Assert.NotNull(listUserGroupsResponse.Result);
                        Assert.NotNull(listUserGroupsResponse.Result.Values);

                        // there should be 'Developers' group by default
                        Assert.Equal(1, listUserGroupsResponse.Result.TotalCount);
                        Assert.Equal(1, listUserGroupsResponse.Result.Values.Count);

                        // add the user to the group
                        var addResponse = ApiManagementClient.UserGroups.AddToGroup(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            group.Id);

                        Assert.NotNull(addResponse);

                        // list user groups
                        listUserGroupsResponse = ApiManagementClient.UserGroups.List(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            null);

                        Assert.NotNull(listUserGroupsResponse);
                        Assert.NotNull(listUserGroupsResponse.Result);
                        Assert.NotNull(listUserGroupsResponse.Result.Values);

                        // there should be two groups now
                        Assert.Equal(2, listUserGroupsResponse.Result.TotalCount);
                        Assert.Equal(2, listUserGroupsResponse.Result.Values.Count);

                        var userGroup = listUserGroupsResponse.Result.Values.Single(g => g.Name == group.Name);
                        Assert.Equal(group.Id, userGroup.Id);
                        Assert.Equal(group.IdPath, userGroup.IdPath);
                        Assert.Equal(group.Name, userGroup.Name);
                        Assert.Equal(group.Description, userGroup.Description);
                        Assert.Equal(group.ExternalId, userGroup.ExternalId);
                        Assert.Equal(group.System, userGroup.System);
                        Assert.Equal(group.Type, userGroup.Type);

                        // remove the user from the group
                        var removeReponse = ApiManagementClient.UserGroups.RemoveFromGroup(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            group.Id);

                        Assert.NotNull(removeReponse);

                        // list to make sure it was removed
                        listUserGroupsResponse = ApiManagementClient.UserGroups.List(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            null);

                        Assert.NotNull(listUserGroupsResponse);
                        Assert.NotNull(listUserGroupsResponse.Result);
                        Assert.NotNull(listUserGroupsResponse.Result.Values);

                        // there should be default 'Developers' group
                        Assert.Equal(1, listUserGroupsResponse.Result.TotalCount);
                        Assert.Equal(1, listUserGroupsResponse.Result.Values.Count);
                        Assert.Equal("Developers", listUserGroupsResponse.Result.Values.Single().Name);
                    }
                    finally
                    {
                        // delete the user
                        ApiManagementClient.Users.Delete(ResourceGroupName, ApiManagementServiceName, userId, "*", true);
                    }
                }
                finally
                {
                    // delete the group
                    ApiManagementClient.Groups.Delete(ResourceGroupName, ApiManagementServiceName, gid, "*");
                }
            }
            finally
            {
                TestUtilities.EndTest();
            }
        }
 /// <summary>
 /// Creates new user.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.ApiManagement.IUsersOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of the Api Management service.
 /// </param>
 /// <param name='uid'>
 /// Required. Identifier of the user.
 /// </param>
 /// <param name='parameters'>
 /// Required. Create or update parameters.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task<AzureOperationResponse> CreateAsync(this IUsersOperations operations, string resourceGroupName, string serviceName, string uid, UserCreateParameters parameters)
 {
     return operations.CreateAsync(resourceGroupName, serviceName, uid, parameters, CancellationToken.None);
 }
        public void UsersCreateListUpdateDelete()
        {
            TestUtilities.StartTest("SmapiFunctionalTests", "UsersCreateListUpdateDelete");

            try
            {
                // list users
                var listUsersResponse = ApiManagementClient.Users.List(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    null);

                Assert.NotNull(listUsersResponse);
                Assert.NotNull(listUsersResponse.Result);
                Assert.Equal(1, listUsersResponse.Result.TotalCount);
                Assert.Equal(1, listUsersResponse.Result.Values.Count);

                // create user
                string userId = TestUtilities.GenerateName("userId");
                string userEmail = "*****@*****.**";
                string userFirstName = TestUtilities.GenerateName("userFirstName");
                string userLastName = TestUtilities.GenerateName("userLastName");
                string userPassword = TestUtilities.GenerateName("userPassword");
                string userNote = TestUtilities.GenerateName("userNote");
                UserStateContract? userSate = UserStateContract.Active;

                var userCreateParameters = new UserCreateParameters
                {
                    Email = userEmail,
                    FirstName = userFirstName,
                    LastName = userLastName,
                    Password = userPassword,
                    Note = userNote,
                    State = userSate
                };

                var createUserResponse = ApiManagementClient.Users.Create(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    userId,
                    userCreateParameters);

                Assert.NotNull(createUserResponse);
                Assert.Equal(HttpStatusCode.Created, createUserResponse.StatusCode);

                // get the user to check it was added
                var getUserResponse = ApiManagementClient.Users.Get(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    userId);

                Assert.NotNull(getUserResponse);

                Assert.Equal(userId, getUserResponse.Value.Id);
                Assert.Equal(userEmail, getUserResponse.Value.Email);
                Assert.Equal(userFirstName, getUserResponse.Value.FirstName);
                Assert.Equal(userLastName, getUserResponse.Value.LastName);
                Assert.Equal(userNote, getUserResponse.Value.Note);
                Assert.Equal(userSate, getUserResponse.Value.State);

                // list users
                listUsersResponse = ApiManagementClient.Users.List(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    new QueryParameters {Top = 1});

                Assert.NotNull(listUsersResponse);
                Assert.NotNull(listUsersResponse.Result);
                Assert.NotNull(listUsersResponse.Result.NextLink);
                Assert.Equal(2, listUsersResponse.Result.TotalCount);
                Assert.Equal(1, listUsersResponse.Result.Values.Count);

                // generate SSO token URL

                var genrateSsoResponse = ApiManagementClient.Users.GenerateSsoUrl(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    userId);

                Assert.NotNull(genrateSsoResponse);
                Assert.NotNull(genrateSsoResponse.Value);
                Uri uri;
                Assert.True(Uri.TryCreate(genrateSsoResponse.Value, UriKind.Absolute, out uri));

                // remove the user
                var deleteUserResponse = ApiManagementClient.Users.Delete(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    userId,
                    getUserResponse.ETag,
                    deleteSubscriptions: true);

                Assert.NotNull(deleteUserResponse);

                // get the deleted user to make sure it was deleted
                try
                {
                    ApiManagementClient.Users.Get(ResourceGroupName, ApiManagementServiceName, userId);
                    throw new Exception("This code should not have been executed.");
                }
                catch (CloudException ex)
                {
                    Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode);
                }
            }
            finally
            {
                TestUtilities.EndTest();
            }
        }