Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 10s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 10s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 1m12s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 3m4s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m17s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 10m36s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Version every /api route to /api/v1 (251 controller routes + ~24 Location
headers + the scanner callback URL + the Startup request-log literal),
uniform across the machine API, auth, scanner and scripted-build surfaces.
Add ApiVersionRewriteMiddleware: a legacy unversioned /api/* request is
rewritten (NOT redirected) to /api/v1/* in-pipeline — method, body, auth
headers and query survive — carrying RFC 8594 Deprecation/Sunset headers,
so curl / the future MCP server / bookmarks keep working. An already-
versioned path passes through; a future /api/v2 is never forced to v1.
Standardize the route convention (leading-slash absolute route per method,
no class-[Route] — except the two Scanner/Scripted controllers whose ~all
actions share a parametrized {id} prefix), enforced by ApiRouteVersioningTests
(^/api/v\d+/ over the whole Controllers.Api surface; browser-nav
/auth/oidc/login is out of scope).
Regenerate v1.json (160 paths, all /api/v1)/endpoint-index/v1.d.ts; sweep 945
SPA request literals + the test mocks (regex + positional URL parsers). /api/v1
is additive-only after freeze; the legacy-rewrite shim sunsets in ~2 releases
(owner decision) with removal tracked as a Phase-3 follow-up.
Docs: decisions.md 2026-07-13, api-conventions §1/§9, rest-api/spa-conventions/
blazor-route-parity/e2e-local/domain-model.
fixes #286
refs #197
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
148 lines
5.2 KiB
C#
148 lines
5.2 KiB
C#
using System.Reflection;
|
|
using ErsatzTV.Application.Images;
|
|
using ErsatzTV.Controllers.Api;
|
|
using ErsatzTV.Controllers.Api.Requests;
|
|
using ErsatzTV.Core.Api.Images;
|
|
using LanguageExt;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Routing;
|
|
using NSubstitute;
|
|
using NUnit.Framework;
|
|
using Shouldly;
|
|
|
|
namespace ErsatzTV.Tests.Controllers;
|
|
|
|
[TestFixture]
|
|
public class ImagesControllerTests
|
|
{
|
|
private ImagesController _controller = null!;
|
|
private IMediator _mediator = null!;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_mediator = Substitute.For<IMediator>();
|
|
_controller = new ImagesController(_mediator);
|
|
}
|
|
|
|
[Test]
|
|
public void Controller_Should_Expose_Idiomatic_Rest_Routes()
|
|
{
|
|
ShouldHaveActionRoute(nameof(ImagesController.GetFolders), "GET", "/api/v1/images/folders");
|
|
ShouldHaveActionRoute(
|
|
nameof(ImagesController.UpdateDuration),
|
|
"PUT",
|
|
"/api/v1/images/folders/{id:int}/duration");
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetFolders_Should_Map_Duration_Option_To_Nullable()
|
|
{
|
|
_mediator.Send(Arg.Any<GetImageFolders>(), Arg.Any<CancellationToken>())
|
|
.Returns(
|
|
[
|
|
new ImageFolderViewModel(1, "Root", "/images", 2, 5, Option<double>.None),
|
|
new ImageFolderViewModel(2, "Child", "/images/child", 0, 3, Option<double>.Some(4.5))
|
|
]);
|
|
|
|
List<ImageFolderResponseModel> result = await _controller.GetFolders(null, CancellationToken.None);
|
|
|
|
result.Count.ShouldBe(2);
|
|
result[0].DurationSeconds.ShouldBeNull();
|
|
result[1].DurationSeconds.ShouldBe(4.5);
|
|
result[1].Name.ShouldBe("Child");
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetFolders_Should_Pass_None_When_ParentId_Omitted()
|
|
{
|
|
_mediator.Send(Arg.Any<GetImageFolders>(), Arg.Any<CancellationToken>()).Returns([]);
|
|
|
|
await _controller.GetFolders(null, CancellationToken.None);
|
|
|
|
await _mediator.Received().Send(
|
|
Arg.Is<GetImageFolders>(q => q.LibraryFolderId.IsNone),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetFolders_Should_Pass_Some_When_ParentId_Given()
|
|
{
|
|
_mediator.Send(Arg.Any<GetImageFolders>(), Arg.Any<CancellationToken>()).Returns([]);
|
|
|
|
await _controller.GetFolders(42, CancellationToken.None);
|
|
|
|
await _mediator.Received().Send(
|
|
Arg.Is<GetImageFolders>(q => q.LibraryFolderId == Option<int>.Some(42)),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task UpdateDuration_Should_Return_400_For_Non_Positive_Duration()
|
|
{
|
|
IActionResult result = await _controller.UpdateDuration(
|
|
1,
|
|
new UpdateImageFolderDurationRequest(0),
|
|
CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<BadRequestObjectResult>().StatusCode.ShouldBe(400);
|
|
await _mediator.DidNotReceive().Send(Arg.Any<ImageFolderExists>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task UpdateDuration_Should_Return_404_When_Folder_Missing()
|
|
{
|
|
_mediator.Send(Arg.Any<ImageFolderExists>(), Arg.Any<CancellationToken>()).Returns(false);
|
|
|
|
IActionResult result = await _controller.UpdateDuration(
|
|
1,
|
|
new UpdateImageFolderDurationRequest(3.0),
|
|
CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<NotFoundObjectResult>().StatusCode.ShouldBe(404);
|
|
await _mediator.DidNotReceive().Send(Arg.Any<UpdateImageFolderDuration>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task UpdateDuration_Should_Return_200_And_Update_When_Valid()
|
|
{
|
|
_mediator.Send(Arg.Any<ImageFolderExists>(), Arg.Any<CancellationToken>()).Returns(true);
|
|
_mediator.Send(Arg.Any<UpdateImageFolderDuration>(), Arg.Any<CancellationToken>()).Returns(3.0);
|
|
|
|
IActionResult result = await _controller.UpdateDuration(
|
|
1,
|
|
new UpdateImageFolderDurationRequest(3.0),
|
|
CancellationToken.None);
|
|
|
|
var ok = result.ShouldBeOfType<OkObjectResult>();
|
|
ok.Value.ShouldBeOfType<UpdateImageFolderDurationResponseModel>().DurationSeconds.ShouldBe(3.0);
|
|
}
|
|
|
|
[Test]
|
|
public async Task UpdateDuration_Should_Allow_Null_To_Clear()
|
|
{
|
|
_mediator.Send(Arg.Any<ImageFolderExists>(), Arg.Any<CancellationToken>()).Returns(true);
|
|
_mediator.Send(Arg.Any<UpdateImageFolderDuration>(), Arg.Any<CancellationToken>())
|
|
.Returns((double?)null);
|
|
|
|
IActionResult result = await _controller.UpdateDuration(
|
|
1,
|
|
new UpdateImageFolderDurationRequest(null),
|
|
CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<OkObjectResult>()
|
|
.Value.ShouldBeOfType<UpdateImageFolderDurationResponseModel>().DurationSeconds.ShouldBeNull();
|
|
}
|
|
|
|
private static void ShouldHaveActionRoute(string actionName, string httpMethod, string route)
|
|
{
|
|
MethodInfo action = typeof(ImagesController).GetMethod(actionName)
|
|
?? throw new AssertionException($"Missing action {actionName}");
|
|
|
|
HttpMethodAttribute attribute = action.GetCustomAttributes<HttpMethodAttribute>(inherit: true).Single();
|
|
attribute.HttpMethods.ShouldContain(httpMethod);
|
|
attribute.Template.ShouldBe(route);
|
|
}
|
|
}
|