mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 06:03:12 +00:00
* refactor(api): enforce service and data-service boundaries * refactor(api): complete feature service boundaries * refactor(identity): enforce service and data boundaries * refactor(vendors): enforce service and data boundaries * refactor(workorders): enforce service and data boundaries * refactor(backend): enforce architecture and optimize dispatch * style(backend): format changed architecture files * fix(architecture): address backend review follow-ups * fix(backend): sanitize exception disclosure in changed API endpoints Replace raw exception-message disclosure (ex.Message) returned to API callers with a stable sanitized public message plus correlated structured internal logging, across the endpoints changed in this PR. - Add SanitizedErrors helper: logs the original exception at Error with a generated correlation id and returns a stable public message referencing it so support can trace without exposing internals. - Inject ILogger<T> into the 14 changed controllers and route every ex.Message/dbex.Message disclosure through the helper, preserving status codes, response shapes, and business data (e.g. OpenWorkOrders). - Leave FluentValidation (vex.Errors) and existing fixed-message catches untouched; out-of-scope controllers (Account/Contact/Employee/Asset/ PMSchedule) are unchanged. - Add focused tests proving internal exception text is not returned and that Error logging carrying the original exception is invoked. * fix(architecture): abstract job run state access * style: format board update service * test: use collection assertion idiom
168 lines
6.5 KiB
C#
168 lines
6.5 KiB
C#
using Data.SeaHavenIndustries;
|
|
using FluentAssertions;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using SeaHaven.DataServices.Implementation;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Implementation;
|
|
using Xunit;
|
|
|
|
namespace Api.SeaHavenIndustries.Tests;
|
|
|
|
public class SettingsServiceTests
|
|
{
|
|
private static ApplicationDbContext NewContext()
|
|
{
|
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
|
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
|
|
.Options;
|
|
return new ApplicationDbContext(options);
|
|
}
|
|
|
|
private static SettingsService NewService(ApplicationDbContext ctx) =>
|
|
new(new SettingsDataService(ctx), null!);
|
|
|
|
private static (ApplicationDbContext ctx, UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager) NewIdentityContext()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddLogging();
|
|
services.AddDbContext<ApplicationDbContext>(o => o.UseInMemoryDatabase(Guid.NewGuid().ToString()));
|
|
services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();
|
|
var provider = services.BuildServiceProvider();
|
|
var scope = provider.CreateScope();
|
|
var ctx = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
|
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
|
return (ctx, userManager, roleManager);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddCategoryAsync_PersistsName()
|
|
{
|
|
using var ctx = NewContext();
|
|
await NewService(ctx).AddCategoryAsync("Plumbing", CancellationToken.None);
|
|
ctx.Categories.Single().Name.Should().Be("Plumbing");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateCategoryAsync_UpdatesNameAndReturnsFalseWhenMissing()
|
|
{
|
|
using var ctx = NewContext();
|
|
ctx.Categories.Add(new Category { Name = "Old" });
|
|
ctx.SaveChanges();
|
|
|
|
var service = NewService(ctx);
|
|
(await service.UpdateCategoryAsync(1, "New", CancellationToken.None)).Should().BeTrue();
|
|
ctx.Categories.Single().Name.Should().Be("New");
|
|
(await service.UpdateCategoryAsync(999, "X", CancellationToken.None)).Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteCategoryAsync_CascadesTemplatesAndWorkOrderCategories()
|
|
{
|
|
using var ctx = NewContext();
|
|
ctx.Categories.Add(new Category { Id = 1, Name = "C1" });
|
|
ctx.Templates.Add(new Template { CategoryId = 1, WorkerOrderTitle = "T" });
|
|
ctx.workOrderCategories.Add(new WorkOrderCategories { CategoryId = 1 });
|
|
ctx.SaveChanges();
|
|
|
|
var service = NewService(ctx);
|
|
(await service.DeleteCategoryAsync(1, CancellationToken.None)).Should().BeTrue();
|
|
(await service.DeleteCategoryAsync(1, CancellationToken.None)).Should().BeFalse();
|
|
|
|
ctx.Categories.Should().BeEmpty();
|
|
ctx.Templates.Should().BeEmpty();
|
|
ctx.workOrderCategories.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetCategoryListAsync_MapsToServiceDtoAndPages()
|
|
{
|
|
using var ctx = NewContext();
|
|
ctx.Categories.Add(new Category { Name = "A" });
|
|
ctx.Categories.Add(new Category { Name = "B" });
|
|
ctx.SaveChanges();
|
|
|
|
var (items, total) = await NewService(ctx).GetCategoryListAsync(1, 10, CancellationToken.None);
|
|
|
|
total.Should().Be(2);
|
|
items.Should().OnlyContain(c => c is SettingsCategoryDTO);
|
|
items.Select(c => c.Name).Should().Contain(new[] { "A", "B" });
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetTemplatesAsync_MapsWorkOrderScalarsToTypedDto()
|
|
{
|
|
using var ctx = NewContext();
|
|
var wo = new WorkOrder
|
|
{
|
|
istemplate = true,
|
|
InternalWONumber = "WO-100",
|
|
WorkerOrderTitle = "Template A",
|
|
Status = "Active",
|
|
Priority = "High",
|
|
Trade = "HVAC",
|
|
VendorNTE = 1500m,
|
|
Customer = "Cust"
|
|
};
|
|
ctx.workOrders.Add(wo);
|
|
ctx.SaveChanges();
|
|
|
|
var (items, total) = await NewService(ctx).GetTemplatesAsync(1, 10, CancellationToken.None);
|
|
|
|
total.Should().Be(1);
|
|
var dto = items.Single();
|
|
dto.Should().BeOfType<SettingsTemplateDTO>();
|
|
dto.InternalWONumber.Should().Be("WO-100");
|
|
dto.WorkerOrderTitle.Should().Be("Template A");
|
|
dto.Status.Should().Be("Active");
|
|
dto.Priority.Should().Be("High");
|
|
dto.Trade.Should().Be("HVAC");
|
|
dto.VendorNTE.Should().Be(1500m);
|
|
dto.Customer.Should().Be("Cust");
|
|
dto.istemplate.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetUsersAsync_ExcludesAdminsAndResolvesRole()
|
|
{
|
|
var (ctx, userManager, roleManager) = NewIdentityContext();
|
|
var t1 = roleManager.CreateAsync(new IdentityRole("Admin"));
|
|
var t2 = roleManager.CreateAsync(new IdentityRole("User"));
|
|
await Task.WhenAll(t1, t2);
|
|
|
|
var admin = new ApplicationUser { UserName = "admin1", Email = "a@x.com", FirstName = "Boss" };
|
|
var worker = new ApplicationUser { UserName = "worker1", Email = "w@x.com", FirstName = "Hand" };
|
|
await userManager.CreateAsync(admin);
|
|
await userManager.CreateAsync(worker);
|
|
await userManager.AddToRoleAsync(admin, "Admin");
|
|
await userManager.AddToRoleAsync(worker, "User");
|
|
|
|
ctx.SaveChanges();
|
|
|
|
var service = new SettingsService(new SettingsDataService(ctx), userManager);
|
|
var (users, total) = await service.GetUsersAsync("", 1, 10, CancellationToken.None);
|
|
|
|
total.Should().BeGreaterThanOrEqualTo(2);
|
|
users.Should().NotContain(u => u.RoleName == "Admin");
|
|
users.Should().Contain(u => u.UserName == "Hand" && u.RoleName == "User");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetRolesAsync_MapsKnownRoleNamesToDescriptions()
|
|
{
|
|
using var ctx = NewContext();
|
|
ctx.Roles.Add(new IdentityRole("Admin"));
|
|
ctx.Roles.Add(new IdentityRole("Manager"));
|
|
ctx.Roles.Add(new IdentityRole("Unknown"));
|
|
ctx.SaveChanges();
|
|
|
|
var roles = await NewService(ctx).GetRolesAsync(CancellationToken.None);
|
|
|
|
var byName = roles.ToDictionary(r => r.Name!, r => r.Description);
|
|
byName["Admin"].Should().Be("Full Access");
|
|
byName["Manager"].Should().Be("Full access accept quotes");
|
|
roles.Should().NotContain(r => r.Name == "Unknown");
|
|
}
|
|
}
|