2026-07-23 09:59:30 +00:00
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.DTOs;
|
|
|
|
|
using Heimdall.Application.Sites.Commands.CreateSite;
|
|
|
|
|
using Heimdall.Application.Tests.Common;
|
|
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Tests.Handlers.Sites;
|
|
|
|
|
|
|
|
|
|
public class CreateSiteHandlerTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_site_should_return_success()
|
|
|
|
|
{
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
2026-07-23 14:22:21 +00:00
|
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>());
|
2026-07-23 09:59:30 +00:00
|
|
|
var handler = new CreateSiteHandler(context);
|
|
|
|
|
var command = new CreateSiteCommand("Test Site", "Paris", "Europe/Paris");
|
|
|
|
|
|
|
|
|
|
var result = await handler.Handle(command, CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data.ShouldNotBeNull();
|
|
|
|
|
result.Data.Name.ShouldBe("Test Site");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Duplicate_name_should_return_conflict()
|
|
|
|
|
{
|
|
|
|
|
var existingSite = new Site("Test Site", "Paris", "Europe/Paris");
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
2026-07-23 14:22:21 +00:00
|
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([existingSite]));
|
2026-07-23 09:59:30 +00:00
|
|
|
var handler = new CreateSiteHandler(context);
|
|
|
|
|
var command = new CreateSiteCommand("Test Site", "Lyon", "Europe/Paris");
|
|
|
|
|
|
|
|
|
|
var result = await handler.Handle(command, CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
|
|
|
result.StatusCode.ShouldBe(409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_site_should_call_save_changes()
|
|
|
|
|
{
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
2026-07-23 14:22:21 +00:00
|
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>());
|
2026-07-23 09:59:30 +00:00
|
|
|
var handler = new CreateSiteHandler(context);
|
|
|
|
|
var command = new CreateSiteCommand("New Site", null, "UTC");
|
|
|
|
|
|
|
|
|
|
await handler.Handle(command, CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
await context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
|
|
|
|
|
}
|
|
|
|
|
}
|