From c9f37b1b754b98adba43e424a1abba06818cb395 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 8 Oct 2024 19:00:42 +0100 Subject: [PATCH 1/2] added tests for delete user --- src/PinguApps.Appwrite.Playground/App.cs | 4 +- .../Clients/IUsersClient.cs | 8 +- .../Clients/UsersClient.cs | 17 ++++- .../Users/UsersClientTests.DeleteUser.cs | 74 +++++++++++++++++++ 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.DeleteUser.cs diff --git a/src/PinguApps.Appwrite.Playground/App.cs b/src/PinguApps.Appwrite.Playground/App.cs index 3cd82a1e..bd92dacb 100644 --- a/src/PinguApps.Appwrite.Playground/App.cs +++ b/src/PinguApps.Appwrite.Playground/App.cs @@ -17,12 +17,12 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server, public async Task Run(string[] args) { - var request = new GetUserRequest() + var request = new DeleteUserRequest() { UserId = "670565fa00209de81fb0" }; - var response = await _server.Users.GetUser(request); + var response = await _server.Users.DeleteUser(request); Console.WriteLine(response.Result.Match( result => result.ToString(), diff --git a/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs b/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs index 255e9aac..df3ef137 100644 --- a/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs @@ -100,7 +100,13 @@ public interface IUsersClient /// The request content /// The user Task> CreateUserWithShaPassword(CreateUserWithShaPasswordRequest request); - [Obsolete("This method hasn't yet been implemented.", true)] + + /// + /// Delete a user by its unique ID, thereby releasing it's ID. Since ID is released and can be reused, all user-related resources like documents or storage files should be deleted before user deletion. If you want to keep ID reserved, use instead. + /// Appwrite Docs + /// + /// The request content + /// 204 success code Task DeleteUser(DeleteUserRequest request); /// diff --git a/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs b/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs index 218ebc01..44628b5d 100644 --- a/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs @@ -213,9 +213,22 @@ public async Task> CreateUserWithShaPassword(CreateUserWith } } - [ExcludeFromCodeCoverage] /// - public Task DeleteUser(DeleteUserRequest request) => throw new NotImplementedException(); + public async Task DeleteUser(DeleteUserRequest request) + { + try + { + request.Validate(true); + + var result = await _usersApi.DeleteUser(request.UserId); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse(); + } + } /// public async Task> GetUser(GetUserRequest request) diff --git a/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.DeleteUser.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.DeleteUser.cs new file mode 100644 index 00000000..5dbc6008 --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.DeleteUser.cs @@ -0,0 +1,74 @@ +using System.Net; +using PinguApps.Appwrite.Shared.Requests.Users; +using PinguApps.Appwrite.Shared.Tests; +using RichardSzalay.MockHttp; + +namespace PinguApps.Appwrite.Server.Tests.Clients.Users; +public partial class UsersClientTests +{ + [Theory] + [InlineData("user123")] + [InlineData("user456")] + public async Task DeleteUser_ShouldReturnSuccess_WhenApiCallSucceeds(string userId) + { + // Arrange + var request = new DeleteUserRequest + { + UserId = userId + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/users/{userId}") + .ExpectedHeaders() + .Respond(HttpStatusCode.NoContent); + + // Act + var result = await _appwriteClient.Users.DeleteUser(request); + + // Assert + Assert.True(result.Success); + _mockHttp.VerifyNoOutstandingExpectation(); + } + + [Fact] + public async Task DeleteUser_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = new DeleteUserRequest + { + UserId = "user123" + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/users/user123") + .ExpectedHeaders() + .Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); + + // Act + var result = await _appwriteClient.Users.DeleteUser(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task DeleteUser_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = new DeleteUserRequest + { + UserId = "user123" + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/users/user123") + .ExpectedHeaders() + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Users.DeleteUser(request); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +} From 1a1fbc44a4300dbd2f1cb57b1c71c0f73a5206b5 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 8 Oct 2024 19:01:34 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7181d3e9..ece4bebe 100644 --- a/README.md +++ b/README.md @@ -140,9 +140,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ``` ## ⌛ Progress -![Server & Client - 67 / 291](https://img.shields.io/badge/Server_&_Client-67%20%2F%20291-red?style=for-the-badge) +![Server & Client - 68 / 291](https://img.shields.io/badge/Server_&_Client-68%20%2F%20291-red?style=for-the-badge) -![Server - 23 / 201](https://img.shields.io/badge/Server-23%20%2F%20201-red?style=for-the-badge) +![Server - 24 / 201](https://img.shields.io/badge/Server-24%20%2F%20201-red?style=for-the-badge) ![Client - 44 / 90](https://img.shields.io/badge/Client-44%20%2F%2090-gold?style=for-the-badge) @@ -207,7 +207,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [Create Phone Verification (Confirmation)](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePhoneVerification) | ✅ | ❌ | | ### Users -![Account - 12 / 41](https://img.shields.io/badge/Users-12%20%2F%2041-red?style=for-the-badge) +![Account - 13 / 41](https://img.shields.io/badge/Users-13%20%2F%2041-red?style=for-the-badge) | Endpoint | Client | Server | |:-:|:-:|:-:| @@ -223,7 +223,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [Create User with Scrypt Modified Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createScryptModifiedUser) | ❌ | ✅ | | [Create User with SHA Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createSHAUser) | ❌ | ✅ | | [Get User](https://appwrite.io/docs/references/1.6.x/server-rest/users#get) | ❌ | ✅ | -| [Delete User](https://appwrite.io/docs/references/1.6.x/server-rest/users#delete) | ❌ | ⬛ | +| [Delete User](https://appwrite.io/docs/references/1.6.x/server-rest/users#delete) | ❌ | ✅ | | [Update Email](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateEmail) | ❌ | ⬛ | | [Create User JWT](https://appwrite.io/docs/references/1.6.x/server-rest/users#createJWT) | ❌ | ⬛ | | [Update User Labels](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateLabels) | ❌ | ⬛ |