Skip to content

Commit

Permalink
Merge pull request #120 from PinguApps/86-update-mfa
Browse files Browse the repository at this point in the history
Implemented update mfa
  • Loading branch information
pingu2k4 authored Aug 8, 2024
2 parents 1a7b945 + e2ffa33 commit 887f08b
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 9 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

## ⌛ Progress
<!-- ![20 / 288](https://progress-bar.dev/20/?scale=288&suffix=%20/%20288&width=500) -->
![Server & Client - 20 / 288](https://img.shields.io/badge/Server_&_Client-20%20%2F%20288-red?style=for-the-badge)
<!-- ![21 / 288](https://progress-bar.dev/21/?scale=288&suffix=%20/%20288&width=500) -->
![Server & Client - 21 / 288](https://img.shields.io/badge/Server_&_Client-21%20%2F%20288-red?style=for-the-badge)

<!-- ![2 / 195](https://progress-bar.dev/2/?scale=195&suffix=%20/%20195&width=300) -->
![Server - 2 / 195](https://img.shields.io/badge/Server-2%20%2F%20195-red?style=for-the-badge)

<!-- ![18 / 93](https://progress-bar.dev/18/?scale=93&suffix=%20/%2093&width=300) -->
![Client - 18 / 93](https://img.shields.io/badge/Client-18%20%2F%2093-red?style=for-the-badge)
<!-- ![19 / 93](https://progress-bar.dev/19/?scale=93&suffix=%20/%2093&width=300) -->
![Client - 19 / 93](https://img.shields.io/badge/Client-19%20%2F%2093-red?style=for-the-badge)

### 🔑 Key
| Icon | Definition |
Expand All @@ -155,8 +155,8 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
|| There is currently no intention to implement the endpoint for the given SDK type (client or server) |

### Account
<!-- ![20 / 52](https://progress-bar.dev/20/?scale=52&suffix=%20/%2052&width=120) -->
![Account - 20 / 52](https://img.shields.io/badge/Account-20%20%2F%2052-yellow?style=for-the-badge)
<!-- ![21 / 52](https://progress-bar.dev/21/?scale=52&suffix=%20/%2052&width=120) -->
![Account - 21 / 52](https://img.shields.io/badge/Account-21%20%2F%2052-yellow?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand All @@ -167,7 +167,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Delete Identity](https://appwrite.io/docs/references/1.5.x/client-rest/account#deleteIdentity) |||
| [Create JWT](https://appwrite.io/docs/references/1.5.x/client-rest/account#createJWT) |||
| [List Logs](https://appwrite.io/docs/references/1.5.x/client-rest/account#listLogs) |||
| [Update MFA](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateMFA) | ||
| [Update MFA](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateMFA) | ||
| [Add Authenticator](https://appwrite.io/docs/references/1.5.x/client-rest/account#createMfaAuthenticator) |||
| [Verify Authenticator](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateMfaAuthenticator) |||
| [Delete Authenticator](https://appwrite.io/docs/references/1.5.x/client-rest/account#deleteMfaAuthenticator) |||
Expand Down
17 changes: 17 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,21 @@ public async Task<AppwriteResult<User>> VerifyAuthenticator(VerifyAuthenticatorR
return e.GetExceptionResponse<User>();
}
}

/// <inheritdoc/>
public async Task<AppwriteResult<User>> UpdateMfa(UpdateMfaRequest request)
{
try
{
request.Validate(true);

var result = await _accountApi.UpdateMfa(Session, request);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<User>();
}
}
}
7 changes: 7 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,11 @@ public interface IAccountClient
/// <param name="type">Type of authenticator</param>
/// <returns>The User</returns>
Task<AppwriteResult<User>> VerifyAuthenticator(VerifyAuthenticatorRequest request, string type = "totp");

/// <summary>
/// Enable or disable MFA on an account
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> UpdateMfa(UpdateMfaRequest request);
}
3 changes: 3 additions & 0 deletions src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ internal interface IAccountApi : IBaseApi

[Put("/account/mfa/authenticators/{type}")]
Task<IApiResponse<User>> VerifyAuthenticator([Header("x-appwrite-session")] string? session, string type, VerifyAuthenticatorRequest request);

[Patch("/account/mfa")]
Task<IApiResponse<User>> UpdateMfa([Header("x-appwrite-session")] string? session, UpdateMfaRequest request);
}
4 changes: 2 additions & 2 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public async Task Run(string[] args)
_client.SetSession(_session);

//var response = await _client.Account.AddAuthenticator();
var response = await _client.Account.VerifyAuthenticator(new Shared.Requests.VerifyAuthenticatorRequest
var response = await _client.Account.UpdateMfa(new Shared.Requests.UpdateMfaRequest
{
Otp = "623850"
MfaEnabled = false
});

Console.WriteLine(response.Result.Match(
Expand Down
16 changes: 16 additions & 0 deletions src/PinguApps.Appwrite.Shared/Requests/UpdateMfaRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;
using PinguApps.Appwrite.Shared.Requests.Validators;

namespace PinguApps.Appwrite.Shared.Requests;

/// <summary>
/// The request for updating Mfa
/// </summary>
public class UpdateMfaRequest : BaseRequest<UpdateMfaRequest, UpdateMfaRequestValidator>
{
/// <summary>
/// Enable or disable MFA
/// </summary>
[JsonPropertyName("mfa")]
public bool MfaEnabled { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using FluentValidation;

namespace PinguApps.Appwrite.Shared.Requests.Validators;
public class UpdateMfaRequestValidator : AbstractValidator<UpdateMfaRequest>
{
public UpdateMfaRequestValidator()
{
RuleFor(x => x.MfaEnabled).NotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Account;
public partial class AccountClientTests
{
[Fact]
public async Task UpdateMfa_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new UpdateMfaRequest()
{
MfaEnabled = true
};

_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/mfa")
.ExpectedHeaders(true)
.WithJsonContent(request)
.Respond(Constants.AppJson, Constants.UserResponse);

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.UpdateMfa(request);

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

[Fact]
public async Task UpdateMfa_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new UpdateMfaRequest()
{
MfaEnabled = true
};

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

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.UpdateMfa(request);

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

[Fact]
public async Task UpdateMfa_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new UpdateMfaRequest()
{
MfaEnabled = true
};

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

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.UpdateMfa(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using PinguApps.Appwrite.Shared.Requests;

namespace PinguApps.Appwrite.Shared.Tests.Requests;
public class UpdateMfaRequestTests
{
[Fact]
public void Constructor_InitializesWithExpectedValues()
{
// Arrange & Act
var request = new UpdateMfaRequest();

// Assert
Assert.False(request.MfaEnabled);
}

[Fact]
public void Properties_CanBeSet()
{
// Arrange
var request = new UpdateMfaRequest();

// Act
request.MfaEnabled = true;

// Assert
Assert.True(request.MfaEnabled);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsValid_WithValidData_ReturnsTrue(bool mfaEnabled)
{
// Arrange
var request = new UpdateMfaRequest
{
MfaEnabled = mfaEnabled
};

// Act
var isValid = request.IsValid();

// Assert
Assert.True(isValid);
}
}

0 comments on commit 887f08b

Please sign in to comment.