mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 18:21:10 +00:00
Adds unit tests improving presentation api project code coverage
This commit is contained in:
1 parent
042c5e4729
commit
f3578f88d1
8 files changed
+373
-10
No files matched your search
+128
-5
@@ -1,10 +1,133 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.AddBook;
|
||||
using Lumina.Contracts.Fixtures.Core.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Contracts.Fixtures.Core.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.AddBook;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.AddBook;
|
||||
internal class AddBookEndpointTests
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="AddBookEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class AddBookEndpointTests
|
||||
{
|
||||
private readonly Lumina.Application.Common.CQRS.ICommandHandler<AddBookCommand, Result<BookResponse>> _mockHandler;
|
||||
private readonly AddBookEndpoint _sut;
|
||||
private readonly AddBookRequestFixture _addBookRequestFixture = new();
|
||||
private readonly BookResponseFixture _bookResponseFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AddBookEndpointTests"/> class.
|
||||
/// </summary>
|
||||
public AddBookEndpointTests()
|
||||
{
|
||||
_mockHandler = Substitute.For<Lumina.Application.Common.CQRS.ICommandHandler<AddBookCommand, Result<BookResponse>>>();
|
||||
_sut = Factory.Create<AddBookEndpoint>(_mockHandler);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnCreatedResultWithBookResponse()
|
||||
{
|
||||
// Arrange
|
||||
AddBookRequest request = _addBookRequestFixture.Create();
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookResponse expectedResponse = _bookResponseFixture.Create();
|
||||
_mockHandler.HandleAsync(Arg.Any<AddBookCommand>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(expectedResponse));
|
||||
|
||||
// Act
|
||||
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
|
||||
|
||||
// Assert
|
||||
Created<BookResponse> createdResult = Assert.IsType<Created<BookResponse>>(result);
|
||||
Assert.Equal(expectedResponse, createdResult.Value);
|
||||
Assert.Contains(expectedResponse.Id.ToString(), createdResult.Location);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteAsync_WhenHandlerReturnsConflictError_ShouldReturnProblemResult()
|
||||
{
|
||||
// Arrange
|
||||
AddBookRequest request = _addBookRequestFixture.Create();
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
Error expectedError = Error.Conflict("Book.AlreadyExists", "BookAlreadyExists");
|
||||
_mockHandler.HandleAsync(Arg.Any<AddBookCommand>(), Arg.Any<CancellationToken>())
|
||||
.Returns(expectedError);
|
||||
|
||||
// Act
|
||||
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
|
||||
|
||||
// Assert
|
||||
ProblemHttpResult problemDetails = Assert.IsType<ProblemHttpResult>(result);
|
||||
Assert.Equal(StatusCodes.Status409Conflict, problemDetails.StatusCode);
|
||||
Assert.Equal("application/problem+json", problemDetails.ContentType);
|
||||
Microsoft.AspNetCore.Mvc.ProblemDetails problemDetailsBody = Assert.IsType<Microsoft.AspNetCore.Mvc.ProblemDetails>(problemDetails.ProblemDetails);
|
||||
Assert.Equal(StatusCodes.Status409Conflict, problemDetailsBody.Status);
|
||||
Assert.Equal("Book.AlreadyExists", problemDetailsBody.Title);
|
||||
Assert.Equal("BookAlreadyExists", problemDetailsBody.Detail);
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc9110#section-15.5.10", problemDetailsBody.Type);
|
||||
Assert.NotNull(problemDetailsBody.Extensions["traceId"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteAsync_WhenCalled_ShouldSendAddBookCommandToHandler()
|
||||
{
|
||||
// Arrange
|
||||
AddBookRequest request = _addBookRequestFixture.Create();
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
_mockHandler.HandleAsync(Arg.Any<AddBookCommand>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(_bookResponseFixture.Create()));
|
||||
|
||||
// Act
|
||||
await _sut.ExecuteAsync(request, cancellationToken);
|
||||
|
||||
// Assert
|
||||
await _mockHandler.Received(1).HandleAsync(
|
||||
Arg.Is<AddBookCommand>(command =>
|
||||
command.LibraryId == request.LibraryId &&
|
||||
command.Path == request.Path),
|
||||
Arg.Is(cancellationToken));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteAsync_WhenCancellationRequested_ShouldCancelOperation()
|
||||
{
|
||||
// Arrange
|
||||
AddBookRequest request = _addBookRequestFixture.Create();
|
||||
CancellationTokenSource cts = new();
|
||||
TaskCompletionSource<bool> operationStarted = new();
|
||||
TaskCompletionSource<bool> cancellationRequested = new();
|
||||
|
||||
_mockHandler.HandleAsync(Arg.Any<AddBookCommand>(), Arg.Any<CancellationToken>())
|
||||
.Returns(callInfo => Task.Run(async () =>
|
||||
{
|
||||
operationStarted.SetResult(true);
|
||||
await cancellationRequested.Task;
|
||||
callInfo.Arg<CancellationToken>().ThrowIfCancellationRequested();
|
||||
return Result.From(_bookResponseFixture.Create());
|
||||
}, callInfo.Arg<CancellationToken>()));
|
||||
|
||||
// Act
|
||||
Task<IResult> operationTask = _sut.ExecuteAsync(request, cts.Token);
|
||||
await operationStarted.Task;
|
||||
cts.Cancel();
|
||||
cancellationRequested.SetResult(true);
|
||||
|
||||
// Assert
|
||||
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => operationTask);
|
||||
}
|
||||
}
|
||||
+32
@@ -79,6 +79,38 @@ public class GetReadingResourceEndpointTests
|
||||
Assert.Equal("ResourceNotFound", problemResult.ProblemDetails.Detail);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("image/png", "image/png")] // a non-svg image is inert content
|
||||
[InlineData("image/svg+xml", "application/octet-stream")] // an svg can be executed, so it is served as an opaque download
|
||||
[InlineData("audio/mpeg", "audio/mpeg")] // audio is inert content
|
||||
[InlineData("video/mp4", "video/mp4")] // video is inert content
|
||||
[InlineData("font/woff2", "font/woff2")] // fonts are inert content
|
||||
[InlineData("application/font-woff", "application/font-woff")] // legacy woff font media type
|
||||
[InlineData("application/font-woff2", "application/font-woff2")] // legacy woff2 font media type
|
||||
[InlineData("application/vnd.ms-opentype", "application/vnd.ms-opentype")] // opentype font media type
|
||||
[InlineData("application/x-font-ttf", "application/x-font-ttf")] // ttf font media type
|
||||
[InlineData("application/x-font-opentype", "application/x-font-opentype")] // otf font media type
|
||||
[InlineData("text/css", "text/css")] // stylesheets are inert content
|
||||
[InlineData("text/html", "application/octet-stream")] // an html document can be executed, so it is served as an opaque download
|
||||
public async Task ExecuteAsync_WhenServingAResource_ShouldUseTheSafeContentType(string mimeType, string expectedContentType)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceRequest request = _getReadingResourceRequestFixture.Create();
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
ReadingResourceDataDto response = new(Guid.NewGuid().ToByteArray(), mimeType);
|
||||
_mockHandler.HandleAsync(Arg.Any<GetReadingResourceQuery>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(response));
|
||||
|
||||
// Act
|
||||
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
|
||||
|
||||
// Assert
|
||||
FileContentHttpResult bytesResult = Assert.IsType<FileContentHttpResult>(result);
|
||||
Assert.Equal(response.Data, bytesResult.FileContents);
|
||||
Assert.Equal(expectedContentType, bytesResult.ContentType);
|
||||
Assert.Equal("nosniff", _sut.HttpContext.Response.Headers["X-Content-Type-Options"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteAsync_WhenCalled_ShouldSendGetReadingResourceQueryToSender()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user