From 946fe8553562837be468d1d5532d6ca8ed1d4d9c Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Sat, 10 Aug 2024 22:45:46 +0100 Subject: [PATCH] Created client tests --- ...countClientTests.CreateMfaRecoveryCodes.cs | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateMfaRecoveryCodes.cs diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateMfaRecoveryCodes.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateMfaRecoveryCodes.cs new file mode 100644 index 00000000..2be9b3c3 --- /dev/null +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateMfaRecoveryCodes.cs @@ -0,0 +1,74 @@ +using System.Net; +using PinguApps.Appwrite.Client.Clients; +using PinguApps.Appwrite.Shared.Tests; +using RichardSzalay.MockHttp; + +namespace PinguApps.Appwrite.Client.Tests.Clients.Account; +public partial class AccountClientTests +{ + [Fact] + public async Task CreateMfaRecoveryCodes_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + _mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/recovery-codes") + .ExpectedHeaders(true) + .Respond(Constants.AppJson, Constants.JwtResponse); + + _appwriteClient.SetSession(Constants.Session); + + // Act + var result = await _appwriteClient.Account.CreateMfaRecoveryCodes(); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task CreateMfaRecoveryCodes_ShouldReturnError_WhenSessionIsNull() + { + // Act + var result = await _appwriteClient.Account.CreateMfaRecoveryCodes(); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsInternalError); + Assert.Equal(ISessionAware.SessionExceptionMessage, result.Result.AsT2.Message); + } + + [Fact] + public async Task CreateMfaRecoveryCodes_ShouldHandleException_WhenApiCallFails() + { + // Arrange + _mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/recovery-codes") + .ExpectedHeaders(true) + .Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); + + _appwriteClient.SetSession(Constants.Session); + + // Act + var result = await _appwriteClient.Account.CreateMfaRecoveryCodes(); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task CreateMfaRecoveryCodes_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + _mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/recovery-codes") + .ExpectedHeaders(true) + .Throw(new HttpRequestException("An error occurred")); + + _appwriteClient.SetSession(Constants.Session); + + // Act + var result = await _appwriteClient.Account.CreateMfaRecoveryCodes(); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +}