Implements tests for commit 525ac1348d

Covers the favorites feature and the playlist and favorite pagination changes with unit, integration and security tests, and fixtures for the new request, response, DTO and entity types.

Also fixes the issues found while covering the commit: declares FavoriteMediaKind.None as the default value and rejects it in every favorite operation; queues the favorite added domain event only after the persisted favorite is read back; and preserves the private, no-store cache header on view pages after the antiforgery and session middlewares overwrite it.
This commit is contained in:
BinaryAlley committed 2026-10-29 10:13:57 +02:00
1 parent 468f1846f1
commit 394f6b510c
135 files changed
+19167 -13

No files matched your search

@@ -0,0 +1,61 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Common.DTO.Favorites;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.Fixtures.Common.DTO.Favorites;
/// <summary>
/// Fixture class for the <see cref="FavoriteItemDisplayDto"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class FavoriteItemDisplayDtoFixture
{
private readonly Faker _faker = new();
/// <summary>
/// Creates a random valid <see cref="FavoriteItemDisplayDto"/>.
/// </summary>
/// <param name="libraryId">Optional. The Id of the media library that contains the media item.</param>
/// <param name="title">Optional. The title of the media item.</param>
/// <param name="collectionId">Optional. The Id of the collection that contains the media item.</param>
/// <param name="collectionTitle">Optional. The title of the collection that contains the media item.</param>
/// <param name="creatorId">Optional. The Id of the creator of the media item.</param>
/// <param name="creatorName">Optional. The name of the creator of the media item.</param>
/// <param name="coverPath">Optional. The path of the cover art of the media item.</param>
/// <param name="includeCoverPath">Whether the cover path should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="FavoriteItemDisplayDto"/>.</returns>
public FavoriteItemDisplayDto Create(
Guid? libraryId = null,
string? title = null,
Guid? collectionId = null,
string? collectionTitle = null,
Guid? creatorId = null,
string? creatorName = null,
string? coverPath = null,
bool includeCoverPath = true)
{
return new FavoriteItemDisplayDto(
libraryId ?? _faker.Random.Guid(),
title ?? _faker.Lorem.Sentence(3),
collectionId ?? _faker.Random.Guid(),
collectionTitle ?? _faker.Lorem.Sentence(3),
creatorId ?? _faker.Random.Guid(),
creatorName ?? _faker.Name.FullName(),
includeCoverPath ? (coverPath ?? $"/covers/{_faker.Random.Guid()}.jpg") : null);
}
/// <summary>
/// Creates a list of <see cref="FavoriteItemDisplayDto"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="FavoriteItemDisplayDto"/> instances.</returns>
public List<FavoriteItemDisplayDto> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
}
@@ -0,0 +1,47 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Favorites;
using Lumina.Application.Common.DTO.Favorites;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Favorites;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.Fixtures.Common.DTO.Favorites;
/// <summary>
/// Fixture class for the <see cref="FavoriteItemDto"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class FavoriteItemDtoFixture
{
private readonly FavoriteEntityFixture _favoriteEntityFixture = new();
private readonly FavoriteItemDisplayDtoFixture _favoriteItemDisplayDtoFixture = new();
/// <summary>
/// Creates a random valid <see cref="FavoriteItemDto"/>.
/// </summary>
/// <param name="favorite">Optional. The stored favorite.</param>
/// <param name="display">Optional. The display data of the referenced media item.</param>
/// <param name="includeDisplay">Whether the display data should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="FavoriteItemDto"/>.</returns>
public FavoriteItemDto Create(
FavoriteEntity? favorite = null,
FavoriteItemDisplayDto? display = null,
bool includeDisplay = true)
{
return new FavoriteItemDto(
favorite ?? _favoriteEntityFixture.Create(),
includeDisplay ? (display ?? _favoriteItemDisplayDtoFixture.Create()) : null);
}
/// <summary>
/// Creates a list of <see cref="FavoriteItemDto"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="FavoriteItemDto"/> instances.</returns>
public List<FavoriteItemDto> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
}
@@ -0,0 +1,38 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Playlists;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.DTO.Playlists;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Playlists;
using Lumina.Application.Fixtures.Common.DTO.Pagination;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.Fixtures.Common.DTO.Playlists;
/// <summary>
/// Fixture class for the <see cref="PlaylistDetailsDto"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class PlaylistDetailsDtoFixture
{
private readonly PlaylistEntityFixture _playlistEntityFixture = new();
private readonly PlaylistEntryDtoFixture _playlistEntryDtoFixture = new();
private readonly PaginatedResultDtoFixture<PlaylistEntryDto> _paginatedResultDtoFixture = new();
/// <summary>
/// Creates a random valid <see cref="PlaylistDetailsDto"/>.
/// </summary>
/// <param name="playlist">Optional. The stored playlist.</param>
/// <param name="entries">Optional. The requested page of entries. Defaults to a page of random entries.</param>
/// <returns>The created <see cref="PlaylistDetailsDto"/>.</returns>
public PlaylistDetailsDto Create(
PlaylistEntity? playlist = null,
PaginatedResultDto<PlaylistEntryDto>? entries = null)
{
List<PlaylistEntryDto> entryDtos = _playlistEntryDtoFixture.CreateMany();
return new PlaylistDetailsDto(
playlist ?? _playlistEntityFixture.Create(),
entries ?? _paginatedResultDtoFixture.Create(data: entryDtos, count: entryDtos.Count));
}
}
@@ -0,0 +1,69 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Common.DTO.Playlists;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.Fixtures.Common.DTO.Playlists;
/// <summary>
/// Fixture class for the <see cref="PlaylistEntryDisplayDto"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class PlaylistEntryDisplayDtoFixture
{
private const int MAXIMUM_DURATION_SECONDS = 36_000;
private readonly Faker _faker = new();
/// <summary>
/// Creates a random valid <see cref="PlaylistEntryDisplayDto"/>.
/// </summary>
/// <param name="trackId">Optional. The Id of the track.</param>
/// <param name="trackTitle">Optional. The title of the track.</param>
/// <param name="durationInSeconds">Optional. The duration of the track, in seconds. Defaults to a bounded random duration.</param>
/// <param name="libraryId">Optional. The Id of the media library that contains the track.</param>
/// <param name="albumId">Optional. The Id of the album that contains the track.</param>
/// <param name="albumTitle">Optional. The title of the album that contains the track.</param>
/// <param name="artistId">Optional. The Id of the artist that produced the track.</param>
/// <param name="artistName">Optional. The name of the artist that produced the track.</param>
/// <param name="coverPath">Optional. The path of the cover art of the album.</param>
/// <param name="includeCoverPath">Whether the cover path should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="PlaylistEntryDisplayDto"/>.</returns>
public PlaylistEntryDisplayDto Create(
Guid? trackId = null,
string? trackTitle = null,
int? durationInSeconds = null,
Guid? libraryId = null,
Guid? albumId = null,
string? albumTitle = null,
Guid? artistId = null,
string? artistName = null,
string? coverPath = null,
bool includeCoverPath = true)
{
return new PlaylistEntryDisplayDto(
trackId ?? _faker.Random.Guid(),
trackTitle ?? _faker.Lorem.Sentence(3),
durationInSeconds ?? _faker.Random.Int(0, MAXIMUM_DURATION_SECONDS),
libraryId ?? _faker.Random.Guid(),
albumId ?? _faker.Random.Guid(),
albumTitle ?? _faker.Lorem.Sentence(3),
artistId ?? _faker.Random.Guid(),
artistName ?? _faker.Name.FullName(),
includeCoverPath ? (coverPath ?? $"/covers/{_faker.Random.Guid()}.jpg") : null);
}
/// <summary>
/// Creates a list of <see cref="PlaylistEntryDisplayDto"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="PlaylistEntryDisplayDto"/> instances.</returns>
public List<PlaylistEntryDisplayDto> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
}
@@ -0,0 +1,52 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Common.DataAccess.Entities.Playlists;
using Lumina.Application.Common.DTO.Playlists;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Playlists;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.Fixtures.Common.DTO.Playlists;
/// <summary>
/// Fixture class for the <see cref="PlaylistEntryDto"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class PlaylistEntryDtoFixture
{
private readonly Faker _faker = new();
private readonly PlaylistEntryEntityFixture _playlistEntryEntityFixture = new();
private readonly PlaylistEntryDisplayDtoFixture _playlistEntryDisplayDtoFixture = new();
/// <summary>
/// Creates a random valid <see cref="PlaylistEntryDto"/>.
/// </summary>
/// <param name="entry">Optional. The stored playlist entry.</param>
/// <param name="display">Optional. The display data of the referenced media item.</param>
/// <param name="displayPosition">Optional. The dense zero based position shown to the client.</param>
/// <param name="includeDisplay">Whether the display data should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="PlaylistEntryDto"/>.</returns>
public PlaylistEntryDto Create(
PlaylistEntryEntity? entry = null,
PlaylistEntryDisplayDto? display = null,
int? displayPosition = null,
bool includeDisplay = true)
{
return new PlaylistEntryDto(
entry ?? _playlistEntryEntityFixture.Create(),
includeDisplay ? (display ?? _playlistEntryDisplayDtoFixture.Create()) : null,
displayPosition ?? _faker.Random.Int(0, 1_000));
}
/// <summary>
/// Creates a list of <see cref="PlaylistEntryDto"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="PlaylistEntryDto"/> instances.</returns>
public List<PlaylistEntryDto> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
}
@@ -0,0 +1,78 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Common.DataAccess.Entities.Favorites;
using Lumina.Domain.SharedKernel.Common.Enums.Favorites;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.Favorites;
/// <summary>
/// Fixture class for the <see cref="FavoriteEntity"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class FavoriteEntityFixture
{
private readonly Faker _faker = new();
private readonly FavoriteMediaKind[] _supportedMediaKinds =
[
FavoriteMediaKind.MusicArtist,
FavoriteMediaKind.MusicAlbum,
FavoriteMediaKind.MusicTrack,
FavoriteMediaKind.Book
];
/// <summary>
/// Creates a random valid <see cref="FavoriteEntity"/>.
/// </summary>
/// <param name="id">Optional. The Id of the favorite.</param>
/// <param name="userId">Optional. The Id of the user that owns the favorite.</param>
/// <param name="mediaKind">Optional. The kind of the favorited media item. Defaults to a random supported kind.</param>
/// <param name="mediaItemId">Optional. The Id of the favorited media item.</param>
/// <param name="createdOnUtc">Optional. The date and time when the favorite was created. Defaults to a moment in the past.</param>
/// <param name="createdBy">Optional. The Id of the user that created the row.</param>
/// <param name="updatedOnUtc">Optional. The date and time when the favorite was last updated. Defaults to a moment at or after the creation date.</param>
/// <param name="updatedBy">Optional. The Id of the user that last updated the row.</param>
/// <param name="includeUpdatedOnUtc">Whether the update time should be included, or forced to <see langword="null"/>.</param>
/// <param name="includeUpdatedBy">Whether the updating user Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="FavoriteEntity"/>.</returns>
public FavoriteEntity Create(
Guid? id = null,
Guid? userId = null,
FavoriteMediaKind? mediaKind = null,
Guid? mediaItemId = null,
DateTime? createdOnUtc = null,
Guid? createdBy = null,
DateTime? updatedOnUtc = null,
Guid? updatedBy = null,
bool includeUpdatedOnUtc = true,
bool includeUpdatedBy = true)
{
DateTime resolvedCreatedOnUtc = createdOnUtc ?? DateTime.UtcNow.AddDays(-_faker.Random.Int(1, 30)).AddMinutes(-_faker.Random.Int(0, 59));
return new FavoriteEntity
{
Id = id ?? _faker.Random.Guid(),
UserId = userId ?? _faker.Random.Guid(),
MediaKind = mediaKind ?? _faker.Random.ArrayElement(_supportedMediaKinds),
MediaItemId = mediaItemId ?? _faker.Random.Guid(),
CreatedOnUtc = resolvedCreatedOnUtc,
CreatedBy = createdBy ?? _faker.Random.Guid(),
UpdatedOnUtc = includeUpdatedOnUtc ? (updatedOnUtc ?? resolvedCreatedOnUtc.AddMinutes(_faker.Random.Int(0, 120))) : null,
UpdatedBy = includeUpdatedBy ? (updatedBy ?? _faker.Random.Guid()) : null
};
}
/// <summary>
/// Creates a list of <see cref="FavoriteEntity"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="FavoriteEntity"/> instances.</returns>
public List<FavoriteEntity> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
}