From d12da836f24be52ed624f91f79cced99956c65c8 Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 23 Jul 2026 19:43:58 +0200 Subject: [PATCH] fix(458): reject duplicate names on playlist & playlist-group rename ReplacePlaylistItemsHandler (playlist rename) and RenamePlaylistGroupHandler (group rename) validated name non-empty/length but not uniqueness, so a rename could produce two identically-named siblings even though the create paths reject duplicates. Mirror the existing ReplaceBlockItems/ReplaceTemplate sibling pattern: an async BindT step scoped to the group (playlist) or global (group, matching PlaylistGroup.Name's unique index), self-excluded so a no-op rename to its own name still succeeds, returning BaseError -> 422 like create. For groups this also upgrades a would-be raw DbUpdateException (create-group has no explicit guard, only the DB index) into a clean 422. Closes #458. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Commands/RenamePlaylistGroupHandler.cs | 22 ++++++- .../Commands/ReplacePlaylistItemsHandler.cs | 23 ++++++- .../PlaylistGroupHandlerTests.cs | 51 ++++++++++++++++ ...PlaylistItemsHandlerNameValidationTests.cs | 61 +++++++++++++++++++ 4 files changed, 155 insertions(+), 2 deletions(-) diff --git a/ErsatzTV.Application/MediaCollections/Commands/RenamePlaylistGroupHandler.cs b/ErsatzTV.Application/MediaCollections/Commands/RenamePlaylistGroupHandler.cs index a3b914015..a4f5f145c 100644 --- a/ErsatzTV.Application/MediaCollections/Commands/RenamePlaylistGroupHandler.cs +++ b/ErsatzTV.Application/MediaCollections/Commands/RenamePlaylistGroupHandler.cs @@ -35,7 +35,8 @@ public class RenamePlaylistGroupHandler(IDbContextFactory dbContextFa CancellationToken cancellationToken) => PlaylistGroupMustExist(dbContext, request, cancellationToken) .BindT(PlaylistGroupMustNotBeSystem) - .BindT(playlistGroup => ValidateName(request).Map(_ => playlistGroup)); + .BindT(playlistGroup => ValidateName(request).Map(_ => playlistGroup)) + .BindT(playlistGroup => NameMustBeUnique(dbContext, request, playlistGroup)); private static Task> PlaylistGroupMustExist( TvContext dbContext, @@ -57,4 +58,23 @@ public class RenamePlaylistGroupHandler(IDbContextFactory dbContextFa private static Validation ValidateName(RenamePlaylistGroup request) => request.NotEmpty(x => x.Name) .Bind(_ => request.NotLongerThan(50)(x => x.Name)); + + // Issue #458: PlaylistGroup.Name carries a global unique index, but CreatePlaylistGroupHandler + // has no explicit duplicate guard (it relies on the DB constraint). Add one on rename so a + // collision surfaces as a clean 422 rather than a raw DbUpdateException. Excludes the group + // itself so a no-op rename to its own name still succeeds. + private static async Task> NameMustBeUnique( + TvContext dbContext, + RenamePlaylistGroup request, + PlaylistGroup playlistGroup) + { + Option maybeExisting = await dbContext.PlaylistGroups + .AsNoTracking() + .FirstOrDefaultAsync(pg => pg.Id != request.PlaylistGroupId && pg.Name == request.Name) + .Map(Optional); + + return maybeExisting.IsSome + ? BaseError.New($"A playlist group named \"{request.Name}\" already exists") + : Success(playlistGroup); + } } diff --git a/ErsatzTV.Application/MediaCollections/Commands/ReplacePlaylistItemsHandler.cs b/ErsatzTV.Application/MediaCollections/Commands/ReplacePlaylistItemsHandler.cs index d5f60f9e0..3764722b4 100644 --- a/ErsatzTV.Application/MediaCollections/Commands/ReplacePlaylistItemsHandler.cs +++ b/ErsatzTV.Application/MediaCollections/Commands/ReplacePlaylistItemsHandler.cs @@ -75,12 +75,33 @@ public class ReplacePlaylistItemsHandler(IDbContextFactory dbContextF PlaylistMustExist(dbContext, request.PlaylistId, cancellationToken) .BindT(playlist => CollectionTypesMustBeValid(request, playlist)) .BindT(playlist => PlaybackOrdersMustBeSupported(request, playlist)) - .BindT(playlist => ValidateName(request).Map(_ => playlist)); + .BindT(playlist => ValidateName(request).Map(_ => playlist)) + .BindT(playlist => PlaylistNameMustBeUnique(dbContext, playlist, request)); private static Validation ValidateName(ReplacePlaylistItems request) => request.NotEmpty(x => x.Name) .Bind(_ => request.NotLongerThan(50)(x => x.Name)); + // Issue #458: mirror CreatePlaylistHandler's duplicate-name guard on rename. Uniqueness is scoped + // to the loaded playlist's group (rename cannot move groups) and excludes the playlist itself, so + // a no-op rename to its own name still succeeds. Backstopped by the (PlaylistGroupId, Name) unique + // index; this pre-check turns the common collision into a clean 422 instead of a DbUpdateException. + private static async Task> PlaylistNameMustBeUnique( + TvContext dbContext, + Playlist playlist, + ReplacePlaylistItems request) + { + Option maybeExisting = await dbContext.Playlists + .AsNoTracking() + .FirstOrDefaultAsync(p => + p.Id != request.PlaylistId && p.PlaylistGroupId == playlist.PlaylistGroupId && p.Name == request.Name) + .Map(Optional); + + return maybeExisting.IsSome + ? BaseError.New($"A playlist named \"{request.Name}\" already exists in that playlist group") + : Success(playlist); + } + private static Validation PlaybackOrdersMustBeSupported( ReplacePlaylistItems request, Playlist playlist) => diff --git a/ErsatzTV.Tests/Application/MediaCollections/PlaylistGroupHandlerTests.cs b/ErsatzTV.Tests/Application/MediaCollections/PlaylistGroupHandlerTests.cs index d32acf53a..7ca8dce20 100644 --- a/ErsatzTV.Tests/Application/MediaCollections/PlaylistGroupHandlerTests.cs +++ b/ErsatzTV.Tests/Application/MediaCollections/PlaylistGroupHandlerTests.cs @@ -57,6 +57,57 @@ public class PlaylistGroupHandlerTests : MediaCollectionHandlerTestBase result.Match(Left: _ => "", Right: vm => vm.Name).ShouldBe("Family"); } + // Issue #458: group rename must reject a name already used by another playlist group + // (PlaylistGroup.Name is globally unique). Create-group relies on the DB unique index; the + // explicit check here yields a clean 422 instead of a raw DbUpdateException. + [Test] + public async Task Rename_Should_Reject_Duplicate_Group_Name() + { + await SeedPlaylistGroup(6, "Kids", isSystem: false); + await SeedPlaylistGroup(7, "Movies", isSystem: false); + + var handler = new RenamePlaylistGroupHandler(Db.Factory); + + Either result = + await handler.Handle(new RenamePlaylistGroup(6, "Movies"), CancellationToken.None); + + BaseError error = LeftOf(result); + error.ShouldNotBeOfType(); + error.Value.ShouldBe("A playlist group named \"Movies\" already exists"); + } + + // Uniqueness is not scoped to user groups: renaming onto a SYSTEM group's name is also rejected + // (a user must not be able to collide with e.g. the generated "Channel Lineups" group). + [Test] + public async Task Rename_Should_Reject_Name_Taken_By_System_Group() + { + await SeedPlaylistGroup(6, "Kids", isSystem: false); + await SeedPlaylistGroup(7, "Channel Lineups", isSystem: true); + + var handler = new RenamePlaylistGroupHandler(Db.Factory); + + Either result = + await handler.Handle(new RenamePlaylistGroup(6, "Channel Lineups"), CancellationToken.None); + + BaseError error = LeftOf(result); + error.Value.ShouldBe("A playlist group named \"Channel Lineups\" already exists"); + } + + // A no-op rename (same name) must still succeed: the sibling check excludes the row itself. + [Test] + public async Task Rename_To_Its_Own_Name_Should_Succeed() + { + await SeedPlaylistGroup(6, "Kids", isSystem: false); + + var handler = new RenamePlaylistGroupHandler(Db.Factory); + + Either result = + await handler.Handle(new RenamePlaylistGroup(6, "Kids"), CancellationToken.None); + + result.IsRight.ShouldBeTrue(); + result.Match(Left: _ => "", Right: vm => vm.Name).ShouldBe("Kids"); + } + private async Task SeedPlaylistGroup(int id, string name, bool isSystem) { await using TvContext context = Db.CreateContext(); diff --git a/ErsatzTV.Tests/Application/MediaCollections/ReplacePlaylistItemsHandlerNameValidationTests.cs b/ErsatzTV.Tests/Application/MediaCollections/ReplacePlaylistItemsHandlerNameValidationTests.cs index 829782076..9690fc1cd 100644 --- a/ErsatzTV.Tests/Application/MediaCollections/ReplacePlaylistItemsHandlerNameValidationTests.cs +++ b/ErsatzTV.Tests/Application/MediaCollections/ReplacePlaylistItemsHandlerNameValidationTests.cs @@ -45,6 +45,22 @@ public class ReplacePlaylistItemsHandlerNameValidationTests await ctx.SaveChangesAsync(); } + private async Task SeedSiblingAsync(int id, int playlistGroupId, string name) + { + await using TvContext ctx = _db.CreateContext(); + ctx.Playlists.Add( + new Playlist + { + Id = id, + PlaylistGroupId = playlistGroupId, + Name = name, + IsSystem = false, + Version = 1, + Items = new List() + }); + await ctx.SaveChangesAsync(); + } + private static ReplacePlaylistItems Command(string name) => new( 1, @@ -117,4 +133,49 @@ public class ReplacePlaylistItemsHandlerNameValidationTests result.IsRight.ShouldBeTrue(); (await ReadNameAsync()).ShouldBe(maxLength); } + + // Issue #458: rename must reject a name already used by a SIBLING playlist in the same group, + // mirroring CreatePlaylistHandler's duplicate-name check. + [Test] + public async Task Duplicate_Name_In_Same_Group_Should_Be_Rejected_And_Not_Mutate() + { + await SeedPlaylistAsync(); + await SeedSiblingAsync(2, playlistGroupId: 1, name: "Movies"); + var handler = new ReplacePlaylistItemsHandler(_db.Factory); + + Either> result = + await handler.Handle(Command("Movies"), CancellationToken.None); + + LeftOrNull(result).ShouldNotBeNull(); + (await ReadNameAsync()).ShouldBe("Kids"); + } + + // A no-op rename (same name) must still succeed: the sibling check excludes the row itself. + [Test] + public async Task Rename_To_Its_Own_Name_Should_Succeed() + { + await SeedPlaylistAsync(); + var handler = new ReplacePlaylistItemsHandler(_db.Factory); + + Either> result = + await handler.Handle(Command("Kids"), CancellationToken.None); + + result.IsRight.ShouldBeTrue(); + (await ReadNameAsync()).ShouldBe("Kids"); + } + + // Uniqueness is scoped to the playlist group: the same name in a DIFFERENT group is allowed. + [Test] + public async Task Duplicate_Name_In_Different_Group_Should_Be_Allowed() + { + await SeedPlaylistAsync(); + await SeedSiblingAsync(2, playlistGroupId: 2, name: "Movies"); + var handler = new ReplacePlaylistItemsHandler(_db.Factory); + + Either> result = + await handler.Handle(Command("Movies"), CancellationToken.None); + + result.IsRight.ShouldBeTrue(); + (await ReadNameAsync()).ShouldBe("Movies"); + } }