Skip to content

Commit

Permalink
Added all client tests for new code
Browse files Browse the repository at this point in the history
  • Loading branch information
pingu2k4 committed Aug 9, 2024
1 parent f0c69ff commit 1afad7b
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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 DeleteAuthenticator_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new DeleteAuthenticatorRequest()
{
Otp = "123456"
};

_mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/account/mfa/authenticators/totp")
.ExpectedHeaders(true)
.WithJsonContent(request)
.Respond(Constants.AppJson, Constants.MfaTypeResponse);

_appwriteClient.SetSession(Constants.Session);

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

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

[Fact]
public async Task DeleteAuthenticator_ShouldHitDifferentEndpoint_WhenNewTypeIsUsed()
{
// Arrange
var type = "newAuth";
var request = new DeleteAuthenticatorRequest()
{
Type = type,
Otp = "123456"
};
var requestUri = $"{Constants.Endpoint}/account/mfa/authenticators/{type}";
var mockRequest = _mockHttp.Expect(HttpMethod.Delete, requestUri)
.ExpectedHeaders(true)
.WithJsonContent(request)
.Respond(Constants.AppJson, Constants.MfaTypeResponse);

_appwriteClient.SetSession(Constants.Session);

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

// Assert
_mockHttp.VerifyNoOutstandingExpectation();
var matches = _mockHttp.GetMatchCount(mockRequest);
Assert.Equal(1, matches);
}

[Fact]
public async Task DeleteAuthenticator_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new DeleteAuthenticatorRequest()
{
Otp = "123456"
};

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

_appwriteClient.SetSession(Constants.Session);

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

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

[Fact]
public async Task DeleteAuthenticator_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new DeleteAuthenticatorRequest()
{
Otp = "123456"
};

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

_appwriteClient.SetSession(Constants.Session);

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

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}
77 changes: 73 additions & 4 deletions tests/PinguApps.Appwrite.Client.Tests/Utils/ResponseUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
using System.Net;
using Moq;
using OneOf.Types;
using PinguApps.Appwrite.Client.Utils;
using PinguApps.Appwrite.Shared.Tests;
using Refit;

namespace PinguApps.Appwrite.Client.Tests.Utils;
public class ResponseUtilsTests
{

[Fact]
public void GetApiResponse_Success_ReturnsContent()
{
var mockApiResponse = new Mock<IApiResponse>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(true);

var result = mockApiResponse.Object.GetApiResponse();

Assert.True(result.Success);
Assert.IsType<Success>(result.Result.AsT0);
}

[Fact]
public async Task GetApiResponse_Failure_ReturnsError()
{
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(Constants.AppwriteError)
};
var exception = await ApiException.Create(new HttpRequestMessage(), HttpMethod.Get, response, new RefitSettings());

var mockApiResponse = new Mock<IApiResponse>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(false);
mockApiResponse.SetupGet(x => x.Error).Returns(exception);

var result = mockApiResponse.Object.GetApiResponse();

Assert.False(result.Success);
Assert.True(result.IsAppwriteError);
Assert.True(result.Result.IsT1);
}

[Fact]
public async Task GetApiResponse_FailureButNullErrorContent_ThrowsException()
{
var exception = await ApiException.Create(new HttpRequestMessage(), HttpMethod.Get, new HttpResponseMessage(HttpStatusCode.InternalServerError), new RefitSettings());

var mockApiResponse = new Mock<IApiResponse>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(false);
mockApiResponse.SetupGet(x => x.Error).Returns(exception);

Assert.Throws<Exception>(() => mockApiResponse.Object.GetApiResponse());
}

[Fact]
public void GetApiResponse_FailureButNullError_ThrowsException()
{
var mockApiResponse = new Mock<IApiResponse>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(false);
mockApiResponse.SetupGet(x => x.Error).Returns((ApiException)null!);

Assert.Throws<Exception>(() => mockApiResponse.Object.GetApiResponse());
}

[Fact]
public void GenericGetApiResponse_Success_ReturnsContent()
{
var mockApiResponse = new Mock<IApiResponse<string>>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(true);
Expand All @@ -21,7 +77,7 @@ public void GetApiResponse_Success_ReturnsContent()
}

[Fact]
public void GetApiResponse_SuccessButNullContent_ReturnsInternalError()
public void GenericGetApiResponse_SuccessButNullContent_ReturnsInternalError()
{
var mockApiResponse = new Mock<IApiResponse<string>>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(true);
Expand All @@ -34,7 +90,7 @@ public void GetApiResponse_SuccessButNullContent_ReturnsInternalError()
}

[Fact]
public async Task GetApiResponse_Failure_ReturnsError()
public async Task GenericGetApiResponse_Failure_ReturnsError()
{
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Expand All @@ -54,7 +110,7 @@ public async Task GetApiResponse_Failure_ReturnsError()
}

[Fact]
public async Task GetApiResponse_FailureButNullErrorContent_ThrowsException()
public async Task GenericGetApiResponse_FailureButNullErrorContent_ThrowsException()
{
var exception = await ApiException.Create(new HttpRequestMessage(), HttpMethod.Get, new HttpResponseMessage(HttpStatusCode.InternalServerError), new RefitSettings());

Expand All @@ -66,7 +122,7 @@ public async Task GetApiResponse_FailureButNullErrorContent_ThrowsException()
}

[Fact]
public void GetApiResponse_FailureButNullError_ThrowsException()
public void GenericGetApiResponse_FailureButNullError_ThrowsException()
{
var mockApiResponse = new Mock<IApiResponse<string>>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(false);
Expand All @@ -80,6 +136,19 @@ public void GetExceptionResponse_ReturnsInternalError()
{
var exception = new Exception("Test exception");

var result = exception.GetExceptionResponse();

Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.True(result.Result.IsT2);
Assert.Equal("Test exception", result.Result.AsT2.Message);
}

[Fact]
public void GenericGetExceptionResponse_ReturnsInternalError()
{
var exception = new Exception("Test exception");

var result = exception.GetExceptionResponse<string>();

Assert.False(result.Success);
Expand Down

0 comments on commit 1afad7b

Please sign in to comment.