Search in sources :

Example 1 with MotechUserProfile

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

the class MotechUserServiceTest method shouldChangePasswordWhenWhenOldHasBeenExpired.

@Test
public void shouldChangePasswordWhenWhenOldHasBeenExpired() {
    MotechUser motechUser = new MotechUser();
    motechUser.setUserStatus(UserStatus.MUST_CHANGE_PASSWORD);
    motechUser.setPassword(PASSWORD);
    motechUser.setUserName(USER);
    motechUser.setFailureLoginCounter(1);
    when(motechUsersDao.findByUserName(USER)).thenReturn(motechUser);
    when(motechPasswordEncoder.isPasswordValid(PASSWORD, PASSWORD)).thenReturn(true);
    when(motechPasswordEncoder.isPasswordValid(PASSWORD, NEW_PASSWORD)).thenReturn(false);
    when(motechPasswordEncoder.encodePassword(NEW_PASSWORD)).thenReturn(NEW_PASSWORD + "_encoded");
    when(settingService.getFailureLoginLimit()).thenReturn(2);
    MotechUserProfile profile = motechUserService.changeExpiredPassword(USER, PASSWORD, NEW_PASSWORD);
    verify(motechUsersDao).update(userCaptor.capture());
    verify(motechPasswordEncoder).encodePassword(NEW_PASSWORD);
    MotechUser capturedUser = userCaptor.getValue();
    assertEquals(USER, capturedUser.getUserName());
    assertEquals(NEW_PASSWORD + "_encoded", capturedUser.getPassword());
    assertNotNull(profile);
    assertEquals(USER, profile.getUserName());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechUserProfile(org.motechproject.security.domain.MotechUserProfile) Test(org.junit.Test)

Example 2 with MotechUserProfile

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

the class MotechAccessVoterTest method shouldVoteAffirmativeIfUserHasAccess.

@Test
public void shouldVoteAffirmativeIfUserHasAccess() {
    List<ConfigAttribute> attributes = new ArrayList<ConfigAttribute>();
    attributes.add(new SecurityConfig("access_testuser"));
    attributes.add(new SecurityConfig("access_motechUser"));
    MotechUserProfile userProfile = new MotechUserProfile(new MotechUser("TestUser", "p@ssw0rd", "", "", null, "", Locale.ENGLISH));
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("TestUser", "p@ssw0rd");
    authentication.setDetails(userProfile);
    MotechAccessVoter voter = new MotechAccessVoter();
    assertEquals(1, voter.vote(authentication, null, attributes));
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechUserProfile(org.motechproject.security.domain.MotechUserProfile) ConfigAttribute(org.springframework.security.access.ConfigAttribute) SecurityConfig(org.springframework.security.access.SecurityConfig) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 3 with MotechUserProfile

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

the class MotechAccessVoterTest method shouldAbstrainIfNoAccessAttributes.

public void shouldAbstrainIfNoAccessAttributes() {
    List<ConfigAttribute> attributes = new ArrayList<ConfigAttribute>();
    MotechUserProfile userProfile = new MotechUserProfile(new MotechUser("TestUser", "p@ssw0rd", "", "", null, "", Locale.ENGLISH));
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("TestUser", "p@ssw0rd");
    authentication.setDetails(userProfile);
    MotechAccessVoter voter = new MotechAccessVoter();
    assertEquals(0, voter.vote(authentication, null, attributes));
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechUserProfile(org.motechproject.security.domain.MotechUserProfile) ConfigAttribute(org.springframework.security.access.ConfigAttribute) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 4 with MotechUserProfile

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

the class MotechAccessVoterTest method shouldVoteNegativeIfUserDoesNotHaveAccess.

public void shouldVoteNegativeIfUserDoesNotHaveAccess() {
    List<ConfigAttribute> attributes = new ArrayList<ConfigAttribute>();
    attributes.add(new SecurityConfig("access_otheruser"));
    attributes.add(new SecurityConfig("access_motechUser"));
    MotechUserProfile userProfile = new MotechUserProfile(new MotechUser("TestUser", "p@ssw0rd", "", "", null, "", Locale.ENGLISH));
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("TestUser", "p@ssw0rd");
    authentication.setDetails(userProfile);
    MotechAccessVoter voter = new MotechAccessVoter();
    assertEquals(-1, voter.vote(authentication, null, attributes));
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechUserProfile(org.motechproject.security.domain.MotechUserProfile) ConfigAttribute(org.springframework.security.access.ConfigAttribute) SecurityConfig(org.springframework.security.access.SecurityConfig) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 5 with MotechUserProfile

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

the class MotechUserServiceBundleIT method shouldNotActivateUserWhenPasswordsAreTheSame.

@Test
public void shouldNotActivateUserWhenPasswordsAreTheSame() {
    motechUserService.register("expired", "password", "1234", "", asList("IT_ADMIN"), Locale.ENGLISH, UserStatus.MUST_CHANGE_PASSWORD, "");
    MotechUserProfile profile = motechUserService.changeExpiredPassword("expired", "password", "password");
    assertNull(profile);
    UserDto userDto = motechUserService.getUser("expired");
    assertEquals(UserStatus.MUST_CHANGE_PASSWORD, userDto.getUserStatus());
}
Also used : MotechUserProfile(org.motechproject.security.domain.MotechUserProfile) UserDto(org.motechproject.security.model.UserDto) Test(org.junit.Test)

Aggregations

MotechUserProfile (org.motechproject.security.domain.MotechUserProfile)12 MotechUser (org.motechproject.security.domain.MotechUser)8 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 ConfigAttribute (org.springframework.security.access.ConfigAttribute)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)3 Transactional (org.springframework.transaction.annotation.Transactional)3 UserDto (org.motechproject.security.model.UserDto)2 SecurityConfig (org.springframework.security.access.SecurityConfig)2 Predicate (org.apache.commons.collections.Predicate)1 PasswordValidatorException (org.motechproject.security.exception.PasswordValidatorException)1 ChangePasswordViewData (org.motechproject.server.web.dto.ChangePasswordViewData)1 ChangePasswordFormValidator (org.motechproject.server.web.validator.ChangePasswordFormValidator)1 LockedException (org.springframework.security.authentication.LockedException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1