Add extended permissions for bulk metadata, reading progress, and read status actions (#2091)

Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
ACX
2026-01-01 11:41:22 -07:00
committed by GitHub
co-authored by acx10
parent 60524ee8a6
commit e0dec6f2f6
34 changed files with 1117 additions and 331 deletions
@@ -29,19 +29,32 @@ import static org.mockito.Mockito.*;
class BookUpdateServiceTest {
@Mock private BookRepository bookRepository;
@Mock private PdfViewerPreferencesRepository pdfViewerPreferencesRepository;
@Mock private EpubViewerPreferencesRepository epubViewerPreferencesRepository;
@Mock private CbxViewerPreferencesRepository cbxViewerPreferencesRepository;
@Mock private NewPdfViewerPreferencesRepository newPdfViewerPreferencesRepository;
@Mock private ShelfRepository shelfRepository;
@Mock private BookMapper bookMapper;
@Mock private UserRepository userRepository;
@Mock private UserBookProgressRepository userBookProgressRepository;
@Mock private AuthenticationService authenticationService;
@Mock private BookQueryService bookQueryService;
@Mock private UserProgressService userProgressService;
@Mock private KoboReadingStateService koboReadingStateService;
@Mock
private BookRepository bookRepository;
@Mock
private PdfViewerPreferencesRepository pdfViewerPreferencesRepository;
@Mock
private EpubViewerPreferencesRepository epubViewerPreferencesRepository;
@Mock
private CbxViewerPreferencesRepository cbxViewerPreferencesRepository;
@Mock
private NewPdfViewerPreferencesRepository newPdfViewerPreferencesRepository;
@Mock
private ShelfRepository shelfRepository;
@Mock
private BookMapper bookMapper;
@Mock
private UserRepository userRepository;
@Mock
private UserBookProgressRepository userBookProgressRepository;
@Mock
private AuthenticationService authenticationService;
@Mock
private BookQueryService bookQueryService;
@Mock
private UserProgressService userProgressService;
@Mock
private KoboReadingStateService koboReadingStateService;
@InjectMocks
private BookUpdateService bookUpdateService;
@@ -207,6 +220,10 @@ class BookUpdateServiceTest {
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
when(user.getId()).thenReturn(1L);
BookLoreUser.UserPermissions permissions = mock(BookLoreUser.UserPermissions.class);
when(user.getPermissions()).thenReturn(permissions);
when(permissions.isCanBulkResetBookReadStatus()).thenReturn(true);
List<Long> bookIds = Arrays.asList(1L, 2L, 3L);
when(bookRepository.countByIdIn(bookIds)).thenReturn(3L);
Set<Long> existing = new HashSet<>(Arrays.asList(1L, 2L));
@@ -222,12 +239,32 @@ class BookUpdateServiceTest {
assertEquals(ReadStatus.READ, result.get(0).getReadStatus());
}
@Test
void updateReadStatus_shouldThrowIfNoBulkPermission() {
BookLoreUser user = mock(BookLoreUser.class);
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
when(user.getId()).thenReturn(1L);
BookLoreUser.UserPermissions permissions = mock(BookLoreUser.UserPermissions.class);
when(user.getPermissions()).thenReturn(permissions);
when(permissions.isCanBulkResetBookReadStatus()).thenReturn(false);
List<Long> bookIds = Arrays.asList(1L, 2L, 3L);
when(bookRepository.countByIdIn(bookIds)).thenReturn(3L);
assertThrows(APIException.class, () -> bookUpdateService.updateReadStatus(bookIds, "READ"));
}
@Test
void resetProgress_shouldCallBulkReset() {
BookLoreUser user = mock(BookLoreUser.class);
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
when(user.getId()).thenReturn(1L);
BookLoreUser.UserPermissions permissions = mock(BookLoreUser.UserPermissions.class);
when(user.getPermissions()).thenReturn(permissions);
when(permissions.isCanBulkResetBookloreReadProgress()).thenReturn(true);
List<Long> bookIds = Arrays.asList(1L, 2L);
when(bookRepository.countByIdIn(bookIds)).thenReturn(2L);
Set<Long> existing = new HashSet<>(bookIds);
@@ -238,6 +275,61 @@ class BookUpdateServiceTest {
assertEquals(2, result.size());
}
@Test
void resetProgress_shouldThrowIfNoBulkPermission() {
BookLoreUser user = mock(BookLoreUser.class);
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
when(user.getId()).thenReturn(1L);
BookLoreUser.UserPermissions permissions = mock(BookLoreUser.UserPermissions.class);
when(user.getPermissions()).thenReturn(permissions);
when(permissions.isCanBulkResetBookloreReadProgress()).thenReturn(false);
List<Long> bookIds = Arrays.asList(1L, 2L);
when(bookRepository.countByIdIn(bookIds)).thenReturn(2L);
assertThrows(APIException.class, () -> bookUpdateService.resetProgress(bookIds, ResetProgressType.BOOKLORE));
}
@Test
void resetProgress_shouldAllowSingleBookWithoutPermission() {
BookLoreUser user = mock(BookLoreUser.class);
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
when(user.getId()).thenReturn(1L);
BookLoreUser.UserPermissions permissions = mock(BookLoreUser.UserPermissions.class);
when(user.getPermissions()).thenReturn(permissions);
when(permissions.isCanBulkResetBookloreReadProgress()).thenReturn(false);
List<Long> bookIds = Collections.singletonList(1L);
when(bookRepository.countByIdIn(bookIds)).thenReturn(1L);
Set<Long> existing = new HashSet<>(bookIds);
when(userBookProgressRepository.findExistingProgressBookIds(1L, new HashSet<>(bookIds))).thenReturn(existing);
assertDoesNotThrow(() -> bookUpdateService.resetProgress(bookIds, ResetProgressType.BOOKLORE));
}
@Test
void updateReadStatus_shouldAllowSingleBookWithoutPermission() {
BookLoreUser user = mock(BookLoreUser.class);
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
when(user.getId()).thenReturn(1L);
BookLoreUser.UserPermissions permissions = mock(BookLoreUser.UserPermissions.class);
when(user.getPermissions()).thenReturn(permissions);
when(permissions.isCanBulkResetBookReadStatus()).thenReturn(false);
List<Long> bookIds = Collections.singletonList(1L);
when(bookRepository.countByIdIn(bookIds)).thenReturn(1L);
Set<Long> existing = new HashSet<>(bookIds);
when(userBookProgressRepository.findExistingProgressBookIds(1L, new HashSet<>(bookIds))).thenReturn(existing);
BookLoreUserEntity userEntity = new BookLoreUserEntity();
when(userRepository.findById(1L)).thenReturn(Optional.of(userEntity));
assertDoesNotThrow(() -> bookUpdateService.updateReadStatus(bookIds, "READ"));
}
@Test
void updatePersonalRating_shouldUpdateAndCreate() {
BookLoreUser user = mock(BookLoreUser.class);
@@ -283,8 +375,10 @@ class BookUpdateServiceTest {
when(user.getId()).thenReturn(1L);
BookLoreUserEntity userEntity = new BookLoreUserEntity();
ShelfEntity shelf1 = new ShelfEntity(); shelf1.setId(10L);
ShelfEntity shelf2 = new ShelfEntity(); shelf2.setId(20L);
ShelfEntity shelf1 = new ShelfEntity();
shelf1.setId(10L);
ShelfEntity shelf2 = new ShelfEntity();
shelf2.setId(20L);
userEntity.setShelves(new HashSet<>(Arrays.asList(shelf1, shelf2)));
when(userRepository.findById(1L)).thenReturn(Optional.of(userEntity));
@@ -311,7 +405,8 @@ class BookUpdateServiceTest {
bookEntity2.setFileName("file2.pdf");
when(bookQueryService.findAllWithMetadataByIds(bookIds)).thenReturn(Arrays.asList(bookEntity1, bookEntity2));
ShelfEntity assignShelf = new ShelfEntity(); assignShelf.setId(10L);
ShelfEntity assignShelf = new ShelfEntity();
assignShelf.setId(10L);
when(shelfRepository.findAllById(assignIds)).thenReturn(Collections.singletonList(assignShelf));
Book mockBook = mock(Book.class);
when(bookMapper.toBook(any())).thenReturn(mockBook);
@@ -330,7 +425,8 @@ class BookUpdateServiceTest {
when(user.getId()).thenReturn(1L);
BookLoreUserEntity userEntity = new BookLoreUserEntity();
ShelfEntity shelf1 = new ShelfEntity(); shelf1.setId(10L);
ShelfEntity shelf1 = new ShelfEntity();
shelf1.setId(10L);
userEntity.setShelves(new HashSet<>(Collections.singletonList(shelf1)));
when(userRepository.findById(1L)).thenReturn(Optional.of(userEntity));