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,78 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Contracts.Responses.Favorites;
using Lumina.Domain.SharedKernel.Common.Enums.Favorites;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Contracts.Fixtures.Core.Responses.Favorites;
/// <summary>
/// Fixture class for the <see cref="FavoriteItemResponse"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class FavoriteItemResponseFixture
{
private readonly Faker _faker = new();
private readonly FavoriteMediaKind[] _supportedMediaKinds =
[
FavoriteMediaKind.MusicArtist,
FavoriteMediaKind.MusicAlbum,
FavoriteMediaKind.MusicTrack,
FavoriteMediaKind.Book
];
/// <summary>
/// Creates a random valid <see cref="FavoriteItemResponse"/>.
/// </summary>
/// <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="isAvailable">Optional. Whether the media item is available.</param>
/// <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="FavoriteItemResponse"/>.</returns>
public FavoriteItemResponse Create(
FavoriteMediaKind? mediaKind = null,
Guid? mediaItemId = null,
bool? isAvailable = null,
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 FavoriteItemResponse(
mediaKind ?? _faker.Random.ArrayElement(_supportedMediaKinds),
mediaItemId ?? _faker.Random.Guid(),
isAvailable ?? _faker.Random.Bool(),
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="FavoriteItemResponse"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="FavoriteItemResponse"/> instances.</returns>
public List<FavoriteItemResponse> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
}
@@ -0,0 +1,58 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Contracts.Responses.Favorites;
using Lumina.Domain.SharedKernel.Common.Enums.Favorites;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Contracts.Fixtures.Core.Responses.Favorites;
/// <summary>
/// Fixture class for the <see cref="FavoriteResponse"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class FavoriteResponseFixture
{
private readonly Faker _faker = new();
private readonly FavoriteMediaKind[] _supportedMediaKinds =
[
FavoriteMediaKind.MusicArtist,
FavoriteMediaKind.MusicAlbum,
FavoriteMediaKind.MusicTrack,
FavoriteMediaKind.Book
];
/// <summary>
/// Creates a random valid <see cref="FavoriteResponse"/>.
/// </summary>
/// <param name="id">Optional. The Id of 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="favoritedOnUtc">Optional. The date and time when the media item was favorited. Defaults to a moment in the past.</param>
/// <returns>The created <see cref="FavoriteResponse"/>.</returns>
public FavoriteResponse Create(
Guid? id = null,
FavoriteMediaKind? mediaKind = null,
Guid? mediaItemId = null,
DateTime? favoritedOnUtc = null)
{
return new FavoriteResponse(
id ?? _faker.Random.Guid(),
mediaKind ?? _faker.Random.ArrayElement(_supportedMediaKinds),
mediaItemId ?? _faker.Random.Guid(),
favoritedOnUtc ?? DateTime.UtcNow.AddDays(-_faker.Random.Int(1, 30)).AddMinutes(-_faker.Random.Int(0, 59)));
}
/// <summary>
/// Creates a list of <see cref="FavoriteResponse"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="FavoriteResponse"/> instances.</returns>
public List<FavoriteResponse> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
}
@@ -0,0 +1,51 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Contracts.Responses.Favorites;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Contracts.Fixtures.Core.Responses.Favorites;
/// <summary>
/// Fixture class for the <see cref="FavoriteStatusResponse"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class FavoriteStatusResponseFixture
{
private const int DEFAULT_FAVORITED_COUNT = 3;
private readonly Faker _faker = new();
/// <summary>
/// Creates a random valid <see cref="FavoriteStatusResponse"/>.
/// </summary>
/// <param name="favoritedMediaItemIds">Optional. The Ids of the media items that are favorites. Defaults to a bounded list of random Ids.</param>
/// <returns>The created <see cref="FavoriteStatusResponse"/>.</returns>
public FavoriteStatusResponse Create(IReadOnlyList<Guid>? favoritedMediaItemIds = null)
{
return new FavoriteStatusResponse(
favoritedMediaItemIds ?? CreateRandomMediaItemIds());
}
/// <summary>
/// Creates a list of <see cref="FavoriteStatusResponse"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="FavoriteStatusResponse"/> instances.</returns>
public List<FavoriteStatusResponse> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
/// <summary>
/// Creates a bounded list of random media item Ids.
/// </summary>
/// <returns>The created list of media item Ids.</returns>
private List<Guid> CreateRandomMediaItemIds()
{
return [.. Enumerable.Range(0, DEFAULT_FAVORITED_COUNT).Select(_ => _faker.Random.Guid())];
}
}
@@ -0,0 +1,77 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Contracts.Responses.Playlists;
using Lumina.Domain.SharedKernel.Common.Enums.Playlists;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Contracts.Fixtures.Core.Responses.Playlists;
/// <summary>
/// Fixture class for the <see cref="PlaylistEntryResponse"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class PlaylistEntryResponseFixture
{
private readonly Faker _faker = new();
/// <summary>
/// Creates a random valid <see cref="PlaylistEntryResponse"/>.
/// </summary>
/// <param name="id">Optional. The Id of the entry.</param>
/// <param name="mediaItemId">Optional. The Id of the media item referenced by the entry.</param>
/// <param name="mediaItemType">Optional. The kind of media item referenced by the entry.</param>
/// <param name="position">Optional. The zero based position of the entry in the playlist.</param>
/// <param name="libraryId">Optional. The Id of the media library that contains the media item.</param>
/// <param name="collectionId">Optional. The Id 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="mediaItemTitle">Optional. The title of the media item.</param>
/// <param name="collectionTitle">Optional. The title of the collection that contains the media item.</param>
/// <param name="creatorName">Optional. The name of the creator of the media item.</param>
/// <param name="durationInSeconds">Optional. The duration of the media item in seconds.</param>
/// <param name="coverPath">Optional. The path of the cover art of the collection.</param>
/// <param name="includeCoverPath">Whether the cover path should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="PlaylistEntryResponse"/>.</returns>
public PlaylistEntryResponse Create(
Guid? id = null,
Guid? mediaItemId = null,
PlaylistMediaItemType? mediaItemType = null,
int? position = null,
Guid? libraryId = null,
Guid? collectionId = null,
Guid? creatorId = null,
string? mediaItemTitle = null,
string? collectionTitle = null,
string? creatorName = null,
int? durationInSeconds = null,
string? coverPath = null,
bool includeCoverPath = true)
{
return new PlaylistEntryResponse(
id ?? _faker.Random.Guid(),
mediaItemId ?? _faker.Random.Guid(),
mediaItemType ?? _faker.PickRandom<PlaylistMediaItemType>(),
position ?? _faker.Random.Int(0, 1_000),
libraryId ?? _faker.Random.Guid(),
collectionId ?? _faker.Random.Guid(),
creatorId ?? _faker.Random.Guid(),
mediaItemTitle ?? _faker.Lorem.Sentence(3),
collectionTitle ?? _faker.Lorem.Sentence(2),
creatorName ?? _faker.Name.FullName(),
durationInSeconds ?? _faker.Random.Int(1, 3_600),
includeCoverPath ? (coverPath ?? $"/covers/{_faker.Random.Guid()}.jpg") : null);
}
/// <summary>
/// Creates a list of <see cref="PlaylistEntryResponse"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="PlaylistEntryResponse"/> instances.</returns>
public List<PlaylistEntryResponse> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
}
@@ -0,0 +1,83 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Contracts.Fixtures.Responses.Common;
using Lumina.Contracts.Responses.Common;
using Lumina.Contracts.Responses.Playlists;
using Lumina.Domain.SharedKernel.Common.Enums.Playlists;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Contracts.Fixtures.Core.Responses.Playlists;
/// <summary>
/// Fixture class for the <see cref="PlaylistResponse"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class PlaylistResponseFixture
{
private const int DEFAULT_ENTRY_COUNT = 3;
private readonly Faker _faker = new();
private readonly PlaylistEntryResponseFixture _playlistEntryResponseFixture = new();
private readonly PaginatedResponseFixture<PlaylistEntryResponse> _paginatedResponseFixture = new();
/// <summary>
/// Creates a random valid <see cref="PlaylistResponse"/>.
/// </summary>
/// <param name="id">Optional. The Id of the playlist.</param>
/// <param name="name">Optional. The name of the playlist.</param>
/// <param name="description">Optional. The description of the playlist.</param>
/// <param name="type">Optional. The kind of media the playlist holds.</param>
/// <param name="entries">Optional. The page of entries of the playlist.</param>
/// <param name="createdOnUtc">Optional. The date and time when the playlist was created.</param>
/// <param name="updatedOnUtc">Optional. The date and time when the playlist was last updated.</param>
/// <param name="includeDescription">Whether the description should be included, or forced to <see langword="null"/>.</param>
/// <param name="includeUpdatedOnUtc">Whether the update time should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="PlaylistResponse"/>.</returns>
public PlaylistResponse Create(
Guid? id = null,
string? name = null,
string? description = null,
PlaylistType? type = null,
PaginatedResponse<PlaylistEntryResponse>? entries = null,
DateTime? createdOnUtc = null,
DateTime? updatedOnUtc = null,
bool includeDescription = true,
bool includeUpdatedOnUtc = true)
{
return new PlaylistResponse(
id ?? _faker.Random.Guid(),
name ?? _faker.Lorem.Sentence(3),
includeDescription ? (description ?? _faker.Lorem.Sentence()) : null,
type ?? PlaylistType.Music,
entries ?? CreateEntriesPage(),
createdOnUtc ?? DateTime.UtcNow.AddDays(-_faker.Random.Int(1, 30)),
includeUpdatedOnUtc ? (updatedOnUtc ?? DateTime.UtcNow.AddMinutes(-_faker.Random.Int(1, 1_440))) : null);
}
/// <summary>
/// Creates a list of <see cref="PlaylistResponse"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="PlaylistResponse"/> instances.</returns>
public List<PlaylistResponse> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
/// <summary>
/// Creates a page of random playlist entries.
/// </summary>
/// <returns>The created page of playlist entries.</returns>
private PaginatedResponse<PlaylistEntryResponse> CreateEntriesPage()
{
List<PlaylistEntryResponse> entries = [];
for (int index = 0; index < DEFAULT_ENTRY_COUNT; index++)
entries.Add(_playlistEntryResponseFixture.Create(position: index));
return _paginatedResponseFixture.Create(data: entries, currentPage: 1, perPage: DEFAULT_ENTRY_COUNT, count: DEFAULT_ENTRY_COUNT, numberOfPages: 1);
}
}