From c7a75184ae3f6262cbc960495610f57039b835c8 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 8 Oct 2024 19:08:23 +0100 Subject: [PATCH 1/3] implemented update email --- src/PinguApps.Appwrite.Playground/App.cs | 7 ++++--- .../Clients/IUsersClient.cs | 8 +++++++- .../Clients/UsersClient.cs | 17 +++++++++++++++-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/PinguApps.Appwrite.Playground/App.cs b/src/PinguApps.Appwrite.Playground/App.cs index bd92dacb..ca672181 100644 --- a/src/PinguApps.Appwrite.Playground/App.cs +++ b/src/PinguApps.Appwrite.Playground/App.cs @@ -17,12 +17,13 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server, public async Task Run(string[] args) { - var request = new DeleteUserRequest() + var request = new UpdateEmailRequest() { - UserId = "670565fa00209de81fb0" + UserId = "664aac1a00113f82e620", + Email = "test@example.com" }; - var response = await _server.Users.DeleteUser(request); + var response = await _server.Users.UpdateEmail(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 df3ef137..51d4ec46 100644 --- a/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs @@ -116,7 +116,13 @@ public interface IUsersClient /// The request content /// The user Task> GetUser(GetUserRequest request); - [Obsolete("This method hasn't yet been implemented.", true)] + + /// + /// Update the user email by its unique ID + /// Appwrite Docs + /// + /// The request content + /// The user Task> UpdateEmail(UpdateEmailRequest request); [Obsolete("This method hasn't yet been implemented.", true)] Task> CreateUserJwt(CreateUserJwtRequest request); diff --git a/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs b/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs index 44628b5d..13ebe25e 100644 --- a/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs @@ -247,9 +247,22 @@ public async Task> GetUser(GetUserRequest request) } } - [ExcludeFromCodeCoverage] /// - public Task> UpdateEmail(UpdateEmailRequest request) => throw new NotImplementedException(); + public async Task> UpdateEmail(UpdateEmailRequest request) + { + try + { + request.Validate(true); + + var result = await _usersApi.UpdateEmail(request.UserId, request); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse(); + } + } [ExcludeFromCodeCoverage] /// From 4ede33fa19c23f88bb2c6443f76e795280f02f83 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 8 Oct 2024 19:14:04 +0100 Subject: [PATCH 2/3] added tests for update email --- .../Users/UsersClientTests.UpdateEmail.cs | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdateEmail.cs diff --git a/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdateEmail.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdateEmail.cs new file mode 100644 index 00000000..97e89516 --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdateEmail.cs @@ -0,0 +1,79 @@ +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", "newemail@example.com")] + [InlineData("user456", "anotheremail@example.com")] + public async Task UpdateEmail_ShouldReturnSuccess_WhenApiCallSucceeds(string userId, string newEmail) + { + // Arrange + var request = new UpdateEmailRequest + { + UserId = userId, + Email = newEmail + }; + + _mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{userId}/email") + .WithJsonContent(request) + .ExpectedHeaders() + .Respond(Constants.AppJson, Constants.UserResponse); + + // Act + var result = await _appwriteClient.Users.UpdateEmail(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task UpdateEmail_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = new UpdateEmailRequest + { + UserId = "user123", + Email = "newemail@example.com" + }; + + _mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/user123/email") + .WithJsonContent(request) + .ExpectedHeaders() + .Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); + + // Act + var result = await _appwriteClient.Users.UpdateEmail(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task UpdateEmail_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = new UpdateEmailRequest + { + UserId = "user123", + Email = "newemail@example.com" + }; + + _mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/user123/email") + .WithJsonContent(request) + .ExpectedHeaders() + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Users.UpdateEmail(request); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +} From e7591e2971a596acfee67c54fabb11c7916da6c5 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 8 Oct 2024 19:15:10 +0100 Subject: [PATCH 3/3] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ece4bebe..29f9d654 100644 --- a/README.md +++ b/README.md @@ -140,9 +140,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ``` ## ⌛ Progress -![Server & Client - 68 / 291](https://img.shields.io/badge/Server_&_Client-68%20%2F%20291-red?style=for-the-badge) +![Server & Client - 69 / 291](https://img.shields.io/badge/Server_&_Client-69%20%2F%20291-red?style=for-the-badge) -![Server - 24 / 201](https://img.shields.io/badge/Server-24%20%2F%20201-red?style=for-the-badge) +![Server - 25 / 201](https://img.shields.io/badge/Server-25%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 - 13 / 41](https://img.shields.io/badge/Users-13%20%2F%2041-red?style=for-the-badge) +![Account - 14 / 41](https://img.shields.io/badge/Users-14%20%2F%2041-gold?style=for-the-badge) | Endpoint | Client | Server | |:-:|:-:|:-:| @@ -224,7 +224,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [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) | ❌ | ✅ | -| [Update Email](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateEmail) | ❌ | ⬛ | +| [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) | ❌ | ⬛ | | [List User Logs](https://appwrite.io/docs/references/1.6.x/server-rest/users#listLogs) | ❌ | ⬛ |