Skip to content

Commit

Permalink
Merge pull request #251 from PinguApps/196-update-user-labels
Browse files Browse the repository at this point in the history
Implemented update user labels
  • Loading branch information
pingu2k4 authored Oct 8, 2024
2 parents eda5882 + c0f0088 commit ce39217
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 10 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 - 69 / 291](https://img.shields.io/badge/Server_&_Client-69%20%2F%20291-red?style=for-the-badge)
![Server & Client - 70 / 291](https://img.shields.io/badge/Server_&_Client-70%20%2F%20291-red?style=for-the-badge)

![Server - 25 / 201](https://img.shields.io/badge/Server-25%20%2F%20201-red?style=for-the-badge)
![Server - 26 / 201](https://img.shields.io/badge/Server-26%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 - 14 / 41](https://img.shields.io/badge/Users-14%20%2F%2041-gold?style=for-the-badge)
![Account - 15 / 41](https://img.shields.io/badge/Users-15%20%2F%2041-gold?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand All @@ -226,7 +226,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [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) || |
| [Update User Labels](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateLabels) || |
| [List User Logs](https://appwrite.io/docs/references/1.6.x/server-rest/users#listLogs) |||
| [List User Memberships](https://appwrite.io/docs/references/1.6.x/server-rest/users#listMemberships) |||
| [Update MFA](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateMfa) |||
Expand Down
6 changes: 3 additions & 3 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new UpdateEmailRequest()
var request = new UpdateUserLabelsRequest()
{
UserId = "664aac1a00113f82e620",
Email = "test@example.com"
Labels = ["test", "admin"]
};

var response = await _server.Users.UpdateEmail(request);
var response = await _server.Users.UpdateUserLabels(request);

Console.WriteLine(response.Result.Match(
result => result.ToString(),
Expand Down
9 changes: 8 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,14 @@ public interface IUsersClient
Task<AppwriteResult<User>> UpdateEmail(UpdateEmailRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<Jwt>> CreateUserJwt(CreateUserJwtRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// <para>Update the user labels by its unique ID.</para>
/// <para>Labels can be used to grant access to resources. While teams are a way for user's to share access to a resource, labels can be defined by the developer to grant access without an invitation. See the <see href="https://appwrite.io/docs/permissions">Permissions docs</see> for more info</para>
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#updateLabels">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> UpdateUserLabels(UpdateUserLabelsRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<LogsList>> ListUserLogs(ListUserLogsRequest 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 @@ -268,9 +268,22 @@ public async Task<AppwriteResult<User>> UpdateEmail(UpdateEmailRequest request)
/// <inheritdoc/>
public Task<AppwriteResult<Jwt>> CreateUserJwt(CreateUserJwtRequest request) => throw new NotImplementedException();

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

var result = await _usersApi.UpdateUserLabels(request.UserId, request);

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

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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
{
public static TheoryData<UpdateUserLabelsRequest> UpdateUserLabels_ValidRequestData =
[
new UpdateUserLabelsRequest
{
UserId = IdUtils.GenerateUniqueId(),
Labels = ["label1", "label2"]
},
new UpdateUserLabelsRequest
{
UserId = IdUtils.GenerateUniqueId(),
Labels = ["label3", "label4"]
}
];

[Theory]
[MemberData(nameof(UpdateUserLabels_ValidRequestData))]
public async Task UpdateUserLabels_ShouldReturnSuccess_WhenApiCallSucceeds(UpdateUserLabelsRequest request)
{
// Arrange
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/users/{request.UserId}/labels")
.WithJsonContent(request)
.ExpectedHeaders()
.Respond(Constants.AppJson, Constants.UserResponse);

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

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

[Fact]
public async Task UpdateUserLabels_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new UpdateUserLabelsRequest
{
UserId = "user123",
Labels = ["label1", "label2"]
};

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

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

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

[Fact]
public async Task UpdateUserLabels_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new UpdateUserLabelsRequest
{
UserId = "user123",
Labels = ["label1", "label2"]
};

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

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

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

0 comments on commit ce39217

Please sign in to comment.