This commit is contained in:
2025-10-27 17:39:18 -04:00
commit 31f723bea4
1579 changed files with 642409 additions and 0 deletions

View File

@@ -0,0 +1,135 @@
using System.Security.Claims;
using Prefab.Catalog.Domain.Entities;
using Prefab.Catalog.Domain.Services;
using Prefab.Tests.Infrastructure;
using Prefab.Web.Client.Models.Shared;
using Prefab.Web.Client.Pages;
using Prefab.Web.Client.Services;
namespace Prefab.Tests.Integration.Web.Gateway;
//[Collection("Prefab.Debug")]
//[Collection("Prefab.Ephemeral")]
[Trait(TraitName.Category, TraitCategory.Integration)]
//public sealed class CategoriesOverHttpShould(PrefabCompositeFixture_Debug fixture)
//public sealed class CategoriesOverHttpShould(PrefabCompositeFixture_Ephemeral fixture)
public sealed class CategoriesOverHttpShould(PrefabCompositeFixture_Ephemeral fixture)
: IClassFixture<PrefabCompositeFixture_Ephemeral>
{
[Theory]
[InlineData("Accessories")]
[InlineData("Shoes")]
public async Task ReturnSeededCategoryRecords(string prefix)
{
var cancellationToken = TestContext.Current.CancellationToken;
var suffix = Guid.NewGuid().ToString("N")[..8];
var categoryName = $"{prefix}-{suffix}";
var categoryDescription = $"{prefix} description {suffix}";
await fixture.SeedAsync(async (db, token) =>
{
var checker = new CategoriesTestHelpers.TestUniqueChecker();
var category = await Category.Create(categoryName, categoryDescription, checker, token);
db.Categories.Add(category);
await db.SaveChangesAsync(token);
}, cancellationToken);
var client = fixture.CreateHttpClientForWeb();
var pageService = new CategoriesPageService(client);
var result = await pageService.GetPage(cancellationToken);
var model = CategoriesTestHelpers.EnsureSuccess(result);
Assert.Contains(model.Categories, c => c.Name == categoryName);
}
}
//[Collection("Prefab.InProcess")]
[Trait(TraitName.Category, TraitCategory.Integration)]
public sealed class CategoriesInProcessShould(PrefabCompositeFixture_Ephemeral fixture)
: IClassFixture<PrefabCompositeFixture_Ephemeral>
{
[Fact]
public async Task ResolveServerImplementationWithoutHttp()
{
var cancellationToken = TestContext.Current.CancellationToken;
fixture.AuthenticationStateProvider.SetAnonymous();
using var scope = fixture.CreateWebScope();
var service = scope.ServiceProvider.GetRequiredService<ICategoriesPageService>();
var result = await service.GetPage(cancellationToken);
var model = CategoriesTestHelpers.EnsureSuccess(result);
Assert.NotNull(model.Categories);
}
}
internal static class CategoriesTestHelpers
{
public static CategoriesPageModel EnsureSuccess(Result<CategoriesPageModel> result)
{
if (!result.IsSuccess || result.Value is null)
{
var detail = result.Problem?.Detail ?? "Categories page request failed.";
throw new InvalidOperationException(detail);
}
return result.Value;
}
public sealed class TestUniqueChecker : IUniqueChecker
{
public Task<bool> CategoryNameIsUnique(string name, CancellationToken cancellationToken = default)
=> Task.FromResult(true);
public Task<bool> ProductModelNameIsUnique(string name, CancellationToken cancellationToken = default)
=> Task.FromResult(true);
public Task<bool> ProductSlugIsUnique(string? slug, CancellationToken cancellationToken = default)
=> Task.FromResult(true);
public Task<bool> ProductSkuIsUnique(string sku, CancellationToken cancellationToken = default)
=> Task.FromResult(true);
public Task<bool> OptionCodeIsUniqueForProduct(Guid productId, string code, CancellationToken cancellationToken = default)
=> Task.FromResult(true);
}
}
[Collection("Prefab.InProcess")]
[Trait(TraitName.Category, TraitCategory.Integration)]
public sealed class CategoriesAuthExample(PrefabCompositeFixture_InProcess fixture)
{
[Fact(Skip = "Documentation example for simulated authenticated user. Enable when wiring real auth scenarios.")]
public async Task Example_UsingAuthenticatedPrincipal()
{
var cancellationToken = TestContext.Current.CancellationToken;
var principal = new ClaimsPrincipal(new ClaimsIdentity(
[
new Claim(ClaimTypes.NameIdentifier, "user-123"),
new Claim(ClaimTypes.Name, "Integration Tester"),
new Claim(ClaimTypes.Role, "Catalog.Editor")
], "TestHarness"));
fixture.AuthenticationStateProvider.SetPrincipal(principal);
try
{
using var scope = fixture.CreateWebScope();
var service = scope.ServiceProvider.GetRequiredService<ICategoriesPageService>();
var result = await service.GetPage(cancellationToken);
var model = CategoriesTestHelpers.EnsureSuccess(result);
Assert.NotNull(model.Categories);
}
finally
{
fixture.AuthenticationStateProvider.SetAnonymous();
}
}
}

View File

@@ -0,0 +1,25 @@
using System.Net.Http.Json;
using Prefab.Tests.Infrastructure;
using Prefab.Web.Client.Models.Shared;
using Shouldly;
namespace Prefab.Tests.Integration.Web.Nav;
[Trait(TraitName.Category, TraitCategory.Integration)]
public sealed class NavMenuShould(PrefabCompositeFixture_Ephemeral fixture)
: IClassFixture<PrefabCompositeFixture_Ephemeral>
{
[Fact]
public async Task ReturnNavigationColumnsFromBff()
{
var cancellationToken = TestContext.Current.CancellationToken;
var client = fixture.CreateHttpClientForWeb();
var result = await client.GetFromJsonAsync<Result<NavMenuModel>>("/bff/nav-menu/2", cancellationToken);
result.ShouldNotBeNull();
result!.IsSuccess.ShouldBeTrue(result.Problem?.Detail);
result.Value.ShouldNotBeNull();
result.Value!.Columns.ShouldNotBeNull();
}
}