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 update password #264

Merged
merged 3 commits into from
Oct 9, 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
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 - 80 / 291](https://img.shields.io/badge/Server_&_Client-80%20%2F%20291-red?style=for-the-badge)
![Server & Client - 81 / 291](https://img.shields.io/badge/Server_&_Client-81%20%2F%20291-red?style=for-the-badge)

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

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -236,7 +236,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Regenerate MFA Recovery Codes](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateMfaRecoveryCodes) | ❌ | ✅ |
| [Create MFA Recovery Codes](https://appwrite.io/docs/references/1.6.x/server-rest/users#createMfaRecoveryCodes) | ❌ | ✅ |
| [Update Name](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateName) | ❌ | ✅ |
| [Update Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePassword) | ❌ | |
| [Update Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePassword) | ❌ | |
| [Update Phone](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePhone) | ❌ | ⬛ |
| [Get User Preferences](https://appwrite.io/docs/references/1.6.x/server-rest/users#getPrefs) | ❌ | ⬛ |
| [Update User Preferences](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePrefs) | ❌ | ⬛ |
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 UpdateNameRequest()
var request = new UpdatePasswordRequest()
{
UserId = "664aac1a00113f82e620",
Name = "My Name"
Password = "MySuperPassword"
};

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

/// <summary>
/// Update the user password by its unique ID
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePassword">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> UpdatePassword(UpdatePasswordRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<User>> UpdatePhone(UpdatePhoneRequest 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 @@ -446,9 +446,22 @@ public async Task<AppwriteResult<User>> UpdateName(UpdateNameRequest request)
}
}

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

var result = await _usersApi.UpdatePassword(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,75 @@
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 UpdatePassword_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new UpdatePasswordRequest
{
UserId = IdUtils.GenerateUniqueId(),
Password = "newPassword"
};

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

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

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

[Fact]
public async Task UpdatePassword_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new UpdatePasswordRequest
{
UserId = IdUtils.GenerateUniqueId(),
Password = "newPassword"
};

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

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

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

[Fact]
public async Task UpdatePassword_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new UpdatePasswordRequest
{
UserId = IdUtils.GenerateUniqueId(),
Password = "newPassword"
};

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

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

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