Skip to content

Commit

Permalink
Merge pull request #270 from PinguApps/212-delete-user-sessions
Browse files Browse the repository at this point in the history
Implemented delete user sessions
pingu2k4 authored Oct 10, 2024
2 parents d3073e4 + 90abfd6 commit 49620c4
Showing 5 changed files with 102 additions and 15 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
<!-- `red` for first third, `gold` for second third, `forestgreen` for final third -->
![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) |||
10 changes: 3 additions & 7 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
@@ -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<string, string>()
{
{ "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(),
9 changes: 7 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs
Original file line number Diff line number Diff line change
@@ -171,7 +171,7 @@ public interface IUsersClient
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteMfaAuthenticator">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The user</returns>
/// <returns>204 success code</returns>
Task<AppwriteResult> DeleteAuthenticator(DeleteAuthenticatorRequest request);

/// <summary>
@@ -245,7 +245,12 @@ public interface IUsersClient
/// <param name="request">The request content</param>
/// <returns>The user preferences</returns>
Task<AppwriteResult<IReadOnlyDictionary<string, string>>> UpdateUserPreferences(UpdateUserPreferencesRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// Delete all user's sessions by using the user's unique ID
/// </summary>
/// <param name="request">The request content</param>
/// <returns>204 success code</returns>
Task<AppwriteResult> DeleteUserSessions(DeleteUserSessionsRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<SessionsList>> ListUserSessions(ListUserSessionsRequest request);
17 changes: 15 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/UsersClient.cs
Original file line number Diff line number Diff line change
@@ -514,9 +514,22 @@ public async Task<AppwriteResult<IReadOnlyDictionary<string, string>>> UpdateUse
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult> DeleteUserSessions(DeleteUserSessionsRequest request) => throw new NotImplementedException();
public async Task<AppwriteResult> DeleteUserSessions(DeleteUserSessionsRequest request)
{
try
{
request.Validate(true);

var result = await _usersApi.DeleteUserSessions(request.UserId);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse();
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 49620c4

Please sign in to comment.