fix(458): reject duplicate names on playlist & playlist-group rename
PR Gates / CI image pin matches docker/ci (pull_request) Successful in 9s
PR Gates / Docs update reminder (pull_request) Successful in 12s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 13s
PR Gates / decisions lifecycle (pull_request) Successful in 17s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 1m28s
Build ErsatzTV Image / Functional E2E (curl contracts) (pull_request) Successful in 15m47s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 16m2s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 19m55s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
PR Gates / CI image pin matches docker/ci (pull_request) Successful in 9s
PR Gates / Docs update reminder (pull_request) Successful in 12s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 13s
PR Gates / decisions lifecycle (pull_request) Successful in 17s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 1m28s
Build ErsatzTV Image / Functional E2E (curl contracts) (pull_request) Successful in 15m47s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 16m2s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 19m55s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
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) <noreply@anthropic.com>
This commit is contained in:
@@ -35,7 +35,8 @@ public class RenamePlaylistGroupHandler(IDbContextFactory<TvContext> 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<Validation<BaseError, PlaylistGroup>> PlaylistGroupMustExist(
|
||||
TvContext dbContext,
|
||||
@@ -57,4 +58,23 @@ public class RenamePlaylistGroupHandler(IDbContextFactory<TvContext> dbContextFa
|
||||
private static Validation<BaseError, string> 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<Validation<BaseError, PlaylistGroup>> NameMustBeUnique(
|
||||
TvContext dbContext,
|
||||
RenamePlaylistGroup request,
|
||||
PlaylistGroup playlistGroup)
|
||||
{
|
||||
Option<PlaylistGroup> 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<BaseError, PlaylistGroup>(playlistGroup);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,12 +75,33 @@ public class ReplacePlaylistItemsHandler(IDbContextFactory<TvContext> 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<BaseError, string> 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<Validation<BaseError, Playlist>> PlaylistNameMustBeUnique(
|
||||
TvContext dbContext,
|
||||
Playlist playlist,
|
||||
ReplacePlaylistItems request)
|
||||
{
|
||||
Option<Playlist> 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<BaseError, Playlist>(playlist);
|
||||
}
|
||||
|
||||
private static Validation<BaseError, Playlist> PlaybackOrdersMustBeSupported(
|
||||
ReplacePlaylistItems request,
|
||||
Playlist playlist) =>
|
||||
|
||||
@@ -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<BaseError, PlaylistGroupViewModel> result =
|
||||
await handler.Handle(new RenamePlaylistGroup(6, "Movies"), CancellationToken.None);
|
||||
|
||||
BaseError error = LeftOf(result);
|
||||
error.ShouldNotBeOfType<NotFoundError>();
|
||||
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<BaseError, PlaylistGroupViewModel> 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<BaseError, PlaylistGroupViewModel> 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();
|
||||
|
||||
+61
@@ -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<PlaylistItem>()
|
||||
});
|
||||
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<BaseError, List<PlaylistItemViewModel>> 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<BaseError, List<PlaylistItemViewModel>> 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<BaseError, List<PlaylistItemViewModel>> result =
|
||||
await handler.Handle(Command("Movies"), CancellationToken.None);
|
||||
|
||||
result.IsRight.ShouldBeTrue();
|
||||
(await ReadNameAsync()).ShouldBe("Movies");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user