Skip to content

Commit

Permalink
Merge pull request #236 from PinguApps/187-users-delete-identity
Browse files Browse the repository at this point in the history
Implemented delete identity for users api
  • Loading branch information
pingu2k4 authored Oct 7, 2024
2 parents 516835d + d0e94dd commit 268d380
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

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

![Server - 16 / 201](https://img.shields.io/badge/Server-16%20%2F%20201-red?style=for-the-badge)
![Server - 17 / 201](https://img.shields.io/badge/Server-17%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 @@ -205,7 +205,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create Phone Verification (Confirmation)](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePhoneVerification) ||| |

### Users
![Account - 5 / 41](https://img.shields.io/badge/Users-5%20%2F%2041-red?style=for-the-badge)
![Account - 6 / 41](https://img.shields.io/badge/Users-6%20%2F%2041-red?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand All @@ -214,7 +214,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create User with Argon2 Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createArgon2User) |||
| [Create User with Bcrypt Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createBcryptUser) |||
| [List Identities](https://appwrite.io/docs/references/1.6.x/server-rest/users#listIdentities) |||
| [Delete Identity](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteIdentity) || |
| [Delete Identity](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteIdentity) || |
| [Create User with MD5 Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createMD5User) |||
| [Create User with PHPass Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createPHPassUser) |||
| [Create User with Scrypt Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createScryptUser) |||
Expand Down
7 changes: 5 additions & 2 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new ListIdentitiesRequest();
var request = new DeleteIdentityRequest()
{
IdentityId = "66b8e4aecc12287deb47"
};

var response = await _server.Users.ListIdentities(request);
var response = await _server.Users.DeleteIdentity(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 @@ -52,7 +52,13 @@ public interface IUsersClient
/// <param name="request">The request content</param>
/// <returns>The identities list</returns>
Task<AppwriteResult<IdentitiesList>> ListIdentities(ListIdentitiesRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// Delete an identity by its unique ID
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteIdentity">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>204 success code</returns>
Task<AppwriteResult> DeleteIdentity(DeleteIdentityRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<User>> CreateUserWithMd5Password(CreateUserWithMd5PasswordRequest 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 @@ -111,9 +111,22 @@ public async Task<AppwriteResult<IdentitiesList>> ListIdentities(ListIdentitiesR
}
}

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

var result = await _usersApi.DeleteIdentity(request.IdentityId);

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,73 @@
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("identity123")]
[InlineData("identity456")]
public async Task DeleteIdentity_ShouldReturnSuccess_WhenApiCallSucceeds(string identityId)
{
// Arrange
var request = new DeleteIdentityRequest
{
IdentityId = identityId
};

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

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

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

[Fact]
public async Task DeleteIdentity_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new DeleteIdentityRequest
{
IdentityId = "identity123"
};

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

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

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

[Fact]
public async Task DeleteIdentity_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new DeleteIdentityRequest
{
IdentityId = "identity123"
};

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

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

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

0 comments on commit 268d380

Please sign in to comment.