Search in sources :

Example 1 with MotechUser

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

the class MotechLoginErrorHandlerTest method shouldNotBlockUser.

@Test
public void shouldNotBlockUser() throws ServletException, IOException {
    AuthenticationException exception = new BadCredentialsException("Wrong Password");
    exception.setAuthentication(authentication);
    MotechUser user = createUser(UserStatus.ACTIVE, 2);
    when(authentication.getName()).thenReturn("testUser");
    when(motechUsersDao.findByUserName("testUser")).thenReturn(user);
    when(settingService.getFailureLoginLimit()).thenReturn(3);
    motechLoginErrorHandler.onAuthenticationFailure(request, response, exception);
    verify(response).sendRedirect(LOGIN_ERROR);
    verify(motechUsersDao).update(userCaptor.capture());
    MotechUser capturedUser = userCaptor.getValue();
    assertEquals((Integer) 3, capturedUser.getFailureLoginCounter());
    assertEquals(UserStatus.ACTIVE, capturedUser.getUserStatus());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) AuthenticationException(org.springframework.security.core.AuthenticationException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 2 with MotechUser

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

the class MotechLoginErrorHandlerTest method shouldRedirectUserWithExpiredPassword.

@Test
public void shouldRedirectUserWithExpiredPassword() throws ServletException, IOException {
    AuthenticationException exception = new CredentialsExpiredException("Credentials expired");
    exception.setAuthentication(authentication);
    MotechUser user = createUser(UserStatus.MUST_CHANGE_PASSWORD, 0);
    when(authentication.getName()).thenReturn("testUser");
    when(motechUsersDao.findByUserName("testUser")).thenReturn(user);
    when(settingService.getFailureLoginLimit()).thenReturn(3);
    motechLoginErrorHandler.onAuthenticationFailure(request, response, exception);
    verify(response).sendRedirect(CHANGE_PASSWORD);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) CredentialsExpiredException(org.springframework.security.authentication.CredentialsExpiredException) AuthenticationException(org.springframework.security.core.AuthenticationException) Test(org.junit.Test)

Example 3 with MotechUser

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

the class MotechLoginErrorHandlerTest method shouldBlockUser.

@Test
public void shouldBlockUser() throws ServletException, IOException {
    AuthenticationException exception = new BadCredentialsException("Wrong Password");
    exception.setAuthentication(authentication);
    MotechUser user = createUser(UserStatus.ACTIVE, 3);
    when(authentication.getName()).thenReturn("testUser");
    when(motechUsersDao.findByUserName("testUser")).thenReturn(user);
    when(settingService.getFailureLoginLimit()).thenReturn(3);
    motechLoginErrorHandler.onAuthenticationFailure(request, response, exception);
    verify(response).sendRedirect(LOGIN_BLOCKED);
    verify(motechUsersDao).update(userCaptor.capture());
    MotechUser capturedUser = userCaptor.getValue();
    assertEquals((Integer) 0, capturedUser.getFailureLoginCounter());
    assertEquals(UserStatus.BLOCKED, capturedUser.getUserStatus());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) AuthenticationException(org.springframework.security.core.AuthenticationException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 4 with MotechUser

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

the class MotechAuthenticationProviderTest method shouldThrowExceptionIfUserIsBlocked.

@Test(expected = LockedException.class)
public void shouldThrowExceptionIfUserIsBlocked() {
    MotechUser motechUser = new MotechUser("bob", "encodedPassword", "entity_1", "", asList("some_role"), "", Locale.ENGLISH);
    motechUser.setUserStatus(UserStatus.BLOCKED);
    motechUser.setFailureLoginCounter(3);
    when(motechUsersDao.findByUserName("bob")).thenReturn(motechUser);
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("bob", "password");
    authenticationProvider.retrieveUser("bob", authentication);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 5 with MotechUser

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

the class MotechAuthenticationProviderTest method shouldRetrieveUserFromDatabase.

@Test
public void shouldRetrieveUserFromDatabase() {
    MotechUser motechUser = new MotechUser("bob", "encodedPassword", "entity_1", "", asList("some_role"), "", Locale.ENGLISH);
    MotechRole motechRole = new MotechRole("some_role", asList("some_permission"), false);
    when(motechUsersDao.findByUserName("bob")).thenReturn(motechUser);
    // when(allMotechRoles.findByRoleName("some_role")).thenReturn(motechRole);
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("bob", "password");
    UserDetails userDetails = authenticationProvider.retrieveUser("bob", authentication);
    assertEquals("encodedPassword", userDetails.getPassword());
    assertEquals(motechUser.getUserName(), ((MotechUserProfile) authentication.getDetails()).getUserName());
    assertEquals(motechUser.getUserName(), userDetails.getUsername());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) UserDetails(org.springframework.security.core.userdetails.UserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) MotechRole(org.motechproject.security.domain.MotechRole) 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