using Heimdall.Application.ApiKeys.Commands.RevokeApiKey; using Heimdall.Application.Common; using Heimdall.Application.Tests.Common; using Heimdall.Domain.Entities; using NSubstitute; namespace Heimdall.Application.Tests.Handlers.ApiKeys; public class RevokeApiKeyHandlerTests { [Fact] public async Task Revoke_existing_key_should_return_success() { var key = new ApiKey("hash1", "prefix1"); var context = Substitute.For(); context.ApiKeys.Returns(MockDbSetHelper.CreateFor([key])); context.SaveChangesWithRetryAsync(Arg.Any(), Arg.Any()).Returns(Task.CompletedTask); var handler = new RevokeApiKeyCommandHandler(context); var result = await handler.Handle(new RevokeApiKeyCommand(key.Id), CancellationToken.None); result.IsSuccess.ShouldBeTrue(); key.Enabled.ShouldBeFalse(); } [Fact] public async Task Revoke_nonexistent_key_should_return_not_found() { var context = Substitute.For(); context.ApiKeys.Returns(MockDbSetHelper.CreateFor()); var handler = new RevokeApiKeyCommandHandler(context); var result = await handler.Handle(new RevokeApiKeyCommand(Guid.NewGuid()), CancellationToken.None); result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(404); } [Fact] public async Task Revoke_already_revoked_key_should_return_failure() { var key = new ApiKey("hash1", "prefix1"); key.Disable(); // Already revoked var context = Substitute.For(); context.ApiKeys.Returns(MockDbSetHelper.CreateFor([key])); var handler = new RevokeApiKeyCommandHandler(context); var result = await handler.Handle(new RevokeApiKeyCommand(key.Id), CancellationToken.None); result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(400); } }