Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 3m43s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m58s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Build ErsatzTV Image / Build & test (.NET) (push) Successful in 3m44s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (push) Successful in 5m0s
Build ErsatzTV Image / Build & push image (amd64) (push) Successful in 3m41s
First slice of the REST API (ersatztv#2). Idiomatic REST over existing MediatR handlers; design in docs/rest-api.md.
Foundation: NotFoundError : BaseError (Core) to split 404 from 422; ApiResults mapping helpers (Either/Option -> 201+Location / 200 / 204 / 404 / 422), additive (existing *ToActionResult untouched); ApiKeyAuthorizationFilter (optional X-Api-Key on mutating actions, gated by Api:WriteKey, no-op when unset) — independent of the IPTV JWT toggle so Jellyfin/Dispatcharr reads are unaffected, applied at controller-class level so every mutating action incl. ResetPlayout is covered fail-safe.
Channels: POST /api/channels (201+Location), PUT/{id} (200/404), DELETE/{id} (204/404), GET/{id} (200/404). Request DTOs -> existing commands; responses reuse ChannelViewModel. Ported page-only validations into handlers (ShowInEpg-when-disabled, external-logo-URL, Group NotEmpty) + FFmpegProfile/Watermark/Filler existence on update for create-parity. Fixed a latent 500: the .Filter(c>0) existence pattern threw TaskCanceledException on not-found; rewritten to AnyAsync.
Tests (NUnit, TZ=UTC): handler success/404/422, ApiResults mapping, API-key filter, controller status/Location/mapping, a controller-security regression net, and a create->read->delete EF integration test on a new in-memory SQLite harness. ErsatzTV.Tests 46/46, Architecture 5/5.
Refs #34
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
120 lines
3.7 KiB
C#
120 lines
3.7 KiB
C#
using ErsatzTV.Application.Channels;
|
|
using ErsatzTV.Core;
|
|
using LanguageExt;
|
|
using ErsatzTV.Core.Errors;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Tests.Support;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using NUnit.Framework;
|
|
using Shouldly;
|
|
|
|
namespace ErsatzTV.Tests.Application.Channels;
|
|
|
|
[TestFixture]
|
|
public class UpdateChannelHandlerTests : ChannelHandlerTestBase
|
|
{
|
|
private UpdateChannelHandler MakeHandler() => new(Worker, Db.Factory, SearchTargets);
|
|
|
|
[Test]
|
|
public async Task Should_Return_NotFoundError_When_Channel_Missing()
|
|
{
|
|
Either<BaseError, ChannelViewModel> result =
|
|
await MakeHandler().Handle(MakeUpdate(999, number: "5"), CancellationToken.None);
|
|
|
|
BaseError error = LeftOf(result);
|
|
error.ShouldBeOfType<NotFoundError>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Should_Update_Existing_Channel()
|
|
{
|
|
await SeedFFmpegProfile();
|
|
await SeedChannel(1, "5", "Old Name");
|
|
|
|
Either<BaseError, ChannelViewModel> result =
|
|
await MakeHandler().Handle(MakeUpdate(1, number: "5", name: "New Name"), CancellationToken.None);
|
|
|
|
result.IsRight.ShouldBeTrue();
|
|
|
|
await using TvContext context = Db.CreateContext();
|
|
var channel = await context.Channels.SingleAsync(c => c.Id == 1);
|
|
channel.Name.ShouldBe("New Name");
|
|
}
|
|
|
|
[Test]
|
|
public async Task Should_Allow_Keeping_Own_Number()
|
|
{
|
|
await SeedFFmpegProfile();
|
|
await SeedChannel(1, "5", "Old Name");
|
|
|
|
Either<BaseError, ChannelViewModel> result =
|
|
await MakeHandler().Handle(MakeUpdate(1, number: "5", name: "Renamed"), CancellationToken.None);
|
|
|
|
result.IsRight.ShouldBeTrue();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Should_Reject_Number_Used_By_Another_Channel()
|
|
{
|
|
await SeedFFmpegProfile();
|
|
await SeedChannel(1, "5");
|
|
await SeedChannel(2, "6");
|
|
|
|
Either<BaseError, ChannelViewModel> result =
|
|
await MakeHandler().Handle(MakeUpdate(1, number: "6"), CancellationToken.None);
|
|
|
|
BaseError error = LeftOf(result);
|
|
error.ShouldNotBeOfType<NotFoundError>();
|
|
error.Value.ShouldContain("unique");
|
|
}
|
|
|
|
[Test]
|
|
public async Task Should_Reject_ShowInEpg_When_Disabled()
|
|
{
|
|
await SeedFFmpegProfile();
|
|
await SeedChannel(1, "5");
|
|
|
|
Either<BaseError, ChannelViewModel> result =
|
|
await MakeHandler().Handle(
|
|
MakeUpdate(1, number: "5", isEnabled: false, showInEpg: true),
|
|
CancellationToken.None);
|
|
|
|
BaseError error = LeftOf(result);
|
|
error.ShouldNotBeOfType<NotFoundError>();
|
|
error.Value.ShouldContain("EPG");
|
|
}
|
|
|
|
[Test]
|
|
public async Task Should_Reject_Empty_Group()
|
|
{
|
|
await SeedFFmpegProfile();
|
|
await SeedChannel(1, "5");
|
|
|
|
Either<BaseError, ChannelViewModel> result =
|
|
await MakeHandler().Handle(MakeUpdate(1, number: "5", group: ""), CancellationToken.None);
|
|
|
|
BaseError error = LeftOf(result);
|
|
error.ShouldNotBeOfType<NotFoundError>();
|
|
error.Value.ShouldContain("group");
|
|
}
|
|
|
|
[Test]
|
|
public async Task Should_Reject_Nonexistent_FFmpegProfile()
|
|
{
|
|
await SeedFFmpegProfile();
|
|
await SeedChannel(1, "5");
|
|
|
|
Either<BaseError, ChannelViewModel> result =
|
|
await MakeHandler().Handle(
|
|
MakeUpdate(1, number: "5", ffmpegProfileId: 999),
|
|
CancellationToken.None);
|
|
|
|
BaseError error = LeftOf(result);
|
|
error.ShouldNotBeOfType<NotFoundError>();
|
|
error.Value.ShouldContain("FFmpegProfile");
|
|
}
|
|
|
|
private static BaseError LeftOf<TR>(Either<BaseError, TR> either) =>
|
|
either.Match(Left: e => e, Right: _ => throw new AssertionException("Expected a Left result"));
|
|
}
|