Search in sources :

Example 6 with User

use of org.summerb.users.api.dto.User in project summerb by skarpushin.

the class AuthTokenServiceImpl method isAuthTokenValid.

@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken isAuthTokenValid(String userUuid, String authTokenUuid, String tokenValue) throws UserNotFoundException {
    Preconditions.checkArgument(userUuid != null);
    Preconditions.checkArgument(authTokenUuid != null);
    Preconditions.checkArgument(StringUtils.hasText(tokenValue), "TokenValue is mandatory");
    try {
        // First - check token itself
        AuthToken authToken = getAuthTokenByUuid(authTokenUuid);
        if (authToken.getExpiresAt() < getNow()) {
            authTokenDao.deleteAuthToken(authTokenUuid);
            return null;
        }
        if (!tokenValue.equals(authToken.getTokenValue())) {
            return null;
        }
        // Check reference to user
        User user = userService.getUserByUuid(userUuid);
        if (!authToken.getUserUuid().equals(user.getUuid())) {
            return null;
        }
        // Now we need to update time when token was checked
        authToken.setTokenValue(UUID.randomUUID().toString());
        authToken.setLastVerifiedAt(getNow());
        authTokenDao.updateToken(authTokenUuid, authToken.getLastVerifiedAt(), authToken.getTokenValue());
        return authToken;
    } catch (AuthTokenNotFoundException nfe) {
        return null;
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        String msg = String.format("Failed to check auth token '%s' validity for user '%s'", authTokenUuid, userUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserNotFoundException(org.summerb.users.api.exceptions.UserNotFoundException) User(org.summerb.users.api.dto.User) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.users.api.dto.AuthToken) AuthTokenNotFoundException(org.summerb.users.api.exceptions.AuthTokenNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with User

use of org.summerb.users.api.dto.User in project summerb by skarpushin.

the class UserServiceImplTest method testFindUsersByDisplayNamePartial_blackbox_shouldReturnExpectedResults.

@Test
public void testFindUsersByDisplayNamePartial_blackbox_shouldReturnExpectedResults() throws Exception {
    UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl();
    PagerParams pagerParams = new PagerParams(20, 40);
    PaginatedList<User> resultFromDao = new PaginatedList<User>();
    resultFromDao.setTotalResults(1);
    resultFromDao.setPagerParams(pagerParams);
    resultFromDao.setItems(new LinkedList<User>());
    resultFromDao.getItems().add(UserFactory.createNewUserTemplate());
    when(fixture.getUserDao().findUserByDisplayNamePartial("asd", pagerParams)).thenReturn(resultFromDao);
    PaginatedList<User> results = fixture.findUsersByDisplayNamePartial("asd", pagerParams);
    assertNotNull(results);
    assertNotNull(results.getPagerParams());
    assertEquals(pagerParams.getOffset(), results.getPagerParams().getOffset());
    assertEquals(pagerParams.getMax(), results.getPagerParams().getMax());
    assertEquals(1, results.getTotalResults());
}
Also used : User(org.summerb.users.api.dto.User) PagerParams(org.summerb.easycrud.api.dto.PagerParams) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) Test(org.junit.Test)

Example 8 with User

use of org.summerb.users.api.dto.User in project summerb by skarpushin.

the class UserServiceImplTest method testCreateUser_whitebox_expectOurExceptionInsteadOfUnexpectedOne.

@Test(expected = UserServiceUnexpectedException.class)
public void testCreateUser_whitebox_expectOurExceptionInsteadOfUnexpectedOne() throws Exception {
    UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl();
    User user = UserFactory.createNewUserTemplate();
    doThrow(new IllegalArgumentException("test exception")).when(fixture.getUserDao()).createUser(any(User.class));
    fixture.createUser(user);
    fail();
}
Also used : User(org.summerb.users.api.dto.User) Test(org.junit.Test)

Example 9 with User

use of org.summerb.users.api.dto.User in project summerb by skarpushin.

the class UserServiceImplTest method testCreateUser_blackbox_expectValidationExceptionOnInvalidEmail1.

@Test(expected = FieldValidationException.class)
public void testCreateUser_blackbox_expectValidationExceptionOnInvalidEmail1() throws Exception {
    UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl();
    User user = UserFactory.createNewUserTemplate();
    user.setEmail(null);
    fixture.createUser(user);
    fail();
}
Also used : User(org.summerb.users.api.dto.User) Test(org.junit.Test)

Example 10 with User

use of org.summerb.users.api.dto.User in project summerb by skarpushin.

the class UserServiceImplTest method testCreateUser_blackbox_expectGuidWillBeCreatedForTheUser.

@Test
public void testCreateUser_blackbox_expectGuidWillBeCreatedForTheUser() throws Exception {
    UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl();
    User result = fixture.createUser(UserFactory.createNewUserTemplate());
    // add additional test code here
    assertNotNull(result);
    assertNotNull(result.getUuid());
}
Also used : User(org.summerb.users.api.dto.User) Test(org.junit.Test)

Aggregations

User (org.summerb.users.api.dto.User)61 Test (org.junit.Test)33 UserNotFoundException (org.summerb.users.api.exceptions.UserNotFoundException)13 AuthToken (org.summerb.users.api.dto.AuthToken)11 UserServiceUnexpectedException (org.summerb.users.api.exceptions.UserServiceUnexpectedException)11 FieldValidationException (org.summerb.validation.FieldValidationException)11 Transactional (org.springframework.transaction.annotation.Transactional)8 Date (java.util.Date)4 PagerParams (org.summerb.easycrud.api.dto.PagerParams)4 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)3 InvalidPasswordException (org.summerb.users.api.exceptions.InvalidPasswordException)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 GenericException (org.summerb.utils.exceptions.GenericException)2 ValidationContext (org.summerb.validation.ValidationContext)2 CacheBuilder (com.google.common.cache.CacheBuilder)1 EventBus (com.google.common.eventbus.EventBus)1 Locale (java.util.Locale)1 Secured (org.springframework.security.access.annotation.Secured)1 AuthenticationServiceException (org.springframework.security.authentication.AuthenticationServiceException)1