From 5ddb79f9b91b6dfef430887a09059942d97385ef Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Sun, 11 Aug 2024 14:06:11 +0100 Subject: [PATCH] added client tests --- ...countClientTests.CreateAnonymousSession.cs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateAnonymousSession.cs diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateAnonymousSession.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateAnonymousSession.cs new file mode 100644 index 00000000..eed155f5 --- /dev/null +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateAnonymousSession.cs @@ -0,0 +1,55 @@ +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 CreateAnonymousSession_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + _mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/anonymous") + .ExpectedHeaders(false) + .Respond(Constants.AppJson, Constants.SessionResponse); + + // Act + var result = await _appwriteClient.Account.CreateAnonymousSession(); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task CreateAnonymousSession_ShouldHandleException_WhenApiCallFails() + { + // Arrange + _mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/anonymous") + .ExpectedHeaders(false) + .Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); + + // Act + var result = await _appwriteClient.Account.CreateAnonymousSession(); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task CreateAnonymousSession_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + _mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/anonymous") + .ExpectedHeaders(false) + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Account.CreateAnonymousSession(); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +}