mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 03:11:10 +00:00
Implements tests for commit 00d7eff777
Covers genre consumption and delete-item-history across application, contracts, API, and Web. Changes the new string-bound request pipelines to return validation problems instead of 500 for malformed values.
This commit is contained in:
1 parent
968061e89a
commit
1afb078efc
91 files changed
+14499
-50
No files matched your search
+6
-8
@@ -1,7 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
@@ -11,13 +9,13 @@ namespace Lumina.Application.Core.MediaConsumption.Commands.DeleteConsumptionIte
|
||||
/// Command for deleting every consumption record of a media item of a user.
|
||||
/// </summary>
|
||||
/// <param name="TargetUserId">The Id of the user the records must belong to, or <see langword="null"/> for the current user.</param>
|
||||
/// <param name="MediaKind">The kind of the consumed media item.</param>
|
||||
/// <param name="MediaItemId">The Id of the consumed media item.</param>
|
||||
/// <param name="LibraryId">The Id of the media library that contains the media item.</param>
|
||||
/// <param name="MediaKind">The name of the kind of the consumed media item, taken from the route.</param>
|
||||
/// <param name="MediaItemId">The Id of the consumed media item, taken from the route.</param>
|
||||
/// <param name="LibraryId">The Id of the media library that contains the media item, taken from the route.</param>
|
||||
[DebuggerDisplay("TargetUserId: {TargetUserId}; MediaKind: {MediaKind}; MediaItemId: {MediaItemId}")]
|
||||
public record DeleteConsumptionItemHistoryCommand(
|
||||
string? TargetUserId,
|
||||
MediaKind MediaKind,
|
||||
Guid MediaItemId,
|
||||
Guid LibraryId
|
||||
string? MediaKind,
|
||||
string? MediaItemId,
|
||||
string? LibraryId
|
||||
) : ICommand;
|
||||
+7
-1
@@ -6,6 +6,7 @@ using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization.Policies.ConsumptionActivity;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
@@ -69,10 +70,15 @@ public class DeleteConsumptionItemHistoryCommandHandler : ICommandHandler<Delete
|
||||
if (!canAccessTarget)
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
// The validator guarantees that the route values are well formed before this point.
|
||||
MediaKind mediaKind = Enum.Parse<MediaKind>(command.MediaKind!, ignoreCase: true);
|
||||
Guid mediaItemId = Guid.Parse(command.MediaItemId!);
|
||||
Guid libraryId = Guid.Parse(command.LibraryId!);
|
||||
|
||||
// The deletion covers every recorded session of the item in its library, not only the sessions of a date range, so the row
|
||||
// disappears from the history consistently with clearing the whole history.
|
||||
Result<Deleted> deleteResult = await _unitOfWork.MediaConsumptionRecordRepository
|
||||
.DeleteAllByMediaItemIdForUserAsync(targetUserId, command.MediaKind, command.MediaItemId, command.LibraryId, cancellationToken).ConfigureAwait(false);
|
||||
.DeleteAllByMediaItemIdForUserAsync(targetUserId, mediaKind, mediaItemId, libraryId, cancellationToken).ConfigureAwait(false);
|
||||
if (deleteResult.IsFailure)
|
||||
return deleteResult.Errors;
|
||||
|
||||
|
||||
+20
-4
@@ -24,20 +24,36 @@ public class DeleteConsumptionItemHistoryCommandValidator : AbstractValidator<De
|
||||
(Guid.TryParse(targetUserId, out Guid parsedTargetUserId) && parsedTargetUserId != Guid.Empty))
|
||||
.WithError(Errors.MediaConsumption.TargetUserIdIsInvalid);
|
||||
|
||||
// Only the member names of the media kind enumeration are accepted, so a numeric value that happens to parse as a kind is rejected.
|
||||
RuleFor(command => command.MediaKind)
|
||||
.IsInEnum()
|
||||
.NotEmpty()
|
||||
.WithError(Errors.MediaConsumption.UnsupportedMediaKind);
|
||||
|
||||
RuleFor(command => command.MediaKind)
|
||||
.Must(mediaKind => mediaKind != MediaKind.None)
|
||||
.IsInEnum<DeleteConsumptionItemHistoryCommand, MediaKind>()
|
||||
.WithError(Errors.MediaConsumption.UnsupportedMediaKind);
|
||||
|
||||
RuleFor(command => command.MediaKind)
|
||||
.Must(mediaKind => mediaKind is null ||
|
||||
!string.Equals(mediaKind, nameof(MediaKind.None), StringComparison.OrdinalIgnoreCase))
|
||||
.WithError(Errors.MediaConsumption.UnsupportedMediaKind);
|
||||
|
||||
RuleFor(command => command.MediaItemId)
|
||||
.Must(mediaItemId => mediaItemId != Guid.Empty)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.MediaConsumption.MediaItemIdCannotBeEmpty);
|
||||
|
||||
RuleFor(command => command.MediaItemId)
|
||||
.Must(mediaItemId => Guid.TryParse(mediaItemId, out Guid parsedMediaItemId) && parsedMediaItemId != Guid.Empty)
|
||||
.When(command => command.MediaItemId is not null && command.MediaItemId.Length > 0)
|
||||
.WithError(Errors.MediaConsumption.MediaItemIdCannotBeEmpty);
|
||||
|
||||
RuleFor(command => command.LibraryId)
|
||||
.Must(libraryId => libraryId != Guid.Empty)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
RuleFor(command => command.LibraryId)
|
||||
.Must(libraryId => Guid.TryParse(libraryId, out Guid parsedLibraryId) && parsedLibraryId != Guid.Empty)
|
||||
.When(command => command.LibraryId is not null && command.LibraryId.Length > 0)
|
||||
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+6
-8
@@ -1,7 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
@@ -12,14 +10,14 @@ namespace Lumina.Application.Core.MediaConsumption.Queries.GetGenreConsumption;
|
||||
/// </summary>
|
||||
/// <param name="TargetUserId">The Id of the user whose statistics are retrieved, or <see langword="null"/> for the current user.</param>
|
||||
/// <param name="LibraryId">The Id of the media library to restrict the statistics to, or <see langword="null"/> for every library.</param>
|
||||
/// <param name="MediaKind">The kind of media to restrict the statistics to, or <see langword="null"/> for every kind.</param>
|
||||
/// <param name="FromUtc">The inclusive start of the date range, or <see langword="null"/> when the range has no start.</param>
|
||||
/// <param name="ToUtc">The inclusive end of the date range, or <see langword="null"/> when the range has no end.</param>
|
||||
/// <param name="MediaKind">The name of the kind of media to restrict the statistics to, or <see langword="null"/> for every kind.</param>
|
||||
/// <param name="FromUtc">The inclusive start of the date range, in UTC, as a text value, or <see langword="null"/> when the range has no start.</param>
|
||||
/// <param name="ToUtc">The inclusive end of the date range, in UTC, as a text value, or <see langword="null"/> when the range has no end.</param>
|
||||
[DebuggerDisplay("TargetUserId: {TargetUserId}; MediaKind: {MediaKind}")]
|
||||
public record GetGenreConsumptionQuery(
|
||||
string? TargetUserId,
|
||||
string? LibraryId,
|
||||
MediaKind? MediaKind,
|
||||
DateTime? FromUtc,
|
||||
DateTime? ToUtc
|
||||
string? MediaKind,
|
||||
string? FromUtc,
|
||||
string? ToUtc
|
||||
) : IQuery;
|
||||
+5
-1
@@ -18,6 +18,7 @@ using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.V
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -83,9 +84,12 @@ public class GetGenreConsumptionQueryHandler : IQueryHandler<GetGenreConsumption
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
Guid? libraryId = query.LibraryId is null ? null : Guid.Parse(query.LibraryId);
|
||||
MediaKind? mediaKind = query.MediaKind is null ? null : Enum.Parse<MediaKind>(query.MediaKind, ignoreCase: true);
|
||||
DateTime? fromUtc = query.FromUtc is null ? null : DateTime.Parse(query.FromUtc, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
|
||||
DateTime? toUtc = query.ToUtc is null ? null : DateTime.Parse(query.ToUtc, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
|
||||
|
||||
Result<IReadOnlyList<MediaConsumptionRecordEntity>> getRecordsResult = await _unitOfWork.ConsumptionStatisticsRepository
|
||||
.GetForUserAsync(targetUserId, libraryId, query.MediaKind, query.FromUtc, query.ToUtc, cancellationToken).ConfigureAwait(false);
|
||||
.GetForUserAsync(targetUserId, libraryId, mediaKind, fromUtc, toUtc, cancellationToken).ConfigureAwait(false);
|
||||
if (getRecordsResult.IsFailure)
|
||||
return getRecordsResult.Errors;
|
||||
|
||||
|
||||
+33
-8
@@ -4,6 +4,7 @@ using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.MediaConsumption.Queries.GetGenreConsumption;
|
||||
@@ -29,18 +30,42 @@ public class GetGenreConsumptionQueryValidator : AbstractValidator<GetGenreConsu
|
||||
(Guid.TryParse(libraryId, out Guid parsedLibraryId) && parsedLibraryId != Guid.Empty))
|
||||
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
// Only the member names of the media kind enumeration are accepted, so a numeric value that happens to parse as a kind is rejected.
|
||||
RuleFor(query => query.MediaKind)
|
||||
.IsInEnum()
|
||||
.WithError(Errors.MediaConsumption.UnsupportedMediaKind)
|
||||
.When(query => query.MediaKind is not null);
|
||||
|
||||
RuleFor(query => query.MediaKind)
|
||||
.Must(mediaKind => mediaKind is null || mediaKind != MediaKind.None)
|
||||
.IsInEnum<GetGenreConsumptionQuery, MediaKind>()
|
||||
.WithError(Errors.MediaConsumption.UnsupportedMediaKind);
|
||||
|
||||
// An inverted range would silently return no records, so it is rejected as an invalid request instead.
|
||||
RuleFor(query => query.MediaKind)
|
||||
.Must(mediaKind => mediaKind is null ||
|
||||
!string.Equals(mediaKind, nameof(MediaKind.None), StringComparison.OrdinalIgnoreCase))
|
||||
.WithError(Errors.MediaConsumption.UnsupportedMediaKind);
|
||||
|
||||
// A malformed bound, or an inverted range, would silently return no records, so both are rejected as an invalid request instead.
|
||||
RuleFor(query => query.FromUtc)
|
||||
.Must((query, fromUtc) => !fromUtc.HasValue || !query.ToUtc.HasValue || fromUtc.Value <= query.ToUtc.Value)
|
||||
.Must(fromUtc => fromUtc is null || DateTime.TryParse(
|
||||
fromUtc,
|
||||
CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
|
||||
out _))
|
||||
.WithError(Errors.MediaConsumption.ConsumptionDateRangeIsInvalid);
|
||||
|
||||
RuleFor(query => query.ToUtc)
|
||||
.Must(toUtc => toUtc is null || DateTime.TryParse(
|
||||
toUtc,
|
||||
CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
|
||||
out _))
|
||||
.WithError(Errors.MediaConsumption.ConsumptionDateRangeIsInvalid);
|
||||
|
||||
RuleFor(query => query.FromUtc)
|
||||
.Must((query, fromUtc) =>
|
||||
{
|
||||
if (fromUtc is null || query.ToUtc is null ||
|
||||
!DateTime.TryParse(fromUtc, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out DateTime parsedFromUtc) ||
|
||||
!DateTime.TryParse(query.ToUtc, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out DateTime parsedToUtc))
|
||||
return true;
|
||||
return parsedFromUtc <= parsedToUtc;
|
||||
})
|
||||
.WithError(Errors.MediaConsumption.ConsumptionDateRangeIsInvalid);
|
||||
}
|
||||
}
|
||||
+6
-8
@@ -1,6 +1,4 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
@@ -9,12 +7,12 @@ namespace Lumina.Contracts.Requests.MediaConsumption;
|
||||
/// <summary>
|
||||
/// Represents a request to delete every consumption record of a media item.
|
||||
/// </summary>
|
||||
/// <param name="MediaKind">The kind of the consumed media item.</param>
|
||||
/// <param name="MediaItemId">The Id of the consumed media item.</param>
|
||||
/// <param name="LibraryId">The Id of the media library that contains the media item.</param>
|
||||
/// <param name="MediaKind">The name of the kind of the consumed media item, taken from the route.</param>
|
||||
/// <param name="MediaItemId">The Id of the consumed media item, taken from the route.</param>
|
||||
/// <param name="LibraryId">The Id of the media library that contains the media item, taken from the route.</param>
|
||||
[DebuggerDisplay("MediaKind: {MediaKind}; MediaItemId: {MediaItemId}; LibraryId: {LibraryId}")]
|
||||
public sealed record DeleteConsumptionItemHistoryRequest(
|
||||
MediaKind MediaKind,
|
||||
Guid MediaItemId,
|
||||
Guid LibraryId
|
||||
string? MediaKind,
|
||||
string? MediaItemId,
|
||||
string? LibraryId
|
||||
);
|
||||
@@ -1,6 +1,4 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
@@ -10,13 +8,13 @@ namespace Lumina.Contracts.Requests.MediaConsumption;
|
||||
/// Represents a request for getting the consumption statistics of the genres of a user.
|
||||
/// </summary>
|
||||
/// <param name="LibraryId">The Id of the media library to restrict the statistics to. Optional.</param>
|
||||
/// <param name="MediaKind">The kind of media to restrict the statistics to. Optional.</param>
|
||||
/// <param name="FromUtc">The inclusive start of the date range. Optional.</param>
|
||||
/// <param name="ToUtc">The inclusive end of the date range. Optional.</param>
|
||||
/// <param name="MediaKind">The name of the kind of media to restrict the statistics to. Optional.</param>
|
||||
/// <param name="FromUtc">The inclusive start of the date range, in UTC, as a text value. Optional.</param>
|
||||
/// <param name="ToUtc">The inclusive end of the date range, in UTC, as a text value. Optional.</param>
|
||||
[DebuggerDisplay("MediaKind: {MediaKind}; FromUtc: {FromUtc}; ToUtc: {ToUtc}")]
|
||||
public sealed record GetGenreConsumptionRequest(
|
||||
string? LibraryId,
|
||||
MediaKind? MediaKind,
|
||||
DateTime? FromUtc,
|
||||
DateTime? ToUtc
|
||||
string? MediaKind,
|
||||
string? FromUtc,
|
||||
string? ToUtc
|
||||
);
|
||||
+2
-2
@@ -26,8 +26,8 @@ public class GetGenreConsumptionEndpointSummary : Summary<GetGenreConsumptionEnd
|
||||
ExampleRequest = new GetGenreConsumptionRequest(
|
||||
LibraryId: null,
|
||||
MediaKind: null,
|
||||
FromUtc: new DateTime(2026, 10, 1, 0, 0, 0, DateTimeKind.Utc),
|
||||
ToUtc: new DateTime(2026, 10, 31, 23, 59, 59, DateTimeKind.Utc)
|
||||
FromUtc: "2026-10-01T00:00:00.0000000Z",
|
||||
ToUtc: "2026-10-31T23:59:59.0000000Z"
|
||||
);
|
||||
|
||||
RequestParam(request => request.LibraryId, "The Id of the media library to restrict the statistics to. Optional.");
|
||||
|
||||
+2
-2
@@ -26,8 +26,8 @@ public class GetUserGenreConsumptionEndpointSummary : Summary<GetUserGenreConsum
|
||||
ExampleRequest = new GetGenreConsumptionRequest(
|
||||
LibraryId: null,
|
||||
MediaKind: null,
|
||||
FromUtc: new DateTime(2026, 10, 1, 0, 0, 0, DateTimeKind.Utc),
|
||||
ToUtc: new DateTime(2026, 10, 31, 23, 59, 59, DateTimeKind.Utc)
|
||||
FromUtc: "2026-10-01T00:00:00.0000000Z",
|
||||
ToUtc: "2026-10-31T23:59:59.0000000Z"
|
||||
);
|
||||
|
||||
Params["userId"] = "The Id of the user whose statistics are retrieved. An Admin can name any user; a regular user can only name themselves.";
|
||||
|
||||
Reference in new issue
Block a user