From 89b509768d92e4e8af23872c01c64de709e896e7 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Thu, 10 Oct 2024 19:09:22 +0100 Subject: [PATCH 1/3] implemented delete user sessions --- src/PinguApps.Appwrite.Playground/App.cs | 10 +++------- .../Clients/IUsersClient.cs | 9 +++++++-- .../Clients/UsersClient.cs | 17 +++++++++++++++-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/PinguApps.Appwrite.Playground/App.cs b/src/PinguApps.Appwrite.Playground/App.cs index 3e1934b0..f8f96fce 100644 --- a/src/PinguApps.Appwrite.Playground/App.cs +++ b/src/PinguApps.Appwrite.Playground/App.cs @@ -17,16 +17,12 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server, public async Task Run(string[] args) { - var request = new UpdateUserPreferencesRequest() + var request = new DeleteUserSessionsRequest() { - UserId = "664aac1a00113f82e620", - Preferences = new Dictionary() - { - { "theme", "dark" } - } + UserId = "664aac1a00113f82e620" }; - var response = await _server.Users.UpdateUserPreferences(request); + var response = await _server.Users.DeleteUserSessions(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 0c9e41cc..03c71499 100644 --- a/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs @@ -171,7 +171,7 @@ public interface IUsersClient /// Appwrite Docs /// /// The request content - /// The user + /// 204 success code Task DeleteAuthenticator(DeleteAuthenticatorRequest request); /// @@ -245,7 +245,12 @@ public interface IUsersClient /// The request content /// The user preferences Task>> UpdateUserPreferences(UpdateUserPreferencesRequest request); - [Obsolete("This method hasn't yet been implemented.", true)] + + /// + /// Delete all user's sessions by using the user's unique ID + /// + /// The request content + /// 204 success code Task DeleteUserSessions(DeleteUserSessionsRequest request); [Obsolete("This method hasn't yet been implemented.", true)] Task> ListUserSessions(ListUserSessionsRequest request); diff --git a/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs b/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs index 70a3f2a6..92a31d00 100644 --- a/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/UsersClient.cs @@ -514,9 +514,22 @@ public async Task>> UpdateUse } } - [ExcludeFromCodeCoverage] /// - public Task DeleteUserSessions(DeleteUserSessionsRequest request) => throw new NotImplementedException(); + public async Task DeleteUserSessions(DeleteUserSessionsRequest request) + { + try + { + request.Validate(true); + + var result = await _usersApi.DeleteUserSessions(request.UserId); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse(); + } + } [ExcludeFromCodeCoverage] /// From 4ff5a549054468a8f98c9df8ccd7119daf8fbe35 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Thu, 10 Oct 2024 19:10:55 +0100 Subject: [PATCH 2/3] added tests for delete user sessions --- .../UsersClientTests.DeleteUserSessions.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.DeleteUserSessions.cs diff --git a/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.DeleteUserSessions.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.DeleteUserSessions.cs new file mode 100644 index 00000000..1c52aeb4 --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Users/UsersClientTests.DeleteUserSessions.cs @@ -0,0 +1,72 @@ +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 DeleteUserSessions_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + var request = new DeleteUserSessionsRequest + { + UserId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/users/{request.UserId}/sessions") + .ExpectedHeaders() + .Respond(HttpStatusCode.NoContent); + + // Act + var result = await _appwriteClient.Users.DeleteUserSessions(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task DeleteUserSessions_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = new DeleteUserSessionsRequest + { + UserId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/users/{request.UserId}/sessions") + .ExpectedHeaders() + .Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); + + // Act + var result = await _appwriteClient.Users.DeleteUserSessions(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task DeleteUserSessions_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = new DeleteUserSessionsRequest + { + UserId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/users/{request.UserId}/sessions") + .ExpectedHeaders() + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Users.DeleteUserSessions(request); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +} From 90abfd632a9797eeed169a26f97f3b79fdfd8c1c Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Thu, 10 Oct 2024 19:13:41 +0100 Subject: [PATCH 3/3] Update README.md --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 97acb740..6da7391b 100644 --- a/README.md +++ b/README.md @@ -140,9 +140,10 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ``` ## ⌛ Progress -![Server & Client - 84 / 291](https://img.shields.io/badge/Server_&_Client-84%20%2F%20291-red?style=for-the-badge) + +![Server & Client - 85 / 291](https://img.shields.io/badge/Server_&_Client-85%20%2F%20291-red?style=for-the-badge) -![Server - 40 / 201](https://img.shields.io/badge/Server-40%20%2F%20201-red?style=for-the-badge) +![Server - 41 / 201](https://img.shields.io/badge/Server-41%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 +208,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [Create Phone Verification (Confirmation)](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePhoneVerification) | ✅ | ❌ | | ### Users -![Account - 29 / 41](https://img.shields.io/badge/Users-29%20%2F%2041-gold?style=for-the-badge) +![Account - 30 / 41](https://img.shields.io/badge/Users-30%20%2F%2041-forestgreen?style=for-the-badge) | Endpoint | Client | Server | |:-:|:-:|:-:| @@ -242,7 +243,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [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) | ❌ | ⬛ | | [Create Session](https://appwrite.io/docs/references/1.6.x/server-rest/users#createSession) | ❌ | ⬛ | -| [Delete User Sessions](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteSessions) | ❌ | ⬛ | +| [Delete User Sessions](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteSessions) | ❌ | ✅ | | [Delete User Session](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteSession) | ❌ | ⬛ | | [Update User Status](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateStatus) | ❌ | ⬛ | | [List User Targets](https://appwrite.io/docs/references/1.6.x/server-rest/users#listTargets) | ❌ | ⬛ |