Skip to content

Commit

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

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

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

public async Task Run(string[] args)
{
var request = new DeleteUserRequest()
var request = new UpdateEmailRequest()
{
UserId = "670565fa00209de81fb0"
UserId = "664aac1a00113f82e620",
Email = "[email protected]"
};

var response = await _server.Users.DeleteUser(request);
var response = await _server.Users.UpdateEmail(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 @@ -116,7 +116,13 @@ public interface IUsersClient
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> GetUser(GetUserRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// Update the user email by its unique ID
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#updateEmail">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> UpdateEmail(UpdateEmailRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<Jwt>> CreateUserJwt(CreateUserJwtRequest 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 @@ -247,9 +247,22 @@ public async Task<AppwriteResult<User>> GetUser(GetUserRequest request)
}
}

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

var result = await _usersApi.UpdateEmail(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,79 @@
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", "[email protected]")]
[InlineData("user456", "[email protected]")]
public async Task UpdateEmail_ShouldReturnSuccess_WhenApiCallSucceeds(string userId, string newEmail)
{
// Arrange
var request = new UpdateEmailRequest
{
UserId = userId,
Email = newEmail
};

_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{userId}/email")
.WithJsonContent(request)
.ExpectedHeaders()
.Respond(Constants.AppJson, Constants.UserResponse);

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

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

[Fact]
public async Task UpdateEmail_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new UpdateEmailRequest
{
UserId = "user123",
Email = "[email protected]"
};

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

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

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

[Fact]
public async Task UpdateEmail_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new UpdateEmailRequest
{
UserId = "user123",
Email = "[email protected]"
};

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

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

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

0 comments on commit eda5882

Please sign in to comment.