refactor(browse): extract shared pagination, scoping, and facet responses (#2351)

Co-authored-by: James Ward <james@notjam.es>
This commit is contained in:
Zach
2026-10-04 02:37:24 -06:00
committed by GitHub
co-authored by James Ward
parent d58345b350
commit 3c8c3987b8
11 changed files with 400 additions and 203 deletions
@@ -17,7 +17,10 @@ import org.booklore.model.entity.BookMetadataEntity;
import org.booklore.model.entity.CategoryEntity;
import org.booklore.model.entity.LibraryEntity;
import org.booklore.model.entity.LibraryPathEntity;
import org.booklore.model.entity.UserContentRestrictionEntity;
import org.booklore.model.enums.BookFileType;
import org.booklore.model.enums.ContentRestrictionMode;
import org.booklore.model.enums.ContentRestrictionType;
import org.booklore.repository.BookRepository;
import org.booklore.service.task.TaskCronService;
import org.flywaydb.core.Flyway;
@@ -325,4 +328,43 @@ class BookBrowseServiceTest {
assertThat(result.content()).hasSize(1);
assertThat(result.content().get(0).getId()).isNotEqualTo(outside.getId());
}
@Test
void nonAdminWithNoLibrariesSeesNothing() {
book("Alpha", List.of());
em.flush();
when(authenticationService.getAuthenticatedUser()).thenReturn(noLibrariesUser());
BrowsePage<Book> result = browse(null, null, null, null, 0, 20);
assertThat(result.content()).isEmpty();
assertThat(result.page().totalElements()).isZero();
assertThat(browseService.findAllIds(null, null, null, null)).isEmpty();
}
@Test
void contentRestrictionsExcludeBooks() {
em.persist(UserContentRestrictionEntity.builder()
.user(userEntity)
.restrictionType(ContentRestrictionType.CATEGORY)
.mode(ContentRestrictionMode.EXCLUDE)
.value("Horror")
.build());
Long romance = book("R", List.of("Romance")).getId();
book("H", List.of("Horror"));
em.flush();
assertThat(browse(null, null, null, null, 0, 20).content().stream().map(Book::getId))
.containsExactly(romance);
assertThat(browseService.findAllIds(null, null, null, null)).containsExactly(romance);
}
private BookLoreUser noLibrariesUser() {
BookLoreUser.UserPermissions permissions = new BookLoreUser.UserPermissions();
permissions.setAdmin(false);
return BookLoreUser.builder()
.id(userEntity.getId())
.assignedLibraries(List.of())
.permissions(permissions)
.build();
}
}
@@ -17,7 +17,10 @@ import org.booklore.model.entity.BookMetadataEntity;
import org.booklore.model.entity.CategoryEntity;
import org.booklore.model.entity.LibraryEntity;
import org.booklore.model.entity.LibraryPathEntity;
import org.booklore.model.entity.UserContentRestrictionEntity;
import org.booklore.model.enums.BookFileType;
import org.booklore.model.enums.ContentRestrictionMode;
import org.booklore.model.enums.ContentRestrictionType;
import org.booklore.service.task.TaskCronService;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.BeforeEach;
@@ -366,4 +369,41 @@ class BookFacetServiceTest {
assertThat(json).doesNotContain("\"rel\":[\"self\"]");
assertThat(json).doesNotContain("\"rel\":[\"facet\"]");
}
@Test
void nonAdminWithNoLibrariesGetsNoFacetValues() {
book("A", "Horror", "Alice");
em.flush();
BookLoreUser.UserPermissions permissions = new BookLoreUser.UserPermissions();
permissions.setAdmin(false);
when(authenticationService.getAuthenticatedUser()).thenReturn(BookLoreUser.builder()
.id(userEntity.getId())
.assignedLibraries(List.of())
.permissions(permissions)
.build());
FacetGroupsResponse response = facetService.getFacets(null, null, null);
assertThat(group(response, "genre").links()).isEmpty();
assertThat(group(response, "author").links()).isEmpty();
}
@Test
void contentRestrictionsExcludeBooksFromCounts() {
em.persist(UserContentRestrictionEntity.builder()
.user(userEntity)
.restrictionType(ContentRestrictionType.CATEGORY)
.mode(ContentRestrictionMode.EXCLUDE)
.value("Horror")
.build());
book("H", "Horror", "Alice");
book("R", "Romance", "Bob");
em.flush();
FacetGroupsResponse response = facetService.getFacets(null, null, null);
assertThat(count(group(response, "genre"), "Romance")).isEqualTo(1);
assertThat(count(group(response, "genre"), "Horror")).isNull();
assertThat(count(group(response, "author"), "Alice")).isNull();
}
}