Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Sites/DeleteSiteHandlerTests.cs
T
Arnaud Dartois cbf4708d41
Heimdall Gitea / Build and tests (pull_request) Successful in 18m18s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
test(audit): assert resource mutation audit snapshots
2026-10-21 15:45:27 +02:00

132 lines
5.6 KiB
C#

using Heimdall.Application.Common;
using Heimdall.Application.Sites.Commands.DeleteSite;
using Heimdall.Application.Tests.Common;
using Heimdall.Domain.Entities;
using NSubstitute;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
namespace Heimdall.Application.Tests.Handlers.Sites;
public class DeleteSiteHandlerTests
{
private readonly Site _existingSite = new("Test Site", "Paris", "Europe/Paris");
[Fact]
public async Task Delete_site_should_return_success()
{
var context = Substitute.For<IApplicationDbContext>();
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>());
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>());
var handler = new DeleteSiteHandler(context);
var command = new DeleteSiteCommand(_existingSite.Id);
var result = await handler.Handle(command, CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
}
[Fact]
public async Task Delete_nonexistent_site_should_return_not_found()
{
var context = Substitute.For<IApplicationDbContext>();
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>());
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>());
var handler = new DeleteSiteHandler(context);
var command = new DeleteSiteCommand(Guid.NewGuid());
var result = await handler.Handle(command, CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(404);
}
[Fact]
public async Task Delete_site_should_soft_delete_and_save()
{
var context = Substitute.For<IApplicationDbContext>();
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>());
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>());
var handler = new DeleteSiteHandler(context);
var command = new DeleteSiteCommand(_existingSite.Id);
await handler.Handle(command, CancellationToken.None);
_existingSite.IsDeleted.ShouldBeTrue();
_existingSite.DeletedAt.ShouldNotBeNull();
await context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Delete_site_should_cascade_soft_delete_agents_and_cameras()
{
var siteId = _existingSite.Id;
var agent1 = new Agent(siteId, "Agent1", "host1", "1.0");
var agent2 = new Agent(siteId, "Agent2", "host2", "1.0");
var camera1 = new Camera(siteId, "Camera1");
var camera2 = new Camera(siteId, "Camera2");
var context = Substitute.For<IApplicationDbContext>();
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>([agent1, agent2]));
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>([camera1, camera2]));
var handler = new DeleteSiteHandler(context);
var command = new DeleteSiteCommand(siteId);
var result = await handler.Handle(command, CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
// Site should be soft-deleted
_existingSite.IsDeleted.ShouldBeTrue();
_existingSite.DeletedAt.ShouldNotBeNull();
// Agents should be soft-deleted
agent1.IsDeleted.ShouldBeTrue();
agent1.DeletedAt.ShouldNotBeNull();
agent2.IsDeleted.ShouldBeTrue();
agent2.DeletedAt.ShouldNotBeNull();
// Cameras should be soft-deleted
camera1.IsDeleted.ShouldBeTrue();
camera1.DeletedAt.ShouldNotBeNull();
camera2.IsDeleted.ShouldBeTrue();
camera2.DeletedAt.ShouldNotBeNull();
}
[Fact]
public async Task Delete_site_should_audit_actor_and_cascade_snapshot_in_the_same_save()
{
var actorId = Guid.NewGuid();
var siteId = _existingSite.Id;
var agent = new Agent(siteId, "Agent1", "host1", "1.0");
var camera = new Camera(siteId, "Camera1");
AuditLog? recordedAudit = null;
var auditLogs = Substitute.For<DbSet<AuditLog>>();
auditLogs.When(x => x.Add(Arg.Any<AuditLog>()))
.Do(call => recordedAudit = call.Arg<AuditLog>());
var context = Substitute.For<IApplicationDbContext>();
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>([agent]));
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>([camera]));
context.AuditLogs.Returns(auditLogs);
var handler = new DeleteSiteHandler(context);
var result = await handler.Handle(new DeleteSiteCommand(siteId, actorId), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
recordedAudit.ShouldNotBeNull();
recordedAudit.UserId.ShouldBe(actorId);
recordedAudit.Action.ShouldBe("Site.Deleted");
recordedAudit.ResourceId.ShouldBe(siteId.ToString());
JsonDocument.Parse(recordedAudit.OldValue!).RootElement.GetProperty("Name").GetString()
.ShouldBe(_existingSite.Name);
var newValue = JsonDocument.Parse(recordedAudit.NewValue!).RootElement;
newValue.GetProperty("IsDeleted").GetBoolean().ShouldBeTrue();
newValue.GetProperty("AgentsSoftDeleted").GetInt32().ShouldBe(1);
newValue.GetProperty("CamerasSoftDeleted").GetInt32().ShouldBe(1);
await context.Received(1).SaveChangesWithRetryAsync(ct: Arg.Any<CancellationToken>());
}
}