use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.
the class PasswordRecoveryServiceTest method shouldSendRecoveryEmail.
@Test
public void shouldSendRecoveryEmail() {
motechSettings = mock(MotechSettings.class);
when(settingsFacade.getPlatformSettings()).thenReturn(motechSettings);
when(motechSettings.getServerHost()).thenReturn("serverurl");
when(motechSettings.getServerUrl()).thenReturn("http://serverurl");
when(motechSettings.getLoginMode()).thenReturn(LoginMode.REPOSITORY);
PasswordRecovery newRecovery = new PasswordRecovery();
newRecovery.setUsername(USERNAME);
newRecovery.setEmail(EMAIL);
newRecovery.setToken(TOKEN);
newRecovery.setExpirationDate(DateTime.now().plusHours(1));
newRecovery.setLocale(Locale.ENGLISH);
emailSenderInjected.sendRecoveryEmail(newRecovery);
verify(eventRelay).sendEventMessage(any(emailEvent.getClass()));
}
use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.
the class PasswordRecoveryServiceTest method shouldCreatePasswordRecoveryForOpenID.
@Test
public void shouldCreatePasswordRecoveryForOpenID() throws UserNotFoundException, NonAdminUserException {
final DateTime now = DateTime.now();
final int expiration = EXPIRATION_HOURS;
testCreateOpenIDRecoveryTemplate(now, EMAIL, now.plusHours(expiration));
ArgumentCaptor<PasswordRecovery> captor = ArgumentCaptor.forClass(PasswordRecovery.class);
verify(passwordRecoveriesDataService).create(captor.capture());
PasswordRecovery createdRecovery = captor.getValue();
assertEquals(USERNAME, createdRecovery.getUsername());
assertEquals(EMAIL, createdRecovery.getEmail());
assertEquals(now.plusHours(expiration), createdRecovery.getExpirationDate());
assertEquals(Locale.ENGLISH, createdRecovery.getLocale());
assertEquals(60, createdRecovery.getToken().length());
}
use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.
the class PasswordRecoveryServiceImpl method createRecovery.
private PasswordRecovery createRecovery(String username, String email, String token, DateTime expirationDate, Locale locale) {
PasswordRecovery oldRecovery = findForUser(username);
if (oldRecovery != null) {
passwordRecoveriesDataService.delete(oldRecovery);
}
PasswordRecovery recovery = new PasswordRecovery();
recovery.setUsername(username);
recovery.setEmail(email);
recovery.setToken(token);
recovery.setExpirationDate(expirationDate);
recovery.setLocale(locale);
add(recovery);
return recovery;
}
use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.
the class PasswordRecoveryServiceImpl method passwordRecoveryRequest.
@Override
@Transactional
public String passwordRecoveryRequest(String email, DateTime expiration, boolean notify) throws UserNotFoundException {
MotechUser user = motechUsersDao.findUserByEmail(email);
DateTime expirationDate = expiration;
if (expirationDate == null) {
expirationDate = DateTime.now().plusHours(DEFAULT_EXPIRATION_HOURS);
} else if (expirationDate.isBefore(DateTime.now())) {
throw new IllegalArgumentException("The expiration date shouldn't be a past date!");
}
if (user == null) {
throw new UserNotFoundException("User with email not found: " + email);
}
String token = RandomStringUtils.randomAlphanumeric(TOKEN_LENGTH);
PasswordRecovery recovery = createRecovery(user.getUserName(), user.getEmail(), token, expirationDate, user.getLocale());
if (notify) {
emailSender.sendRecoveryEmail(recovery);
}
LOGGER.info("Created a password recovery for user " + user.getUserName());
return token;
}
Aggregations