91 lines
3.6 KiB
C#
91 lines
3.6 KiB
C#
using Heimdall.Application.ApiKeys.Commands.CreateApiKey;
|
|||
|
|
using Heimdall.Application.Common;
|
||
|
|
using Heimdall.Application.Tests.Common;
|
||
|
|
using Heimdall.Domain.Entities;
|
||
|
|
using NSubstitute;
|
||
|
|
|
||
|
|
namespace Heimdall.Application.Tests.Handlers.ApiKeys;
|
||
|
|
|
||
|
|
public class CreateApiKeyHandlerTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public async Task Create_api_key_should_return_success()
|
||
|
|
{
|
||
|
|
var context = Substitute.For<IApplicationDbContext>();
|
||
|
|
context.ApiKeys.Returns(MockDbSetHelper.CreateFor<ApiKey>());
|
||
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>());
|
||
|
|
context.SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
|
||
|
|
var handler = new CreateApiKeyCommandHandler(context);
|
||
|
|
|
||
|
|
var result = await handler.Handle(
|
||
|
|
new CreateApiKeyCommand(null, null), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeTrue();
|
||
|
|
result.Data.ShouldNotBeNull();
|
||
|
|
result.Data.PlainKey.ShouldNotBeNullOrEmpty();
|
||
|
|
result.Data.KeyPrefix.ShouldBe(result.Data.PlainKey[..8]);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Create_api_key_with_site_should_return_success()
|
||
|
|
{
|
||
|
|
var site = new Site("Test Site", "192.168.1.0/24");
|
||
|
|
var context = Substitute.For<IApplicationDbContext>();
|
||
|
|
context.ApiKeys.Returns(MockDbSetHelper.CreateFor<ApiKey>());
|
||
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([site]));
|
||
|
|
context.SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
|
||
|
|
var handler = new CreateApiKeyCommandHandler(context);
|
||
|
|
|
||
|
|
var result = await handler.Handle(
|
||
|
|
new CreateApiKeyCommand(site.Id, null), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeTrue();
|
||
|
|
result.Data!.SiteId.ShouldBe(site.Id);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Create_api_key_with_nonexistent_site_should_return_not_found()
|
||
|
|
{
|
||
|
|
var context = Substitute.For<IApplicationDbContext>();
|
||
|
|
context.ApiKeys.Returns(MockDbSetHelper.CreateFor<ApiKey>());
|
||
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>());
|
||
|
|
var handler = new CreateApiKeyCommandHandler(context);
|
||
|
|
|
||
|
|
var result = await handler.Handle(
|
||
|
|
new CreateApiKeyCommand(Guid.NewGuid(), null), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeFalse();
|
||
|
|
result.StatusCode.ShouldBe(404);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Create_api_key_with_past_expiry_should_return_failure()
|
||
|
|
{
|
||
|
|
var context = Substitute.For<IApplicationDbContext>();
|
||
|
|
context.ApiKeys.Returns(MockDbSetHelper.CreateFor<ApiKey>());
|
||
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>());
|
||
|
|
var handler = new CreateApiKeyCommandHandler(context);
|
||
|
|
|
||
|
|
var result = await handler.Handle(
|
||
|
|
new CreateApiKeyCommand(null, DateTime.UtcNow.AddDays(-1)), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeFalse();
|
||
|
|
result.StatusCode.ShouldBe(400);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Create_api_key_with_future_expiry_should_return_success()
|
||
|
|
{
|
||
|
|
var context = Substitute.For<IApplicationDbContext>();
|
||
|
|
context.ApiKeys.Returns(MockDbSetHelper.CreateFor<ApiKey>());
|
||
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>());
|
||
|
|
context.SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
|
||
|
|
var handler = new CreateApiKeyCommandHandler(context);
|
||
|
|
|
||
|
|
var result = await handler.Handle(
|
||
|
|
new CreateApiKeyCommand(null, DateTime.UtcNow.AddDays(30)), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeTrue();
|
||
|
|
result.Data!.ExpiresAt.ShouldNotBeNull();
|
||
|
|
}
|
||
|
|
}
|