59 lines
2.1 KiB
C#
59 lines
2.1 KiB
C#
using System.Security.Claims;
|
|
using Heimdall.Api.Controllers;
|
|
using Heimdall.Application.Cameras.Commands.ToggleCameraStatus;
|
|
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 CamerasControllerTests
|
|
{
|
|
[Fact]
|
|
public async Task ToggleStatus_passes_authenticated_actor()
|
|
{
|
|
var mediator = Substitute.For<IMediator>();
|
|
mediator.Send(Arg.Any<ToggleCameraStatusCommand>(), Arg.Any<CancellationToken>())
|
|
.Returns(ServiceResult<CameraResponse>.NotFound("Camera not found"));
|
|
var actorId = Guid.NewGuid();
|
|
var cameraId = Guid.NewGuid();
|
|
var controller = new CamerasController(mediator)
|
|
{
|
|
ControllerContext = new ControllerContext
|
|
{
|
|
HttpContext = new DefaultHttpContext
|
|
{
|
|
User = new ClaimsPrincipal(new ClaimsIdentity([
|
|
new Claim(ClaimTypes.NameIdentifier, actorId.ToString())
|
|
], "test"))
|
|
}
|
|
}
|
|
};
|
|
|
|
var result = await controller.ToggleStatus(cameraId, CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<NotFoundObjectResult>();
|
|
await mediator.Received(1).Send(
|
|
Arg.Is<ToggleCameraStatusCommand>(command => command != null
|
|
&& command.Id == cameraId && command.ActorId == actorId),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ToggleStatus_rejects_missing_actor_claim()
|
|
{
|
|
var mediator = Substitute.For<IMediator>();
|
|
var controller = new CamerasController(mediator)
|
|
{
|
|
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }
|
|
};
|
|
|
|
(await controller.ToggleStatus(Guid.NewGuid(), CancellationToken.None)).ShouldBeOfType<UnauthorizedResult>();
|
|
await mediator.DidNotReceive().Send(Arg.Any<ToggleCameraStatusCommand>(), Arg.Any<CancellationToken>());
|
|
}
|
|
}
|