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

@@ -44,7 +44,10 @@ public class HttpContextCurrentUserServiceTests
public void UserId_WhenUserIsNull_ShouldReturnNull()
{
// Arrange
HttpContext mockHttpContext = new DefaultHttpContext();
HttpContext mockHttpContext = new DefaultHttpContext
{
User = null
};
_mockHttpContextAccessor.HttpContext.Returns(mockHttpContext);
// Act
@@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NSubstitute;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
@@ -85,6 +86,31 @@ public class ExceptionHandlingMiddlewareTests
Arg.Any<Func<object, Exception?, string>>());
}
[Fact]
public async Task InvokeAsync_WhenNextThrowsExceptionAndActivityIsCurrent_ShouldUseTheActivityIdAsTraceId()
{
// Arrange
using (Activity activity = new Activity("TestActivity"))
{
activity.Start();
DefaultHttpContext context = new()
{
Response = { Body = new MemoryStream() }
};
_nextDelegate = _ => throw new InvalidOperationException("sensitive internal details");
// Act
await _sut.InvokeAsync(context);
// Assert
context.Response.Body.Seek(0, SeekOrigin.Begin);
using StreamReader reader = new(context.Response.Body, Encoding.UTF8);
string responseBody = await reader.ReadToEndAsync();
ProblemDetails problemDetails = JsonSerializer.Deserialize<ProblemDetails>(responseBody, _jsonOptions)!;
Assert.Equal(activity.Id, problemDetails.Extensions["traceId"]?.ToString());
}
}
[Fact]
public async Task InvokeAsync_WhenNextThrowsOperationCanceledException_ShouldRethrow()
{
@@ -0,0 +1,41 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.SharedKernel.Common.Enums.PhotoLibrary;
using Lumina.Presentation.Api.Common.Utilities;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Common.Utilities;
/// <summary>
/// Contains unit tests for the <see cref="MimeTypes"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class MimeTypesTests
{
[Theory]
[InlineData(ImageType.BMP, "image/bmp")] // bmp image type
[InlineData(ImageType.JPEG, "image/jpeg")] // jpeg image type
[InlineData(ImageType.JPEG_CANON, "image/jpeg")] // canon jpeg image type
[InlineData(ImageType.SVG, "image/svg+xml")] // svg image type
[InlineData(ImageType.TGA, "image/x-tga")] // tga image type
public void GetMimeType_WhenTypeIsKnown_ShouldReturnTheMappedMimeType(ImageType type, string expectedMimeType)
{
// Act
string result = MimeTypes.GetMimeType(type);
// Assert
Assert.Equal(expectedMimeType, result);
}
[Theory]
[InlineData(ImageType.None)] // the none image type has no mime mapping
[InlineData((ImageType)999)] // an unknown image type has no mime mapping
public void GetMimeType_WhenTypeIsUnknown_ShouldReturnOpaqueBinaryMimeType(ImageType type)
{
// Act
string result = MimeTypes.GetMimeType(type);
// Assert
Assert.Equal("application/octet-stream", result);
}
}