use of com.epam.ta.reportportal.ws.model.user.EditUserRQ in project service-api by reportportal.
the class EditUserHandlerImplTest method editUserWithIncorrectEmail.
@Test
void editUserWithIncorrectEmail() {
User user = new User();
user.setUserType(UserType.INTERNAL);
when(userRepository.findByLogin("test")).thenReturn(Optional.of(user));
final EditUserRQ editUserRQ = new EditUserRQ();
editUserRQ.setEmail("incorrect#domain.com");
final ReportPortalException exception = assertThrows(ReportPortalException.class, () -> handler.editUser("test", editUserRQ, getRpUser("not_exist", UserRole.USER, ProjectRole.MEMBER, 1L)));
assertEquals("Error in handled Request. Please, check specified parameters: ' wrong email: incorrect#domain.com'", exception.getMessage());
}
use of com.epam.ta.reportportal.ws.model.user.EditUserRQ in project service-api by reportportal.
the class EditUserHandlerImpl method editUser.
@Override
public OperationCompletionRS editUser(String username, EditUserRQ editUserRQ, ReportPortalUser editor) {
User user = userRepository.findByLogin(username).orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, username));
if (null != editUserRQ.getRole()) {
BusinessRule.expect(editor.getUserRole(), equalTo(UserRole.ADMINISTRATOR)).verify(ACCESS_DENIED, "Current Account Role can't update roles.");
BusinessRule.expect(user, u -> !u.getLogin().equalsIgnoreCase(editor.getUsername())).verify(ErrorType.ACCESS_DENIED, "You cannot update your role.");
UserRole newRole = UserRole.findByName(editUserRQ.getRole()).orElseThrow(() -> new ReportPortalException(BAD_REQUEST_ERROR, "Incorrect specified Account Role parameter."));
user.setRole(newRole);
}
if (null != editUserRQ.getEmail() && !editUserRQ.getEmail().equals(user.getEmail())) {
String updEmail = editUserRQ.getEmail().toLowerCase().trim();
expect(user.getUserType(), equalTo(INTERNAL)).verify(ACCESS_DENIED, "Unable to change email for external user");
expect(UserUtils.isEmailValid(updEmail), equalTo(true)).verify(BAD_REQUEST_ERROR, " wrong email: " + updEmail);
final Optional<User> byEmail = userRepository.findByEmail(updEmail);
expect(byEmail, Predicates.not(Optional::isPresent)).verify(USER_ALREADY_EXISTS, updEmail);
List<Project> userProjects = projectRepository.findUserProjects(username);
userProjects.forEach(project -> ProjectUtils.updateProjectRecipients(user.getEmail(), updEmail, project));
user.setEmail(updEmail);
try {
projectRepository.saveAll(userProjects);
} catch (Exception exp) {
throw new ReportPortalException("PROJECT update exception while USER editing.", exp);
}
}
if (null != editUserRQ.getFullName()) {
expect(user.getUserType(), equalTo(INTERNAL)).verify(ACCESS_DENIED, "Unable to change full name for external user");
user.setFullName(editUserRQ.getFullName());
}
try {
userRepository.save(user);
} catch (Exception exp) {
throw new ReportPortalException("Error while User editing.", exp);
}
return new OperationCompletionRS("User with login = '" + user.getLogin() + "' successfully updated");
}
use of com.epam.ta.reportportal.ws.model.user.EditUserRQ in project service-api by reportportal.
the class EditUserHandlerImplTest method editNotExistUser.
@Test
void editNotExistUser() {
when(userRepository.findByLogin("not_exist")).thenReturn(Optional.empty());
final ReportPortalException exception = assertThrows(ReportPortalException.class, () -> handler.editUser("not_exist", new EditUserRQ(), getRpUser("not_exist", UserRole.USER, ProjectRole.MEMBER, 1L)));
assertEquals("User 'not_exist' not found.", exception.getMessage());
}
use of com.epam.ta.reportportal.ws.model.user.EditUserRQ in project service-api by reportportal.
the class EditUserHandlerImplTest method editUserWithIncorrectRole.
@Test
void editUserWithIncorrectRole() {
User user = new User();
user.setLogin("test");
when(userRepository.findByLogin("test")).thenReturn(Optional.of(user));
final EditUserRQ editUserRQ = new EditUserRQ();
editUserRQ.setRole("not_exist_role");
final ReportPortalException exception = assertThrows(ReportPortalException.class, () -> handler.editUser("test", editUserRQ, getRpUser("not_exist", UserRole.ADMINISTRATOR, ProjectRole.MEMBER, 1L)));
assertEquals("Error in handled Request. Please, check specified parameters: 'Incorrect specified Account Role parameter.'", exception.getMessage());
}
use of com.epam.ta.reportportal.ws.model.user.EditUserRQ in project service-api by reportportal.
the class EditUserHandlerImplTest method editUserWithAlreadyExistedEmail.
@Test
void editUserWithAlreadyExistedEmail() {
User user = new User();
user.setUserType(UserType.INTERNAL);
when(userRepository.findByLogin("test")).thenReturn(Optional.of(user));
when(userRepository.findByEmail("existed@domain.com")).thenReturn(Optional.of(new User()));
final EditUserRQ editUserRQ = new EditUserRQ();
editUserRQ.setEmail("existed@domain.com");
final ReportPortalException exception = assertThrows(ReportPortalException.class, () -> handler.editUser("test", editUserRQ, getRpUser("not_exist", UserRole.USER, ProjectRole.MEMBER, 1L)));
assertEquals("User with 'existed@domain.com' already exists. You couldn't create the duplicate.", exception.getMessage());
}
Aggregations