Search in sources :

Example 1 with Book

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);
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Book(com.aidanwhiteley.books.domain.Book)

Example 2 with Book

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;
}
Also used : User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) Principal(java.security.Principal) Around(org.aspectj.lang.annotation.Around)

Example 3 with Book

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());
}
Also used : Book(com.aidanwhiteley.books.domain.Book) BookRepositoryTest(com.aidanwhiteley.books.repository.BookRepositoryTest) Test(org.junit.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 4 with Book

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());
}
Also used : User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) BookRepositoryTest(com.aidanwhiteley.books.repository.BookRepositoryTest) Test(org.junit.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 5 with Book

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());
}
Also used : User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) BookRepositoryTest(com.aidanwhiteley.books.repository.BookRepositoryTest) Test(org.junit.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Aggregations

Book (com.aidanwhiteley.books.domain.Book)23 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)17 Test (org.junit.Test)17 User (com.aidanwhiteley.books.domain.User)15 BookRepositoryTest (com.aidanwhiteley.books.repository.BookRepositoryTest)12 URI (java.net.URI)4 Comment (com.aidanwhiteley.books.domain.Comment)3 Owner (com.aidanwhiteley.books.domain.Owner)3 HttpHeaders (org.springframework.http.HttpHeaders)3 Principal (java.security.Principal)2 Around (org.aspectj.lang.annotation.Around)2 PageRequest (org.springframework.data.domain.PageRequest)2 AccessForbiddenException (com.aidanwhiteley.books.controller.exceptions.AccessForbiddenException)1 Page (org.springframework.data.domain.Page)1 HttpEntity (org.springframework.http.HttpEntity)1