Implements tests for commit 1aa427b1e2

This commit is contained in:
BinaryAlley committed 2026-10-28 13:39:12 +02:00
1 parent e52081533a
commit 8b8d077c6a
6 files changed
+305 -16

No files matched your search

@@ -41,7 +41,9 @@ using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
@@ -151,6 +153,56 @@ public class UpdateTrackEndpointTests : IClassFixture<AuthenticatedLuminaApiFact
Assert.Equal(originalPath, storedTrack.Path);
}
[Fact]
public async Task UpdateTrack_WhenRequestBodyCarriesAFilePath_ShouldIgnoreItAndPreserveTheStoredPath()
{
// Arrange
Guid userId = GetCurrentUserId();
Guid libraryId = await SeedLibraryAsync(userId);
string originalPath = Path.Combine(s_contentRootPath, "bohemian-rhapsody.flac");
string attemptedPath = Path.Combine(Path.GetTempPath(), "lumina-outside-content-root", "etc", "hosts");
(Guid artistId, Guid albumId, Guid trackId) = await SeedArtistGraphAsync(libraryId, originalPath);
UpdateTrackRequest request = _updateTrackRequestFixture.Create(
metadata: _musicTrackMetadataDtoFixture.Create(title: "Somebody to Love"),
moods: [],
isrcs: [],
contributors: [],
ratings: [],
includeDiscNumber: false,
includeScript: false,
includeKey: false,
includeBpm: false,
includeWork: false,
includeMusicBrainzRecordingId: false,
includeMusicBrainzTrackId: false);
JsonSerializerOptions serializationOptions = new(JsonSerializerDefaults.Web)
{
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
};
JsonObject body = JsonSerializer.SerializeToNode(request, serializationOptions)!.AsObject();
body["path"] = attemptedPath;
using StringContent jsonContent = new(body.ToJsonString(serializationOptions), Encoding.UTF8, "application/json");
// Act
HttpResponseMessage response = await _client.PutAsync($"/api/v1/libraries/{libraryId}/artists/{artistId}/albums/{albumId}/tracks/{trackId}", jsonContent);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
string content = await response.Content.ReadAsStringAsync();
TrackResponse? trackResponse = JsonSerializer.Deserialize<TrackResponse>(content, _jsonOptions);
Assert.NotNull(trackResponse);
// the posted file system path is ignored, because a track path is set only when the track is created by the scanner
Assert.Equal(originalPath, trackResponse!.Path);
Assert.NotEqual(attemptedPath, trackResponse.Path);
Assert.Equal("Somebody to Love", trackResponse.Metadata!.Title);
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
TrackEntity storedTrack = dbContext.Tracks.Single(track => track.Id == trackId);
Assert.Equal(originalPath, storedTrack.Path);
Assert.NotEqual(attemptedPath, storedTrack.Path);
}
[Fact]
public async Task UpdateTrack_WhenCalledByAdminOnAnotherUsersLibrary_ShouldUpdateTrack()
{
@@ -8,11 +8,15 @@ using Lumina.Presentation.Web.Fixtures.Common.TestHelpers;
using Lumina.Presentation.Web.IntegrationTests.Common.Setup;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
#endregion
@@ -24,6 +28,11 @@ namespace Lumina.Presentation.Web.IntegrationTests.Core.Endpoints.Library.AudioL
[ExcludeFromCodeCoverage]
public class SaveMusicTrackEndpointTests : IClassFixture<LuminaWebFactory>
{
private static readonly JsonSerializerOptions s_jsonOptions = new(JsonSerializerDefaults.Web)
{
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
};
private readonly LuminaWebFactory _apiFactory;
private readonly UpdateMusicTrackRequestFixture _updateMusicTrackRequestFixture = new();
private readonly MusicTrackDtoFixture _musicTrackDtoFixture = new();
@@ -66,6 +75,41 @@ public class SaveMusicTrackEndpointTests : IClassFixture<LuminaWebFactory>
Assert.Contains(_apiFactory.ApiClientStub.PutRequests, putRequest => putRequest.Endpoint == $"libraries/{libraryId}/artists/{artistId}/albums/{albumId}/tracks/{trackId}" && putRequest.Data is UpdateMusicTrackRequest forwarded && forwarded.Metadata!.Title == request.Metadata!.Title);
}
[Fact]
public async Task SaveMusicTrack_WhenPostedBodyCarriesAFilePath_ShouldNotForwardAFilePathToTheApi()
{
// Arrange
_apiFactory.ApiClientStub.Reset();
Guid libraryId = Guid.NewGuid();
Guid artistId = Guid.NewGuid();
Guid albumId = Guid.NewGuid();
Guid trackId = Guid.NewGuid();
string attemptedPath = Path.Combine(Path.GetTempPath(), "lumina-outside-content-root", "etc", "hosts");
UpdateMusicTrackRequest request = _updateMusicTrackRequestFixture.Create();
MusicTrackDto expectedTrack = _musicTrackDtoFixture.Create(id: trackId, albumId: albumId, libraryId: libraryId);
_apiFactory.ApiClientStub.RegisterPutResponse($"libraries/{libraryId}/artists/{artistId}/albums/{albumId}/tracks/{trackId}", expectedTrack);
AuthenticatedWebClient webClient = await WebTestHelpers.CreateAuthenticatedClientAsync(_apiFactory);
HttpRequestMessage saveRequest = CreateSaveRequest(artistId, albumId, trackId, libraryId, request, attemptedPath);
saveRequest.Headers.Add("RequestVerificationToken", webClient.AntiforgeryToken);
// Act
HttpResponseMessage response = await webClient.Client.SendAsync(saveRequest);
string content = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
using JsonDocument json = JsonDocument.Parse(content);
Assert.True(json.RootElement.GetProperty("success").GetBoolean());
(string Endpoint, object? Data) forwardedRequest = Assert.Single(
_apiFactory.ApiClientStub.PutRequests,
putRequest => putRequest.Endpoint == $"libraries/{libraryId}/artists/{artistId}/albums/{albumId}/tracks/{trackId}");
UpdateMusicTrackRequest forwarded = Assert.IsType<UpdateMusicTrackRequest>(forwardedRequest.Data);
Assert.Equal(request.Metadata!.Title, forwarded.Metadata!.Title);
// The Web request type has no file system path, so the posted path is dropped before the API call.
using JsonDocument forwardedJson = JsonDocument.Parse(JsonSerializer.Serialize(forwarded, s_jsonOptions));
Assert.False(forwardedJson.RootElement.TryGetProperty("path", out _));
}
[Fact]
public async Task SaveMusicTrack_WhenCalledWithoutAntiforgeryToken_ShouldRejectRequest()
{
@@ -123,4 +167,28 @@ public class SaveMusicTrackEndpointTests : IClassFixture<LuminaWebFactory>
saveRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return saveRequest;
}
/// <summary>
/// Builds the save request that PUTs the given <paramref name="request"/>, carrying an extra file system path property,
/// to the track identified by the route, with the owning library passed through the query string.
/// </summary>
/// <param name="artistId">The route Id of the artist the track belongs to.</param>
/// <param name="albumId">The route Id of the album the track belongs to.</param>
/// <param name="trackId">The route Id of the track to save.</param>
/// <param name="libraryId">The Id of the media library the track belongs to.</param>
/// <param name="request">The track data to send.</param>
/// <param name="attemptedPath">The file system path to inject into the posted body.</param>
/// <returns>The configured save request.</returns>
private static HttpRequestMessage CreateSaveRequest(Guid artistId, Guid albumId, Guid trackId, Guid libraryId, UpdateMusicTrackRequest request, string attemptedPath)
{
JsonObject body = JsonSerializer.SerializeToNode(request, s_jsonOptions)!.AsObject();
body["path"] = attemptedPath;
HttpRequestMessage saveRequest = new(HttpMethod.Put, $"/en-us/library/audio-library/music-library/artists/{artistId}/albums/{albumId}/tracks/{trackId}/api-save-track?libraryId={libraryId}")
{
Content = new StringContent(body.ToJsonString(s_jsonOptions), Encoding.UTF8, "application/json")
};
saveRequest.Content!.Headers.ContentType = new MediaTypeHeaderValue("application/json");
saveRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return saveRequest;
}
}