shoc-backend/Api.SeaHavenIndustries.Tests/SettingsControllerTests.cs
Alexandre Brandizzi 7d245eb717
refactor: enforce backend boundaries and optimize dispatch (#30)
* 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
2026-07-24 17:35:34 -03:00

120 lines
5.1 KiB
C#

using Api.SeaHavenIndustries.Controllers;
using Api.SeaHavenIndustries.DTOs;
using Api.SeaHavenIndustries.Helper.ViewModel;
using Data.SeaHavenIndustries;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using SeaHaven.Services.DTOs;
using SeaHaven.Services.Interfaces;
using Xunit;
namespace Api.SeaHavenIndustries.Tests;
public class SettingsControllerTests
{
private static SettingsController NewController(Mock<ISettingsService> service) =>
new(service.Object, Mock.Of<ILogger<SettingsController>>());
[Fact]
public async Task GetCategoryList_ReturnsPaginationEnvelopeWithServiceItems()
{
var service = new Mock<ISettingsService>();
service.Setup(s => s.GetCategoryListAsync(1, 10, It.IsAny<CancellationToken>()))
.ReturnsAsync((new List<SettingsCategoryDTO> { new() { Id = 1, Name = "A" } }, 1));
var result = await NewController(service).GetCategoryList(1, 10, CancellationToken.None);
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
var envelope = ok.Value.Should().BeOfType<Pagination_DTO>().Subject;
envelope.TotalCount.Should().Be(1);
envelope.TotalPages.Should().Be(1);
}
[Fact]
public async Task GetTemplates_ReturnsTypedServiceDtosInEnvelope()
{
var service = new Mock<ISettingsService>();
service.Setup(s => s.GetTemplatesAsync(1, 10, It.IsAny<CancellationToken>()))
.ReturnsAsync((new List<SettingsTemplateDTO> { new() { Id = 1, WorkerOrderTitle = "T" } }, 1));
var result = await NewController(service).GetTemplates(1, 10, CancellationToken.None);
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
var envelope = ok.Value.Should().BeOfType<Pagination_DTO>().Subject;
envelope.TotalCount.Should().Be(1);
}
[Fact]
public async Task AddCategory_WhenServiceSucceeds_ReturnsSaved()
{
var service = new Mock<ISettingsService>();
var result = await NewController(service).AddCategory(new Category_DTO { Name = "X" }, CancellationToken.None);
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
var response = ok.Value.Should().BeOfType<DataResponse>().Subject;
response.Status.Should().Be("200");
response.Message.Should().Be("Successfully Saved Data");
service.Verify(s => s.AddCategoryAsync("X", It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task EditCategory_WhenMissing_ReturnsBadRequest()
{
var service = new Mock<ISettingsService>();
service.Setup(s => s.UpdateCategoryAsync(9, "X", It.IsAny<CancellationToken>()))
.ReturnsAsync(false);
var result = await NewController(service).EditCategory(new EditCategory_DTO { Id = 9, Name = "X" }, CancellationToken.None);
var bad = result.Should().BeOfType<BadRequestObjectResult>().Subject;
bad.Value.Should().BeOfType<Response>().Subject.Message.Should().Be("Category not found");
}
[Fact]
public async Task DeleteCategory_WhenMissing_ReturnsIdNotMatched()
{
var service = new Mock<ISettingsService>();
service.Setup(s => s.DeleteCategoryAsync(9, It.IsAny<CancellationToken>()))
.ReturnsAsync(false);
var result = await NewController(service).DeleteCategory(9, CancellationToken.None);
var bad = result.Should().BeOfType<BadRequestObjectResult>().Subject;
bad.Value.Should().BeOfType<Response>().Subject.Message.Should().Be("ID not matched!");
}
[Fact]
public async Task GetUsers_MapsServiceUsersToUserRoleViewModel()
{
var service = new Mock<ISettingsService>();
service.Setup(s => s.GetUsersAsync("", 1, 10, It.IsAny<CancellationToken>()))
.ReturnsAsync((new List<SettingsUserDTO>
{
new() { Id = "u1", UserName = "Sara", Email = "s@x.com", RoleName = "User", Status = "Active", Date = "2026-01-01" }
}, 1));
var result = await NewController(service).GetUsers("", 1, 10, CancellationToken.None);
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
var envelope = ok.Value.Should().BeOfType<Pagination_DTO>().Subject;
object data = envelope.Data!;
var rows = ((System.Collections.IEnumerable)data).Cast<UserRoleViewModel>().ToList();
rows.Should().ContainSingle(u => u.UserName == "Sara" && u.RoleName == "User" && u.Email == "s@x.com");
}
[Fact]
public async Task GetRoles_MapsServiceRolesToRolesDto()
{
var service = new Mock<ISettingsService>();
service.Setup(s => s.GetRolesAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<RoleDTO> { new() { Name = "Admin", Description = "Full Access" } });
var result = await NewController(service).GetRoles(CancellationToken.None);
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
var roles = ((System.Collections.IEnumerable)ok.Value!).Cast<Roles_DTO>().ToList();
roles.Should().ContainSingle(r => r.Name == "Admin" && r.Description == "Full Access");
}
}