Search in sources :

Example 16 with User

use of com.aidanwhiteley.books.domain.User 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 17 with User

use of com.aidanwhiteley.books.domain.User 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 18 with User

use of com.aidanwhiteley.books.domain.User 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 19 with User

use of com.aidanwhiteley.books.domain.User 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)

Example 20 with User

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

the class JwtUtilsTest method testCreadAndReadGoodToken.

@Test
public void testCreadAndReadGoodToken() {
    JwtUtils jwt = new JwtUtils();
    jwt.setIssuer("A test issuer");
    jwt.setExpiryInMilliSeconds(60 * 1000);
    jwt.setSecretKey("A test secret key");
    User testUser = BookControllerTestUtils.getTestUser();
    testUser.addRole(User.Role.ROLE_ADMIN);
    String token = jwt.createTokenForUser(testUser);
    LOGGER.debug("Token was: {}", token);
    User userFromToken = jwt.getUserFromToken(token);
    assertEquals(testUser.getFullName(), userFromToken.getFullName());
}
Also used : User(com.aidanwhiteley.books.domain.User) Test(org.junit.Test)

Aggregations

User (com.aidanwhiteley.books.domain.User)36 Test (org.junit.Test)19 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)16 Book (com.aidanwhiteley.books.domain.Book)15 BookRepositoryTest (com.aidanwhiteley.books.repository.BookRepositoryTest)9 URI (java.net.URI)3 DefaultOAuth2User (org.springframework.security.oauth2.core.user.DefaultOAuth2User)3 OAuth2User (org.springframework.security.oauth2.core.user.OAuth2User)3 Principal (java.security.Principal)2 Around (org.aspectj.lang.annotation.Around)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ClientRoles (com.aidanwhiteley.books.controller.dtos.ClientRoles)1 AccessForbiddenException (com.aidanwhiteley.books.controller.exceptions.AccessForbiddenException)1 Comment (com.aidanwhiteley.books.domain.Comment)1 Owner (com.aidanwhiteley.books.domain.Owner)1 Claims (io.jsonwebtoken.Claims)1 ExpiredJwtException (io.jsonwebtoken.ExpiredJwtException)1 LocalDateTime (java.time.LocalDateTime)1 List (java.util.List)1 Cookie (javax.servlet.http.Cookie)1