Skip to content

Commit

Permalink
Merge pull request #248 from PinguApps/194-delete-user
Browse files Browse the repository at this point in the history
Implemented delete user
  • Loading branch information
pingu2k4 authored Oct 8, 2024
2 parents 3add83c + 1a1fbc4 commit ca57a83
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

## ⌛ Progress
![Server & Client - 67 / 291](https://img.shields.io/badge/Server_&_Client-67%20%2F%20291-red?style=for-the-badge)
![Server & Client - 68 / 291](https://img.shields.io/badge/Server_&_Client-68%20%2F%20291-red?style=for-the-badge)

![Server - 23 / 201](https://img.shields.io/badge/Server-23%20%2F%20201-red?style=for-the-badge)
![Server - 24 / 201](https://img.shields.io/badge/Server-24%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 +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 - 12 / 41](https://img.shields.io/badge/Users-12%20%2F%2041-red?style=for-the-badge)
![Account - 13 / 41](https://img.shields.io/badge/Users-13%20%2F%2041-red?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand All @@ -223,7 +223,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create User with Scrypt Modified Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createScryptModifiedUser) |||
| [Create User with SHA Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createSHAUser) |||
| [Get User](https://appwrite.io/docs/references/1.6.x/server-rest/users#get) |||
| [Delete User](https://appwrite.io/docs/references/1.6.x/server-rest/users#delete) || |
| [Delete User](https://appwrite.io/docs/references/1.6.x/server-rest/users#delete) || |
| [Update Email](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateEmail) |||
| [Create User JWT](https://appwrite.io/docs/references/1.6.x/server-rest/users#createJWT) |||
| [Update User Labels](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateLabels) |||
Expand Down
4 changes: 2 additions & 2 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new GetUserRequest()
var request = new DeleteUserRequest()
{
UserId = "670565fa00209de81fb0"
};

var response = await _server.Users.GetUser(request);
var response = await _server.Users.DeleteUser(request);

Console.WriteLine(response.Result.Match(
result => result.ToString(),
Expand Down
8 changes: 7 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ public interface IUsersClient
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> CreateUserWithShaPassword(CreateUserWithShaPasswordRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// Delete a user by its unique ID, thereby releasing it's ID. Since ID is released and can be reused, all user-related resources like documents or storage files should be deleted before user deletion. If you want to keep ID reserved, use <see cref="UpdateUserStatus(UpdateUserStatusRequest)"/> instead.
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#delete">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>204 success code</returns>
Task<AppwriteResult> DeleteUser(DeleteUserRequest request);

/// <summary>
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 @@ -213,9 +213,22 @@ public async Task<AppwriteResult<User>> CreateUserWithShaPassword(CreateUserWith
}
}

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

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

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

/// <inheritdoc/>
public async Task<AppwriteResult<User>> GetUser(GetUserRequest request)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests.Users;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Clients.Users;
public partial class UsersClientTests
{
[Theory]
[InlineData("user123")]
[InlineData("user456")]
public async Task DeleteUser_ShouldReturnSuccess_WhenApiCallSucceeds(string userId)
{
// Arrange
var request = new DeleteUserRequest
{
UserId = userId
};

_mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/users/{userId}")
.ExpectedHeaders()
.Respond(HttpStatusCode.NoContent);

// Act
var result = await _appwriteClient.Users.DeleteUser(request);

// Assert
Assert.True(result.Success);
_mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task DeleteUser_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new DeleteUserRequest
{
UserId = "user123"
};

_mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/users/user123")
.ExpectedHeaders()
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

// Act
var result = await _appwriteClient.Users.DeleteUser(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task DeleteUser_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new DeleteUserRequest
{
UserId = "user123"
};

_mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/users/user123")
.ExpectedHeaders()
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteClient.Users.DeleteUser(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}

0 comments on commit ca57a83

Please sign in to comment.