Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pingu2k4 committed Sep 3, 2024
1 parent c1c6f10 commit 7fcd217
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Net;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Servers.Account;
public partial class AccountServerTests
{
[Fact]
public async Task CreateAnonymousSession_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/anonymous")
.ExpectedHeaders()
.Respond(Constants.AppJson, Constants.SessionResponse);

// Act
var result = await _appwriteServer.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()
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

// Act
var result = await _appwriteServer.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()
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteServer.Account.CreateAnonymousSession();

// 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
Expand Up @@ -25,18 +25,3 @@ public AccountServerTests()
_appwriteServer = serviceProvider.GetRequiredService<IAppwriteServer>();
}
}

public static class AccountTestsExtensions
{
public static MockedRequest ExpectedHeaders(this MockedRequest request)
{
return request
.WithHeaders("x-appwrite-project", Constants.ProjectId)
.WithHeaders("x-appwrite-key", Constants.ApiKey)
.WithHeaders("x-sdk-name", Constants.SdkName)
.WithHeaders("x-sdk-platform", "server")
.WithHeaders("x-sdk-language", Constants.SdkLanguage)
.WithHeaders("x-sdk-version", Constants.SdkVersion)
.WithHeaders("x-appwrite-response-format", Constants.AppwriteResponseFormat);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Servers.Account;

public static class AccountTestsExtensions
{
public static MockedRequest ExpectedHeaders(this MockedRequest request)
{
return request
.WithHeaders("x-appwrite-project", Constants.ProjectId)
.WithHeaders("x-appwrite-key", Constants.ApiKey)
.WithHeaders("x-sdk-name", Constants.SdkName)
.WithHeaders("x-sdk-platform", "server")
.WithHeaders("x-sdk-language", Constants.SdkLanguage)
.WithHeaders("x-sdk-version", Constants.SdkVersion)
.WithHeaders("x-appwrite-response-format", Constants.AppwriteResponseFormat);
}
}

0 comments on commit 7fcd217

Please sign in to comment.