mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-06-22 06:50:32 -04:00
* Moved the Server Settings out into a button on nav header * Refactored Mange Users page to the new design (skeleton). Implemented skeleton code for Invite User. * Hashed out more of the code, but need to move all the email code to a Kavita controlled API server due to password credentials. * Cleaned up some warnings * When no user exists for an api key in Plugin controller, throw 401. * Hooked in the ability to check if the Kavita instance can be accessed externally so we can determine if the user can invite or not. * Hooked up some logic if the user's server isn't accessible, then default to old flow * Basic flow is working for confirm email. Needs validation, error handling, etc. * Refactored Password validation to account service * Cleaned up the code in confirm-email to work much better. * Refactored the login page to have a container functionality, so we can reuse the styles on multiple pages (registration pages). Hooked up the code for confirm email. * Messy code, but making progress. Refactored Register to be used only for first time user registration. Added a new register component to handle first time flow only. * Invite works much better, still needs a bit of work for non-accessible server setup. Started work on underlying manage users page to meet new design. * Changed (you) to a star to indicate who you're logged in as. * Inviting a user is now working and tested fully. * Removed the register member component as we now have invite and confirm components. * Editing a user is now working. Username change and Role/Library access from within one screen. Email changing is on hold. * Cleaned up code for edit user and disabled email field for now. * Cleaned up the code to indicate changing a user's email is not possible. * Implemented a migration for existing accounts so they can validate their emails and still login. * Change url for email server * Implemented the ability to resend an email confirmation code (or regenerate for non accessible servers). Fixed an overflow on the confirm dialog. * Took care of some code cleanup * Removed 3 db calls from cover refresh and some misc cleanup * Fixed a broken test
1269 lines
40 KiB
C#
1269 lines
40 KiB
C#
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.IO.Abstractions.TestingHelpers;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Data;
|
|
using API.Data.Repositories;
|
|
using API.DTOs;
|
|
using API.Entities;
|
|
using API.Entities.Enums;
|
|
using API.Helpers;
|
|
using API.Services;
|
|
using API.SignalR;
|
|
using API.Tests.Helpers;
|
|
using AutoMapper;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
|
|
namespace API.Tests.Services;
|
|
|
|
public class ReaderServiceTests
|
|
{
|
|
private readonly ILogger<CacheService> _logger = Substitute.For<ILogger<CacheService>>();
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly IHubContext<MessageHub> _messageHub = Substitute.For<IHubContext<MessageHub>>();
|
|
|
|
private readonly DbConnection _connection;
|
|
private readonly DataContext _context;
|
|
|
|
private const string CacheDirectory = "C:/kavita/config/cache/";
|
|
private const string CoverImageDirectory = "C:/kavita/config/covers/";
|
|
private const string BackupDirectory = "C:/kavita/config/backups/";
|
|
private const string DataDirectory = "C:/data/";
|
|
|
|
public ReaderServiceTests()
|
|
{
|
|
var contextOptions = new DbContextOptionsBuilder().UseSqlite(CreateInMemoryDatabase()).Options;
|
|
_connection = RelationalOptionsExtension.Extract(contextOptions).Connection;
|
|
|
|
_context = new DataContext(contextOptions);
|
|
Task.Run(SeedDb).GetAwaiter().GetResult();
|
|
|
|
var config = new MapperConfiguration(cfg => cfg.AddProfile<AutoMapperProfiles>());
|
|
var mapper = config.CreateMapper();
|
|
_unitOfWork = new UnitOfWork(_context, mapper, null);
|
|
}
|
|
|
|
#region Setup
|
|
|
|
private static DbConnection CreateInMemoryDatabase()
|
|
{
|
|
var connection = new SqliteConnection("Filename=:memory:");
|
|
|
|
connection.Open();
|
|
|
|
return connection;
|
|
}
|
|
|
|
public void Dispose() => _connection.Dispose();
|
|
|
|
private async Task<bool> SeedDb()
|
|
{
|
|
await _context.Database.MigrateAsync();
|
|
var filesystem = CreateFileSystem();
|
|
|
|
await Seed.SeedSettings(_context,
|
|
new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem));
|
|
|
|
var setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.CacheDirectory).SingleAsync();
|
|
setting.Value = CacheDirectory;
|
|
|
|
setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.BackupDirectory).SingleAsync();
|
|
setting.Value = BackupDirectory;
|
|
|
|
_context.ServerSetting.Update(setting);
|
|
|
|
_context.Library.Add(new Library()
|
|
{
|
|
Name = "Manga", Folders = new List<FolderPath>() {new FolderPath() {Path = "C:/data/"}}
|
|
});
|
|
return await _context.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
private async Task ResetDB()
|
|
{
|
|
_context.Series.RemoveRange(_context.Series.ToList());
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
private static MockFileSystem CreateFileSystem()
|
|
{
|
|
var fileSystem = new MockFileSystem();
|
|
fileSystem.Directory.SetCurrentDirectory("C:/kavita/");
|
|
fileSystem.AddDirectory("C:/kavita/config/");
|
|
fileSystem.AddDirectory(CacheDirectory);
|
|
fileSystem.AddDirectory(CoverImageDirectory);
|
|
fileSystem.AddDirectory(BackupDirectory);
|
|
fileSystem.AddDirectory(DataDirectory);
|
|
|
|
return fileSystem;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region FormatBookmarkFolderPath
|
|
|
|
[Theory]
|
|
[InlineData("/manga/", 1, 1, 1, "/manga/1/1/1")]
|
|
[InlineData("C:/manga/", 1, 1, 10001, "C:/manga/1/1/10001")]
|
|
public void FormatBookmarkFolderPathTest(string baseDir, int userId, int seriesId, int chapterId, string expected)
|
|
{
|
|
Assert.Equal(expected, ReaderService.FormatBookmarkFolderPath(baseDir, userId, seriesId, chapterId));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region CapPageToChapter
|
|
|
|
[Fact]
|
|
public async Task CapPageToChapterTest()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
new Volume()
|
|
{
|
|
Chapters = new List<Chapter>()
|
|
{
|
|
new Chapter()
|
|
{
|
|
Pages = 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
Assert.Equal(0, await readerService.CapPageToChapter(1, -1));
|
|
Assert.Equal(1, await readerService.CapPageToChapter(1, 10));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region SaveReadingProgress
|
|
|
|
[Fact]
|
|
public async Task SaveReadingProgress_ShouldCreateNewEntity()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
new Volume()
|
|
{
|
|
Chapters = new List<Chapter>()
|
|
{
|
|
new Chapter()
|
|
{
|
|
Pages = 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
var successful = await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
ChapterId = 1,
|
|
PageNum = 1,
|
|
SeriesId = 1,
|
|
VolumeId = 1,
|
|
BookScrollId = null
|
|
}, 1);
|
|
|
|
Assert.True(successful);
|
|
Assert.NotNull(await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(1, 1));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveReadingProgress_ShouldUpdateExisting()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
new Volume()
|
|
{
|
|
Chapters = new List<Chapter>()
|
|
{
|
|
new Chapter()
|
|
{
|
|
Pages = 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
var successful = await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
ChapterId = 1,
|
|
PageNum = 1,
|
|
SeriesId = 1,
|
|
VolumeId = 1,
|
|
BookScrollId = null
|
|
}, 1);
|
|
|
|
Assert.True(successful);
|
|
Assert.NotNull(await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(1, 1));
|
|
|
|
Assert.True(await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
ChapterId = 1,
|
|
PageNum = 1,
|
|
SeriesId = 1,
|
|
VolumeId = 1,
|
|
BookScrollId = "/h1/"
|
|
}, 1));
|
|
|
|
Assert.Equal("/h1/", (await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(1, 1)).BookScrollId);
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region MarkChaptersAsRead
|
|
|
|
[Fact]
|
|
public async Task MarkChaptersAsReadTest()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
new Volume()
|
|
{
|
|
Chapters = new List<Chapter>()
|
|
{
|
|
new Chapter()
|
|
{
|
|
Pages = 1
|
|
},
|
|
new Chapter()
|
|
{
|
|
Pages = 2
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
var volumes = await _unitOfWork.VolumeRepository.GetVolumes(1);
|
|
readerService.MarkChaptersAsRead(await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Progress), 1, volumes.First().Chapters);
|
|
await _context.SaveChangesAsync();
|
|
|
|
Assert.Equal(2, (await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Progress)).Progresses.Count);
|
|
}
|
|
#endregion
|
|
|
|
#region MarkChapterAsUnread
|
|
|
|
[Fact]
|
|
public async Task MarkChapterAsUnreadTest()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
new Volume()
|
|
{
|
|
Chapters = new List<Chapter>()
|
|
{
|
|
new Chapter()
|
|
{
|
|
Pages = 1
|
|
},
|
|
new Chapter()
|
|
{
|
|
Pages = 2
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
var volumes = (await _unitOfWork.VolumeRepository.GetVolumes(1)).ToList();
|
|
readerService.MarkChaptersAsRead(await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Progress), 1, volumes.First().Chapters);
|
|
|
|
await _context.SaveChangesAsync();
|
|
Assert.Equal(2, (await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Progress)).Progresses.Count);
|
|
|
|
readerService.MarkChaptersAsUnread(await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Progress), 1, volumes.First().Chapters);
|
|
await _context.SaveChangesAsync();
|
|
|
|
var progresses = (await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Progress)).Progresses;
|
|
Assert.Equal(0, progresses.Max(p => p.PagesRead));
|
|
Assert.Equal(2, progresses.Count);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetNextChapterIdAsync
|
|
|
|
[Fact]
|
|
public async Task GetNextChapterIdAsync_ShouldGetNextVolume()
|
|
{
|
|
// V1 -> V2
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("2", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("21", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("22", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("3", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("31", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("32", false, new List<MangaFile>()),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
var nextChapter = await readerService.GetNextChapterIdAsync(1, 1, 1, 1);
|
|
var actualChapter = await _unitOfWork.ChapterRepository.GetChapterAsync(nextChapter);
|
|
Assert.Equal("2", actualChapter.Range);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetNextChapterIdAsync_ShouldRollIntoNextVolume()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("2", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("21", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("22", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("3", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("31", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("32", false, new List<MangaFile>()),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
|
|
var nextChapter = await readerService.GetNextChapterIdAsync(1, 1, 2, 1);
|
|
var actualChapter = await _unitOfWork.ChapterRepository.GetChapterAsync(nextChapter);
|
|
Assert.Equal("21", actualChapter.Range);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetNextChapterIdAsync_ShouldNotMoveFromVolumeToSpecial()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("0", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("A.cbz", true, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("B.cbz", true, new List<MangaFile>()),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
|
|
var nextChapter = await readerService.GetNextChapterIdAsync(1, 1, 2, 1);
|
|
Assert.Equal(-1, nextChapter);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetNextChapterIdAsync_ShouldMoveFromSpecialToSpecial()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("0", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("A.cbz", true, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("B.cbz", true, new List<MangaFile>()),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
|
|
var nextChapter = await readerService.GetNextChapterIdAsync(1, 2, 3, 1);
|
|
Assert.NotEqual(-1, nextChapter);
|
|
var actualChapter = await _unitOfWork.ChapterRepository.GetChapterAsync(nextChapter);
|
|
Assert.Equal("B.cbz", actualChapter.Range);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetPrevChapterIdAsync
|
|
|
|
[Fact]
|
|
public async Task GetPrevChapterIdAsync_ShouldGetPrevVolume()
|
|
{
|
|
// V1 -> V2
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("2", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("21", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("22", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("3", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("31", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("32", false, new List<MangaFile>()),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
var prevChapter = await readerService.GetPrevChapterIdAsync(1, 1, 2, 1);
|
|
var actualChapter = await _unitOfWork.ChapterRepository.GetChapterAsync(prevChapter);
|
|
Assert.Equal("1", actualChapter.Range);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetPrevChapterIdAsync_ShouldRollIntoPrevVolume()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("2", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("21", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("22", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("3", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("31", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("32", false, new List<MangaFile>()),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
|
|
var prevChapter = await readerService.GetPrevChapterIdAsync(1, 2, 3, 1);
|
|
var actualChapter = await _unitOfWork.ChapterRepository.GetChapterAsync(prevChapter);
|
|
Assert.Equal("2", actualChapter.Range);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetPrevChapterIdAsync_ShouldMoveFromVolumeToSpecial()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("0", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("A.cbz", true, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("B.cbz", true, new List<MangaFile>()),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
|
|
var prevChapter = await readerService.GetPrevChapterIdAsync(1, 1, 1, 1);
|
|
Assert.NotEqual(-1, prevChapter);
|
|
var actualChapter = await _unitOfWork.ChapterRepository.GetChapterAsync(prevChapter);
|
|
Assert.Equal("B.cbz", actualChapter.Range);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetPrevChapterIdAsync_ShouldMoveFromSpecialToSpecial()
|
|
{
|
|
await ResetDB();
|
|
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>()),
|
|
}),
|
|
EntityFactory.CreateVolume("0", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("A.cbz", true, new List<MangaFile>()),
|
|
EntityFactory.CreateChapter("B.cbz", true, new List<MangaFile>()),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
|
|
var prevChapter = await readerService.GetPrevChapterIdAsync(1, 2, 4, 1);
|
|
Assert.NotEqual(-1, prevChapter);
|
|
var actualChapter = await _unitOfWork.ChapterRepository.GetChapterAsync(prevChapter);
|
|
Assert.Equal("A.cbz", actualChapter.Range);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetContinuePoint
|
|
|
|
[Fact]
|
|
public async Task GetContinuePoint_ShouldReturnFirstNonSpecial()
|
|
{
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>(), 1),
|
|
}),
|
|
EntityFactory.CreateVolume("2", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("21", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("22", false, new List<MangaFile>(), 1),
|
|
}),
|
|
EntityFactory.CreateVolume("3", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("31", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("32", false, new List<MangaFile>(), 1),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
// Save progress on first volume chapters and 1st of second volume
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 1,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 2,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 3,
|
|
SeriesId = 1,
|
|
VolumeId = 2
|
|
}, 1);
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var nextChapter = await readerService.GetContinuePoint(1, 1);
|
|
|
|
Assert.Equal("22", nextChapter.Range);
|
|
|
|
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetContinuePoint_ShouldReturnFirstSpecial()
|
|
{
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>(), 1),
|
|
}),
|
|
EntityFactory.CreateVolume("2", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("21", false, new List<MangaFile>(), 1),
|
|
}),
|
|
EntityFactory.CreateVolume("0", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("31", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("32", false, new List<MangaFile>(), 1),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
// Save progress on first volume chapters and 1st of second volume
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 1,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 2,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 3,
|
|
SeriesId = 1,
|
|
VolumeId = 2
|
|
}, 1);
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var nextChapter = await readerService.GetContinuePoint(1, 1);
|
|
|
|
Assert.Equal("31", nextChapter.Range);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetContinuePoint_ShouldReturnFirstChapter_WhenAllRead()
|
|
{
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>(), 1),
|
|
}),
|
|
EntityFactory.CreateVolume("2", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("21", false, new List<MangaFile>(), 1),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
// Save progress on first volume chapters and 1st of second volume
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 1,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 2,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 3,
|
|
SeriesId = 1,
|
|
VolumeId = 2
|
|
}, 1);
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var nextChapter = await readerService.GetContinuePoint(1, 1);
|
|
|
|
Assert.Equal("1", nextChapter.Range);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetContinuePoint_ShouldReturnFirstChapter_WhenAllReadAndAllChapters()
|
|
{
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("0", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("3", false, new List<MangaFile>(), 1),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
// Save progress on first volume chapters and 1st of second volume
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 1,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 2,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 3,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var nextChapter = await readerService.GetContinuePoint(1, 1);
|
|
|
|
Assert.Equal("1", nextChapter.Range);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetContinuePoint_ShouldReturnFirstSpecial_WhenAllReadAndAllChapters()
|
|
{
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("0", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("3", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("Some Special Title", true, new List<MangaFile>(), 1),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
// Save progress on first volume chapters and 1st of second volume
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 1,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 2,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
await readerService.SaveReadingProgress(new ProgressDto()
|
|
{
|
|
PageNum = 1,
|
|
ChapterId = 3,
|
|
SeriesId = 1,
|
|
VolumeId = 1
|
|
}, 1);
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
var nextChapter = await readerService.GetContinuePoint(1, 1);
|
|
|
|
Assert.Equal("Some Special Title", nextChapter.Range);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region MarkChaptersUntilAsRead
|
|
|
|
[Fact]
|
|
public async Task MarkChaptersUntilAsRead_ShouldMarkAllChaptersAsRead()
|
|
{
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("0", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("3", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("Some Special Title", true, new List<MangaFile>(), 1),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync("majora2007", AppUserIncludes.Progress);
|
|
await readerService.MarkChaptersUntilAsRead(user, 1, 5);
|
|
await _context.SaveChangesAsync();
|
|
|
|
// Validate correct chapters have read status
|
|
Assert.Equal(1, (await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(1, 1)).PagesRead);
|
|
Assert.Equal(1, (await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(2, 1)).PagesRead);
|
|
Assert.Equal(1, (await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(3, 1)).PagesRead);
|
|
Assert.Null((await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(4, 1)));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task MarkChaptersUntilAsRead_ShouldMarkUptTillChapterNumberAsRead()
|
|
{
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("0", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("1", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("2", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("2.5", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("3", false, new List<MangaFile>(), 1),
|
|
EntityFactory.CreateChapter("Some Special Title", true, new List<MangaFile>(), 1),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync("majora2007", AppUserIncludes.Progress);
|
|
await readerService.MarkChaptersUntilAsRead(user, 1, 2.5f);
|
|
await _context.SaveChangesAsync();
|
|
|
|
// Validate correct chapters have read status
|
|
Assert.Equal(1, (await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(1, 1)).PagesRead);
|
|
Assert.Equal(1, (await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(2, 1)).PagesRead);
|
|
Assert.Equal(1, (await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(3, 1)).PagesRead);
|
|
Assert.Null((await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(4, 1)));
|
|
Assert.Null((await _unitOfWork.AppUserProgressRepository.GetUserProgressAsync(5, 1)));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task MarkChaptersUntilAsRead_ShouldNotReadOnlyVolumesWithChapter0()
|
|
{
|
|
_context.Series.Add(new Series()
|
|
{
|
|
Name = "Test",
|
|
Library = new Library() {
|
|
Name = "Test LIb",
|
|
Type = LibraryType.Manga,
|
|
},
|
|
Volumes = new List<Volume>()
|
|
{
|
|
EntityFactory.CreateVolume("1", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("0", false, new List<MangaFile>(), 1),
|
|
}),
|
|
EntityFactory.CreateVolume("2", new List<Chapter>()
|
|
{
|
|
EntityFactory.CreateChapter("0", false, new List<MangaFile>(), 1),
|
|
}),
|
|
}
|
|
});
|
|
|
|
_context.AppUser.Add(new AppUser()
|
|
{
|
|
UserName = "majora2007"
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
|
|
var readerService = new ReaderService(_unitOfWork, Substitute.For<ILogger<ReaderService>>());
|
|
|
|
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync("majora2007", AppUserIncludes.Progress);
|
|
await readerService.MarkChaptersUntilAsRead(user, 1, 2);
|
|
await _context.SaveChangesAsync();
|
|
|
|
// Validate correct chapters have read status
|
|
Assert.False(await _unitOfWork.AppUserProgressRepository.UserHasProgress(LibraryType.Manga, 1));
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
}
|