Search in sources :

Example 11 with UserDto

use of org.motechproject.security.model.UserDto in project motech by motech.

the class UserControllerTest method shouldPrintMinPasswordLengthError.

@Test
public void shouldPrintMinPasswordLengthError() throws Exception {
    when(localeService.getUserLocale(any(HttpServletRequest.class))).thenReturn(Locale.GERMAN);
    when(settingService.getPasswordValidator()).thenReturn(validator);
    when(validator.getValidationError(Locale.GERMAN)).thenReturn("Error from validator");
    doThrow(new PasswordTooShortException(20)).when(userService).register(eq("john"), eq("invalid"), eq("john@gmail.com"), eq(""), anyListOf(String.class), any(Locale.class));
    UserDto userDto = new UserDto();
    userDto.setUserName("john");
    userDto.setEmail("john@gmail.com");
    userDto.setPassword("invalid");
    mockMvc.perform(post("/users/create").body(new ObjectMapper().writeValueAsBytes(userDto)).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest()).andExpect(content().string("key:security.validator.error.min_length\nparams:20"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Locale(java.util.Locale) UserDto(org.motechproject.security.model.UserDto) PasswordTooShortException(org.motechproject.security.exception.PasswordTooShortException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 12 with UserDto

use of org.motechproject.security.model.UserDto in project motech by motech.

the class MotechUserServiceTest method shouldValidatePasswordOnEdit.

@Test(expected = PasswordValidatorException.class)
public void shouldValidatePasswordOnEdit() {
    UserDto userDto = new UserDto();
    userDto.setPassword("wrong");
    userDto.setUserName("user");
    when(motechUsersDao.findByUserName("user")).thenReturn(user);
    doThrow(new PasswordValidatorException("error")).when(validator).validate("wrong");
    motechUserService.updateUserDetailsWithPassword(userDto);
}
Also used : PasswordValidatorException(org.motechproject.security.exception.PasswordValidatorException) UserDto(org.motechproject.security.model.UserDto) Test(org.junit.Test)

Example 13 with UserDto

use of org.motechproject.security.model.UserDto in project motech by motech.

the class MotechRoleServiceBundleIT method shouldRefreshMultipleSessionsOnRoleUpdates.

@Test
public void shouldRefreshMultipleSessionsOnRoleUpdates() throws IOException, InterruptedException {
    // create a role
    motechRoleService.createRole(new RoleDto("Role1", asList("permissionA", "permissionB"), true));
    RoleDto role = motechRoleService.getRole("Role1");
    assertNotNull(role);
    // create a second user
    motechUserService.register("duke", "password", "email", "1234", asList("Role1"), Locale.ENGLISH);
    // admin login through HTTP
    login();
    // just start a session
    HttpClient httpClient = HttpClients.createDefault();
    httpClient.execute(new HttpGet(String.format("http://localhost:%d/server/motech-platform-server/", TestContext.getJettyPort())));
    // add a permission to the role
    role.getPermissionNames().add("newPermission");
    motechRoleService.updateRole(role);
    // verify that the role was updated and the user still has it
    role = motechRoleService.getRole("Role1");
    assertNotNull(role);
    assertEquals(asList("permissionA", "permissionB", "newPermission"), role.getPermissionNames());
    UserDto user = motechUserService.getUser("duke");
    assertNotNull(user);
    assertEquals(asList("Role1"), user.getRoles());
    // remove the role from the user so we can delete it
    user.setRoles(new ArrayList<String>());
    motechUserService.updateUserDetailsWithoutPassword(user);
    // delete the role and make sure that it's gone
    motechRoleService.deleteRole(role);
    role = motechRoleService.getRole("Role1");
    assertNull(role);
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) UserDto(org.motechproject.security.model.UserDto) Test(org.junit.Test)

Example 14 with UserDto

use of org.motechproject.security.model.UserDto in project motech by motech.

the class UserControllerTest method shouldReturnCurrentUserDetails.

@Test
public void shouldReturnCurrentUserDetails() throws Exception {
    User user = new User("john", "password", Arrays.asList(new SimpleGrantedAuthority("admin")));
    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, user.getPassword());
    UserDto userDto = new UserDto();
    userDto.setUserName("john");
    userDto.setEmail("john@gmail.com");
    when(userService.getCurrentUser()).thenReturn(userDto);
    mockMvc.perform(get("/users/current").principal(authenticationToken)).andExpect(status().isOk()).andExpect(content().string(new Contains("\"userName\":\"john\"")));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) UserDto(org.motechproject.security.model.UserDto) Contains(org.mockito.internal.matchers.Contains) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 15 with UserDto

use of org.motechproject.security.model.UserDto in project motech by motech.

the class PersistedUserValidatorTest method shouldRejectOnlyUserIfUserExistsAndIsRegisteredWithIdenticalEmail.

@Test
public void shouldRejectOnlyUserIfUserExistsAndIsRegisteredWithIdenticalEmail() {
    PersistedUserValidator persistedUserValidator = new PersistedUserValidator(userService);
    when(userService.hasUser("admin")).thenReturn(true);
    UserDto userDto = new UserDto();
    userDto.setUserName("admin");
    when(userService.hasEmail("admin@motech.org")).thenReturn(true);
    List<String> errors = new ArrayList<>();
    persistedUserValidator.validate(getExampleStartupForm(), errors, ConfigSource.FILE);
    assertTrue(errors.contains("server.error.user.exist"));
    assertFalse(errors.contains("server.error.email.exist"));
}
Also used : UserDto(org.motechproject.security.model.UserDto) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

UserDto (org.motechproject.security.model.UserDto)15 Test (org.junit.Test)12 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MotechUser (org.motechproject.security.domain.MotechUser)2 MotechUserProfile (org.motechproject.security.domain.MotechUserProfile)2 PasswordValidatorException (org.motechproject.security.exception.PasswordValidatorException)2 User (org.springframework.security.core.userdetails.User)2 Transactional (org.springframework.transaction.annotation.Transactional)2 List (java.util.List)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 Contains (org.mockito.internal.matchers.Contains)1 PasswordTooShortException (org.motechproject.security.exception.PasswordTooShortException)1 RoleDto (org.motechproject.security.model.RoleDto)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1