mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 08:23: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
195 lines
7.8 KiB
C#
195 lines
7.8 KiB
C#
using Data.SeaHavenIndustries;
|
|
using FluentAssertions;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Moq;
|
|
using SeaHaven.DataServices.Dto;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Implementation;
|
|
using SeaHaven.Services.Interfaces;
|
|
using Xunit;
|
|
|
|
namespace Api.SeaHavenIndustries.Tests;
|
|
|
|
public class UserServiceTests
|
|
{
|
|
private static UserService NewService(
|
|
Mock<IUserDataService> userData,
|
|
Mock<IEmailSender> email,
|
|
out Mock<IUserRoleStore<ApplicationUser>> store)
|
|
{
|
|
var (manager, s, _) = IdentityTestHelpers.CreateUserManager();
|
|
store = s;
|
|
return new UserService(manager, userData.Object, email.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetUsers_FormatsCreatedDate_AndHandlesNullDate()
|
|
{
|
|
var userData = new Mock<IUserDataService>();
|
|
userData.Setup(u => u.GetAllForListAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new List<UserListRowData>
|
|
{
|
|
new() { Id = "1", Name = "A", Email = "a@x.com", RoleName = "Admin", Status = "Active", CreatedDate = new DateTime(2026, 1, 5) },
|
|
new() { Id = "2", Name = "B", Email = "b@x.com", RoleName = "Tech", Status = "Active", CreatedDate = null }
|
|
});
|
|
|
|
var service = NewService(userData, new Mock<IEmailSender>(), out _);
|
|
|
|
var users = (await service.GetUsersAsync(CancellationToken.None)).ToList();
|
|
|
|
users.Should().HaveCount(2);
|
|
users[0].Date.Should().Be("Jan 05 2026");
|
|
users[0].RoleName.Should().Be("Admin");
|
|
users[0].Status.Should().Be("Active");
|
|
users[1].Date.Should().Be("");
|
|
users[1].Name.Should().Be("B");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteUser_NotFound_ReturnsFalseWithoutCascade()
|
|
{
|
|
var userData = new Mock<IUserDataService>();
|
|
userData.Setup(u => u.GetForEditAsync("missing", It.IsAny<CancellationToken>())).ReturnsAsync((ApplicationUser?)null);
|
|
|
|
var service = NewService(userData, new Mock<IEmailSender>(), out _);
|
|
|
|
var found = await service.DeleteUserAsync("missing", CancellationToken.None);
|
|
|
|
found.Should().BeFalse();
|
|
userData.Verify(u => u.DeleteUserWithCascadeAsync(It.IsAny<ApplicationUser>(), It.IsAny<CancellationToken>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteUser_Found_PerformsCascade()
|
|
{
|
|
var user = IdentityTestHelpers.User();
|
|
var userData = new Mock<IUserDataService>();
|
|
userData.Setup(u => u.GetForEditAsync("u1", It.IsAny<CancellationToken>())).ReturnsAsync(user);
|
|
|
|
var service = NewService(userData, new Mock<IEmailSender>(), out _);
|
|
|
|
var found = await service.DeleteUserAsync("u1", CancellationToken.None);
|
|
|
|
found.Should().BeTrue();
|
|
userData.Verify(u => u.DeleteUserWithCascadeAsync(user, It.IsAny<CancellationToken>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteCurrentUser_SetsIsDeletedFlag()
|
|
{
|
|
var user = IdentityTestHelpers.User();
|
|
var userData = new Mock<IUserDataService>();
|
|
userData.Setup(u => u.GetForEditAsync("u1", It.IsAny<CancellationToken>())).ReturnsAsync(user);
|
|
|
|
var service = NewService(userData, new Mock<IEmailSender>(), out _);
|
|
|
|
await service.DeleteCurrentUserAsync("u1", CancellationToken.None);
|
|
|
|
user.IsDeleted.Should().BeTrue();
|
|
userData.Verify(u => u.UpdateUserAsync(user, It.IsAny<CancellationToken>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task EditUser_NotFound_ReturnsFalse()
|
|
{
|
|
var userData = new Mock<IUserDataService>();
|
|
userData.Setup(u => u.GetForEditAsync("x", It.IsAny<CancellationToken>())).ReturnsAsync((ApplicationUser?)null);
|
|
|
|
var service = NewService(userData, new Mock<IEmailSender>(), out _);
|
|
|
|
var succeeded = await service.EditUserAsync(new EditUserRequestDTO { Id = "x", Email = "e@x.com", Name = "n", Role = "Admin" }, CancellationToken.None);
|
|
|
|
succeeded.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetUserProfile_MapsCreatedDateToAddedDate()
|
|
{
|
|
var user = new UserProfileData
|
|
{
|
|
Id = "u1",
|
|
FirstName = "Alice",
|
|
CreatedDate = new DateTime(2026, 3, 4)
|
|
};
|
|
var userData = new Mock<IUserDataService>();
|
|
userData.Setup(u => u.GetProfileAsync("u1", It.IsAny<CancellationToken>())).ReturnsAsync(user);
|
|
|
|
var service = NewService(userData, new Mock<IEmailSender>(), out _);
|
|
|
|
var rows = await service.GetUserProfileAsync("u1", CancellationToken.None);
|
|
|
|
rows.Should().HaveCount(1);
|
|
rows[0].AddedDate.Should().Be(new DateTime(2026, 3, 4));
|
|
rows[0].Name.Should().Be("Alice");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddUser_CreateSuccess_PersistsRoleAndEmailsLoginInformation()
|
|
{
|
|
var userData = new Mock<IUserDataService>();
|
|
userData.Setup(u => u.GetByIdAsync(It.IsAny<string>())).ReturnsAsync((ApplicationUser?)null);
|
|
var email = new Mock<IEmailSender>();
|
|
|
|
var service = NewService(userData, email, out var store);
|
|
store.As<IUserPasswordStore<ApplicationUser>>()
|
|
.Setup(s => s.SetPasswordHashAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
|
.Returns(Task.CompletedTask);
|
|
store.Setup(s => s.CreateAsync(It.IsAny<ApplicationUser>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(IdentityResult.Success);
|
|
store.Setup(s => s.AddToRoleAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
|
.Returns(Task.CompletedTask);
|
|
|
|
var outcome = await service.AddUserAsync(new AddUserRequestDTO { Name = "N", Email = "n@x.com", Role = "Admin" }, CancellationToken.None);
|
|
|
|
outcome.Success.Should().BeTrue();
|
|
store.Verify(s => s.CreateAsync(It.IsAny<ApplicationUser>(), It.IsAny<CancellationToken>()), Times.Once);
|
|
store.Verify(s => s.AddToRoleAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once);
|
|
email.Verify(
|
|
e => e.SendEmailAsync(
|
|
"n@x.com",
|
|
"Login Information",
|
|
It.Is<string>(body => ContainsStrongGeneratedPassword(body))),
|
|
Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddUser_CreateFails_ReturnsFirstError()
|
|
{
|
|
var userData = new Mock<IUserDataService>();
|
|
userData.Setup(u => u.GetByIdAsync(It.IsAny<string>())).ReturnsAsync((ApplicationUser?)null);
|
|
|
|
var service = NewService(userData, new Mock<IEmailSender>(), out var store);
|
|
store.As<IUserPasswordStore<ApplicationUser>>()
|
|
.Setup(s => s.SetPasswordHashAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
|
.Returns(Task.CompletedTask);
|
|
store.Setup(s => s.CreateAsync(It.IsAny<ApplicationUser>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(IdentityResult.Failed(new IdentityError { Description = "duplicate" }));
|
|
|
|
var outcome = await service.AddUserAsync(new AddUserRequestDTO { Name = "N", Email = "n@x.com", Role = "Admin" }, CancellationToken.None);
|
|
|
|
outcome.Success.Should().BeFalse();
|
|
outcome.Error.Should().Be("duplicate");
|
|
}
|
|
|
|
private static bool ContainsStrongGeneratedPassword(string body)
|
|
{
|
|
const string marker = "Your password is: ";
|
|
var start = body.IndexOf(marker, StringComparison.Ordinal);
|
|
if (start < 0)
|
|
return false;
|
|
|
|
var password = body[(start + marker.Length)..]
|
|
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
|
|
.FirstOrDefault();
|
|
|
|
return password is
|
|
{
|
|
Length: 8
|
|
}
|
|
&& password.Any(char.IsUpper)
|
|
&& password.Any(char.IsLower)
|
|
&& password.Any(char.IsDigit)
|
|
&& password.Any(character => !char.IsLetterOrDigit(character));
|
|
}
|
|
}
|