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>
158 lines
5.7 KiB
C#
158 lines
5.7 KiB
C#
using System.Reflection;
|
|
using ErsatzTV.Application.Logs;
|
|
using ErsatzTV.Controllers.Api;
|
|
using ErsatzTV.Core.Api.Logs;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Mvc.Routing;
|
|
using NSubstitute;
|
|
using NUnit.Framework;
|
|
using Serilog.Events;
|
|
using Shouldly;
|
|
|
|
namespace ErsatzTV.Tests.Controllers;
|
|
|
|
[TestFixture]
|
|
public class LogsControllerTests
|
|
{
|
|
private LogsController _controller = null!;
|
|
private IMediator _mediator = null!;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_mediator = Substitute.For<IMediator>();
|
|
_controller = new LogsController(_mediator);
|
|
}
|
|
|
|
[Test]
|
|
public void Controller_Should_Expose_Idiomatic_Rest_Route()
|
|
{
|
|
MethodInfo action = typeof(LogsController).GetMethod(nameof(LogsController.GetLogs))
|
|
?? throw new AssertionException("Missing action GetLogs");
|
|
|
|
HttpMethodAttribute attribute = action.GetCustomAttributes<HttpMethodAttribute>(inherit: true).Single();
|
|
attribute.HttpMethods.ShouldContain("GET");
|
|
attribute.Template.ShouldBe("/api/v1/logs");
|
|
attribute.Name.ShouldBe("GetLogs");
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetLogs_Should_Clamp_Paging_And_Send_Query()
|
|
{
|
|
_mediator.Send(Arg.Any<GetRecentLogEntries>(), Arg.Any<CancellationToken>())
|
|
.Returns(new PagedLogEntriesViewModel(0, []));
|
|
|
|
await _controller.GetLogs(-1, 500, "boom", cancellationToken: CancellationToken.None);
|
|
|
|
await _mediator.Received(1).Send(
|
|
Arg.Is<GetRecentLogEntries>(q =>
|
|
q.PageNum == 0 &&
|
|
q.PageSize == 100 &&
|
|
q.Filter == "boom"),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetLogs_Should_Default_To_Timestamp_Descending()
|
|
{
|
|
_mediator.Send(Arg.Any<GetRecentLogEntries>(), Arg.Any<CancellationToken>())
|
|
.Returns(new PagedLogEntriesViewModel(0, []));
|
|
|
|
await _controller.GetLogs(cancellationToken: CancellationToken.None);
|
|
|
|
await _mediator.Received(1).Send(
|
|
Arg.Is<GetRecentLogEntries>(q =>
|
|
q.SortDescending == true &&
|
|
SelectsTimestamp(q.SortExpression)),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetLogs_Should_Sort_By_Level_Ascending_When_Requested()
|
|
{
|
|
_mediator.Send(Arg.Any<GetRecentLogEntries>(), Arg.Any<CancellationToken>())
|
|
.Returns(new PagedLogEntriesViewModel(0, []));
|
|
|
|
await _controller.GetLogs(sortField: "level", sortDirection: "asc", cancellationToken: CancellationToken.None);
|
|
|
|
await _mediator.Received(1).Send(
|
|
Arg.Is<GetRecentLogEntries>(q =>
|
|
q.SortDescending == false &&
|
|
SelectsLevel(q.SortExpression)),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetLogs_Should_Reject_Unknown_Sort_Field_And_Fall_Back_To_Timestamp()
|
|
{
|
|
_mediator.Send(Arg.Any<GetRecentLogEntries>(), Arg.Any<CancellationToken>())
|
|
.Returns(new PagedLogEntriesViewModel(0, []));
|
|
|
|
await _controller.GetLogs(sortField: "message; DROP TABLE", cancellationToken: CancellationToken.None);
|
|
|
|
await _mediator.Received(1).Send(
|
|
Arg.Is<GetRecentLogEntries>(q => SelectsTimestamp(q.SortExpression)),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetLogs_Should_Fall_Back_To_Descending_For_Unknown_Direction()
|
|
{
|
|
_mediator.Send(Arg.Any<GetRecentLogEntries>(), Arg.Any<CancellationToken>())
|
|
.Returns(new PagedLogEntriesViewModel(0, []));
|
|
|
|
await _controller.GetLogs(sortDirection: "sideways", cancellationToken: CancellationToken.None);
|
|
|
|
await _mediator.Received(1).Send(
|
|
Arg.Is<GetRecentLogEntries>(q => q.SortDescending == true),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
private static bool SelectsTimestamp(System.Linq.Expressions.Expression<Func<LogEntryViewModel, object>> expr)
|
|
{
|
|
var sample = new LogEntryViewModel(DateTimeOffset.UnixEpoch.AddDays(1), LogEventLevel.Error, "m");
|
|
return Equals(expr.Compile()(sample), sample.Timestamp);
|
|
}
|
|
|
|
private static bool SelectsLevel(System.Linq.Expressions.Expression<Func<LogEntryViewModel, object>> expr)
|
|
{
|
|
var sample = new LogEntryViewModel(DateTimeOffset.UnixEpoch.AddDays(1), LogEventLevel.Error, "m");
|
|
return Equals(expr.Compile()(sample), sample.Level);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetLogs_Should_Map_Entries_To_Response_Model()
|
|
{
|
|
var entries = new List<LogEntryViewModel>
|
|
{
|
|
new(DateTimeOffset.UnixEpoch, LogEventLevel.Warning, "uh oh"),
|
|
new(DateTimeOffset.UnixEpoch.AddMinutes(1), LogEventLevel.Information, "all good")
|
|
};
|
|
_mediator.Send(Arg.Any<GetRecentLogEntries>(), Arg.Any<CancellationToken>())
|
|
.Returns(new PagedLogEntriesViewModel(2, entries));
|
|
|
|
PagedLogEntriesResponseModel result = await _controller.GetLogs(
|
|
cancellationToken: CancellationToken.None);
|
|
|
|
result.TotalCount.ShouldBe(2);
|
|
result.Page.ShouldBe(
|
|
[
|
|
new LogEntryResponseModel(DateTimeOffset.UnixEpoch, "Warning", "uh oh"),
|
|
new LogEntryResponseModel(DateTimeOffset.UnixEpoch.AddMinutes(1), "Information", "all good")
|
|
]);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetLogs_Should_Return_Empty_Page_When_None_Exist()
|
|
{
|
|
_mediator.Send(Arg.Any<GetRecentLogEntries>(), Arg.Any<CancellationToken>())
|
|
.Returns(new PagedLogEntriesViewModel(0, []));
|
|
|
|
PagedLogEntriesResponseModel result = await _controller.GetLogs(
|
|
cancellationToken: CancellationToken.None);
|
|
|
|
result.TotalCount.ShouldBe(0);
|
|
result.Page.ShouldBeEmpty();
|
|
}
|
|
}
|