Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Cameras/SetCameraCredentialsHandlerTests.cs
T
Arnaud Dartois 45c6ce82eb perf: add ConfigureAwait(false) to all async calls in library projects
Add ConfigureAwait(false) to all await expressions in Application and
Infrastructure projects (111 files). This follows .NET best practices
for library code to avoid potential deadlocks when consumed from
synchronization contexts (legacy, unit test runners).

Also fix 4 test assertions that check SaveChangesAsync — update to
SaveChangesWithRetryAsync to match the handler changes from Fix 4.
2026-07-27 18:59:12 +00:00

78 lines
3.2 KiB
C#

using Heimdall.Application.Cameras.Commands.SetCredentials;
using Heimdall.Application.Common;
using Heimdall.Application.Tests.Common;
using Heimdall.Domain.Entities;
using NSubstitute;
namespace Heimdall.Application.Tests.Handlers.Cameras;
public class SetCameraCredentialsHandlerTests
{
private readonly Camera _existingCamera = new(Guid.NewGuid(), "Front Door");
[Fact]
public async Task Set_credentials_should_return_success()
{
var protector = Substitute.For<ICredentialProtector>();
protector.Encrypt(Arg.Any<string>()).Returns(c => $"enc_{c.Arg<string>()}");
var context = Substitute.For<IApplicationDbContext>();
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>([_existingCamera]));
var handler = new SetCameraCredentialsCommandHandler(context, protector);
var command = new SetCameraCredentialsCommand(_existingCamera.Id, "admin", "pass123", "rtsp_user", "rtsp_pass");
var result = await handler.Handle(command, CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
}
[Fact]
public async Task Set_credentials_nonexistent_camera_should_return_not_found()
{
var protector = Substitute.For<ICredentialProtector>();
var context = Substitute.For<IApplicationDbContext>();
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>([_existingCamera]));
var handler = new SetCameraCredentialsCommandHandler(context, protector);
var command = new SetCameraCredentialsCommand(Guid.NewGuid(), "admin", "pass", "user", "pass");
var result = await handler.Handle(command, CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(404);
}
[Fact]
public async Task Set_credentials_should_encrypt_sensitive_fields()
{
var protector = Substitute.For<ICredentialProtector>();
protector.Encrypt(Arg.Any<string>()).Returns(c => $"enc_{c.Arg<string>()}");
var context = Substitute.For<IApplicationDbContext>();
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>([_existingCamera]));
var handler = new SetCameraCredentialsCommandHandler(context, protector);
var command = new SetCameraCredentialsCommand(_existingCamera.Id, "admin", "secret_pwd", "rtsp_user", "rtsp_secret");
await handler.Handle(command, CancellationToken.None);
protector.Received(1).Encrypt("secret_pwd");
protector.Received(1).Encrypt("rtsp_user");
protector.Received(1).Encrypt("rtsp_secret");
}
[Fact]
public async Task Set_credentials_should_save_changes()
{
var protector = Substitute.For<ICredentialProtector>();
protector.Encrypt(Arg.Any<string>()).Returns(c => $"enc_{c.Arg<string>()}");
var context = Substitute.For<IApplicationDbContext>();
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>([_existingCamera]));
var handler = new SetCameraCredentialsCommandHandler(context, protector);
var command = new SetCameraCredentialsCommand(_existingCamera.Id, "admin", "pass", "user", "pass");
await handler.Handle(command, CancellationToken.None);
await context.Received(1).SaveChangesWithRetryAsync(ct: Arg.Any<CancellationToken>());
}
}