test(app): add comprehensive camera access control tests (issue #141)
- Add tests for UserCameraAccess CRUD operations (Create, Delete, Update, List) - Add tests for GroupCameraAccess CRUD operations (Add, Remove, List) - Add tests for site filtering by user access rights - Fix test issues: camera navigation property setup, group/camera ID matching - Add InternalsVisibleTo for test access to internal methods - All 1727 tests pass
This commit is contained in:
1 parent
e068825768
commit
7c40524151
10 files changed
+620
No files matched your search
@@ -0,0 +1,88 @@
|
||||
using Heimdall.Application.Groups.Commands.AddCameraToGroup;
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Application.Tests.Common;
|
||||
using Heimdall.Domain.Entities;
|
||||
using NSubstitute;
|
||||
using Shouldly;
|
||||
|
||||
namespace Heimdall.Application.Tests.Handlers.Groups;
|
||||
|
||||
public class AddCameraToGroupHandlerTests
|
||||
{
|
||||
private readonly Group _group = new Group("Test Group", "Description");
|
||||
private readonly Camera _camera = new Camera(Guid.NewGuid(), "Test Cam");
|
||||
|
||||
[Fact]
|
||||
public async Task Add_should_return_success()
|
||||
{
|
||||
var context = Substitute.For<IApplicationDbContext>();
|
||||
context.Groups.Returns(MockDbSetHelper.CreateFor(new List<Group> { _group }));
|
||||
context.Cameras.Returns(MockDbSetHelper.CreateFor(new List<Camera> { _camera }));
|
||||
context.GroupCameraAccesses.Returns(MockDbSetHelper.CreateFor<GroupCameraAccess>([]));
|
||||
context.AuditLogs.Returns(Substitute.For<Microsoft.EntityFrameworkCore.DbSet<AuditLog>>());
|
||||
|
||||
var handler = new AddCameraToGroupHandler(context);
|
||||
var command = new AddCameraToGroupCommand(_group.Id, _camera.Id);
|
||||
|
||||
var result = await handler.Handle(command, CancellationToken.None);
|
||||
|
||||
result.IsSuccess.ShouldBeTrue();
|
||||
result.Data.ShouldNotBeNull();
|
||||
result.Data.GroupId.ShouldBe(_group.Id);
|
||||
result.Data.CameraId.ShouldBe(_camera.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Add_should_return_not_found_when_group_not_exists()
|
||||
{
|
||||
var context = Substitute.For<IApplicationDbContext>();
|
||||
context.Groups.Returns(MockDbSetHelper.CreateFor<Group>([]));
|
||||
context.Cameras.Returns(MockDbSetHelper.CreateFor(new List<Camera> { _camera }));
|
||||
context.GroupCameraAccesses.Returns(MockDbSetHelper.CreateFor<GroupCameraAccess>([]));
|
||||
|
||||
var handler = new AddCameraToGroupHandler(context);
|
||||
var command = new AddCameraToGroupCommand(Guid.NewGuid(), _camera.Id);
|
||||
|
||||
var result = await handler.Handle(command, CancellationToken.None);
|
||||
|
||||
result.IsSuccess.ShouldBeFalse();
|
||||
result.StatusCode.ShouldBe(404);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Add_should_return_not_found_when_camera_not_exists()
|
||||
{
|
||||
var context = Substitute.For<IApplicationDbContext>();
|
||||
context.Groups.Returns(MockDbSetHelper.CreateFor(new List<Group> { _group }));
|
||||
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>([]));
|
||||
context.GroupCameraAccesses.Returns(MockDbSetHelper.CreateFor<GroupCameraAccess>([]));
|
||||
|
||||
var handler = new AddCameraToGroupHandler(context);
|
||||
var command = new AddCameraToGroupCommand(_group.Id, Guid.NewGuid());
|
||||
|
||||
var result = await handler.Handle(command, CancellationToken.None);
|
||||
|
||||
result.IsSuccess.ShouldBeFalse();
|
||||
result.StatusCode.ShouldBe(404);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Add_should_return_conflict_when_access_already_exists()
|
||||
{
|
||||
var existingAccess = new GroupCameraAccess(_group.Id, _camera.Id);
|
||||
|
||||
var context = Substitute.For<IApplicationDbContext>();
|
||||
context.Groups.Returns(MockDbSetHelper.CreateFor(new List<Group> { _group }));
|
||||
context.Cameras.Returns(MockDbSetHelper.CreateFor(new List<Camera> { _camera }));
|
||||
context.GroupCameraAccesses.Returns(MockDbSetHelper.CreateFor([existingAccess]));
|
||||
|
||||
var handler = new AddCameraToGroupHandler(context);
|
||||
var command = new AddCameraToGroupCommand(_group.Id, _camera.Id);
|
||||
|
||||
var result = await handler.Handle(command, CancellationToken.None);
|
||||
|
||||
result.IsSuccess.ShouldBeFalse();
|
||||
result.StatusCode.ShouldBe(409);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user