use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.
the class BookController method findByRating.
@GetMapping(value = "/books", params = { "rating", "page", "size" })
public Page<Book> findByRating(@RequestParam("rating") String rating, @RequestParam(value = "page") int page, @RequestParam(value = "size") int size, Principal principal) {
if (null == rating || rating.trim().isEmpty()) {
throw new IllegalArgumentException("Rating parameter cannot be empty");
}
Book.Rating aRating = Book.Rating.getRatingByString(rating);
if (null == aRating) {
throw new IllegalArgumentException("Supplied rating parameter not recognised");
}
PageRequest pageObj = PageRequest.of(page, size);
return bookRepository.findByRatingOrderByEnteredDesc(pageObj, aRating);
}
use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.
the class LimitDataVisibilityAspect method limitBookDataImpl.
@Around("limitBookData()")
public Object limitBookDataImpl(ProceedingJoinPoint joinPoint) throws Throwable {
Object retVal = joinPoint.proceed();
Principal principal = getPrincipal(joinPoint);
// Note - we only look at data from the JWT to build the User here - we
// are
// only interested in the users roles and they are in the JWT.
Optional<User> user = authUtils.extractUserFromPrincipal(principal, true);
if (retVal instanceof Book) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("About to call setPermissionsAndContentForUser for {}", joinPoint.getSignature());
}
((Book) retVal).setPermissionsAndContentForUser(user.orElse(null));
} else {
LOGGER.error("Unexpected return type found by aspect");
}
return retVal;
}
use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.
the class BookControllerTest method testSensitiveDataNotReturnedToAnonymousUser.
@Test
public void testSensitiveDataNotReturnedToAnonymousUser() {
ResponseEntity<Book> response = BookControllerTestUtils.postBookToServer(jwtUtils, testRestTemplate);
String location = response.getHeaders().getLocation().toString();
Book book = testRestTemplate.getForObject(location, Book.class);
// Title should be available to everyone
assertEquals(J_UNIT_TESTING_FOR_BEGINNERS, book.getTitle());
// Email should only be available to admins
assertEquals("", book.getCreatedBy().getEmail());
}
use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.
the class BookControllerTest method testSensitiveDataIsReturnedToAdminUser.
@Test
public void testSensitiveDataIsReturnedToAdminUser() {
Book testBook = BookRepositoryTest.createTestBook();
User user = BookControllerTestUtils.getTestUser();
String token = jwtUtils.createTokenForUser(user);
String xsrfToken = BookControllerTestUtils.getXsrfToken(testRestTemplate);
HttpEntity<Book> request = BookControllerTestUtils.getBookHttpEntity(testBook, user, token, xsrfToken);
ResponseEntity<Book> response = testRestTemplate.exchange("/secure/api/books", HttpMethod.POST, request, Book.class);
String location = response.getHeaders().getLocation().toString();
Book book = testRestTemplate.exchange(location, HttpMethod.GET, request, Book.class).getBody();
// Title should be available to everyone
assertEquals(J_UNIT_TESTING_FOR_BEGINNERS, book.getTitle());
// Email should only be available to admins
assertEquals(BookControllerTestUtils.DUMMY_EMAIL, book.getCreatedBy().getEmail());
}
use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.
the class BookControllerTest method testUserDataIsReturnedToEditorUser.
@Test
public void testUserDataIsReturnedToEditorUser() {
Book testBook = BookRepositoryTest.createTestBook();
User user = BookControllerTestUtils.getEditorTestUser();
String token = jwtUtils.createTokenForUser(user);
String xsrfToken = BookControllerTestUtils.getXsrfToken(testRestTemplate);
HttpEntity<Book> request = BookControllerTestUtils.getBookHttpEntity(testBook, user, token, xsrfToken);
ResponseEntity<Book> response = testRestTemplate.exchange("/secure/api/books", HttpMethod.POST, request, Book.class);
String location = response.getHeaders().getLocation().toString();
Book book = testRestTemplate.exchange(location, HttpMethod.GET, request, Book.class).getBody();
// Title should be available to everyone
assertEquals(J_UNIT_TESTING_FOR_BEGINNERS, book.getTitle());
// Email should only be available to admins - not editors
assertEquals("", book.getCreatedBy().getEmail());
// But the name of the person who created the Book should be available
assertEquals(BookControllerTestUtils.USER_WITH_EDITOR_ROLE_FULL_NAME, book.getCreatedBy().getFullName());
}
Aggregations