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