Skip to content

Commit

Permalink
Add UpdateUser UpdateUserPassword DeleteUser (#150)
Browse files Browse the repository at this point in the history
* Authenticate methods updates

* Add UpdateUser and UpdateUserPassword

* Add DeleteUser

* Ignore Id for serialization and fix descriptions

* merge main

* Fix description
  • Loading branch information
Patrick committed Aug 31, 2023
1 parent 858efe0 commit 8b42557
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/WorkOS.net/Services/UserManagement/UserManagementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,61 @@ public UserManagementService(WorkOSClient client)
};
return await this.Client.MakeAPIRequest<User>(request, cancellationToken);
}

/// <summary>
/// Update the User's information.
/// </summary>
/// <param name="options"> Parameters used to update a user.</param>
/// <param name="cancellationToken">An optional token to cancel the request.</param>
/// <returns> The corresponding User object.</returns>
public async Task<User> UpdateUser(
UpdateUserOptions options,
CancellationToken cancellationToken = default)
{
var request = new WorkOSRequest
{
Options = options,
Method = HttpMethod.Put,
Path = $"/users/{options.Id}",
};
return await this.Client.MakeAPIRequest<User>(request, cancellationToken);
}

/// <summary>
/// Update the User's password.
/// </summary>
/// <param name="options"> Parameters used to update a user.</param>
/// <param name="cancellationToken">An optional token to cancel the request.</param>
/// <returns> The corresponding User object.</returns>
public async Task<User> UpdateUserPassword(
UpdateUserPasswordOptions options,
CancellationToken cancellationToken = default)
{
var request = new WorkOSRequest
{
Options = options,
Method = HttpMethod.Put,
Path = $"/users/{options.Id}/password",
};
return await this.Client.MakeAPIRequest<User>(request, cancellationToken);
}

/// <summary>
/// Delete a User.
/// </summary>
/// <param name="id">The unique ID of the User.</param>
/// <param name="cancellationToken">An optional token to cancel the request.</param>
/// <returns> A Task.</returns>
public async Task DeleteUser(
string id,
CancellationToken cancellationToken = default)
{
var request = new WorkOSRequest
{
Method = HttpMethod.Delete,
Path = $"/users/{id}",
};
await this.Client.MakeRawAPIRequest(request, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace WorkOS
{
using Newtonsoft.Json;

/// <summary>
/// The parameters to update the user.
/// </summary>
public class UpdateUserOptions : BaseOptions
{
/// <summary>
/// The ID of the user.
/// </summary>
[JsonIgnore]
public string Id { get; set; }

/// <summary>
/// The email address of the user.
/// </summary>
[JsonProperty("email")]
public string Email { get; set; }

/// <summary>
/// The user's first name.
/// </summary>
[JsonProperty("first_name")]
public string FirstName { get; set; }

/// <summary>
/// The user's last name.
/// </summary>
[JsonProperty("last_name")]
public string LastName { get; set; }

/// <summary>
/// Whether the user's email address was previously verified.
/// </summary>
[JsonProperty("email_verified")]
public bool IsEmailVerified { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace WorkOS
{
using Newtonsoft.Json;

/// <summary>
/// The parameters to update the password of the user.
/// </summary>
public class UpdateUserPasswordOptions : BaseOptions
{
/// <summary>
/// The ID of the user.
/// </summary>
[JsonIgnore]
public string Id { get; set; }

/// <summary>
/// The new password for the user.
/// </summary>
[JsonProperty("password")]
public string Password { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public class UserManagementServiceTest

private readonly CompletePasswordResetOptions mockCompletePasswordResetOptions;

private readonly UpdateUserOptions mockUpdateUserOptions;

private readonly UpdateUserPasswordOptions mockUpdateUserPasswordOptions;

public UserManagementServiceTest()
{
this.httpMock = new HttpMock();
Expand Down Expand Up @@ -241,6 +245,21 @@ public UserManagementServiceTest()
Token = "token_1234",
NewPassword = "new_password_1234",
};

this.mockUpdateUserOptions = new UpdateUserOptions
{
Id = "user_1234",
FirstName = "Marcelina",
LastName = "Davis",
Email = "marcelina.davis@gmail.com",
IsEmailVerified = true,
};

this.mockUpdateUserPasswordOptions = new UpdateUserPasswordOptions
{
Id = "user_1234",
Password = "password_1234",
};
}

[Fact]
Expand Down Expand Up @@ -495,5 +514,59 @@ public async void TestRemoveUserFromOrganization()
HttpMethod.Delete,
$"/users/{this.mockUser.Id}/{this.mockOrganization2.Id}");
}

[Fact]
public async void TestUpdateUser()
{
this.httpMock.MockResponse(
HttpMethod.Put,
$"/users/{this.mockUpdateUserOptions.Id}",
HttpStatusCode.OK,
RequestUtilities.ToJsonString(this.mockUser));

var user = await this.service.UpdateUser(this.mockUpdateUserOptions);

this.httpMock.AssertRequestWasMade(
HttpMethod.Put,
$"/users/{this.mockUpdateUserOptions.Id}");
Assert.Equal(
JsonConvert.SerializeObject(user),
JsonConvert.SerializeObject(this.mockUser));
}

[Fact]
public async void TestUpdateUserPassword()
{
this.httpMock.MockResponse(
HttpMethod.Put,
$"/users/{this.mockUpdateUserPasswordOptions.Id}/password",
HttpStatusCode.OK,
RequestUtilities.ToJsonString(this.mockUser));

var user = await this.service.UpdateUserPassword(this.mockUpdateUserPasswordOptions);

this.httpMock.AssertRequestWasMade(
HttpMethod.Put,
$"/users/{this.mockUpdateUserPasswordOptions.Id}/password");
Assert.Equal(
JsonConvert.SerializeObject(user),
JsonConvert.SerializeObject(this.mockUser));
}

[Fact]
public async void TestDeleteUser()
{
this.httpMock.MockResponse(
HttpMethod.Delete,
$"/users/{this.mockUser.Id}",
HttpStatusCode.Accepted,
"Accepted");

await this.service.DeleteUser(this.mockUser.Id);

this.httpMock.AssertRequestWasMade(
HttpMethod.Delete,
$"/users/{this.mockUser.Id}");
}
}
}

0 comments on commit 8b42557

Please sign in to comment.