-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from PinguApps/87-feat-account-add-authenticator
Implemented add authenticator
- Loading branch information
Showing
9 changed files
with
189 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PinguApps.Appwrite.Shared.Responses; | ||
|
||
/// <summary> | ||
/// An Appwrite Mfa Type object | ||
/// </summary> | ||
/// <param name="Secret">Secret token used for TOTP factor</param> | ||
/// <param name="Uri">URI for authenticator apps</param> | ||
public record MfaType( | ||
[property: JsonPropertyName("secret")] string Secret, | ||
[property: JsonPropertyName("uri")] string Uri | ||
); |
82 changes: 82 additions & 0 deletions
82
tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.AddAuthenticator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Net; | ||
using PinguApps.Appwrite.Shared.Tests; | ||
using RichardSzalay.MockHttp; | ||
|
||
namespace PinguApps.Appwrite.Client.Tests.Clients.Account; | ||
public partial class AccountClientTests | ||
{ | ||
[Fact] | ||
public async Task AddAuthenticator_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/authenticators/totp") | ||
.ExpectedHeaders(true) | ||
.Respond(Constants.AppJson, Constants.MfaTypeResponse); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.AddAuthenticator(); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task AddAuthenticator_ShouldHitDifferentEndpoint_WhenNewTypeIsUsed() | ||
{ | ||
// Arrange | ||
var type = "newAuth"; | ||
var requestUri = $"{Constants.Endpoint}/account/mfa/authenticators/{type}"; | ||
var request = _mockHttp.Expect(HttpMethod.Post, requestUri) | ||
.ExpectedHeaders(true) | ||
.Respond(Constants.AppJson, Constants.MfaTypeResponse); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.AddAuthenticator(type); | ||
|
||
// Assert | ||
_mockHttp.VerifyNoOutstandingExpectation(); | ||
var matches = _mockHttp.GetMatchCount(request); | ||
Assert.Equal(1, matches); | ||
} | ||
|
||
[Fact] | ||
public async Task AddAuthenticator_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/authenticators/totp") | ||
.ExpectedHeaders(true) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.AddAuthenticator(); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task AddAuthenticator_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/authenticators/totp") | ||
.ExpectedHeaders(true) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.AddAuthenticator(); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/PinguApps.Appwrite.Shared.Tests/Responses/MfaTypeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Text.Json; | ||
using PinguApps.Appwrite.Shared.Responses; | ||
|
||
namespace PinguApps.Appwrite.Shared.Tests.Responses; | ||
public class MfaTypeTests | ||
{ | ||
[Fact] | ||
public void LogsList_Constructor_AssignsPropertiesCorrectly() | ||
{ | ||
// Arrange | ||
var secret = "The_Secret"; | ||
var uri = "otpauth://totp/Appwrite%20Test%3Apingu%40appwrite.com?issuer=Appwrite%20Test&secret=The_Secret"; | ||
|
||
// Act | ||
var mfaType = new MfaType(secret, uri); | ||
|
||
// Assert | ||
Assert.Equal(secret, mfaType.Secret); | ||
Assert.Equal(uri, mfaType.Uri); | ||
} | ||
|
||
[Fact] | ||
public void LogsList_CanBeDeserialized_FromJson() | ||
{ | ||
// Act | ||
var mfaType = JsonSerializer.Deserialize<MfaType>(Constants.MfaTypeResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); | ||
|
||
// Assert | ||
Assert.NotNull(mfaType); | ||
Assert.Equal("The_Secret", mfaType.Secret); | ||
Assert.Equal("otpauth://totp/Appwrite%20Test%3Apingu%40appwrite.com?issuer=Appwrite%20Test&secret=The_Secret", mfaType.Uri); | ||
} | ||
} |