mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 09:33:13 +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
135 lines
5.1 KiB
C#
135 lines
5.1 KiB
C#
using System.Security.Claims;
|
|
using Api.SeaHavenIndustries.Controllers;
|
|
using Api.SeaHavenIndustries.DTOs;
|
|
using Data.SeaHavenIndustries;
|
|
using FluentAssertions;
|
|
using Microsoft.AspNetCore.Http;
|
|
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 FollowUpControllerTests
|
|
{
|
|
private static FollowUpController NewController(Mock<IFollowUpService> service)
|
|
{
|
|
var controller = new FollowUpController(service.Object, Mock.Of<ILogger<FollowUpController>>())
|
|
{
|
|
ControllerContext = new ControllerContext
|
|
{
|
|
HttpContext = new DefaultHttpContext
|
|
{
|
|
User = new ClaimsPrincipal(new ClaimsIdentity(new[]
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, "user-1")
|
|
}, "Test"))
|
|
}
|
|
}
|
|
};
|
|
return controller;
|
|
}
|
|
|
|
private static FollowUpController NewControllerNoUser(Mock<IFollowUpService> service) =>
|
|
new(service.Object, Mock.Of<ILogger<FollowUpController>>())
|
|
{
|
|
ControllerContext = new ControllerContext
|
|
{
|
|
HttpContext = new DefaultHttpContext
|
|
{
|
|
User = new ClaimsPrincipal(new ClaimsIdentity())
|
|
}
|
|
}
|
|
};
|
|
|
|
private static object Prop(object source, string name) =>
|
|
source.GetType().GetProperty(name)!.GetValue(source)!;
|
|
|
|
[Fact]
|
|
public async Task GetFollowUps_ReturnsPaginationEnvelopeWithMappedRows()
|
|
{
|
|
var service = new Mock<IFollowUpService>();
|
|
service.Setup(s => s.GetFollowUpListAsync(1, 12, "", "Open", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new PagedResult<FollowUpListRowDTO>
|
|
{
|
|
Items = new List<FollowUpListRowDTO> { new() { Id = 1, Reason = "R", CreatedByName = "Me" } },
|
|
TotalCount = 1,
|
|
Page = 1,
|
|
PageSize = 12
|
|
});
|
|
|
|
var result = await NewController(service).GetFollowUps();
|
|
|
|
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
|
|
var envelope = ok.Value.Should().BeOfType<Pagination_DTO>().Subject;
|
|
envelope.PageSize.Should().Be(12);
|
|
envelope.TotalCount.Should().Be(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetById_WhenMissing_ReturnsNotFound()
|
|
{
|
|
var service = new Mock<IFollowUpService>();
|
|
service.Setup(s => s.GetFollowUpDetailAsync(9, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync((FollowUpDetailDTO?)null);
|
|
|
|
var result = await NewController(service).GetById(9, CancellationToken.None);
|
|
|
|
result.Should().BeOfType<NotFoundResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_WhenNoUser_ReturnsUnauthorized()
|
|
{
|
|
var service = new Mock<IFollowUpService>();
|
|
var result = await NewControllerNoUser(service).Create(new FollowUp_DTO { Reason = "x" }, CancellationToken.None);
|
|
|
|
var unauthorized = result.Should().BeOfType<UnauthorizedObjectResult>().Subject;
|
|
unauthorized.Value.Should().BeOfType<Response>().Subject.Message.Should().Be("User not authenticated");
|
|
service.Verify(s => s.CreateOpenFollowUpAsync(It.IsAny<FollowUpCreateRequestDTO>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_WhenAuthenticated_ReturnsCreatedWithId()
|
|
{
|
|
var service = new Mock<IFollowUpService>();
|
|
service.Setup(s => s.CreateOpenFollowUpAsync(It.IsAny<FollowUpCreateRequestDTO>(), "user-1", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(42);
|
|
|
|
var result = await NewController(service).Create(new FollowUp_DTO { Reason = "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("Follow Up created");
|
|
((object?)response.Data).Should().Be(42);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Complete_WhenMissing_ReturnsNotFound()
|
|
{
|
|
var service = new Mock<IFollowUpService>();
|
|
service.Setup(s => s.SetFollowUpStatusAsync(3, "Completed", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(false);
|
|
|
|
var result = await NewController(service).Complete(3, CancellationToken.None);
|
|
|
|
result.Should().BeOfType<NotFoundResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Delete_WhenMissing_ReturnsNotFoundEnvelope()
|
|
{
|
|
var service = new Mock<IFollowUpService>();
|
|
service.Setup(s => s.RemoveFollowUpAsync(8, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(false);
|
|
|
|
var result = await NewController(service).Delete(8, CancellationToken.None);
|
|
|
|
var notFound = result.Should().BeOfType<NotFoundObjectResult>().Subject;
|
|
notFound.Value.Should().BeOfType<Response>().Subject.Message.Should().Be("Follow Up not found");
|
|
}
|
|
}
|