Search in sources :

Example 6 with Book

use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.

the class BookControllerTestUtils method postBookToServer.

public static ResponseEntity<Book> postBookToServer(JwtUtils jwtUtils, TestRestTemplate testRestTemplate) {
    String xsrfToken = getXsrfToken(testRestTemplate);
    Book testBook = BookRepositoryTest.createTestBook();
    User user = getTestUser();
    String token = jwtUtils.createTokenForUser(user);
    HttpEntity<Book> request = getBookHttpEntity(testBook, user, token, xsrfToken);
    ResponseEntity<Book> book = testRestTemplate.exchange("/secure/api/books", HttpMethod.POST, request, Book.class);
    assertNotNull(book);
    assertEquals(HttpStatus.CREATED, book.getStatusCode());
    LOGGER.debug("postBookToServer posted book to server successfully");
    return book;
}
Also used : User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book)

Example 7 with Book

use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.

the class BookSecureControllerTest method tryToUpdateBookWithInsufficientPermissions.

@Test
public void tryToUpdateBookWithInsufficientPermissions() {
    ResponseEntity<Book> response = BookControllerTestUtils.postBookToServer(jwtUtils, testRestTemplate);
    HttpHeaders headers = response.getHeaders();
    URI uri = headers.getLocation();
    Book book = testRestTemplate.getForObject(uri, Book.class);
    // Set up user with just the ROLE_USER role
    User user = BookControllerTestUtils.getTestUser();
    user.removeRole(Role.ROLE_ADMIN);
    user.removeRole(Role.ROLE_EDITOR);
    final String updatedTitle = "An updated book title";
    book.setTitle(updatedTitle);
    String token = jwtUtils.createTokenForUser(user);
    HttpEntity<Book> putData = BookControllerTestUtils.getBookHttpEntity(book, user, token);
    ResponseEntity<Book> putResponse = testRestTemplate.exchange("/secure/api/books", HttpMethod.PUT, putData, Book.class);
    // See comments in the tryToCreateBookWithNoPermissions test for why a 302 is expected.
    assertEquals(HttpStatus.FOUND, putResponse.getStatusCode());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) URI(java.net.URI) Test(org.junit.Test) BookRepositoryTest(com.aidanwhiteley.books.repository.BookRepositoryTest) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 8 with Book

use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.

the class BookSecureControllerTest method tryToCreateInvalidBook.

@Test
public void tryToCreateInvalidBook() {
    // An empty book should fail
    Book emptyBook = new Book();
    User user = BookControllerTestUtils.getTestUser();
    String token = jwtUtils.createTokenForUser(user);
    String xsrfToken = BookControllerTestUtils.getXsrfToken(testRestTemplate);
    HttpEntity<Book> request = BookControllerTestUtils.getBookHttpEntity(emptyBook, user, token, xsrfToken);
    ResponseEntity<Book> response = testRestTemplate.exchange("/secure/api/books", HttpMethod.POST, request, Book.class);
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
    // Create a valid book and then exceed one of the max field sizes
    Book testBook = BookRepositoryTest.createTestBook();
    testBook.setGenre(GENRE_TOO_LONG);
    request = BookControllerTestUtils.getBookHttpEntity(testBook, user, token, xsrfToken);
    response = testRestTemplate.exchange("/secure/api/books", HttpMethod.POST, request, Book.class);
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
Also used : User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) Test(org.junit.Test) BookRepositoryTest(com.aidanwhiteley.books.repository.BookRepositoryTest) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 9 with Book

use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.

the class BookSecureControllerTest method updateBook.

@Test
public void updateBook() {
    // Create Book
    ResponseEntity<Book> response = BookControllerTestUtils.postBookToServer(jwtUtils, testRestTemplate);
    // Get the location of the book POSTed to the server
    HttpHeaders headers = response.getHeaders();
    URI uri = headers.getLocation();
    // Now go and get the Book
    User user = BookControllerTestUtils.getTestUser();
    Book book = testRestTemplate.getForEntity(uri, Book.class).getBody();
    assertEquals(book.getTitle(), BookRepositoryTest.createTestBook().getTitle());
    // Now update the book - need to supply a JWT / logon token to perform update.
    final String updatedTitle = "An updated book title";
    book.setTitle(updatedTitle);
    String token = jwtUtils.createTokenForUser(user);
    String xsrfToken = BookControllerTestUtils.getXsrfToken(testRestTemplate);
    HttpEntity<Book> putData = BookControllerTestUtils.getBookHttpEntity(book, user, token, xsrfToken);
    ResponseEntity<Book> putResponse = testRestTemplate.exchange("/secure/api/books", HttpMethod.PUT, putData, Book.class);
    assertEquals(HttpStatus.NO_CONTENT, putResponse.getStatusCode());
    headers = response.getHeaders();
    uri = headers.getLocation();
    // And finally check that the book was actually updated
    Book updatedBook = testRestTemplate.getForObject(uri, Book.class);
    assertEquals(updatedBook.getTitle(), updatedTitle);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) URI(java.net.URI) Test(org.junit.Test) BookRepositoryTest(com.aidanwhiteley.books.repository.BookRepositoryTest) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 10 with Book

use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.

the class UserControllerTest method getUserDetailsWithAuthentication.

@Test
public void getUserDetailsWithAuthentication() {
    User user = BookControllerTestUtils.getTestUser();
    String token = jwtUtils.createTokenForUser(user);
    String xsrfToken = BookControllerTestUtils.getXsrfToken(testRestTemplate);
    HttpEntity<Book> request = BookControllerTestUtils.getBookHttpEntity(null, user, token, xsrfToken);
    ResponseEntity<User> response = testRestTemplate.exchange("/secure/api/user", HttpMethod.GET, request, User.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(BookControllerTestUtils.USER_WITH_ALL_ROLES_FULL_NAME, response.getBody().getFullName());
}
Also used : User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest) Test(org.junit.Test)

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