diff --git a/tests/FiscalOS.API.Tests/FiscalOS.API.Tests.csproj b/tests/FiscalOS.API.Tests/FiscalOS.API.Tests.csproj index 409f525..949d3d8 100644 --- a/tests/FiscalOS.API.Tests/FiscalOS.API.Tests.csproj +++ b/tests/FiscalOS.API.Tests/FiscalOS.API.Tests.csproj @@ -17,6 +17,8 @@ + + diff --git a/tests/FiscalOS.API.Tests/Infra/TestApi.cs b/tests/FiscalOS.API.Tests/Infra/TestApi.cs index a5d46f3..8ee18b4 100644 --- a/tests/FiscalOS.API.Tests/Infra/TestApi.cs +++ b/tests/FiscalOS.API.Tests/Infra/TestApi.cs @@ -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 diff --git a/tests/FiscalOS.API.Tests/Integration/IntegrationTest.cs b/tests/FiscalOS.API.Tests/Integration/IntegrationTest.cs index 52cb7be..dd89de9 100644 --- a/tests/FiscalOS.API.Tests/Integration/IntegrationTest.cs +++ b/tests/FiscalOS.API.Tests/Integration/IntegrationTest.cs @@ -19,6 +19,13 @@ public abstract class IntegrationTest(TestApi testApi) : IClassFixture, await action(context); } + protected async Task ExecuteDbContextAsync(Func action) + { + await using var scope = testApi.Services.CreateAsyncScope(); + var context = scope.ServiceProvider.GetRequiredService(); + await action(context, scope.ServiceProvider); + } + protected async Task ExecuteDbContextAsync(Func> action) { await using var scope = testApi.Services.CreateAsyncScope(); @@ -26,6 +33,13 @@ public abstract class IntegrationTest(TestApi testApi) : IClassFixture, return await action(context); } + protected async Task ExecuteDbContextAsync(Func> action) + { + await using var scope = testApi.Services.CreateAsyncScope(); + var context = scope.ServiceProvider.GetRequiredService(); + return await action(context, scope.ServiceProvider); + } + public async ValueTask DisposeAsync() { await ExecuteDbContextAsync(static async context => diff --git a/tests/FiscalOS.API.Tests/Integration/LoginTests.cs b/tests/FiscalOS.API.Tests/Integration/LoginTests.cs index 06dd386..e34dd8d 100644 --- a/tests/FiscalOS.API.Tests/Integration/LoginTests.cs +++ b/tests/FiscalOS.API.Tests/Integration/LoginTests.cs @@ -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(); + context.Add(new User { Username = "Stevan", + HashedPassword = passwordHasher.Hash("@Password1"), }); await context.SaveChangesAsync(TestContext.Current.CancellationToken); diff --git a/tests/FiscalOS.API.Tests/Usings.cs b/tests/FiscalOS.API.Tests/Usings.cs index 9f763fe..aa25f2f 100644 --- a/tests/FiscalOS.API.Tests/Usings.cs +++ b/tests/FiscalOS.API.Tests/Usings.cs @@ -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; \ No newline at end of file diff --git a/tests/FiscalOS.Infra.Tests/FiscalOS.Infra.Tests.csproj b/tests/FiscalOS.Infra.Tests/FiscalOS.Infra.Tests.csproj new file mode 100644 index 0000000..1f791bc --- /dev/null +++ b/tests/FiscalOS.Infra.Tests/FiscalOS.Infra.Tests.csproj @@ -0,0 +1,21 @@ + + + + Exe + FiscalOS.Infra.Tests + + + + + + + + + + + + + + + + diff --git a/tests/FiscalOS.API.Tests/Unit/PasswordHasherTests.cs b/tests/FiscalOS.Infra.Tests/Unit/PasswordHasherTests.cs similarity index 93% rename from tests/FiscalOS.API.Tests/Unit/PasswordHasherTests.cs rename to tests/FiscalOS.Infra.Tests/Unit/PasswordHasherTests.cs index 2703bd0..19f9de7 100644 --- a/tests/FiscalOS.API.Tests/Unit/PasswordHasherTests.cs +++ b/tests/FiscalOS.Infra.Tests/Unit/PasswordHasherTests.cs @@ -1,6 +1,4 @@ -using FiscalOS.API.Identity; - -namespace FiscalOS.API.Tests.Unit; +namespace FiscalOS.Infra.Tests.Unit; public class PasswordHasherTests { diff --git a/tests/FiscalOS.Infra.Tests/Usings.cs b/tests/FiscalOS.Infra.Tests/Usings.cs new file mode 100644 index 0000000..9082166 --- /dev/null +++ b/tests/FiscalOS.Infra.Tests/Usings.cs @@ -0,0 +1 @@ +global using FiscalOS.Infra.Identity; \ No newline at end of file diff --git a/tests/FiscalOS.Infra.Tests/xunit.runner.json b/tests/FiscalOS.Infra.Tests/xunit.runner.json new file mode 100644 index 0000000..86c7ea0 --- /dev/null +++ b/tests/FiscalOS.Infra.Tests/xunit.runner.json @@ -0,0 +1,3 @@ +{ + "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json" +}