Skip to content

Commit

Permalink
Merge b0bf328 into 0143aaa
Browse files Browse the repository at this point in the history
  • Loading branch information
dm430 authored Jun 22, 2018
2 parents 0143aaa + b0bf328 commit ce037bb
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/ZendeskApi_v2/Requests/Users.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ZendeskApi_v2.Extensions;

#if ASYNC
Expand Down Expand Up @@ -74,6 +75,8 @@ public interface IUsers : ICore

bool DeleteUser(long id);

JobStatusResponse BulkDeleteUsers(IEnumerable<User> users);

bool SetUsersPassword(long userId, string newPassword);

bool ChangeUsersPassword(long userId, string oldPassword, string newPassword);
Expand Down Expand Up @@ -150,6 +153,8 @@ public interface IUsers : ICore

Task<bool> DeleteUserAsync(long id);

Task<JobStatusResponse> BulkDeleteUsersAsync(IEnumerable<User> users);

Task<bool> SetUsersPasswordAsync(long userId, string newPassword);

Task<bool> ChangeUsersPasswordAsync(long userId, string oldPassword, string newPassword);
Expand Down Expand Up @@ -345,6 +350,13 @@ public bool DeleteUser(long id)
return GenericDelete($"users/{id}.json");
}

public JobStatusResponse BulkDeleteUsers(IEnumerable<User> users)
{
var userIdsCommaSeperatedList = CreateCommaSeperatedUserIdString(users);

return GenericDelete<JobStatusResponse>($"users/destroy_many.json?ids={userIdsCommaSeperatedList}");
}

public bool SetUsersPassword(long userId, string newPassword)
{
var body = new { password = newPassword };
Expand Down Expand Up @@ -559,6 +571,13 @@ public async Task<bool> DeleteUserAsync(long id)
return await GenericDeleteAsync($"users/{id}.json");
}

public Task<JobStatusResponse> BulkDeleteUsersAsync(IEnumerable<User> users)
{
var userIdsCommaSeperatedList = CreateCommaSeperatedUserIdString(users);

return GenericDeleteAsync<JobStatusResponse>($"users/destroy_many.json?ids={userIdsCommaSeperatedList}");
}

public async Task<bool> SetUsersPasswordAsync(long userId, string newPassword)
{
var body = new { password = newPassword };
Expand Down Expand Up @@ -646,5 +665,13 @@ private string GetResourceStringWithSideLoadOptionsParam(string resource, UserSi

return resource;
}

private static string CreateCommaSeperatedUserIdString(IEnumerable<User> users)
{
var userIds = users.Select(user => user.Id);
var userIdsCommaSeperatedList = string.Join(",", userIds);

return userIdsCommaSeperatedList;
}
}
}
78 changes: 78 additions & 0 deletions test/ZendeskApi_v2.Test/UserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
using ZendeskApi_v2.Requests;
using ZendeskApi_v2.Models.Shared;
using System.IO;
using System.Net;
using System.Threading;

namespace Tests
{
[TestFixture]
[Category("Users")]
public class UserTests
{
private const int MaxRetryAttempts = 10;
private const string JobStatusCompleted = "completed";
private const string JobStatusQueued = "queued";

private ZendeskApi api = new ZendeskApi(Settings.Site, Settings.AdminEmail, Settings.AdminPassword);

[Test]
Expand Down Expand Up @@ -419,5 +425,77 @@ public async Task CanCreateUpdateAndDeleteIdentitiesAsync()
Assert.True(api.Users.DeleteUserIdentity(userId, identityId));
Assert.True(api.Users.DeleteUser(userId));
}

[Test]
public async Task CanBulkDeleteUsersAsync()
{
var users = new List<User>();

for (var i = 0; i < 2; i++)
{
var user = new User()
{
Name = Guid.NewGuid().ToString("N") + " " + Guid.NewGuid().ToString("N"),
Email = Guid.NewGuid().ToString("N") + "@" + Guid.NewGuid().ToString("N") + ".com",
Verified = true
};

var response = await api.Users.CreateUserAsync(user);

users.Add(response.User);
}

var jobResponse = await api.Users.BulkDeleteUsersAsync(users);

Assert.AreEqual(JobStatusQueued, jobResponse.JobStatus.Status.ToLower());

var count = 0;

while (jobResponse.JobStatus.Status.ToLower() != JobStatusCompleted && count < MaxRetryAttempts)
{
await Task.Delay(1000);
jobResponse = api.JobStatuses.GetJobStatus(jobResponse.JobStatus.Id);
count++;
}

Assert.AreEqual(JobStatusCompleted, jobResponse.JobStatus.Status.ToLower());
Assert.AreEqual(users.Count, jobResponse.JobStatus.Total);
}

[Test]
public void CanBulkDeleteUsers()
{
var users = new List<User>();

for (var i = 0; i < 2; i++)
{
var user = new User()
{
Name = Guid.NewGuid().ToString("N") + " " + Guid.NewGuid().ToString("N"),
Email = Guid.NewGuid().ToString("N") + "@" + Guid.NewGuid().ToString("N") + ".com",
Verified = true
};

var response = api.Users.CreateUser(user);

users.Add(response.User);
}

var jobResponse = api.Users.BulkDeleteUsers(users);

Assert.AreEqual(JobStatusQueued, jobResponse.JobStatus.Status.ToLower());

var count = 0;

while (jobResponse.JobStatus.Status.ToLower() != JobStatusCompleted && count < MaxRetryAttempts)
{
Thread.Sleep(1000);
jobResponse = api.JobStatuses.GetJobStatus(jobResponse.JobStatus.Id);
count++;
}

Assert.AreEqual(JobStatusCompleted, jobResponse.JobStatus.Status.ToLower());
Assert.AreEqual(users.Count, jobResponse.JobStatus.Total);
}
}
}

0 comments on commit ce037bb

Please sign in to comment.