mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 01:11:10 +00:00
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:
1 parent
468f1846f1
commit
394f6b510c
135 files changed
+19167
-13
No files matched your search
@@ -31,6 +31,20 @@ public abstract class BaseEndpoint<TRequest, TResponse> : Endpoint<TRequest, TRe
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <c>Cache-Control</c> header, and remembers the chosen value so that it can be restored after later components overwrite it.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The antiforgery token generation and the session middleware both force their own <c>Cache-Control</c> value, and they can run after the
|
||||
/// endpoint has chosen one. The remembered value is restored when the response starts by the <c>CacheControlHeaderMiddleware</c>.
|
||||
/// </remarks>
|
||||
/// <param name="cacheControl">The <c>Cache-Control</c> value chosen by the endpoint.</param>
|
||||
protected void SetCacheControl(string cacheControl)
|
||||
{
|
||||
HttpContext.Response.Headers.CacheControl = cacheControl;
|
||||
HttpContext.Items[HttpContextItemKeys.CACHE_CONTROL] = cacheControl;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Renders a Razor view to the response.
|
||||
/// </summary>
|
||||
@@ -40,6 +54,13 @@ public abstract class BaseEndpoint<TRequest, TResponse> : Endpoint<TRequest, TRe
|
||||
/// <returns>An <see cref="IResult"/> that renders the view.</returns>
|
||||
protected IResult View(string viewName, object? model = null, IReadOnlyDictionary<string, object?>? viewData = null)
|
||||
{
|
||||
// A view endpoint that set the Cache-Control header directly, instead of through SetCacheControl, still has its value remembered.
|
||||
if (!HttpContext.Items.ContainsKey(HttpContextItemKeys.CACHE_CONTROL))
|
||||
{
|
||||
string cacheControl = HttpContext.Response.Headers.CacheControl.ToString();
|
||||
if (!string.IsNullOrEmpty(cacheControl))
|
||||
HttpContext.Items[HttpContextItemKeys.CACHE_CONTROL] = cacheControl;
|
||||
}
|
||||
// Store the path of the displayed view in the session, so that the login redirect after the JWT token expires points to it; ignore the login endpoint,
|
||||
// because it is incorrect to be redirected to the login URL, with the login URL as the return URL.
|
||||
if (HttpContext.Request.Path != "/en-us/auth/login")
|
||||
|
||||
+1
-1
@@ -69,7 +69,7 @@ public class FavoritesIndexViewEndpoint : BaseEndpoint<EmptyRequest, IResult>
|
||||
public override async Task<IResult> ExecuteAsync(EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The page shows the favorites of the authenticated user, so no intermediary may store it, and the response varies by the sign in cookie.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
SetCacheControl("private, no-store");
|
||||
HttpContext.Response.Headers["Vary"] = "Cookie";
|
||||
// The page removes favorites, which submit through the form anti-forgery token, so a fresh token is issued for it.
|
||||
AntiforgeryTokenSet antiforgeryTokens = _antiforgery.GetAndStoreTokens(HttpContext);
|
||||
|
||||
+1
-1
@@ -57,7 +57,7 @@ public class RemoveFavoriteEndpoint : BaseEndpoint<FastEndpoints.EmptyRequest, I
|
||||
string routeMediaKind = HttpContext.Request.RouteValues["mediaKind"]?.ToString() ?? string.Empty;
|
||||
string mediaKind = INVALID_MEDIA_KIND_PLACEHOLDER;
|
||||
foreach (string mediaKindName in Enum.GetNames<FavoriteMediaKind>())
|
||||
if (string.Equals(mediaKindName, routeMediaKind, StringComparison.OrdinalIgnoreCase))
|
||||
if (mediaKindName != nameof(FavoriteMediaKind.None) && string.Equals(mediaKindName, routeMediaKind, StringComparison.OrdinalIgnoreCase))
|
||||
mediaKind = mediaKindName;
|
||||
string routeMediaItemId = HttpContext.Request.RouteValues["mediaItemId"]?.ToString() ?? string.Empty;
|
||||
string mediaItemId = Guid.TryParse(routeMediaItemId, out Guid parsedMediaItemId) ? parsedMediaItemId.ToString() : Guid.Empty.ToString();
|
||||
|
||||
+1
-1
@@ -70,7 +70,7 @@ public class StatisticsIndexViewEndpoint : BaseEndpoint<EmptyRequest, IResult>
|
||||
public override async Task<IResult> ExecuteAsync(EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The page shows the statistics of the authenticated user, so no intermediary may store it, and the response varies by the sign in cookie.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
SetCacheControl("private, no-store");
|
||||
HttpContext.Response.Headers["Vary"] = "Cookie";
|
||||
string title = _localizer["Title"].Value;
|
||||
bool isAdmin = await _authorizationService.IsInRoleAsync("Admin", cancellationToken).ConfigureAwait(false);
|
||||
|
||||
+1
-1
@@ -69,7 +69,7 @@ public class PlaylistsIndexViewEndpoint : BaseEndpoint<EmptyRequest, IResult>
|
||||
public override async Task<IResult> ExecuteAsync(EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The page shows the playlists of the authenticated user, so no intermediary may store it, and the response varies by the sign in cookie.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
SetCacheControl("private, no-store");
|
||||
HttpContext.Response.Headers["Vary"] = "Cookie";
|
||||
// The page creates and deletes playlists, which submit through the form anti-forgery token, so a fresh token is issued for it.
|
||||
AntiforgeryTokenSet antiforgeryTokens = _antiforgery.GetAndStoreTokens(HttpContext);
|
||||
|
||||
+1
-1
@@ -81,7 +81,7 @@ public class AddPlaylistViewEndpoint : BaseEndpoint<EmptyRequest, IResult>
|
||||
public override async Task<IResult> ExecuteAsync(EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The page creates a playlist of the authenticated user, so no intermediary may store it, and the response varies by the sign in cookie.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
SetCacheControl("private, no-store");
|
||||
HttpContext.Response.Headers["Vary"] = "Cookie";
|
||||
|
||||
// The page submits through the form anti-forgery token, so a fresh token is issued for it.
|
||||
|
||||
+1
-1
@@ -83,7 +83,7 @@ public class PlaylistItemViewEndpoint : BaseEndpoint<EmptyRequest, IResult>
|
||||
public override async Task<IResult> ExecuteAsync(EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The page shows a playlist of the authenticated user, so no intermediary may store it, and the response varies by the sign in cookie.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
SetCacheControl("private, no-store");
|
||||
HttpContext.Response.Headers["Vary"] = "Cookie";
|
||||
// The route value is normalized to a Guid before it is substituted into the upstream URL, so that a crafted value can never escape
|
||||
// its URL segment, and an unparseable id is reported by the API as a missing playlist id.
|
||||
|
||||
Reference in new issue
Block a user