fix(shelf): fix public shelf book visibility (#1735)
This commit is contained in:
@@ -12,6 +12,7 @@ import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.UserContentRestrictionRepository;
|
||||
import org.booklore.security.policy.ContentRestrictionSpecification;
|
||||
import org.booklore.service.restriction.ContentRestrictionService;
|
||||
import org.booklore.util.BookUtils;
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
@@ -154,9 +155,7 @@ public class BookQueryService {
|
||||
}
|
||||
|
||||
if (dto.getShelves() != null && userId != null) {
|
||||
dto.setShelves(dto.getShelves().stream()
|
||||
.filter(shelf -> userId.equals(shelf.getUserId()))
|
||||
.collect(Collectors.toSet()));
|
||||
dto.setShelves(BookUtils.filterShelvesByUserId(dto.getShelves(), userId));
|
||||
}
|
||||
|
||||
if (stripForListView) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.booklore.service.book;
|
||||
|
||||
import org.booklore.mapper.v2.BookMapperV2;
|
||||
import org.booklore.model.dto.Book;
|
||||
import org.booklore.model.dto.Shelf;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class BookQueryServiceTest {
|
||||
|
||||
@Test
|
||||
void mapEntitiesToDto_keepsPublicShelvesForUser() {
|
||||
BookMapperV2 bookMapperV2 = mock(BookMapperV2.class);
|
||||
BookQueryService bookQueryService = new BookQueryService(null, bookMapperV2, null, null);
|
||||
BookEntity bookEntity = BookEntity.builder().id(1L).build();
|
||||
Shelf ownShelf = Shelf.builder().id(1L).userId(1L).publicShelf(false).build();
|
||||
Shelf otherPrivateShelf = Shelf.builder().id(2L).userId(2L).publicShelf(false).build();
|
||||
Shelf otherPublicShelf = Shelf.builder().id(3L).userId(2L).publicShelf(true).build();
|
||||
when(bookMapperV2.toDTO(bookEntity)).thenReturn(Book.builder()
|
||||
.shelves(Set.of(ownShelf, otherPrivateShelf, otherPublicShelf))
|
||||
.build());
|
||||
|
||||
List<Book> result = bookQueryService.mapEntitiesToDto(List.of(bookEntity), true, 1L);
|
||||
|
||||
assertThat(result.getFirst().getShelves())
|
||||
.containsExactlyInAnyOrder(ownShelf, otherPublicShelf);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user