Search in sources :

Example 1 with PasswordRecovery

use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.

the class PasswordRecoveryServiceBundleIT method shouldFindRecoveryByTokenAndUserName.

@Test
public void shouldFindRecoveryByTokenAndUserName() throws UserNotFoundException {
    String token = passwordRecoveryService.passwordRecoveryRequest(EMAIL, EXPIRATION, true);
    PasswordRecovery recovery = recoveriesDataService.findForToken(token);
    verifyDefaultRecovery(recovery, token);
    recovery = recoveriesDataService.findForUser(USERNAME);
    verifyDefaultRecovery(recovery, token);
}
Also used : PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) Test(org.junit.Test)

Example 2 with PasswordRecovery

use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.

the class PasswordRecoveryServiceBundleIT method shouldRemoveOldRecovery.

@Test
public void shouldRemoveOldRecovery() throws UserNotFoundException {
    // Given
    PasswordRecovery recovery1 = new PasswordRecovery();
    recovery1.setEmail(EMAIL);
    recovery1.setToken(TOKEN);
    recovery1.setLocale(Locale.ENGLISH);
    recovery1.setUsername(USERNAME);
    recovery1.setExpirationDate(EXPIRATION);
    recoveriesDataService.create(recovery1);
    PasswordRecovery recovery2 = new PasswordRecovery();
    recovery2.setEmail(OTHER_EMAIL);
    recovery2.setToken(OTHER_TOKEN);
    recovery2.setLocale(Locale.ENGLISH);
    recovery2.setUsername(OTHER_USERNAME);
    recovery2.setExpirationDate(EXPIRATION.minusHours(2));
    recoveriesDataService.create(recovery2);
    List<PasswordRecovery> recoveries = recoveriesDataService.retrieveAll();
    assertEquals(2, recoveries.size());
    // When
    passwordRecoveryService.cleanUpExpiredRecoveries();
    // Then
    recoveries = recoveriesDataService.retrieveAll();
    // The expired recovery should no longer be present, the non-expired recovery should still be available
    assertEquals(1, recoveries.size());
    verifyDefaultRecovery(recoveries.get(0), TOKEN);
}
Also used : PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) Test(org.junit.Test)

Example 3 with PasswordRecovery

use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.

the class PasswordRecoveryServiceTest method shouldCreateRecoveryWithoutNotification.

@Test
public void shouldCreateRecoveryWithoutNotification() throws UserNotFoundException {
    final DateTime now = DateTime.now();
    final int expiration = EXPIRATION_HOURS;
    testCreateRecoveryTemplate(now, EMAIL, now.plusHours(expiration), false);
    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());
    verifyZeroInteractions(emailSender);
}
Also used : PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with PasswordRecovery

use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.

the class PasswordRecoveryServiceTest method shouldCreateRecoveryAndSendEmail.

@Test
public void shouldCreateRecoveryAndSendEmail() throws UserNotFoundException {
    final DateTime now = DateTime.now();
    final int expiration = EXPIRATION_HOURS;
    testCreateRecoveryTemplate(now, EMAIL, now.plusHours(expiration), true);
    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());
    verify(emailSender).sendRecoveryEmail(createdRecovery);
}
Also used : PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 5 with PasswordRecovery

use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.

the class PasswordRecoveryServiceTest method shouldCreateRecoveryWithDefaultExpirationTimeIfNoneWasProvided.

@Test
public void shouldCreateRecoveryWithDefaultExpirationTimeIfNoneWasProvided() throws UserNotFoundException {
    final DateTime now = DateTime.now();
    testCreateRecoveryTemplate(now, EMAIL, null, true);
    ArgumentCaptor<PasswordRecovery> captor = ArgumentCaptor.forClass(PasswordRecovery.class);
    verify(passwordRecoveriesDataService).create(captor.capture());
    PasswordRecovery createdRecovery = captor.getValue();
    assertEquals(USERNAME, createdRecovery.getUsername());
    assertEquals(EMAIL, createdRecovery.getEmail());
    // 3 is the default set in PasswordRecoveryServiceImpl
    assertEquals(now.plusHours(3), createdRecovery.getExpirationDate());
    assertEquals(Locale.ENGLISH, createdRecovery.getLocale());
    assertEquals(60, createdRecovery.getToken().length());
    verify(emailSender).sendRecoveryEmail(createdRecovery);
}
Also used : PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

PasswordRecovery (org.motechproject.security.domain.PasswordRecovery)14 DateTime (org.joda.time.DateTime)8 Test (org.junit.Test)8 Transactional (org.springframework.transaction.annotation.Transactional)5 MotechUser (org.motechproject.security.domain.MotechUser)4 UserNotFoundException (org.motechproject.security.exception.UserNotFoundException)2 Range (org.motechproject.commons.api.Range)1 MotechSettings (org.motechproject.config.domain.MotechSettings)1 InvalidTokenException (org.motechproject.security.exception.InvalidTokenException)1 NonAdminUserException (org.motechproject.security.exception.NonAdminUserException)1 Authentication (org.springframework.security.core.Authentication)1 OpenIDAuthenticationToken (org.springframework.security.openid.OpenIDAuthenticationToken)1