tests: refactor test suite to match new project structure

This commit is contained in:
Stevan Freeborn
2026-02-03 04:38:40 -06:00
parent c2b681d267
commit 38a03f56ca
9 changed files with 50 additions and 12 deletions
@@ -17,6 +17,8 @@
<ItemGroup>
<ProjectReference Include="..\..\src\FiscalOS.API\FiscalOS.API.csproj" />
<ProjectReference Include="..\..\src\FiscalOS.Core\FiscalOS.Core.csproj" />
<ProjectReference Include="..\..\src\FiscalOS.Infra\FiscalOS.Infra.csproj" />
</ItemGroup>
</Project>
@@ -1,9 +1,3 @@
using FiscalOS.API.Data;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace FiscalOS.API.Tests.Infra;
public class TestApi : WebApplicationFactory<Program>
@@ -19,6 +19,13 @@ public abstract class IntegrationTest(TestApi testApi) : IClassFixture<TestApi>,
await action(context);
}
protected async Task ExecuteDbContextAsync(Func<DbContext, IServiceProvider, Task> action)
{
await using var scope = testApi.Services.CreateAsyncScope();
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await action(context, scope.ServiceProvider);
}
protected async Task<T> ExecuteDbContextAsync<T>(Func<DbContext, Task<T>> action)
{
await using var scope = testApi.Services.CreateAsyncScope();
@@ -26,6 +33,13 @@ public abstract class IntegrationTest(TestApi testApi) : IClassFixture<TestApi>,
return await action(context);
}
protected async Task<T> ExecuteDbContextAsync<T>(Func<DbContext, IServiceProvider, Task<T>> action)
{
await using var scope = testApi.Services.CreateAsyncScope();
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
return await action(context, scope.ServiceProvider);
}
public async ValueTask DisposeAsync()
{
await ExecuteDbContextAsync(static async context =>
@@ -1,4 +1,4 @@
using FiscalOS.API.Identity;
using FiscalOS.Core.Identity;
namespace FiscalOS.API.Tests.Integration;
@@ -40,11 +40,14 @@ public class LoginTests(TestApi testApi) : IntegrationTest(testApi)
[Fact]
public async Task Login_WhenUserExistsButPasswordIsIncorrect_ItShouldReturn401WithProblemDetails()
{
await ExecuteDbContextAsync(static async context =>
await ExecuteDbContextAsync(static async (context, sp) =>
{
var passwordHasher = sp.GetRequiredService<IPasswordHasher>();
context.Add(new User
{
Username = "Stevan",
HashedPassword = passwordHasher.Hash("@Password1"),
});
await context.SaveChangesAsync(TestContext.Current.CancellationToken);
@@ -1,42 +0,0 @@
using FiscalOS.API.Identity;
namespace FiscalOS.API.Tests.Unit;
public class PasswordHasherTests
{
private readonly PasswordHasher _sut = PasswordHasher.New();
[Fact]
public void Hash_WhenGivenPassword_ShouldReturnHashedPassword()
{
var password = "SecurePassword123!";
var result = _sut.Hash(password);
result.Should().NotBeNullOrEmpty();
result.Should().NotBe(password);
}
[Fact]
public void Verify_WhenGivenCorrectPassword_ShouldReturnTrue()
{
var password = "SecurePassword123!";
var hashedPassword = _sut.Hash(password);
var result = _sut.Verify(password, hashedPassword);
result.Should().BeTrue();
}
[Fact]
public void Verify_WhenGivenIncorrectPassword_ShouldReturnFalse()
{
var password = "SecurePassword123!";
var wrongPassword = "WrongPassword!";
var hashedPassword = _sut.Hash(password);
var result = _sut.Verify(wrongPassword, hashedPassword);
result.Should().BeFalse();
}
}
+3 -1
View File
@@ -1,14 +1,16 @@
global using System.Net;
global using System.Net.Http.Json;
global using FiscalOS.API.Data;
global using FiscalOS.API.Tests.Infra;
global using FiscalOS.Infra.Data;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.Mvc.Testing;
global using Microsoft.AspNetCore.TestHost;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Options;
global using Xunit.Sdk;