From ad5b83781a7d3d2ddbd2903890031257952a05f7 Mon Sep 17 00:00:00 2001 From: Chris Blake <1481712+chrisb92@users.noreply.github.com> Date: Wed, 1 Feb 2023 18:17:18 +0000 Subject: [PATCH] Add 404 response to MarkPlayedItem/MarkUnplayedItem (#9211) Fixes https://github.com/jellyfin/jellyfin/issues/9120 --- .../Controllers/PlaystateController.cs | 37 ++++++++++---- Jellyfin.sln.DotSettings | 3 ++ .../Controllers/PlaystateControllerTests.cs | 50 +++++++++++++++++++ 3 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 Jellyfin.sln.DotSettings create mode 100644 tests/Jellyfin.Server.Integration.Tests/Controllers/PlaystateControllerTests.cs diff --git a/Jellyfin.Api/Controllers/PlaystateController.cs b/Jellyfin.Api/Controllers/PlaystateController.cs index 58f9b7d35..0260f9e2a 100644 --- a/Jellyfin.Api/Controllers/PlaystateController.cs +++ b/Jellyfin.Api/Controllers/PlaystateController.cs @@ -7,6 +7,7 @@ using Jellyfin.Api.Extensions; using Jellyfin.Api.Helpers; using Jellyfin.Api.ModelBinders; using Jellyfin.Data.Entities; +using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Session; using MediaBrowser.Model.Dto; @@ -65,9 +66,11 @@ namespace Jellyfin.Api.Controllers /// Item id. /// Optional. The date the item was played. /// Item marked as played. - /// An containing the . + /// Item not found. + /// An containing the , or a if item was not found. [HttpPost("Users/{userId}/PlayedItems/{itemId}")] [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> MarkPlayedItem( [FromRoute, Required] Guid userId, [FromRoute, Required] Guid itemId, @@ -75,11 +78,18 @@ namespace Jellyfin.Api.Controllers { var user = _userManager.GetUserById(userId); var session = await RequestHelpers.GetSession(_sessionManager, _userManager, HttpContext).ConfigureAwait(false); - var dto = UpdatePlayedStatus(user, itemId, true, datePlayed); + + var item = _libraryManager.GetItemById(itemId); + if (item is null) + { + return NotFound(); + } + + var dto = UpdatePlayedStatus(user, item, true, datePlayed); foreach (var additionalUserInfo in session.AdditionalUsers) { var additionalUser = _userManager.GetUserById(additionalUserInfo.UserId); - UpdatePlayedStatus(additionalUser, itemId, true, datePlayed); + UpdatePlayedStatus(additionalUser, item, true, datePlayed); } return dto; @@ -91,18 +101,27 @@ namespace Jellyfin.Api.Controllers /// User id. /// Item id. /// Item marked as unplayed. - /// A containing the . + /// Item not found. + /// A containing the , or a if item was not found. [HttpDelete("Users/{userId}/PlayedItems/{itemId}")] [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> MarkUnplayedItem([FromRoute, Required] Guid userId, [FromRoute, Required] Guid itemId) { var user = _userManager.GetUserById(userId); var session = await RequestHelpers.GetSession(_sessionManager, _userManager, HttpContext).ConfigureAwait(false); - var dto = UpdatePlayedStatus(user, itemId, false, null); + var item = _libraryManager.GetItemById(itemId); + + if (item is null) + { + return NotFound(); + } + + var dto = UpdatePlayedStatus(user, item, false, null); foreach (var additionalUserInfo in session.AdditionalUsers) { var additionalUser = _userManager.GetUserById(additionalUserInfo.UserId); - UpdatePlayedStatus(additionalUser, itemId, false, null); + UpdatePlayedStatus(additionalUser, item, false, null); } return dto; @@ -328,14 +347,12 @@ namespace Jellyfin.Api.Controllers /// Updates the played status. /// /// The user. - /// The item id. + /// The item. /// if set to true [was played]. /// The date played. /// Task. - private UserItemDataDto UpdatePlayedStatus(User user, Guid itemId, bool wasPlayed, DateTime? datePlayed) + private UserItemDataDto UpdatePlayedStatus(User user, BaseItem item, bool wasPlayed, DateTime? datePlayed) { - var item = _libraryManager.GetItemById(itemId); - if (wasPlayed) { item.MarkPlayed(user, datePlayed, true); diff --git a/Jellyfin.sln.DotSettings b/Jellyfin.sln.DotSettings new file mode 100644 index 000000000..b56741648 --- /dev/null +++ b/Jellyfin.sln.DotSettings @@ -0,0 +1,3 @@ + + True + True \ No newline at end of file diff --git a/tests/Jellyfin.Server.Integration.Tests/Controllers/PlaystateControllerTests.cs b/tests/Jellyfin.Server.Integration.Tests/Controllers/PlaystateControllerTests.cs new file mode 100644 index 000000000..f8f5fecec --- /dev/null +++ b/tests/Jellyfin.Server.Integration.Tests/Controllers/PlaystateControllerTests.cs @@ -0,0 +1,50 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using Xunit; +using Xunit.Priority; + +namespace Jellyfin.Server.Integration.Tests.Controllers; + +[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)] +public class PlaystateControllerTests : IClassFixture +{ + private readonly JellyfinApplicationFactory _factory; + private static readonly Guid _testUserId = Guid.NewGuid(); + private static readonly Guid _testItemId = Guid.NewGuid(); + private static string? _accessToken; + + public PlaystateControllerTests(JellyfinApplicationFactory factory) + { + _factory = factory; + } + + private Task DeleteUserPlayedItems(HttpClient httpClient, Guid userId, Guid itemId) + => httpClient.DeleteAsync($"Users/{userId}/PlayedItems/{itemId}"); + + private Task PostUserPlayedItems(HttpClient httpClient, Guid userId, Guid itemId) + => httpClient.PostAsync($"Users/{userId}/PlayedItems/{itemId}", null); + + [Fact] + [Priority(0)] + public async Task DeleteMarkUnplayedItem_DoesNotExist_NotFound() + { + var client = _factory.CreateClient(); + client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client).ConfigureAwait(false)); + + using var response = await DeleteUserPlayedItems(client, _testUserId, _testItemId).ConfigureAwait(false); + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + } + + [Fact] + [Priority(0)] + public async Task PostMarkPlayedItem_DoesNotExist_NotFound() + { + var client = _factory.CreateClient(); + client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client).ConfigureAwait(false)); + + using var response = await PostUserPlayedItems(client, _testUserId, _testItemId).ConfigureAwait(false); + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + } +}