Search in sources :

Example 56 with User

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

the class LimitDataVisibilityAspect method limitPageOfBookDataImpl.

@SuppressWarnings("unchecked")
@Around("limitPageBookData()")
public Object limitPageOfBookDataImpl(ProceedingJoinPoint joinPoint) throws Throwable {
    Object retVal = joinPoint.proceed();
    Principal principal = getPrincipal(joinPoint);
    Optional<User> user = authUtils.extractUserFromPrincipal(principal, true);
    if (retVal instanceof Page) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("About to call setPermissionsAndContentForUser for {}", joinPoint.getSignature());
        }
        User theUser = user.orElse(null);
        ((Page<Book>) retVal).getContent().forEach(s -> s.setPermissionsAndContentForUser(theUser));
    } 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) Page(org.springframework.data.domain.Page) Principal(java.security.Principal) Around(org.aspectj.lang.annotation.Around)

Example 57 with User

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

the class ActuatorTest method checkActuatorEndpointsNotAvailableWithAdminRole.

@Test
void checkActuatorEndpointsNotAvailableWithAdminRole() {
    // Re-use existing test class functionality to get a user without the ACTUATOR role
    User user = BookControllerTestUtils.getTestUser();
    user.addRole(User.Role.ROLE_ADMIN);
    ResponseEntity<String> response = getResponseStringEntity(user, "/actuator");
    assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode(), "User with only ROLE_ADMIN should be forbidden");
}
Also used : User(com.aidanwhiteley.books.domain.User) Test(org.junit.jupiter.api.Test)

Example 58 with User

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

the class ActuatorTest method checkExpectedEndpointAvailable.

@Test
void checkExpectedEndpointAvailable() {
    User user = BookControllerTestUtils.getTestUser();
    user.addRole(User.Role.ROLE_ACTUATOR);
    ResponseEntity<String> response = getResponseStringEntity(user, "/actuator/scheduledtasks");
    assertEquals(HttpStatus.OK, response.getStatusCode(), "User with ROLE_ACTUATOR should be able to see scheduledtasks");
}
Also used : User(com.aidanwhiteley.books.domain.User) Test(org.junit.jupiter.api.Test)

Example 59 with User

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

the class JwtAuthenticationUtilsTest method tryToGetUserFromPrincipal.

@Test
void tryToGetUserFromPrincipal() {
    // For dumb coverage reasons we want some debug logging to run - it does give us a chance to view the output
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.getLogger(JwtAuthenticationUtils.class).setLevel(Level.valueOf("DEBUG"));
    Optional<User> aUser = jwtAuthenticationUtils.extractUserFromPrincipal(getPrincipal(), false);
    assertFalse(aUser.isPresent(), "Dummy user shouldnt be found in database");
    context.getLogger(JwtAuthenticationUtils.class).setLevel(Level.valueOf("WARN"));
}
Also used : User(com.aidanwhiteley.books.domain.User) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.jupiter.api.Test)

Example 60 with User

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

the class UserServiceTest method testCreateActuatorUser.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
void testCreateActuatorUser() {
    UserService userService = configureUserService();
    userService.setAllowActuatorUserCreation(true);
    User user = userService.createOrUpdateActuatorUser().get();
    assertNotNull(user);
    String id = user.getId();
    assertEquals(LOCAL, user.getAuthProvider());
    User user2 = userService.createOrUpdateActuatorUser().get();
    String id2 = user.getId();
    assertEquals(id, id2);
    assertTrue(user2.getLastLogon().isAfter(user.getFirstLogon()), "Logon timestamp should have been updated");
}
Also used : User(com.aidanwhiteley.books.domain.User) DefaultOAuth2User(org.springframework.security.oauth2.core.user.DefaultOAuth2User) OAuth2User(org.springframework.security.oauth2.core.user.OAuth2User) Test(org.junit.jupiter.api.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Aggregations

User (com.aidanwhiteley.books.domain.User)61 Test (org.junit.jupiter.api.Test)35 Book (com.aidanwhiteley.books.domain.Book)25 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)24 BookRepositoryTest (com.aidanwhiteley.books.repository.BookRepositoryTest)13 URI (java.net.URI)5 DefaultOAuth2User (org.springframework.security.oauth2.core.user.DefaultOAuth2User)4 OAuth2User (org.springframework.security.oauth2.core.user.OAuth2User)4 Principal (java.security.Principal)3 HttpHeaders (org.springframework.http.HttpHeaders)3 Comment (com.aidanwhiteley.books.domain.Comment)2 Around (org.aspectj.lang.annotation.Around)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 ClientRoles (com.aidanwhiteley.books.controller.dtos.ClientRoles)1 NotAuthorisedException (com.aidanwhiteley.books.controller.exceptions.NotAuthorisedException)1 Owner (com.aidanwhiteley.books.domain.Owner)1 Item (com.aidanwhiteley.books.domain.googlebooks.Item)1 Claims (io.jsonwebtoken.Claims)1 ExpiredJwtException (io.jsonwebtoken.ExpiredJwtException)1 LocalDateTime (java.time.LocalDateTime)1