Adds unit tests improving presentation api project code coverage

This commit is contained in:
BinaryAlley committed 2026-09-02 10:30:12 -07:00
1 parent 042c5e4729
commit f3578f88d1
8 files changed
+373 -10

No files matched your search

@@ -147,6 +147,27 @@ public class InstallPluginEndpointTests
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => operationTask);
}
[Fact]
public async Task ExecuteAsync_WhenTheFormIsMalformed_ShouldTreatTheUploadAsMissingAndSendNullArchive()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<InstallPluginCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Success));
ConfigureMalformedForm();
// Act
IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, cancellationToken);
// Assert
Assert.IsType<Ok>(result);
await _mockHandler.Received(1).HandleAsync(
Arg.Is<InstallPluginCommand>(command =>
command.Archive == null &&
command.FileName == null),
Arg.Is(cancellationToken));
}
private void ConfigureFormWithArchive(byte[] content, string fileName)
{
// the stream stays open for the duration of the test, since the endpoint reads it through the form file during execution
@@ -155,8 +176,8 @@ public class InstallPluginEndpointTests
FormFileCollection files = [formFile];
IFormCollection form = new FormCollection([], files);
_sut.HttpContext.Request.ContentType = "multipart/form-data; boundary=----test";
// a real FormFeature is used instead of a substitute, because the endpoint execution reads the form through the
// request machinery, which would bypass a mocked feature and parse the (empty) request body
// A real FormFeature is used instead of a substitute, because the endpoint execution reads the form through the
// request machinery, which would bypass a mocked feature and parse the (empty) request body.
_sut.HttpContext.Features.Set<IFormFeature>(new FormFeature(form));
}
@@ -166,4 +187,12 @@ public class InstallPluginEndpointTests
_sut.HttpContext.Request.ContentType = "multipart/form-data; boundary=----test";
_sut.HttpContext.Features.Set<IFormFeature>(new FormFeature(form));
}
private void ConfigureMalformedForm()
{
_sut.HttpContext.Request.ContentType = "multipart/form-data; boundary=----test";
IFormCollection malformedForm = Substitute.For<IFormCollection>();
malformedForm.Files.Returns(_ => throw new InvalidDataException("Malformed multipart body."));
_sut.HttpContext.Features.Set<IFormFeature>(new FormFeature(malformedForm));
}
}