From 51fdf1b5b322e4f2a13eb3d0f9adca3a6b47c387 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Wed, 9 Oct 2024 22:23:01 +0100 Subject: [PATCH 1/3] implemented update phone --- src/PinguApps.Appwrite.Playground/App.cs | 6 +++--- .../Clients/IUsersClient.cs | 8 +++++++- .../Clients/UsersClient.cs | 17 +++++++++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/PinguApps.Appwrite.Playground/App.cs b/src/PinguApps.Appwrite.Playground/App.cs index 1ffe94ce..df75a028 100644 --- a/src/PinguApps.Appwrite.Playground/App.cs +++ b/src/PinguApps.Appwrite.Playground/App.cs @@ -17,13 +17,13 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server, public async Task Run(string[] args) { - var request = new UpdatePasswordRequest() + var request = new UpdatePhoneRequest() { UserId = "664aac1a00113f82e620", - Password = "MySuperPassword" + PhoneNumber = "+447501234567" }; - var response = await _server.Users.UpdatePassword(request); + var response = await _server.Users.UpdatePhone(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 22c17efa..1c8fed7a 100644 --- a/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs @@ -221,7 +221,13 @@ public interface IUsersClient /// The request content /// The user Task> UpdatePassword(UpdatePasswordRequest request); - [Obsolete("This method hasn't yet been implemented.", true)] + + /// + /// Update the user phone by its unique ID + /// Appwrite Docs + /// + /// The request content + /// The user Task> UpdatePhone(UpdatePhoneRequest request); [Obsolete("This method hasn't yet been implemented.", true)] Task>> GetUserPreferences(GetUserPreferencesRequest request); diff --git a/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs b/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs index 91f54287..ee4f4669 100644 --- a/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs @@ -463,9 +463,22 @@ public async Task> UpdatePassword(UpdatePasswordRequest req } } - [ExcludeFromCodeCoverage] /// - public Task> UpdatePhone(UpdatePhoneRequest request) => throw new NotImplementedException(); + public async Task> UpdatePhone(UpdatePhoneRequest request) + { + try + { + request.Validate(true); + + var result = await _usersApi.UpdatePhone(request.UserId, request); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse(); + } + } [ExcludeFromCodeCoverage] /// From 5d3bdd4d6baed905153133d2b97808fc71c7ae88 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Wed, 9 Oct 2024 22:24:36 +0100 Subject: [PATCH 2/3] added tests for update phone --- .../Users/UsersClientTests.UpdatePhone.cs | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdatePhone.cs diff --git a/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdatePhone.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdatePhone.cs new file mode 100644 index 00000000..f44e4a80 --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.UpdatePhone.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 UpdatePhone_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + var request = new UpdatePhoneRequest + { + UserId = IdUtils.GenerateUniqueId(), + PhoneNumber = "+1234567890" + }; + + _mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{request.UserId}/phone") + .ExpectedHeaders() + .Respond(Constants.AppJson, Constants.UserResponse); + + // Act + var result = await _appwriteClient.Users.UpdatePhone(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task UpdatePhone_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = new UpdatePhoneRequest + { + UserId = IdUtils.GenerateUniqueId(), + PhoneNumber = "+1234567890" + }; + + _mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{request.UserId}/phone") + .ExpectedHeaders() + .Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); + + // Act + var result = await _appwriteClient.Users.UpdatePhone(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task UpdatePhone_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = new UpdatePhoneRequest + { + UserId = IdUtils.GenerateUniqueId(), + PhoneNumber = "+1234567890" + }; + + _mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{request.UserId}/phone") + .ExpectedHeaders() + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Users.UpdatePhone(request); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +} From 6f732d8a442fa7025c85853c30368863ed971543 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Wed, 9 Oct 2024 22:25: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 d476ab5b..3249fb9d 100644 --- a/README.md +++ b/README.md @@ -140,9 +140,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ``` ## ⌛ Progress -![Server & Client - 81 / 291](https://img.shields.io/badge/Server_&_Client-81%20%2F%20291-red?style=for-the-badge) +![Server & Client - 82 / 291](https://img.shields.io/badge/Server_&_Client-82%20%2F%20291-red?style=for-the-badge) -![Server - 37 / 201](https://img.shields.io/badge/Server-37%20%2F%20201-red?style=for-the-badge) +![Server - 38 / 201](https://img.shields.io/badge/Server-38%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 - 26 / 41](https://img.shields.io/badge/Users-26%20%2F%2041-gold?style=for-the-badge) +![Account - 27 / 41](https://img.shields.io/badge/Users-27%20%2F%2041-gold?style=for-the-badge) | Endpoint | Client | Server | |:-:|:-:|:-:| @@ -237,7 +237,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [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 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) | ❌ | ⬛ | +| [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) | ❌ | ⬛ | | [Update User Preferences](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePrefs) | ❌ | ⬛ | | [List User Sessions](https://appwrite.io/docs/references/1.6.x/server-rest/users#listSessions) | ❌ | ⬛ |