test: add AuthApiClientTests with route and success tests

Add unit tests for AuthApiClient covering:
- Route verification tests for GetPublicKey, Login, Logout, GetCurrentUser
- Success tests for all 4 methods
- Proper handling of 204 NoContent for Logout (returns Unit)
This commit is contained in:
Joseph Doherty
2026-01-06 11:31:49 -05:00
parent d06d5f27e2
commit 55bfdb3532
@@ -0,0 +1,149 @@
using System.Net;
using System.Text.Json;
using JdeScoping.Client.Services;
using JdeScoping.Core.ApiContracts;
using JdeScoping.Core.ApiContracts.Results;
using JdeScoping.Core.Models;
using JdeScoping.Core.Models.Auth;
using RichardSzalay.MockHttp;
using Shouldly;
namespace JdeScoping.Client.Tests.Services;
public class AuthApiClientTests
{
private readonly MockHttpMessageHandler _mockHttp;
private readonly AuthApiClient _client;
public AuthApiClientTests()
{
_mockHttp = new MockHttpMessageHandler();
var httpClient = new HttpClient(_mockHttp) { BaseAddress = new Uri("http://localhost/") };
_client = new AuthApiClient(httpClient);
}
// Route verification tests
[Fact]
public async Task GetPublicKeyAsync_CallsCorrectRoute()
{
// Arrange
var request = _mockHttp.Expect(HttpMethod.Get, $"http://localhost/{ApiRoutes.Auth.PublicKey}")
.Respond("application/json", JsonSerializer.Serialize(new PublicKeyResponse("-----BEGIN PUBLIC KEY-----\ntest\n-----END PUBLIC KEY-----")));
// Act
await _client.GetPublicKeyAsync();
// Assert
_mockHttp.GetMatchCount(request).ShouldBe(1);
}
[Fact]
public async Task LoginAsync_CallsCorrectRoute_WithPostMethod()
{
// Arrange
var request = _mockHttp.Expect(HttpMethod.Post, $"http://localhost/{ApiRoutes.Auth.Login}")
.Respond("application/json", JsonSerializer.Serialize(new LoginResultModel(true, null, null)));
// Act
await _client.LoginAsync(new EncryptedLoginRequest("encrypted"));
// Assert
_mockHttp.GetMatchCount(request).ShouldBe(1);
}
[Fact]
public async Task LogoutAsync_CallsCorrectRoute_WithPostMethod()
{
// Arrange
var request = _mockHttp.Expect(HttpMethod.Post, $"http://localhost/{ApiRoutes.Auth.Logout}")
.Respond(HttpStatusCode.NoContent);
// Act
await _client.LogoutAsync();
// Assert
_mockHttp.GetMatchCount(request).ShouldBe(1);
}
[Fact]
public async Task GetCurrentUserAsync_CallsCorrectRoute()
{
// Arrange
var user = new UserInfo { Username = "testuser", FirstName = "Test", LastName = "User" };
var request = _mockHttp.Expect(HttpMethod.Get, $"http://localhost/{ApiRoutes.Auth.Me}")
.Respond("application/json", JsonSerializer.Serialize(user));
// Act
await _client.GetCurrentUserAsync();
// Assert
_mockHttp.GetMatchCount(request).ShouldBe(1);
}
// Success tests
[Fact]
public async Task GetPublicKeyAsync_Success_ReturnsPublicKey()
{
// Arrange
var publicKey = "-----BEGIN PUBLIC KEY-----\nMIIBIjAN...\n-----END PUBLIC KEY-----";
_mockHttp.When(HttpMethod.Get, "*")
.Respond("application/json", JsonSerializer.Serialize(new PublicKeyResponse(publicKey)));
// Act
var result = await _client.GetPublicKeyAsync();
// Assert
result.IsSuccess.ShouldBeTrue();
result.Value.PublicKeyPem.ShouldBe(publicKey);
}
[Fact]
public async Task LoginAsync_Success_ReturnsLoginResult()
{
// Arrange
var user = new UserInfo { Username = "testuser", FirstName = "Test", LastName = "User" };
var loginResult = new LoginResultModel(true, null, user);
_mockHttp.When(HttpMethod.Post, "*")
.Respond("application/json", JsonSerializer.Serialize(loginResult));
// Act
var result = await _client.LoginAsync(new EncryptedLoginRequest("encrypted"));
// Assert
result.IsSuccess.ShouldBeTrue();
result.Value.Success.ShouldBeTrue();
result.Value.User?.Username.ShouldBe("testuser");
}
[Fact]
public async Task LogoutAsync_Success_ReturnsUnit()
{
// Arrange
_mockHttp.When(HttpMethod.Post, "*")
.Respond(HttpStatusCode.NoContent);
// Act
var result = await _client.LogoutAsync();
// Assert
result.IsSuccess.ShouldBeTrue();
}
[Fact]
public async Task GetCurrentUserAsync_Success_ReturnsUserInfo()
{
// Arrange
var userInfo = new UserInfo { Username = "testuser", FirstName = "Test", LastName = "User" };
_mockHttp.When(HttpMethod.Get, "*")
.Respond("application/json", JsonSerializer.Serialize(userInfo));
// Act
var result = await _client.GetCurrentUserAsync();
// Assert
result.IsSuccess.ShouldBeTrue();
result.Value.Username.ShouldBe("testuser");
}
}