fix(audit): record camera access actor
This commit is contained in:
1 parent
0bde7266f8
commit
33ab9c7122
8 files changed
+133
-24
No files matched your search
@@ -0,0 +1,61 @@
|
||||
using System.Security.Claims;
|
||||
using Heimdall.Api.Controllers;
|
||||
using Heimdall.Application.Assignments.Commands.CreateUserCameraAccess;
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NSubstitute;
|
||||
using Shouldly;
|
||||
|
||||
namespace Heimdall.Web.Tests.Controllers;
|
||||
|
||||
public class UserCameraAccessControllerTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Create_passes_authenticated_actor_to_command()
|
||||
{
|
||||
var mediator = Substitute.For<IMediator>();
|
||||
mediator.Send(Arg.Any<CreateUserCameraAccessCommand>(), Arg.Any<CancellationToken>())
|
||||
.Returns(ServiceResult<UserCameraAccessResponse>.NotFound("User not found"));
|
||||
var actorId = Guid.NewGuid();
|
||||
var controller = CreateController(mediator, actorId);
|
||||
|
||||
await controller.Create(new CreateUserCameraAccessRequest(Guid.NewGuid(), Guid.NewGuid()));
|
||||
|
||||
await mediator.Received(1).Send(
|
||||
Arg.Is<CreateUserCameraAccessCommand>(command => command != null && command.ActorId == actorId),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_returns_unauthorized_when_actor_claim_is_missing()
|
||||
{
|
||||
var mediator = Substitute.For<IMediator>();
|
||||
var controller = CreateController(mediator, null);
|
||||
|
||||
var result = await controller.Create(new CreateUserCameraAccessRequest(Guid.NewGuid(), Guid.NewGuid()));
|
||||
|
||||
result.ShouldBeOfType<UnauthorizedResult>();
|
||||
await mediator.DidNotReceive().Send(Arg.Any<CreateUserCameraAccessCommand>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
private static UserCameraAccessController CreateController(IMediator mediator, Guid? userId)
|
||||
{
|
||||
var claims = userId.HasValue
|
||||
? new[] { new Claim(ClaimTypes.NameIdentifier, userId.Value.ToString()) }
|
||||
: [];
|
||||
var controller = new UserCameraAccessController(mediator)
|
||||
{
|
||||
ControllerContext = new ControllerContext
|
||||
{
|
||||
HttpContext = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(claims, "test"))
|
||||
}
|
||||
}
|
||||
};
|
||||
return controller;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user