Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented delete user sessions #270

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -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) | ❌ | ⬛ |
Expand Down
10 changes: 3 additions & 7 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
9 changes: 7 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 15 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/UsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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/>
Expand Down
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);
}
}