From 9aeeb131ff1b353dcd81c41a3a82b655ddec2282 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Wed, 9 Oct 2024 22:12:18 +0100 Subject: [PATCH 1/3] implemente dupdate name --- src/PinguApps.Appwrite.Playground/App.cs | 7 ++++--- .../Clients/IUsersClient.cs | 8 +++++++- .../Clients/UsersClient.cs | 17 +++++++++++++++-- .../Internals/IUsersApi.cs | 2 +- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/PinguApps.Appwrite.Playground/App.cs b/src/PinguApps.Appwrite.Playground/App.cs index 585d383b..1b9029c2 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 CreateMfaRecoveryCodesRequest() + var request = new UpdateNameRequest() { - UserId = "66b8e4aead8e4c1fc222" + UserId = "664aac1a00113f82e620", + Name = "My Name" }; - var response = await _server.Users.CreateMfaRecoveryCodes(request); + var response = await _server.Users.UpdateName(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 07939217..2afe61b2 100644 --- a/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs @@ -205,7 +205,13 @@ public interface IUsersClient /// The request content /// The mfa recovery codes Task> RegenerateMfaRecoveryCodes(RegenerateMfaRecoveryCodesRequest request); - [Obsolete("This method hasn't yet been implemented.", true)] + + /// + /// Update the user name by its unique ID + /// Appwrite Docs + /// + /// The request content + /// The user Task> UpdateName(UpdateNameRequest request); [Obsolete("This method hasn't yet been implemented.", true)] Task> UpdatePassword(UpdatePasswordRequest request); diff --git a/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs b/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs index 81837156..1af70ac3 100644 --- a/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs @@ -429,9 +429,22 @@ public async Task> RegenerateMfaRecoveryCodes(R } } - [ExcludeFromCodeCoverage] /// - public Task> UpdateName(UpdateNameRequest request) => throw new NotImplementedException(); + public async Task> UpdateName(UpdateNameRequest request) + { + try + { + request.Validate(true); + + var result = await _usersApi.UpdateName(request.UserId, request); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse(); + } + } [ExcludeFromCodeCoverage] /// diff --git a/src/PinguApps.Appwrite.Server/Internals/IUsersApi.cs b/src/PinguApps.Appwrite.Server/Internals/IUsersApi.cs index 9d8acfe8..59671d4f 100644 --- a/src/PinguApps.Appwrite.Server/Internals/IUsersApi.cs +++ b/src/PinguApps.Appwrite.Server/Internals/IUsersApi.cs @@ -82,7 +82,7 @@ internal interface IUsersApi : IBaseApi [Put("/users/{userId}/mfa/recovery-codes")] Task> RegenerateMfaRecoveryCodes(string userId); - [Patch("/users/{userId}/mame")] + [Patch("/users/{userId}/name")] Task> UpdateName(string userId, UpdateNameRequest request); [Patch("/users/{userId}/password")] From 757aa1bad78752036c8936d6a55491815717dbca Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Wed, 9 Oct 2024 22:13:29 +0100 Subject: [PATCH 2/3] added tests for update name --- .../Users/UsersClientTests.UpdateName.cs | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdateName.cs diff --git a/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdateName.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdateName.cs new file mode 100644 index 00000000..67ece298 --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdateName.cs @@ -0,0 +1,75 @@ +using System.Net; +using PinguApps.Appwrite.Shared.Requests.Users; +using PinguApps.Appwrite.Shared.Tests; +using PinguApps.Appwrite.Shared.Utils; +using RichardSzalay.MockHttp; + +namespace PinguApps.Appwrite.Server.Tests.Clients.Users; +public partial class UsersClientTests +{ + [Fact] + public async Task UpdateName_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + var request = new UpdateNameRequest + { + UserId = IdUtils.GenerateUniqueId(), + Name = "New Name" + }; + + _mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{request.UserId}/name") + .ExpectedHeaders() + .Respond(Constants.AppJson, Constants.UserResponse); + + // Act + var result = await _appwriteClient.Users.UpdateName(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task UpdateName_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = new UpdateNameRequest + { + UserId = IdUtils.GenerateUniqueId(), + Name = "New Name" + }; + + _mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{request.UserId}/name") + .ExpectedHeaders() + .Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); + + // Act + var result = await _appwriteClient.Users.UpdateName(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task UpdateName_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = new UpdateNameRequest + { + UserId = IdUtils.GenerateUniqueId(), + Name = "New Name" + }; + + _mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{request.UserId}/name") + .ExpectedHeaders() + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Users.UpdateName(request); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +} From 84753236a5effdddb6e888aa48105f98c78edfc5 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Wed, 9 Oct 2024 22:14:27 +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 8bf99fdc..6e96d5b5 100644 --- a/README.md +++ b/README.md @@ -140,9 +140,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ``` ## ⌛ Progress -![Server & Client - 79 / 291](https://img.shields.io/badge/Server_&_Client-79%20%2F%20291-red?style=for-the-badge) +![Server & Client - 80 / 291](https://img.shields.io/badge/Server_&_Client-80%20%2F%20291-red?style=for-the-badge) -![Server - 35 / 201](https://img.shields.io/badge/Server-35%20%2F%20201-red?style=for-the-badge) +![Server - 36 / 201](https://img.shields.io/badge/Server-36%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 - 24 / 41](https://img.shields.io/badge/Users-24%20%2F%2041-gold?style=for-the-badge) +![Account - 25 / 41](https://img.shields.io/badge/Users-25%20%2F%2041-gold?style=for-the-badge) | Endpoint | Client | Server | |:-:|:-:|:-:| @@ -235,7 +235,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [Get MFA Recovery Codes](https://appwrite.io/docs/references/1.6.x/server-rest/users#getMfaRecoveryCodes) | ❌ | ✅ | | [Regenerate MFA Recovery Codes](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateMfaRecoveryCodes) | ❌ | ✅ | | [Create MFA Recovery Codes](https://appwrite.io/docs/references/1.6.x/server-rest/users#createMfaRecoveryCodes) | ❌ | ✅ | -| [Update Name](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateName) | ❌ | ⬛ | +| [Update Name](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateName) | ❌ | ✅ | | [Update Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePassword) | ❌ | ⬛ | | [Update Phone](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePhone) | ❌ | ⬛ | | [Get User Preferences](https://appwrite.io/docs/references/1.6.x/server-rest/users#getPrefs) | ❌ | ⬛ |