feat: wip on testing login flows

This commit is contained in:
Stevan Freeborn
2026-02-03 04:38:40 -06:00
parent 28e4692ca1
commit c037b895f0
12 changed files with 284 additions and 47 deletions
+16
View File
@@ -1,3 +1,9 @@
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>
@@ -7,5 +13,15 @@ public class TestApi : WebApplicationFactory<Program>
base.ConfigureWebHost(builder);
builder.ConfigureLogging(static c => c.ClearProviders());
builder.ConfigureTestServices(static c =>
{
var opts = Options.Create(new AppDbContextOptions()
{
DatabaseFilePath = $"{Guid.NewGuid()}.db",
});
c.AddSingleton(opts);
});
}
}
@@ -0,0 +1,39 @@
using FiscalOS.API.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace FiscalOS.API.Tests.Integration;
public abstract class IntegrationTest(TestApi testApi) : IClassFixture<TestApi>, IAsyncLifetime
{
protected TestApi TestApi { get; } = testApi;
// TODO: This seems not correct
// would need to dispose of scope
// we need to clean up the test
// database files when the test ends
protected DbContext TestDbContext
{
get
{
var scopeFactory = TestApi.Services.GetRequiredService<IServiceScopeFactory>();
var scope = scopeFactory.CreateScope();
return scope.ServiceProvider.GetRequiredService<AppDbContext>();
}
}
public ValueTask InitializeAsync()
{
return new(TestDbContext.Database.EnsureCreatedAsync());
}
public ValueTask DisposeAsync()
{
GC.SuppressFinalize(this);
return new(TestDbContext.Database.EnsureDeletedAsync());
}
}
@@ -1,14 +1,12 @@
namespace FiscalOS.API.Tests.Integration;
public class LoginTests(TestApi testApi) : IClassFixture<TestApi>
public class LoginTests(TestApi testApi) : IntegrationTest(testApi)
{
private readonly TestApi _testApi = testApi;
[Theory]
[ClassData<LoginValidationTestCases>]
public async Task Login_WhenUserSubmitsInvalidRequest_ItShouldReturn400WithProblemDetails(LoginValidationTestCase tc)
{
var client = _testApi.CreateClient();
var client = TestApi.CreateClient();
var req = new
{
@@ -28,7 +26,7 @@ public class LoginTests(TestApi testApi) : IClassFixture<TestApi>
[Fact]
public async Task Login_WhenUserCredentialsAreIncorrect_ItShouldReturn401WithProblemDetails()
{
var client = _testApi.CreateClient();
var client = TestApi.CreateClient();
var req = new
{