Search in sources :

Example 51 with MotechUser

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

the class MotechUsersDaoTest method shouldNotCreateNewAccountIfUserAlreadyExists.

@Test
public void shouldNotCreateNewAccountIfUserAlreadyExists() {
    MotechUser motechUser = new MotechUser("testuser", "testpassword", "test@test.com", "id", asList("ADMIN"), "", Locale.ENGLISH);
    when(usersDataService.findByUserName("testuser")).thenReturn(motechUser);
    motechUsersDao.add(motechUser);
    verify(usersDataService).findByUserName("testuser");
    verify(usersDataService, never()).create(motechUser);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Example 52 with MotechUser

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

the class MotechAuthenticationProviderTest method shouldChangeUserStatusToMustChangePass.

@Test
public void shouldChangeUserStatusToMustChangePass() {
    MotechUser motechUser = new MotechUser("bob", "encodedPassword", "entity_1", "", asList("some_role"), "", Locale.ENGLISH);
    motechUser.setUserStatus(UserStatus.ACTIVE);
    motechUser.setLastPasswordChange(DateTime.now().minusDays(3));
    when(motechUsersDao.findByUserName("bob")).thenReturn(motechUser);
    when(settingService.getNumberOfDaysToChangePassword()).thenReturn(2);
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("bob", "encodedPassword");
    UserDetails details = authenticationProvider.retrieveUser("bob", authentication);
    verify(motechUsersDao).update(userCaptor.capture());
    assertTrue(details.isAccountNonLocked());
    assertFalse(details.isCredentialsNonExpired());
    MotechUser capturedUser = userCaptor.getValue();
    Assert.assertEquals((Integer) 0, capturedUser.getFailureLoginCounter());
    Assert.assertEquals(UserStatus.MUST_CHANGE_PASSWORD, capturedUser.getUserStatus());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) UserDetails(org.springframework.security.core.userdetails.UserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 53 with MotechUser

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

the class MotechUserServiceTest method shouldBlockUserAfterCrossingTheFailureLoginCounter.

@Test(expected = LockedException.class)
public void shouldBlockUserAfterCrossingTheFailureLoginCounter() {
    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(false);
    when(settingService.getFailureLoginLimit()).thenReturn(1);
    motechUserService.changeExpiredPassword(USER, PASSWORD, NEW_PASSWORD);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Example 54 with MotechUser

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

the class MotechUserServiceTest method shouldIncrementFailureLoginCounter.

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

Example 55 with MotechUser

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

the class MotechLoginSuccessHandlerTest method shouldReturnJSON.

@Test
public void shouldReturnJSON() throws ServletException, IOException {
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addHeader("x-requested-with", "XMLHttpRequest");
    MotechUser user = new MotechUser();
    user.setUserName("testUser");
    user.setFailureLoginCounter(3);
    when(authentication.getName()).thenReturn("testUser");
    when(motechUsersDao.findByUserName("testUser")).thenReturn(user);
    motechLoginSuccessHandler.onAuthenticationSuccess(mockRequest, mockResponse, authentication);
    MotechJsonMessage message = new MotechJsonMessage("SUCCESS");
    assertEquals(message.toJson(), mockResponse.getContentAsString());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechJsonMessage(org.motechproject.commons.api.json.MotechJsonMessage) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MotechUser (org.motechproject.security.domain.MotechUser)61 Test (org.junit.Test)27 Transactional (org.springframework.transaction.annotation.Transactional)24 MotechUserProfile (org.motechproject.security.domain.MotechUserProfile)8 ArrayList (java.util.ArrayList)6 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)6 DateTime (org.joda.time.DateTime)4 PasswordRecovery (org.motechproject.security.domain.PasswordRecovery)4 Authentication (org.springframework.security.core.Authentication)4 User (org.springframework.security.core.userdetails.User)4 HttpSession (javax.servlet.http.HttpSession)3 MotechRole (org.motechproject.security.domain.MotechRole)3 RoleDto (org.motechproject.security.model.RoleDto)3 ConfigAttribute (org.springframework.security.access.ConfigAttribute)3 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)3 AuthenticationException (org.springframework.security.core.AuthenticationException)3 UserNotFoundException (org.motechproject.security.exception.UserNotFoundException)2 UserDto (org.motechproject.security.model.UserDto)2 SecurityConfig (org.springframework.security.access.SecurityConfig)2 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)2