Reading List Fixes (#1784)

* Add ability to save readinglist comicinfo fields in Chapter.

* Added the appropriate fields and migration for Reading List generation.

* Started the reading list code

* Started building out the CBL import code with some initial unit tests.

* Fixed first unit test

* Started refactoring control code into services and writing unit tests for ReadingLists. Found a logic issue around reading list title between create/update. Will be corrected in this branch with unit tests.

* Can't figure out how to mock UserManager, so had to uncomment a few tests.

* Tooltip for total pages read shows the full number

* Tweaked the math a bit for average reading per week.

* Fixed up the reading list unit tests. Fixed an issue where when inserting chapters into a blank reading list, the initial reading list item would have an order of 1 instead of 0.

* Cleaned up the code to allow the reading list code to be localized easily and fixed up a bug in last PR.

* Fixed a sorting issue on reading activity

* Tweaked the code around reading list actionables not showing due to some weird filter.

* Fixed edit library settings not opening on library detail page

* Fixed a bug where reading activity dates would be out of order due to a bug in how charts works. A temp hack has been added.

* Disable promotion in edit reading list modal since non-admins can (and should have) been able to use it.

* Fixed a bug where non-admins couldn't update their OWN reading lists. Made uploading a cover image for readinglists now check against the user's reading list access to allow non-admin's to set images.

* Fixed an issue introduced earlier in PR where adding chapters to reading list could cause order to get skewed.

* Fixed another regression from earlier commit

* Hooked in Import CBL flow. No functionality yet.

* Code is a mess. Shifting how the whole import process is going to be done. Commiting so I can pivot drastically.

* Very rough code for first step is done.

* Ui has started, I've run out of steam for this feature.

* Cleaned up the UI code a bit to make the step tracker nature easier without a dedicated component.

* Much flow implementation and tweaking to how validation checks and what is sent back.

* Removed import via cbl code as it's not done. Pushing to next release.
This commit is contained in:
Joe Milazzo 2023-02-12 08:20:51 -08:00 committed by GitHub
parent ae1af22af1
commit 3f24dc7392
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 21951 additions and 170 deletions

View file

@ -1,16 +1,20 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.Data.Repositories;
using API.DTOs.ReadingLists;
using API.DTOs.ReadingLists.CBL;
using API.Entities;
using API.Entities.Enums;
using API.Helpers;
using API.Services;
using API.SignalR;
using API.Tests.Helpers;
using AutoMapper;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
@ -24,7 +28,6 @@ public class ReadingListServiceTests
{
private readonly IUnitOfWork _unitOfWork;
private readonly IReadingListService _readingListService;
private readonly DataContext _context;
private const string CacheDirectory = "C:/kavita/config/cache/";
@ -43,7 +46,7 @@ public class ReadingListServiceTests
var mapper = config.CreateMapper();
_unitOfWork = new UnitOfWork(_context, mapper, null);
_readingListService = new ReadingListService(_unitOfWork, Substitute.For<ILogger<ReadingListService>>());
_readingListService = new ReadingListService(_unitOfWork, Substitute.For<ILogger<ReadingListService>>(), Substitute.For<IEventHub>());
}
#region Setup
@ -83,6 +86,7 @@ public class ReadingListServiceTests
private async Task ResetDb()
{
_context.AppUser.RemoveRange(_context.AppUser);
_context.Library.RemoveRange(_context.Library);
_context.Series.RemoveRange(_context.Series);
_context.ReadingList.RemoveRange(_context.ReadingList);
await _unitOfWork.CommitAsync();
@ -103,8 +107,148 @@ public class ReadingListServiceTests
#endregion
#region AddChaptersToReadingList
[Fact]
public async Task AddChaptersToReadingList_ShouldAddFirstItem_AsOrderZero()
{
await ResetDb();
_context.AppUser.Add(new AppUser()
{
UserName = "majora2007",
ReadingLists = new List<ReadingList>(),
Libraries = new List<Library>()
{
new Library()
{
Name = "Test LIb",
Type = LibraryType.Book,
Series = new List<Series>()
{
new Series()
{
Name = "Test",
Metadata = DbFactory.SeriesMetadata(new List<CollectionTag>()),
Volumes = new List<Volume>()
{
new Volume()
{
Name = "0",
Chapters = new List<Chapter>()
{
new Chapter()
{
Number = "1",
AgeRating = AgeRating.Everyone,
},
new Chapter()
{
Number = "2",
AgeRating = AgeRating.X18Plus
},
new Chapter()
{
Number = "3",
AgeRating = AgeRating.X18Plus
}
}
}
}
}
}
},
}
});
await _context.SaveChangesAsync();
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync("majora2007", AppUserIncludes.ReadingLists);
var readingList = new ReadingList();
user.ReadingLists = new List<ReadingList>()
{
readingList
};
await _readingListService.AddChaptersToReadingList(1, new List<int>() {1}, readingList);
await _unitOfWork.CommitAsync();
Assert.Equal(1, readingList.Items.Count);
Assert.Equal(0, readingList.Items.First().Order);
}
[Fact]
public async Task AddChaptersToReadingList_ShouldNewItems_AfterLastOrder()
{
await ResetDb();
_context.AppUser.Add(new AppUser()
{
UserName = "majora2007",
ReadingLists = new List<ReadingList>(),
Libraries = new List<Library>()
{
new Library()
{
Name = "Test LIb",
Type = LibraryType.Book,
Series = new List<Series>()
{
new Series()
{
Name = "Test",
Metadata = DbFactory.SeriesMetadata(new List<CollectionTag>()),
Volumes = new List<Volume>()
{
new Volume()
{
Name = "0",
Chapters = new List<Chapter>()
{
new Chapter()
{
Number = "1",
AgeRating = AgeRating.Everyone,
},
new Chapter()
{
Number = "2",
AgeRating = AgeRating.X18Plus
},
new Chapter()
{
Number = "3",
AgeRating = AgeRating.X18Plus
}
}
}
}
}
}
},
}
});
await _context.SaveChangesAsync();
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync("majora2007", AppUserIncludes.ReadingLists);
var readingList = new ReadingList();
user.ReadingLists = new List<ReadingList>()
{
readingList
};
await _readingListService.AddChaptersToReadingList(1, new List<int>() {1}, readingList);
await _unitOfWork.CommitAsync();
await _readingListService.AddChaptersToReadingList(1, new List<int>() {2}, readingList);
await _unitOfWork.CommitAsync();
Assert.Equal(2, readingList.Items.Count);
Assert.Equal(0, readingList.Items.First().Order);
Assert.Equal(1, readingList.Items.ElementAt(1).Order);
}
#endregion
#region UpdateReadingListItemPosition
[Fact]
public async Task UpdateReadingListItemPosition_MoveLastToFirst_TwoItemsShouldShift()
{
@ -623,4 +767,516 @@ public class ReadingListServiceTests
}
#endregion
#region CreateReadingList
private async Task CreateReadingList_SetupBaseData()
{
var fablesSeries = DbFactory.Series("Fables");
fablesSeries.Volumes.Add(new Volume()
{
Number = 1,
Name = "2002",
Chapters = new List<Chapter>()
{
EntityFactory.CreateChapter("1", false),
}
});
_context.AppUser.Add(new AppUser()
{
UserName = "majora2007",
ReadingLists = new List<ReadingList>(),
Libraries = new List<Library>()
{
new Library()
{
Name = "Test Lib",
Type = LibraryType.Book,
Series = new List<Series>()
{
fablesSeries,
},
},
},
});
_context.AppUser.Add(new AppUser()
{
UserName = "admin",
ReadingLists = new List<ReadingList>(),
Libraries = new List<Library>()
{
new Library()
{
Name = "Test Lib 2",
Type = LibraryType.Book,
Series = new List<Series>()
{
fablesSeries,
},
},
}
});
await _unitOfWork.CommitAsync();
}
[Fact]
public async Task CreateReadingList_ShouldCreate_WhenNoOtherListsOnUser()
{
await ResetDb();
await CreateReadingList_SetupBaseData();
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists);
await _readingListService.CreateReadingListForUser(user, "Test List");
Assert.NotEmpty((await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists))
.ReadingLists);
}
[Fact]
public async Task CreateReadingList_ShouldNotCreate_WhenExistingList()
{
await ResetDb();
await CreateReadingList_SetupBaseData();
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists);
await _readingListService.CreateReadingListForUser(user, "Test List");
Assert.NotEmpty((await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists))
.ReadingLists);
try
{
await _readingListService.CreateReadingListForUser(user, "Test List");
}
catch (Exception ex)
{
Assert.Equal("A list of this name already exists", ex.Message);
}
Assert.Single((await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists))
.ReadingLists);
}
[Fact]
public async Task CreateReadingList_ShouldNotCreate_WhenPromotedListExists()
{
await ResetDb();
await CreateReadingList_SetupBaseData();
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync("admin", AppUserIncludes.ReadingLists);
var list = await _readingListService.CreateReadingListForUser(user, "Test List");
await _readingListService.UpdateReadingList(list,
new UpdateReadingListDto()
{
ReadingListId = list.Id, Promoted = true, Title = list.Title, Summary = list.Summary,
CoverImageLocked = false
});
try
{
await _readingListService.CreateReadingListForUser(user, "Test List");
}
catch (Exception ex)
{
Assert.Equal("A list of this name already exists", ex.Message);
}
}
#endregion
#region UpdateReadingList
#endregion
#region DeleteReadingList
[Fact]
public async Task DeleteReadingList_ShouldDelete()
{
await ResetDb();
await CreateReadingList_SetupBaseData();
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists);
await _readingListService.CreateReadingListForUser(user, "Test List");
Assert.NotEmpty((await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists))
.ReadingLists);
try
{
await _readingListService.CreateReadingListForUser(user, "Test List");
}
catch (Exception ex)
{
Assert.Equal("A list of this name already exists", ex.Message);
}
Assert.Single((await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists))
.ReadingLists);
await _readingListService.DeleteReadingList(1, user);
Assert.Empty((await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists))
.ReadingLists);
}
#endregion
#region UserHasReadingListAccess
// TODO: UserHasReadingListAccess tests are unavailable because I can't mock UserManager<AppUser>
public async Task UserHasReadingListAccess_ShouldWorkIfTheirList()
{
await ResetDb();
await CreateReadingList_SetupBaseData();
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists);
await _readingListService.CreateReadingListForUser(user, "Test List");
var userWithList = await _readingListService.UserHasReadingListAccess(1, "majora2007");
Assert.NotNull(userWithList);
Assert.Single(userWithList.ReadingLists);
}
public async Task UserHasReadingListAccess_ShouldNotWork_IfNotTheirList()
{
await ResetDb();
await CreateReadingList_SetupBaseData();
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(2, AppUserIncludes.ReadingLists);
await _readingListService.CreateReadingListForUser(user, "Test List");
var userWithList = await _readingListService.UserHasReadingListAccess(1, "majora2007");
Assert.Null(userWithList);
}
public async Task UserHasReadingListAccess_ShouldWork_IfNotTheirList_ButUserIsAdmin()
{
await ResetDb();
await CreateReadingList_SetupBaseData();
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists);
await _readingListService.CreateReadingListForUser(user, "Test List");
//var admin = await _unitOfWork.UserRepository.GetUserByIdAsync(2, AppUserIncludes.ReadingLists);
//_userManager.When(x => x.IsInRoleAsync(user, PolicyConstants.AdminRole)).Returns((info => true), null);
//_userManager.IsInRoleAsync(admin, PolicyConstants.AdminRole).ReturnsForAnyArgs(true);
var userWithList = await _readingListService.UserHasReadingListAccess(1, "majora2007");
Assert.NotNull(userWithList);
Assert.Single(userWithList.ReadingLists);
}
#endregion
//
// #region CreateReadingListFromCBL
//
// private static CblReadingList LoadCblFromPath(string path)
// {
// var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ReadingListService/");
//
// var reader = new System.Xml.Serialization.XmlSerializer(typeof(CblReadingList));
// using var file = new StreamReader(Path.Join(testDirectory, path));
// var cblReadingList = (CblReadingList) reader.Deserialize(file);
// file.Close();
// return cblReadingList;
// }
//
// [Fact]
// public async Task CreateReadingListFromCBL_ShouldCreateList()
// {
// await ResetDb();
// var cblReadingList = LoadCblFromPath("Fables.cbl");
//
// // Mock up our series
// var fablesSeries = DbFactory.Series("Fables");
// var fables2Series = DbFactory.Series("Fables: The Last Castle");
//
// fablesSeries.Volumes.Add(new Volume()
// {
// Number = 1,
// Name = "2002",
// Chapters = new List<Chapter>()
// {
// EntityFactory.CreateChapter("1", false),
// EntityFactory.CreateChapter("2", false),
// EntityFactory.CreateChapter("3", false),
//
// }
// });
// fables2Series.Volumes.Add(new Volume()
// {
// Number = 1,
// Name = "2003",
// Chapters = new List<Chapter>()
// {
// EntityFactory.CreateChapter("1", false),
// EntityFactory.CreateChapter("2", false),
// EntityFactory.CreateChapter("3", false),
//
// }
// });
//
// _context.AppUser.Add(new AppUser()
// {
// UserName = "majora2007",
// ReadingLists = new List<ReadingList>(),
// Libraries = new List<Library>()
// {
// new Library()
// {
// Name = "Test LIb",
// Type = LibraryType.Book,
// Series = new List<Series>()
// {
// fablesSeries,
// fables2Series
// },
// },
// },
// });
// await _unitOfWork.CommitAsync();
//
// var importSummary = await _readingListService.CreateReadingListFromCbl(1, cblReadingList);
//
// Assert.Equal(CblImportResult.Partial, importSummary.Success);
// Assert.NotEmpty(importSummary.Results);
//
// var createdList = await _unitOfWork.ReadingListRepository.GetReadingListByIdAsync(1);
//
// Assert.NotNull(createdList);
// Assert.Equal("Fables", createdList.Title);
//
// Assert.Equal(4, createdList.Items.Count);
// Assert.Equal(1, createdList.Items.First(item => item.Order == 0).ChapterId);
// Assert.Equal(2, createdList.Items.First(item => item.Order == 1).ChapterId);
// Assert.Equal(3, createdList.Items.First(item => item.Order == 2).ChapterId);
// Assert.Equal(4, createdList.Items.First(item => item.Order == 3).ChapterId);
// }
//
// [Fact]
// public async Task CreateReadingListFromCBL_ShouldCreateList_ButOnlyIncludeSeriesThatUserHasAccessTo()
// {
// await ResetDb();
// var cblReadingList = LoadCblFromPath("Fables.cbl");
//
// // Mock up our series
// var fablesSeries = DbFactory.Series("Fables");
// var fables2Series = DbFactory.Series("Fables: The Last Castle");
//
// fablesSeries.Volumes.Add(new Volume()
// {
// Number = 1,
// Name = "2002",
// Chapters = new List<Chapter>()
// {
// EntityFactory.CreateChapter("1", false),
// EntityFactory.CreateChapter("2", false),
// EntityFactory.CreateChapter("3", false),
//
// }
// });
// fables2Series.Volumes.Add(new Volume()
// {
// Number = 1,
// Name = "2003",
// Chapters = new List<Chapter>()
// {
// EntityFactory.CreateChapter("1", false),
// EntityFactory.CreateChapter("2", false),
// EntityFactory.CreateChapter("3", false),
//
// }
// });
//
// _context.AppUser.Add(new AppUser()
// {
// UserName = "majora2007",
// ReadingLists = new List<ReadingList>(),
// Libraries = new List<Library>()
// {
// new Library()
// {
// Name = "Test LIb",
// Type = LibraryType.Book,
// Series = new List<Series>()
// {
// fablesSeries,
// },
// },
// },
// });
//
// _context.Library.Add(new Library()
// {
// Name = "Test Lib 2",
// Type = LibraryType.Book,
// Series = new List<Series>()
// {
// fables2Series,
// },
// });
//
// await _unitOfWork.CommitAsync();
//
// var importSummary = await _readingListService.CreateReadingListFromCbl(1, cblReadingList);
//
// Assert.Equal(CblImportResult.Partial, importSummary.Success);
// Assert.NotEmpty(importSummary.Results);
//
// var createdList = await _unitOfWork.ReadingListRepository.GetReadingListByIdAsync(1);
//
// Assert.NotNull(createdList);
// Assert.Equal("Fables", createdList.Title);
//
// Assert.Equal(3, createdList.Items.Count);
// Assert.Equal(1, createdList.Items.First(item => item.Order == 0).ChapterId);
// Assert.Equal(2, createdList.Items.First(item => item.Order == 1).ChapterId);
// Assert.Equal(3, createdList.Items.First(item => item.Order == 2).ChapterId);
// Assert.NotNull(importSummary.Results.SingleOrDefault(r => r.Series == "Fables: The Last Castle"
// && r.Reason == CblImportReason.SeriesMissing));
// }
//
// [Fact]
// public async Task CreateReadingListFromCBL_ShouldFail_UserHasAccessToNoSeries()
// {
// await ResetDb();
// var cblReadingList = LoadCblFromPath("Fables.cbl");
//
// // Mock up our series
// var fablesSeries = DbFactory.Series("Fables");
// var fables2Series = DbFactory.Series("Fables: The Last Castle");
//
// fablesSeries.Volumes.Add(new Volume()
// {
// Number = 1,
// Name = "2002",
// Chapters = new List<Chapter>()
// {
// EntityFactory.CreateChapter("1", false),
// EntityFactory.CreateChapter("2", false),
// EntityFactory.CreateChapter("3", false),
//
// }
// });
// fables2Series.Volumes.Add(new Volume()
// {
// Number = 1,
// Name = "2003",
// Chapters = new List<Chapter>()
// {
// EntityFactory.CreateChapter("1", false),
// EntityFactory.CreateChapter("2", false),
// EntityFactory.CreateChapter("3", false),
//
// }
// });
//
// _context.AppUser.Add(new AppUser()
// {
// UserName = "majora2007",
// ReadingLists = new List<ReadingList>(),
// Libraries = new List<Library>(),
// });
//
// _context.Library.Add(new Library()
// {
// Name = "Test Lib 2",
// Type = LibraryType.Book,
// Series = new List<Series>()
// {
// fablesSeries,
// fables2Series,
// },
// });
//
// await _unitOfWork.CommitAsync();
//
// var importSummary = await _readingListService.CreateReadingListFromCbl(1, cblReadingList);
//
// Assert.Equal(CblImportResult.Fail, importSummary.Success);
// Assert.NotEmpty(importSummary.Results);
//
// var createdList = await _unitOfWork.ReadingListRepository.GetReadingListByIdAsync(1);
//
// Assert.Null(createdList);
// }
//
//
// [Fact]
// public async Task CreateReadingListFromCBL_ShouldUpdateAnExistingList()
// {
// await ResetDb();
// var cblReadingList = LoadCblFromPath("Fables.cbl");
//
// // Mock up our series
// var fablesSeries = DbFactory.Series("Fables");
// var fables2Series = DbFactory.Series("Fables: The Last Castle");
//
// fablesSeries.Volumes.Add(new Volume()
// {
// Number = 1,
// Name = "2002",
// Chapters = new List<Chapter>()
// {
// EntityFactory.CreateChapter("1", false),
// EntityFactory.CreateChapter("2", false),
// EntityFactory.CreateChapter("3", false),
//
// }
// });
// fables2Series.Volumes.Add(new Volume()
// {
// Number = 1,
// Name = "2003",
// Chapters = new List<Chapter>()
// {
// EntityFactory.CreateChapter("1", false),
// EntityFactory.CreateChapter("2", false),
// EntityFactory.CreateChapter("3", false),
//
// }
// });
//
// _context.AppUser.Add(new AppUser()
// {
// UserName = "majora2007",
// ReadingLists = new List<ReadingList>(),
// Libraries = new List<Library>()
// {
// new Library()
// {
// Name = "Test LIb",
// Type = LibraryType.Book,
// Series = new List<Series>()
// {
// fablesSeries,
// fables2Series
// },
// },
// },
// });
//
// await _unitOfWork.CommitAsync();
//
// // Create a reading list named Fables and add 2 chapters to it
// var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.ReadingLists);
// var readingList = await _readingListService.CreateReadingListForUser(user, "Fables");
// Assert.True(await _readingListService.AddChaptersToReadingList(1, new List<int>() {1, 3}, readingList));
// Assert.Equal(2, readingList.Items.Count);
//
// // Attempt to import a Cbl with same reading list name
// var importSummary = await _readingListService.CreateReadingListFromCbl(1, cblReadingList);
//
// Assert.Equal(CblImportResult.Partial, importSummary.Success);
// Assert.NotEmpty(importSummary.Results);
//
// var createdList = await _unitOfWork.ReadingListRepository.GetReadingListByIdAsync(1);
//
// Assert.NotNull(createdList);
// Assert.Equal("Fables", createdList.Title);
//
// Assert.Equal(4, createdList.Items.Count);
// Assert.Equal(4, importSummary.SuccessfulInserts.Count);
//
// Assert.Equal(1, createdList.Items.First(item => item.Order == 0).ChapterId);
// Assert.Equal(3, createdList.Items.First(item => item.Order == 1).ChapterId); // we inserted 3 first
// Assert.Equal(2, createdList.Items.First(item => item.Order == 2).ChapterId);
// Assert.Equal(4, createdList.Items.First(item => item.Order == 3).ChapterId);
// }
// #endregion
//
}