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;
}
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());
}
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());
}
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);
}
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());
}
Aggregations