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());
}
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);
}
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());
}
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);
}
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());
}
Aggregations