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,405 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Favorites;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Favorites;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Contracts.Responses.Favorites;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
using Lumina.Domain.SharedKernel.Common.Enums.Favorites;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Lumina.Presentation.Api.Core.Endpoints.Favorites.AddFavorite;
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.Favorites.AddFavorite;
/// <summary>
/// Contains security tests for the <see cref="AddFavoriteEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class AddFavoriteEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
{
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
private const string PATH = "/api/v1/users/me/favorites";
private readonly LuminaApiFactory _apiFactory;
private readonly HttpClient _client;
private readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
};
private readonly LibraryEntityFixture _libraryEntityFixture = new();
private readonly ArtistEntityFixture _artistEntityFixture = new();
private readonly AlbumEntityFixture _albumEntityFixture = new();
private readonly TrackEntityFixture _trackEntityFixture = new();
private readonly UserEntityFixture _userEntityFixture = new();
private readonly List<Guid> _seededLibraryIds = [];
private readonly List<string> _seededUsernames = [];
/// <summary>
/// Initializes a new instance of the <see cref="AddFavoriteEndpointTests"/> class.
/// </summary>
/// <param name="apiFactory">Injected in-memory API factory.</param>
public AddFavoriteEndpointTests(LuminaApiFactory apiFactory)
{
_apiFactory = apiFactory;
_client = apiFactory.CreateClient();
// A unique X-Forwarded-For isolates rate limiting state per test.
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
}
[Fact]
public async Task AddFavorite_WhenUnauthorized_ShouldReturnUnauthorizedResult()
{
// Arrange
object body = new { mediaKind = "MusicTrack", mediaItemId = Guid.NewGuid() };
// Act
HttpResponseMessage response = await _client.PostAsJsonAsync(PATH, body);
// Assert
await AssertUnauthorizedAsync(response, PATH);
}
[Theory]
[InlineData("' OR '1'='1")] // boolean-based injection
[InlineData("'; DROP TABLE Favorites--")] // destructive injection
[InlineData("1; DELETE FROM Favorites")] // stacked statement injection
[InlineData("../../../etc/passwd")] // path traversal
[InlineData("<script>alert(1)</script>")] // markup injection
[InlineData("Ünïcödé-💥")] // non ascii input
public async Task AddFavorite_WithInjectionInMediaKind_ShouldRejectWithoutLeakingOrChangingData(string maliciousMediaKind)
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
// Act
HttpResponseMessage response = await _client.PostAsJsonAsync(PATH, new { mediaKind = maliciousMediaKind, mediaItemId = Guid.NewGuid() });
// Assert
string content = await AssertRejectedAsync(response);
Assert.DoesNotContain(maliciousMediaKind, content, StringComparison.Ordinal);
await AssertFavoriteCountAsync(callerId, 0);
}
[Theory]
[InlineData("' OR '1'='1")] // boolean-based injection
[InlineData("'; DROP TABLE Favorites--")] // destructive injection
[InlineData("1; DELETE FROM Favorites")] // stacked statement injection
[InlineData("../../../etc/passwd")] // path traversal
[InlineData("<script>alert(1)</script>")] // markup injection
[InlineData("Ünïcödé-💥")] // non ascii input
public async Task AddFavorite_WithInjectionInMediaItemId_ShouldRejectWithoutLeakingOrChangingData(string maliciousMediaItemId)
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
// Act
HttpResponseMessage response = await _client.PostAsJsonAsync(PATH, new { mediaKind = "MusicTrack", mediaItemId = maliciousMediaItemId });
// Assert
string content = await AssertRejectedAsync(response);
Assert.DoesNotContain(maliciousMediaItemId, content, StringComparison.Ordinal);
await AssertFavoriteCountAsync(callerId, 0);
}
[Fact]
public async Task AddFavorite_WhenTheMediaItemBelongsToAnotherUsersLibrary_ShouldReturnTheSameNotFoundAsAMissingItem()
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
(Guid otherUserId, _) = await SeedOtherUserAsync();
Guid otherTrackId = await SeedTrackAsync(otherUserId);
Guid missingTrackId = Guid.NewGuid();
// Act
HttpResponseMessage existingItemResponse = await _client.PostAsJsonAsync(PATH, new { mediaKind = "MusicTrack", mediaItemId = otherTrackId });
HttpResponseMessage missingItemResponse = await _client.PostAsJsonAsync(PATH, new { mediaKind = "MusicTrack", mediaItemId = missingTrackId });
// Assert
// Both answers are indistinguishable, so the route cannot be used as an existence oracle for another user's library.
Assert.Equal(HttpStatusCode.NotFound, existingItemResponse.StatusCode);
Assert.Equal(HttpStatusCode.NotFound, missingItemResponse.StatusCode);
string existingItemContent = await existingItemResponse.Content.ReadAsStringAsync();
string missingItemContent = await missingItemResponse.Content.ReadAsStringAsync();
Assert.Equal(GetProblemDetail(existingItemContent), GetProblemDetail(missingItemContent));
Assert.Equal("TrackNotFound", GetProblemDetail(existingItemContent));
await AssertFavoriteCountAsync(callerId, 0);
}
[Fact]
public async Task AddFavorite_WhenSuccessful_ShouldNotLeakSensitiveData()
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid trackId = await SeedTrackAsync(callerId);
// Act
HttpResponseMessage response = await _client.PostAsJsonAsync(PATH, new { mediaKind = "MusicTrack", mediaItemId = trackId });
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
AssertNonCacheable(response);
string content = await response.Content.ReadAsStringAsync();
AssertNoSensitiveData(content);
AssertNoDatabaseOnlyFields(content);
FavoriteResponse? favorite = JsonSerializer.Deserialize<FavoriteResponse>(content, _jsonOptions);
Assert.NotNull(favorite);
Assert.Equal(trackId, favorite!.MediaItemId);
}
[Fact]
public async Task AddFavorite_WhenTheMediaKindIsNotSupported_ShouldRejectWithNonCacheableValidationProblem()
{
// Arrange
(_, string username) = await CreateAuthenticatedUserAsync();
// Act
HttpResponseMessage response = await _client.PostAsJsonAsync(PATH, new { mediaKind = "Video", mediaItemId = Guid.NewGuid() });
// Assert
string content = await AssertValidationProblemAsync(response, PATH, "FavoriteMediaKindIsNotSupported");
AssertNonCacheable(response);
Assert.DoesNotContain(username, content, StringComparison.Ordinal);
}
/// <summary>
/// Creates a test user and authenticates it on <paramref name="client"/>.
/// </summary>
/// <returns>The Id and username of the created user.</returns>
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync()
{
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(_client);
_seededUsernames.Add(username);
return (userId, username);
}
/// <summary>
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
/// </summary>
/// <returns>The Id and username of the seeded user.</returns>
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Guid userId = Guid.NewGuid();
string username = $"otheruser_{Guid.NewGuid()}";
dbContext.Users.Add(_userEntityFixture.Create(id: userId, username: username, password: "TestPass123!"));
await dbContext.SaveChangesAsync();
_seededUsernames.Add(username);
return (userId, username);
}
/// <summary>
/// Seeds a music library owned by <paramref name="userId"/> with one track, and returns the Id of that track.
/// </summary>
/// <param name="userId">The Id of the user that owns the seeded library.</param>
/// <returns>The Id of the seeded track.</returns>
private async Task<Guid> SeedTrackAsync(Guid userId)
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Guid libraryId = Guid.NewGuid();
dbContext.Libraries.Add(_libraryEntityFixture.Create(
id: libraryId, userId: userId, title: $"Library-{Guid.NewGuid():N}", libraryType: LibraryType.Music, contentLocations: []));
await dbContext.SaveChangesAsync();
_seededLibraryIds.Add(libraryId);
Guid artistId = Guid.NewGuid();
dbContext.Artists.Add(_artistEntityFixture.Create(id: artistId, libraryId: libraryId, name: "Security Artist", includeAlbums: false, includeContributors: false));
await dbContext.SaveChangesAsync();
Guid albumId = Guid.NewGuid();
dbContext.Albums.Add(_albumEntityFixture.Create(
id: albumId,
artistId: artistId,
libraryId: libraryId,
title: "Security Album",
includeTracks: false,
includeMetadata: false,
includeBarcode: false,
includeOriginalReleaseDate: false,
includeOriginalReleaseYear: false,
includeReReleaseDate: false,
includeReReleaseYear: false));
await dbContext.SaveChangesAsync();
Guid trackId = Guid.NewGuid();
dbContext.Tracks.Add(_trackEntityFixture.Create(
id: trackId,
albumId: albumId,
libraryId: libraryId,
title: "Security Track",
includeMetadata: false,
includeOriginalReleaseDate: false,
includeOriginalReleaseYear: false,
includeReReleaseDate: false,
includeReReleaseYear: false));
await dbContext.SaveChangesAsync();
return trackId;
}
/// <summary>
/// Asserts how many favorites the user identified by <paramref name="userId"/> owns.
/// </summary>
/// <param name="userId">The Id of the user whose favorites are counted.</param>
/// <param name="expectedCount">The expected number of favorites.</param>
private async Task AssertFavoriteCountAsync(Guid userId, int expectedCount)
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Assert.Equal(expectedCount, await dbContext.Favorites.CountAsync(favorite => favorite.UserId == userId));
}
/// <summary>
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <param name="expectedInstance">The expected request instance path.</param>
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
{
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
Assert.NotNull(problemDetails);
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
AssertNoSensitiveData(content);
}
/// <summary>
/// Asserts that the request was rejected without succeeding, and returns the raw response body.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
private static async Task<string> AssertRejectedAsync(HttpResponseMessage response)
{
Assert.False(response.IsSuccessStatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Assert.False(string.IsNullOrWhiteSpace(content));
AssertNoSensitiveData(content);
return content;
}
/// <summary>
/// Asserts the shape of a validation problem details response and returns its raw body.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <param name="expectedInstance">The expected request instance path.</param>
/// <param name="expectedErrorCode">The expected validation error code.</param>
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
private async Task<string> AssertValidationProblemAsync(HttpResponseMessage response, string expectedInstance, string expectedErrorCode)
{
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
Assert.NotNull(problemDetails);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
Assert.Equal("General.Validation", problemDetails["title"].GetString());
Assert.Equal("OneOrMoreValidationErrorsOccurred", problemDetails["detail"].GetString());
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
Assert.False(string.IsNullOrEmpty(problemDetails["traceId"].GetString()));
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
Assert.NotNull(errors);
Assert.Contains("General.Validation", errors!.Keys);
Assert.Contains(expectedErrorCode, errors["General.Validation"]);
AssertNoSensitiveData(content);
return content;
}
/// <summary>
/// Reads the detail of a problem details body.
/// </summary>
/// <param name="content">The problem details body to read.</param>
/// <returns>The problem detail.</returns>
private static string? GetProblemDetail(string content)
{
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content);
Assert.NotNull(problemDetails);
return problemDetails!["detail"].GetString();
}
/// <summary>
/// Asserts that the response is not storable by any intermediary and varies by the authorization header.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
private static void AssertNonCacheable(HttpResponseMessage response)
{
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
}
/// <summary>
/// Asserts that the response body carries no credentials, secrets or stack traces.
/// </summary>
/// <param name="content">The response body to assert on.</param>
private static void AssertNoSensitiveData(string content)
{
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Asserts that the response body exposes only the fields of the documented favorite response contract.
/// </summary>
/// <param name="content">The response body to assert on.</param>
private static void AssertNoDatabaseOnlyFields(string content)
{
Assert.DoesNotContain("version", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("createdBy", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("updatedBy", content, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Removes the catalogue rows seeded by the test and the test users.
/// </summary>
public void Dispose()
{
_client.Dispose();
using (IServiceScope scope = _apiFactory.Services.CreateScope())
{
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
List<Guid> libraryIds = [.. _seededLibraryIds];
dbContext.Tracks.RemoveRange(dbContext.Tracks.Where(track => libraryIds.Contains(track.LibraryId)));
dbContext.Albums.RemoveRange(dbContext.Albums.Where(album => libraryIds.Contains(album.LibraryId)));
dbContext.Artists.RemoveRange(dbContext.Artists.Where(artist => libraryIds.Contains(artist.LibraryId)));
dbContext.Libraries.RemoveRange(dbContext.Libraries.Where(library => libraryIds.Contains(library.Id)));
dbContext.SaveChanges();
}
foreach (string username in _seededUsernames)
_apiFactory.RemoveTestUser(username);
}
}
@@ -0,0 +1,344 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Favorites;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Contracts.Responses.Favorites;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.SharedKernel.Common.Enums.Favorites;
using Lumina.Presentation.Api.Core.Endpoints.Favorites.GetFavoriteStatus;
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.Favorites.GetFavoriteStatus;
/// <summary>
/// Contains security tests for the <see cref="GetFavoriteStatusEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetFavoriteStatusEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
{
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
private const string PATH = "/api/v1/users/me/favorites/status";
private readonly LuminaApiFactory _apiFactory;
private readonly HttpClient _client;
private readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
};
private readonly FavoriteEntityFixture _favoriteEntityFixture = new();
private readonly UserEntityFixture _userEntityFixture = new();
private readonly List<string> _seededUsernames = [];
/// <summary>
/// Initializes a new instance of the <see cref="GetFavoriteStatusEndpointTests"/> class.
/// </summary>
/// <param name="apiFactory">Injected in-memory API factory.</param>
public GetFavoriteStatusEndpointTests(LuminaApiFactory apiFactory)
{
_apiFactory = apiFactory;
_client = apiFactory.CreateClient();
// A unique X-Forwarded-For isolates rate limiting state per test.
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
}
[Fact]
public async Task GetFavoriteStatus_WhenUnauthorized_ShouldReturnUnauthorizedResult()
{
// Arrange
string url = $"{PATH}?mediaKind=MusicTrack&mediaItemIds={Guid.NewGuid()}";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
await AssertUnauthorizedAsync(response, PATH);
}
[Theory]
[InlineData("' OR '1'='1")] // boolean-based injection
[InlineData("'; DROP TABLE Favorites--")] // destructive injection
[InlineData("1; DELETE FROM Favorites")] // stacked statement injection
[InlineData("../../../etc/passwd")] // path traversal
[InlineData("<script>alert(1)</script>")] // markup injection
[InlineData("Ünïcödé-💥")] // non ascii input
public async Task GetFavoriteStatus_WithInjectionInMediaKind_ShouldRejectWithoutLeakingOrChangingData(string maliciousMediaKind)
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid favoritedItemId = await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack);
string url = $"{PATH}?mediaKind={Uri.EscapeDataString(maliciousMediaKind)}&mediaItemIds={favoritedItemId}";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
string content = await AssertRejectedAsync(response);
Assert.DoesNotContain(maliciousMediaKind, content, StringComparison.Ordinal);
Assert.DoesNotContain(favoritedItemId.ToString(), content, StringComparison.OrdinalIgnoreCase);
await AssertFavoriteCountAsync(callerId, 1);
}
[Theory]
[InlineData("' OR '1'='1")] // boolean-based injection
[InlineData("'; DROP TABLE Favorites--")] // destructive injection
[InlineData("1; DELETE FROM Favorites")] // stacked statement injection
[InlineData("../../../etc/passwd")] // path traversal
[InlineData("<script>alert(1)</script>")] // markup injection
[InlineData("Ünïcödé-💥")] // non ascii input
public async Task GetFavoriteStatus_WithInjectionInMediaItemIds_ShouldRejectWithoutLeakingOrChangingData(string maliciousMediaItemId)
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid favoritedItemId = await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack);
string url = $"{PATH}?mediaKind=MusicTrack&mediaItemIds={favoritedItemId}&mediaItemIds={Uri.EscapeDataString(maliciousMediaItemId)}";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
string content = await AssertRejectedAsync(response);
Assert.DoesNotContain(maliciousMediaItemId, content, StringComparison.Ordinal);
Assert.DoesNotContain(favoritedItemId.ToString(), content, StringComparison.OrdinalIgnoreCase);
await AssertFavoriteCountAsync(callerId, 1);
}
[Fact]
public async Task GetFavoriteStatus_WhenSuccessful_ShouldReturnOnlyTheCallersFavoritesWithoutSensitiveData()
{
// Arrange
(Guid callerId, string username) = await CreateAuthenticatedUserAsync();
Guid callerFavoritedItemId = await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack);
(Guid otherUserId, string otherUsername) = await SeedOtherUserAsync();
Guid otherFavoritedItemId = await SeedFavoriteAsync(otherUserId, FavoriteMediaKind.MusicTrack);
string url = $"{PATH}?mediaKind=MusicTrack&mediaItemIds={callerFavoritedItemId}&mediaItemIds={otherFavoritedItemId}";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
AssertNonCacheable(response);
string content = await response.Content.ReadAsStringAsync();
AssertNoSensitiveData(content);
AssertNoDatabaseOnlyFields(content);
Assert.DoesNotContain(username, content, StringComparison.Ordinal);
Assert.DoesNotContain(otherUsername, content, StringComparison.Ordinal);
Assert.DoesNotContain(otherFavoritedItemId.ToString(), content, StringComparison.OrdinalIgnoreCase);
FavoriteStatusResponse? status = JsonSerializer.Deserialize<FavoriteStatusResponse>(content, _jsonOptions);
Assert.NotNull(status);
Guid favoritedId = Assert.Single(status!.FavoritedMediaItemIds);
Assert.Equal(callerFavoritedItemId, favoritedId);
}
[Fact]
public async Task GetFavoriteStatus_WhenTooManyIdsAreRequested_ShouldRejectWithNonCacheableValidationProblem()
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid favoritedItemId = await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack);
List<Guid> requestedIds = [favoritedItemId, .. Enumerable.Range(0, 100).Select(_ => Guid.NewGuid())];
string url = $"{PATH}?mediaKind=MusicTrack&{string.Join("&", requestedIds.Select(requestedId => $"mediaItemIds={requestedId}"))}";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
string content = await AssertValidationProblemAsync(response, PATH, "FavoriteMediaItemIdsExceedMaximumCount");
AssertNonCacheable(response);
Assert.DoesNotContain(favoritedItemId.ToString(), content, StringComparison.OrdinalIgnoreCase);
await AssertFavoriteCountAsync(callerId, 1);
}
/// <summary>
/// Creates a test user and authenticates it on <paramref name="client"/>.
/// </summary>
/// <returns>The Id and username of the created user.</returns>
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync()
{
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(_client);
_seededUsernames.Add(username);
return (userId, username);
}
/// <summary>
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
/// </summary>
/// <returns>The Id and username of the seeded user.</returns>
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Guid userId = Guid.NewGuid();
string username = $"otheruser_{Guid.NewGuid()}";
dbContext.Users.Add(_userEntityFixture.Create(id: userId, username: username, password: "TestPass123!"));
await dbContext.SaveChangesAsync();
_seededUsernames.Add(username);
return (userId, username);
}
/// <summary>
/// Seeds a favorite of <paramref name="userId"/> and returns the Id of the favorited media item.
/// </summary>
/// <param name="userId">The Id of the user that owns the favorite.</param>
/// <param name="mediaKind">The kind of the favorited media item.</param>
/// <returns>The Id of the favorited media item.</returns>
private async Task<Guid> SeedFavoriteAsync(Guid userId, FavoriteMediaKind mediaKind)
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Guid mediaItemId = Guid.NewGuid();
dbContext.Favorites.Add(_favoriteEntityFixture.Create(
userId: userId,
mediaKind: mediaKind,
mediaItemId: mediaItemId,
createdBy: userId,
includeUpdatedOnUtc: false,
includeUpdatedBy: false));
await dbContext.SaveChangesAsync();
return mediaItemId;
}
/// <summary>
/// Asserts how many favorites the user identified by <paramref name="userId"/> owns.
/// </summary>
/// <param name="userId">The Id of the user whose favorites are counted.</param>
/// <param name="expectedCount">The expected number of favorites.</param>
private async Task AssertFavoriteCountAsync(Guid userId, int expectedCount)
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Assert.Equal(expectedCount, await dbContext.Favorites.CountAsync(favorite => favorite.UserId == userId));
}
/// <summary>
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <param name="expectedInstance">The expected request instance path.</param>
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
{
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
Assert.NotNull(problemDetails);
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
AssertNoSensitiveData(content);
}
/// <summary>
/// Asserts that the request was rejected without succeeding, and returns the raw response body.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
private static async Task<string> AssertRejectedAsync(HttpResponseMessage response)
{
Assert.False(response.IsSuccessStatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Assert.False(string.IsNullOrWhiteSpace(content));
AssertNoSensitiveData(content);
Assert.DoesNotContain("\"favoritedMediaItemIds\"", content, StringComparison.Ordinal);
return content;
}
/// <summary>
/// Asserts the shape of a validation problem details response and returns its raw body.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <param name="expectedInstance">The expected request instance path.</param>
/// <param name="expectedErrorCode">The expected validation error code.</param>
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
private async Task<string> AssertValidationProblemAsync(HttpResponseMessage response, string expectedInstance, string expectedErrorCode)
{
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
Assert.NotNull(problemDetails);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
Assert.Equal("General.Validation", problemDetails["title"].GetString());
Assert.Equal("OneOrMoreValidationErrorsOccurred", problemDetails["detail"].GetString());
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
Assert.False(string.IsNullOrEmpty(problemDetails["traceId"].GetString()));
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
Assert.NotNull(errors);
Assert.Contains("General.Validation", errors!.Keys);
Assert.Contains(expectedErrorCode, errors["General.Validation"]);
AssertNoSensitiveData(content);
return content;
}
/// <summary>
/// Asserts that the response is not storable by any intermediary and varies by the authorization header.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
private static void AssertNonCacheable(HttpResponseMessage response)
{
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
}
/// <summary>
/// Asserts that the response body carries no credentials, secrets or stack traces.
/// </summary>
/// <param name="content">The response body to assert on.</param>
private static void AssertNoSensitiveData(string content)
{
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Asserts that the response body exposes only the fields of the documented favorite status response contract.
/// </summary>
/// <param name="content">The response body to assert on.</param>
private static void AssertNoDatabaseOnlyFields(string content)
{
Assert.DoesNotContain("version", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("createdBy", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("updatedBy", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("createdOnUtc", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("updatedOnUtc", content, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Removes the test users created by the test.
/// </summary>
public void Dispose()
{
_client.Dispose();
foreach (string username in _seededUsernames)
_apiFactory.RemoveTestUser(username);
}
}
@@ -0,0 +1,318 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Favorites;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Favorites;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Contracts.Responses.Common;
using Lumina.Contracts.Responses.Favorites;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.SharedKernel.Common.Enums.Favorites;
using Lumina.Presentation.Api.Core.Endpoints.Favorites.GetFavorites;
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.Favorites.GetFavorites;
/// <summary>
/// Contains security tests for the <see cref="GetFavoritesEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetFavoritesEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
{
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
private const string PATH = "/api/v1/users/me/favorites";
private readonly LuminaApiFactory _apiFactory;
private readonly HttpClient _client;
private readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
};
private readonly FavoriteEntityFixture _favoriteEntityFixture = new();
private readonly UserEntityFixture _userEntityFixture = new();
private readonly List<string> _seededUsernames = [];
/// <summary>
/// Initializes a new instance of the <see cref="GetFavoritesEndpointTests"/> class.
/// </summary>
/// <param name="apiFactory">Injected in-memory API factory.</param>
public GetFavoritesEndpointTests(LuminaApiFactory apiFactory)
{
_apiFactory = apiFactory;
_client = apiFactory.CreateClient();
// A unique X-Forwarded-For isolates rate limiting state per test.
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
}
[Fact]
public async Task GetFavorites_WhenUnauthorized_ShouldReturnUnauthorizedResult()
{
// Act
HttpResponseMessage response = await _client.GetAsync(PATH);
// Assert
await AssertUnauthorizedAsync(response, PATH);
}
[Theory]
[InlineData("' OR '1'='1")] // boolean-based injection
[InlineData("'; DROP TABLE Favorites--")] // destructive injection
[InlineData("1; DELETE FROM Favorites")] // stacked statement injection
[InlineData("../../../etc/passwd")] // path traversal
[InlineData("<script>alert(1)</script>")] // markup injection
[InlineData("Ünïcödé-💥")] // non ascii input
public async Task GetFavorites_WithInjectionInMediaKind_ShouldRejectWithoutLeakingOrChangingData(string maliciousMediaKind)
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid favoritedItemId = await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack);
string url = $"{PATH}?mediaKind={Uri.EscapeDataString(maliciousMediaKind)}";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
string content = await AssertRejectedAsync(response);
Assert.DoesNotContain(maliciousMediaKind, content, StringComparison.Ordinal);
Assert.DoesNotContain(favoritedItemId.ToString(), content, StringComparison.OrdinalIgnoreCase);
await AssertFavoriteCountAsync(callerId, 1);
}
[Theory]
[InlineData("' OR '1'='1")] // boolean-based injection
[InlineData("'; DROP TABLE Favorites--")] // destructive injection
[InlineData("1; DELETE FROM Favorites")] // stacked statement injection
[InlineData("<script>alert(1)</script>")] // markup injection
[InlineData("Ünïcödé-💥")] // non ascii input
public async Task GetFavorites_WithInjectionInCurrentPage_ShouldRejectWithoutLeakingOrChangingData(string maliciousCurrentPage)
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid favoritedItemId = await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack);
string url = $"{PATH}?currentPage={Uri.EscapeDataString(maliciousCurrentPage)}&perPage=10";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
string content = await AssertRejectedAsync(response);
Assert.DoesNotContain(maliciousCurrentPage, content, StringComparison.Ordinal);
Assert.DoesNotContain(favoritedItemId.ToString(), content, StringComparison.OrdinalIgnoreCase);
await AssertFavoriteCountAsync(callerId, 1);
}
[Theory]
[InlineData("' OR '1'='1")] // boolean-based injection
[InlineData("'; DROP TABLE Favorites--")] // destructive injection
[InlineData("1; DELETE FROM Favorites")] // stacked statement injection
[InlineData("<script>alert(1)</script>")] // markup injection
[InlineData("Ünïcödé-💥")] // non ascii input
public async Task GetFavorites_WithInjectionInPerPage_ShouldRejectWithoutLeakingOrChangingData(string maliciousPerPage)
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid favoritedItemId = await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack);
string url = $"{PATH}?currentPage=1&perPage={Uri.EscapeDataString(maliciousPerPage)}";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
string content = await AssertRejectedAsync(response);
Assert.DoesNotContain(maliciousPerPage, content, StringComparison.Ordinal);
Assert.DoesNotContain(favoritedItemId.ToString(), content, StringComparison.OrdinalIgnoreCase);
await AssertFavoriteCountAsync(callerId, 1);
}
[Fact]
public async Task GetFavorites_WhenSuccessful_ShouldReturnOnlyTheCallersFavoritesWithoutSensitiveData()
{
// Arrange
(Guid callerId, string username) = await CreateAuthenticatedUserAsync();
Guid favoritedItemId = await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack);
(Guid otherUserId, string otherUsername) = await SeedOtherUserAsync();
Guid otherFavoritedItemId = await SeedFavoriteAsync(otherUserId, FavoriteMediaKind.MusicTrack);
// Act
HttpResponseMessage response = await _client.GetAsync(PATH);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
AssertNonCacheable(response);
string content = await response.Content.ReadAsStringAsync();
AssertNoSensitiveData(content);
AssertNoDatabaseOnlyFields(content);
Assert.DoesNotContain(username, content, StringComparison.Ordinal);
Assert.DoesNotContain(otherUsername, content, StringComparison.Ordinal);
Assert.Contains(favoritedItemId.ToString(), content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(otherFavoritedItemId.ToString(), content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(otherUserId.ToString(), content, StringComparison.OrdinalIgnoreCase);
PaginatedResponse<FavoriteItemResponse>? favorites = JsonSerializer.Deserialize<PaginatedResponse<FavoriteItemResponse>>(content, _jsonOptions);
Assert.NotNull(favorites);
FavoriteItemResponse favorite = Assert.Single(favorites!.Data);
Assert.Equal(favoritedItemId, favorite.MediaItemId);
}
/// <summary>
/// Creates a test user and authenticates it on <paramref name="client"/>.
/// </summary>
/// <returns>The Id and username of the created user.</returns>
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync()
{
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(_client);
_seededUsernames.Add(username);
return (userId, username);
}
/// <summary>
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
/// </summary>
/// <returns>The Id and username of the seeded user.</returns>
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Guid userId = Guid.NewGuid();
string username = $"otheruser_{Guid.NewGuid()}";
dbContext.Users.Add(_userEntityFixture.Create(id: userId, username: username, password: "TestPass123!"));
await dbContext.SaveChangesAsync();
_seededUsernames.Add(username);
return (userId, username);
}
/// <summary>
/// Seeds a favorite of <paramref name="userId"/> and returns the Id of the favorited media item.
/// </summary>
/// <param name="userId">The Id of the user that owns the favorite.</param>
/// <param name="mediaKind">The kind of the favorited media item.</param>
/// <returns>The Id of the favorited media item.</returns>
private async Task<Guid> SeedFavoriteAsync(Guid userId, FavoriteMediaKind mediaKind)
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Guid mediaItemId = Guid.NewGuid();
dbContext.Favorites.Add(_favoriteEntityFixture.Create(
userId: userId,
mediaKind: mediaKind,
mediaItemId: mediaItemId,
createdBy: userId,
includeUpdatedOnUtc: false,
includeUpdatedBy: false));
await dbContext.SaveChangesAsync();
return mediaItemId;
}
/// <summary>
/// Asserts how many favorites the user identified by <paramref name="userId"/> owns.
/// </summary>
/// <param name="userId">The Id of the user whose favorites are counted.</param>
/// <param name="expectedCount">The expected number of favorites.</param>
private async Task AssertFavoriteCountAsync(Guid userId, int expectedCount)
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Assert.Equal(expectedCount, await dbContext.Favorites.CountAsync(favorite => favorite.UserId == userId));
}
/// <summary>
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <param name="expectedInstance">The expected request instance path.</param>
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
{
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
Assert.NotNull(problemDetails);
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
AssertNoSensitiveData(content);
}
/// <summary>
/// Asserts that the request was rejected without succeeding, and returns the raw response body.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
private static async Task<string> AssertRejectedAsync(HttpResponseMessage response)
{
Assert.False(response.IsSuccessStatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Assert.False(string.IsNullOrWhiteSpace(content));
AssertNoSensitiveData(content);
Assert.DoesNotContain("\"favoritedMediaItemIds\"", content, StringComparison.Ordinal);
return content;
}
/// <summary>
/// Asserts that the response is not storable by any intermediary and varies by the authorization header.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
private static void AssertNonCacheable(HttpResponseMessage response)
{
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
}
/// <summary>
/// Asserts that the response body carries no credentials, secrets or stack traces.
/// </summary>
/// <param name="content">The response body to assert on.</param>
private static void AssertNoSensitiveData(string content)
{
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Asserts that the response body exposes only the fields of the documented favorites response contract.
/// </summary>
/// <param name="content">The response body to assert on.</param>
private static void AssertNoDatabaseOnlyFields(string content)
{
Assert.DoesNotContain("version", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("createdBy", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("updatedBy", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("createdOnUtc", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("updatedOnUtc", content, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Removes the test users created by the test.
/// </summary>
public void Dispose()
{
_client.Dispose();
foreach (string username in _seededUsernames)
_apiFactory.RemoveTestUser(username);
}
}
@@ -0,0 +1,306 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Favorites;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Favorites;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.SharedKernel.Common.Enums.Favorites;
using Lumina.Presentation.Api.Core.Endpoints.Favorites.RemoveFavorite;
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.Favorites.RemoveFavorite;
/// <summary>
/// Contains security tests for the <see cref="RemoveFavoriteEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class RemoveFavoriteEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
{
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
private const string PATH = "/api/v1/users/me/favorites";
private readonly LuminaApiFactory _apiFactory;
private readonly HttpClient _client;
private readonly FavoriteEntityFixture _favoriteEntityFixture = new();
private readonly UserEntityFixture _userEntityFixture = new();
private readonly List<string> _seededUsernames = [];
/// <summary>
/// Initializes a new instance of the <see cref="RemoveFavoriteEndpointTests"/> class.
/// </summary>
/// <param name="apiFactory">Injected in-memory API factory.</param>
public RemoveFavoriteEndpointTests(LuminaApiFactory apiFactory)
{
_apiFactory = apiFactory;
_client = apiFactory.CreateClient();
// A unique X-Forwarded-For isolates rate limiting state per test.
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
}
[Fact]
public async Task RemoveFavorite_WhenUnauthorized_ShouldReturnUnauthorizedResult()
{
// Arrange
string url = $"{PATH}/MusicTrack/{Guid.NewGuid()}";
// Act
HttpResponseMessage response = await _client.DeleteAsync(url);
// Assert
await AssertUnauthorizedAsync(response, url);
}
[Theory]
[InlineData("' OR '1'='1")] // boolean-based injection
[InlineData("'; DROP TABLE Favorites--")] // destructive injection
[InlineData("1; DELETE FROM Favorites")] // stacked statement injection
[InlineData("../../../etc/passwd")] // path traversal
[InlineData("<script>alert(1)</script>")] // markup injection
[InlineData("Ünïcödé-💥")] // non ascii input
public async Task RemoveFavorite_WithInjectionInMediaKind_ShouldRejectWithoutLeakingOrChangingData(string maliciousMediaKind)
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid mediaItemId = Guid.NewGuid();
await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack, mediaItemId);
string url = $"{PATH}/{Uri.EscapeDataString(maliciousMediaKind)}/{mediaItemId}";
// Act
HttpResponseMessage response = await _client.DeleteAsync(url);
// Assert
string content = await AssertValidationProblemAsync(response, $"{PATH}/", "FavoriteMediaKindIsInvalid");
AssertNonCacheable(response);
Assert.DoesNotContain(maliciousMediaKind, content, StringComparison.Ordinal);
await AssertFavoriteExistsAsync(callerId, mediaItemId);
}
[Theory]
[InlineData("' OR '1'='1")] // boolean-based injection
[InlineData("'; DROP TABLE Favorites--")] // destructive injection
[InlineData("1; DELETE FROM Favorites")] // stacked statement injection
[InlineData("../../../etc/passwd")] // path traversal
[InlineData("<script>alert(1)</script>")] // markup injection
[InlineData("Ünïcödé-💥")] // non ascii input
public async Task RemoveFavorite_WithInjectionInMediaItemId_ShouldRejectWithoutLeakingOrChangingData(string maliciousMediaItemId)
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid mediaItemId = Guid.NewGuid();
await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack, mediaItemId);
string url = $"{PATH}/MusicTrack/{Uri.EscapeDataString(maliciousMediaItemId)}";
// Act
HttpResponseMessage response = await _client.DeleteAsync(url);
// Assert
string content = await AssertValidationProblemAsync(response, $"{PATH}/MusicTrack/", "FavoriteMediaItemIdCannotBeEmpty");
AssertNonCacheable(response);
Assert.DoesNotContain(maliciousMediaItemId, content, StringComparison.Ordinal);
await AssertFavoriteExistsAsync(callerId, mediaItemId);
}
[Fact]
public async Task RemoveFavorite_WhenAnotherUserFavoritedTheSameItem_ShouldRemoveOnlyTheCallersRow()
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid mediaItemId = Guid.NewGuid();
await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack, mediaItemId);
(Guid otherUserId, _) = await SeedOtherUserAsync();
await SeedFavoriteAsync(otherUserId, FavoriteMediaKind.MusicTrack, mediaItemId);
// Act
HttpResponseMessage response = await _client.DeleteAsync($"{PATH}/MusicTrack/{mediaItemId}");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
AssertNonCacheable(response);
string content = await response.Content.ReadAsStringAsync();
AssertNoSensitiveData(content);
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Assert.False(await dbContext.Favorites.AnyAsync(favorite => favorite.UserId == callerId));
Assert.True(await dbContext.Favorites.AnyAsync(favorite => favorite.UserId == otherUserId && favorite.MediaItemId == mediaItemId));
}
[Fact]
public async Task RemoveFavorite_WhenSuccessful_ShouldNotLeakSensitiveData()
{
// Arrange
(Guid callerId, string username) = await CreateAuthenticatedUserAsync();
Guid mediaItemId = Guid.NewGuid();
await SeedFavoriteAsync(callerId, FavoriteMediaKind.MusicTrack, mediaItemId);
// Act
HttpResponseMessage response = await _client.DeleteAsync($"{PATH}/MusicTrack/{mediaItemId}");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
AssertNonCacheable(response);
string content = await response.Content.ReadAsStringAsync();
AssertNoSensitiveData(content);
Assert.DoesNotContain(username, content, StringComparison.Ordinal);
}
/// <summary>
/// Creates a test user and authenticates it on <paramref name="client"/>.
/// </summary>
/// <returns>The Id and username of the created user.</returns>
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync()
{
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(_client);
_seededUsernames.Add(username);
return (userId, username);
}
/// <summary>
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
/// </summary>
/// <returns>The Id and username of the seeded user.</returns>
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Guid userId = Guid.NewGuid();
string username = $"otheruser_{Guid.NewGuid()}";
dbContext.Users.Add(_userEntityFixture.Create(id: userId, username: username, password: "TestPass123!"));
await dbContext.SaveChangesAsync();
_seededUsernames.Add(username);
return (userId, username);
}
/// <summary>
/// Seeds a favorite of <paramref name="userId"/>.
/// </summary>
/// <param name="userId">The Id of the user that owns the favorite.</param>
/// <param name="mediaKind">The kind of the favorited media item.</param>
/// <param name="mediaItemId">The Id of the favorited media item.</param>
private async Task SeedFavoriteAsync(Guid userId, FavoriteMediaKind mediaKind, Guid mediaItemId)
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
dbContext.Favorites.Add(_favoriteEntityFixture.Create(
userId: userId,
mediaKind: mediaKind,
mediaItemId: mediaItemId,
createdBy: userId,
includeUpdatedOnUtc: false,
includeUpdatedBy: false));
await dbContext.SaveChangesAsync();
}
/// <summary>
/// Asserts that the favorite of <paramref name="userId"/> on <paramref name="mediaItemId"/> still exists.
/// </summary>
/// <param name="userId">The Id of the user that owns the favorite.</param>
/// <param name="mediaItemId">The Id of the favorited media item.</param>
private async Task AssertFavoriteExistsAsync(Guid userId, Guid mediaItemId)
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Assert.True(await dbContext.Favorites.AnyAsync(favorite => favorite.UserId == userId && favorite.MediaItemId == mediaItemId));
}
/// <summary>
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <param name="expectedInstance">The expected request instance path.</param>
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
{
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content);
Assert.NotNull(problemDetails);
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
AssertNoSensitiveData(content);
}
/// <summary>
/// Asserts the shape of a validation problem details response and returns its raw body.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <param name="expectedInstancePrefix">The expected prefix of the request instance path.</param>
/// <param name="expectedErrorCode">The expected validation error code.</param>
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
private async Task<string> AssertValidationProblemAsync(HttpResponseMessage response, string expectedInstancePrefix, string expectedErrorCode)
{
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content);
Assert.NotNull(problemDetails);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
Assert.Equal("General.Validation", problemDetails["title"].GetString());
Assert.Equal("OneOrMoreValidationErrorsOccurred", problemDetails["detail"].GetString());
// The instance is the routed path, which normalizes the escaping of some characters, so only its prefix is asserted.
Assert.StartsWith(expectedInstancePrefix, problemDetails["instance"].GetString());
Assert.False(string.IsNullOrEmpty(problemDetails["traceId"].GetString()));
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>();
Assert.NotNull(errors);
Assert.Contains("General.Validation", errors!.Keys);
Assert.Contains(expectedErrorCode, errors["General.Validation"]);
AssertNoSensitiveData(content);
return content;
}
/// <summary>
/// Asserts that the response is not storable by any intermediary and varies by the authorization header.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
private static void AssertNonCacheable(HttpResponseMessage response)
{
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
}
/// <summary>
/// Asserts that the response body carries no credentials, secrets or stack traces.
/// </summary>
/// <param name="content">The response body to assert on.</param>
private static void AssertNoSensitiveData(string content)
{
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Removes the test users created by the test.
/// </summary>
public void Dispose()
{
_client.Dispose();
foreach (string username in _seededUsernames)
_apiFactory.RemoveTestUser(username);
}
}
@@ -0,0 +1,435 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Entities.Playlists;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Playlists;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Contracts.Responses.Playlists;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Lumina.Domain.SharedKernel.Common.Enums.Playlists;
using Lumina.Presentation.Api.Core.Endpoints.Playlists.GetPlaylist;
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.Playlists.GetPlaylist;
/// <summary>
/// Contains security tests for the <see cref="GetPlaylistEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPlaylistEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
{
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
private const string PATH = "/api/v1/users/me/playlists";
private readonly LuminaApiFactory _apiFactory;
private readonly HttpClient _client;
private readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
};
private readonly LibraryEntityFixture _libraryEntityFixture = new();
private readonly ArtistEntityFixture _artistEntityFixture = new();
private readonly AlbumEntityFixture _albumEntityFixture = new();
private readonly TrackEntityFixture _trackEntityFixture = new();
private readonly PlaylistEntityFixture _playlistEntityFixture = new();
private readonly PlaylistEntryEntityFixture _playlistEntryEntityFixture = new();
private readonly UserEntityFixture _userEntityFixture = new();
private readonly List<Guid> _seededLibraryIds = [];
private readonly List<Guid> _seededPlaylistIds = [];
private readonly List<string> _seededUsernames = [];
/// <summary>
/// Initializes a new instance of the <see cref="GetPlaylistEndpointTests"/> class.
/// </summary>
/// <param name="apiFactory">Injected in-memory API factory.</param>
public GetPlaylistEndpointTests(LuminaApiFactory apiFactory)
{
_apiFactory = apiFactory;
_client = apiFactory.CreateClient();
// A unique X-Forwarded-For isolates rate limiting state per test.
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
}
[Fact]
public async Task GetPlaylist_WhenUnauthorized_ShouldReturnUnauthorizedResult()
{
// Arrange
string url = $"{PATH}/{Guid.NewGuid()}";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
await AssertUnauthorizedAsync(response, url);
}
[Theory]
[InlineData("' OR '1'='1")] // boolean-based injection
[InlineData("'; DROP TABLE Playlists--")] // destructive injection
[InlineData("1; DELETE FROM Playlists")] // stacked statement injection
[InlineData("../../../etc/passwd")] // path traversal
[InlineData("<script>alert(1)</script>")] // markup injection
[InlineData("Ünïcödé-💥")] // non ascii input
public async Task GetPlaylist_WithInjectionInPlaylistId_ShouldRejectWithoutLeakingOrChangingData(string maliciousPlaylistId)
{
// Arrange
(Guid callerId, _) = await CreateAuthenticatedUserAsync();
Guid playlistId = await SeedPlaylistAsync(callerId, "Caller playlist", trackCount: 1);
string url = $"{PATH}/{Uri.EscapeDataString(maliciousPlaylistId)}";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
string content = await AssertValidationProblemAsync(response, $"{PATH}/", "PlaylistIdCannotBeEmpty");
AssertNonCacheable(response);
Assert.DoesNotContain(maliciousPlaylistId, content, StringComparison.Ordinal);
await AssertPlaylistStillExistsAsync(playlistId);
}
[Fact]
public async Task GetPlaylist_WhenThePlaylistBelongsToAnotherUser_ShouldReturnTheSameNotFoundAsAMissingPlaylist()
{
// Arrange
(_, _) = await CreateAuthenticatedUserAsync();
(Guid otherUserId, _) = await SeedOtherUserAsync();
Guid otherPlaylistId = await SeedPlaylistAsync(otherUserId, "Someone else's playlist", trackCount: 1);
Guid missingPlaylistId = Guid.NewGuid();
// Act
HttpResponseMessage existingPlaylistResponse = await _client.GetAsync($"{PATH}/{otherPlaylistId}");
HttpResponseMessage missingPlaylistResponse = await _client.GetAsync($"{PATH}/{missingPlaylistId}");
// Assert
// Both answers are indistinguishable, so the route cannot be used as an existence oracle for another user's playlist.
Assert.Equal(HttpStatusCode.NotFound, existingPlaylistResponse.StatusCode);
Assert.Equal(HttpStatusCode.NotFound, missingPlaylistResponse.StatusCode);
string existingPlaylistContent = await existingPlaylistResponse.Content.ReadAsStringAsync();
string missingPlaylistContent = await missingPlaylistResponse.Content.ReadAsStringAsync();
Assert.Equal(GetProblemDetail(existingPlaylistContent), GetProblemDetail(missingPlaylistContent));
Assert.Equal("PlaylistNotFound", GetProblemDetail(existingPlaylistContent));
Assert.DoesNotContain("Someone else's playlist", existingPlaylistContent, StringComparison.Ordinal);
}
[Fact]
public async Task GetPlaylist_WhenSuccessful_ShouldReturnOnlyTheCallersPlaylistWithoutSensitiveData()
{
// Arrange
(Guid callerId, string username) = await CreateAuthenticatedUserAsync();
Guid playlistId = await SeedPlaylistAsync(callerId, "Caller playlist", trackCount: 2);
(Guid otherUserId, string otherUsername) = await SeedOtherUserAsync();
await SeedPlaylistAsync(otherUserId, "Other playlist", trackCount: 1);
// Act
HttpResponseMessage response = await _client.GetAsync($"{PATH}/{playlistId}");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
AssertNonCacheable(response);
string content = await response.Content.ReadAsStringAsync();
AssertNoSensitiveData(content);
AssertNoDatabaseOnlyFields(content);
Assert.DoesNotContain(username, content, StringComparison.Ordinal);
Assert.DoesNotContain(otherUsername, content, StringComparison.Ordinal);
Assert.DoesNotContain("Other playlist", content, StringComparison.Ordinal);
PlaylistResponse? playlist = JsonSerializer.Deserialize<PlaylistResponse>(content, _jsonOptions);
Assert.NotNull(playlist);
Assert.Equal(playlistId, playlist!.Id);
Assert.Equal("Caller playlist", playlist.Name);
Assert.Equal(2, playlist.Entries.Data.Count);
}
[Fact]
public async Task GetPlaylist_WhenThePlaylistDoesNotExist_ShouldRejectWithNonCacheableNotFound()
{
// Arrange
await CreateAuthenticatedUserAsync();
string url = $"{PATH}/{Guid.NewGuid()}";
// Act
HttpResponseMessage response = await _client.GetAsync(url);
// Assert
string content = await AssertNotFoundAsync(response, url, "PlaylistNotFound");
AssertNonCacheable(response);
AssertNoSensitiveData(content);
}
/// <summary>
/// Creates a test user and authenticates it on <paramref name="client"/>.
/// </summary>
/// <returns>The Id and username of the created user.</returns>
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync()
{
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(_client);
_seededUsernames.Add(username);
return (userId, username);
}
/// <summary>
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
/// </summary>
/// <returns>The Id and username of the seeded user.</returns>
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Guid userId = Guid.NewGuid();
string username = $"otheruser_{Guid.NewGuid()}";
dbContext.Users.Add(_userEntityFixture.Create(id: userId, username: username, password: "TestPass123!"));
await dbContext.SaveChangesAsync();
_seededUsernames.Add(username);
return (userId, username);
}
/// <summary>
/// Seeds a playlist of <paramref name="userId"/> with the requested number of entries, each one referencing its own seeded track.
/// </summary>
/// <param name="userId">The Id of the user that owns the playlist.</param>
/// <param name="name">The name of the playlist.</param>
/// <param name="trackCount">The number of entries to seed.</param>
/// <returns>The Id of the seeded playlist.</returns>
private async Task<Guid> SeedPlaylistAsync(Guid userId, string name, int trackCount)
{
Guid libraryId = Guid.NewGuid();
Guid playlistId = Guid.NewGuid();
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
dbContext.Libraries.Add(_libraryEntityFixture.Create(
id: libraryId, userId: userId, title: $"Library-{Guid.NewGuid():N}", libraryType: LibraryType.Music, contentLocations: []));
await dbContext.SaveChangesAsync();
_seededLibraryIds.Add(libraryId);
Guid artistId = Guid.NewGuid();
dbContext.Artists.Add(_artistEntityFixture.Create(id: artistId, libraryId: libraryId, name: "Playlist Artist", includeAlbums: false, includeContributors: false));
await dbContext.SaveChangesAsync();
Guid albumId = Guid.NewGuid();
dbContext.Albums.Add(_albumEntityFixture.Create(
id: albumId,
artistId: artistId,
libraryId: libraryId,
title: "Playlist Album",
includeTracks: false,
includeMetadata: false,
includeBarcode: false,
includeOriginalReleaseDate: false,
includeOriginalReleaseYear: false,
includeReReleaseDate: false,
includeReReleaseYear: false));
await dbContext.SaveChangesAsync();
dbContext.Playlists.Add(_playlistEntityFixture.Create(
id: playlistId,
userId: userId,
name: name,
type: PlaylistType.Music,
entryCount: trackCount,
version: 0,
createdBy: userId));
await dbContext.SaveChangesAsync();
_seededPlaylistIds.Add(playlistId);
for (int index = 0; index < trackCount; index++)
{
Guid trackId = Guid.NewGuid();
dbContext.Tracks.Add(_trackEntityFixture.Create(
id: trackId,
albumId: albumId,
libraryId: libraryId,
title: $"Playlist Track {index}",
includeMetadata: false,
includeOriginalReleaseDate: false,
includeOriginalReleaseYear: false,
includeReReleaseDate: false,
includeReReleaseYear: false));
await dbContext.SaveChangesAsync();
dbContext.PlaylistEntries.Add(_playlistEntryEntityFixture.Create(
playlistId: playlistId,
mediaItemId: trackId,
mediaItemType: PlaylistMediaItemType.MusicTrack,
position: index * 1_000_000_000L));
await dbContext.SaveChangesAsync();
}
return playlistId;
}
/// <summary>
/// Asserts that the playlist identified by <paramref name="playlistId"/> still exists.
/// </summary>
/// <param name="playlistId">The Id of the playlist to look up.</param>
private async Task AssertPlaylistStillExistsAsync(Guid playlistId)
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
Assert.True(await dbContext.Playlists.AnyAsync(playlist => playlist.Id == playlistId));
}
/// <summary>
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <param name="expectedInstance">The expected request instance path.</param>
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
{
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content);
Assert.NotNull(problemDetails);
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
AssertNoSensitiveData(content);
}
/// <summary>
/// Asserts the shape of a validation problem details response and returns its raw body.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <param name="expectedInstancePrefix">The expected prefix of the request instance path.</param>
/// <param name="expectedErrorCode">The expected validation error code.</param>
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
private async Task<string> AssertValidationProblemAsync(HttpResponseMessage response, string expectedInstancePrefix, string expectedErrorCode)
{
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content);
Assert.NotNull(problemDetails);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
Assert.Equal("General.Validation", problemDetails["title"].GetString());
Assert.Equal("OneOrMoreValidationErrorsOccurred", problemDetails["detail"].GetString());
// The instance is the routed path, which normalizes the escaping of some characters, so only its prefix is asserted.
Assert.StartsWith(expectedInstancePrefix, problemDetails["instance"].GetString());
Assert.False(string.IsNullOrEmpty(problemDetails["traceId"].GetString()));
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>();
Assert.NotNull(errors);
Assert.Contains("General.Validation", errors!.Keys);
Assert.Contains(expectedErrorCode, errors["General.Validation"]);
AssertNoSensitiveData(content);
return content;
}
/// <summary>
/// Asserts the shape of a not found problem details response and returns its raw body.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
/// <param name="expectedInstance">The expected request instance path.</param>
/// <param name="expectedDetail">The expected problem detail.</param>
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
private async Task<string> AssertNotFoundAsync(HttpResponseMessage response, string expectedInstance, string expectedDetail)
{
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
string content = await response.Content.ReadAsStringAsync();
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content);
Assert.NotNull(problemDetails);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails!["status"].GetInt32());
Assert.Equal("https://tools.ietf.org/html/rfc9110#section-15.5.5", problemDetails["type"].GetString());
Assert.Equal("General.NotFound", problemDetails["title"].GetString());
Assert.Equal(expectedDetail, problemDetails["detail"].GetString());
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
Assert.False(string.IsNullOrEmpty(problemDetails["traceId"].GetString()));
AssertNoSensitiveData(content);
return content;
}
/// <summary>
/// Reads the detail of a problem details body.
/// </summary>
/// <param name="content">The problem details body to read.</param>
/// <returns>The problem detail.</returns>
private static string? GetProblemDetail(string content)
{
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content);
Assert.NotNull(problemDetails);
return problemDetails!["detail"].GetString();
}
/// <summary>
/// Asserts that the response is not storable by any intermediary and varies by the authorization header.
/// </summary>
/// <param name="response">The HTTP response to assert on.</param>
private static void AssertNonCacheable(HttpResponseMessage response)
{
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
}
/// <summary>
/// Asserts that the response body carries no credentials, secrets or stack traces.
/// </summary>
/// <param name="content">The response body to assert on.</param>
private static void AssertNoSensitiveData(string content)
{
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Asserts that the response body exposes only the fields of the documented playlist response contract.
/// </summary>
/// <param name="content">The response body to assert on.</param>
private static void AssertNoDatabaseOnlyFields(string content)
{
// The creation and update times are part of the documented playlist contract, so they are not asserted absent here.
Assert.DoesNotContain("version", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("createdBy", content, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("updatedBy", content, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Removes the catalogue rows and playlists seeded by the test and the test users.
/// </summary>
public void Dispose()
{
_client.Dispose();
using (IServiceScope scope = _apiFactory.Services.CreateScope())
{
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
List<Guid> libraryIds = [.. _seededLibraryIds];
List<Guid> playlistIds = [.. _seededPlaylistIds];
dbContext.PlaylistEntries.RemoveRange(dbContext.PlaylistEntries.Where(entry => playlistIds.Contains(entry.PlaylistId)));
dbContext.Playlists.RemoveRange(dbContext.Playlists.Where(playlist => playlistIds.Contains(playlist.Id)));
dbContext.Tracks.RemoveRange(dbContext.Tracks.Where(track => libraryIds.Contains(track.LibraryId)));
dbContext.Albums.RemoveRange(dbContext.Albums.Where(album => libraryIds.Contains(album.LibraryId)));
dbContext.Artists.RemoveRange(dbContext.Artists.Where(artist => libraryIds.Contains(artist.LibraryId)));
dbContext.Libraries.RemoveRange(dbContext.Libraries.Where(library => libraryIds.Contains(library.Id)));
dbContext.SaveChanges();
}
foreach (string username in _seededUsernames)
_apiFactory.RemoveTestUser(username);
}
}