*/
package org.sonar.server.common.user.service;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.server.authentication.IdentityProvider;
import org.sonar.api.utils.DateUtils;
import static org.sonar.db.property.PropertyTesting.newUserPropertyDto;
import static org.sonar.db.user.UserTesting.newUserDto;
+@RunWith(DataProviderRunner.class)
public class UserServiceIT {
private static final UsersSearchRequest SEARCH_REQUEST = getBuilderWithDefaultsPageSize().build();
assertThat(updatedUser.getExternalId()).isEqualTo("prov_id");
assertThat(updatedUser.getExternalLogin()).isEqualTo("prov_login");
}
+ @DataProvider
+ public static Object[][] updateUserProvider() {
+ return new Object[][]{
+ {new UpdateUser().setName("new name")},
+ {new UpdateUser().setEmail("newEmail@test.com")},
+ {new UpdateUser().setName("new name").setEmail("newEmail@test.com")}};
+ }
+
+ @Test
+ @UseDataProvider("updateUserProvider")
+ public void updateUser_whenIncorrectPayloadForManagedInstance_shouldThrow(UpdateUser updateUser) {
+ doThrow(BadRequestException.create("message")).when(managedInstanceChecker).throwIfInstanceIsManaged(any());
+
+ UserDto userDto = db.users().insertUser();
+
+ assertThatThrownBy(() -> userService.updateUser(userDto.getUuid(), updateUser))
+ .isInstanceOf(BadRequestException.class)
+ .hasMessage("message");
+ }
private void assertUserWithFilter(Function<UsersSearchRequest.Builder, UsersSearchRequest.Builder> query, String userLogin, boolean isExpectedToBeThere) {
BadRequestException.checkRequest(!managedInstanceService.isInstanceExternallyManaged(), INSTANCE_EXCEPTION_MESSAGE);
}
+ public void throwIfInstanceIsManaged(String errorMessage) {
+ BadRequestException.checkRequest(!managedInstanceService.isInstanceExternallyManaged(), errorMessage);
+ }
+
public void throwIfProjectIsManaged(DbSession dbSession, String projectUuid) {
BadRequestException.checkRequest(!managedProjectService.isProjectManaged(dbSession, projectUuid), PROJECT_EXCEPTION_MESSAGE);
}
public UserInformation updateUser(String uuid, UpdateUser updateUser) {
try (DbSession dbSession = dbClient.openSession(false)) {
throwIfInvalidChangeOfExternalProvider(updateUser);
+ throwIfManagedInstanceAndNameOrEmailUpdated(updateUser);
UserDto userDto = findUserOrThrow(uuid, dbSession);
userUpdater.updateAndCommit(dbSession, userDto, updateUser, u -> {
});
Optional.ofNullable(updateUser.externalIdentityProvider()).ifPresent(this::assertProviderIsSupported);
}
+ private void throwIfManagedInstanceAndNameOrEmailUpdated(UpdateUser updateUser) {
+ boolean isNameChanged = updateUser.isNameChanged();
+ boolean isEmailDefined = updateUser.isEmailChanged();
+ if (isNameChanged || isEmailDefined) {
+ managedInstanceChecker.throwIfInstanceIsManaged("User name and email cannot be updated when the instance is externally managed");
+ }
+ }
+
private void assertProviderIsSupported(String newExternalProvider) {
List<String> allowedIdentityProviders = getAvailableIdentityProviders();
.hasMessage(INSTANCE_EXCEPTION_MESSAGE);
}
+ @Test
+ public void throwIfInstanceIsManaged_whenCustomErrorMessage_shouldThrowWithCustomError() {
+ when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(true);
+
+ String customErrorMessage = "Custom error message";
+
+ assertThatThrownBy(() -> managedInstanceChecker.throwIfInstanceIsManaged(customErrorMessage))
+ .isInstanceOf(BadRequestException.class)
+ .hasMessage(customErrorMessage);
+ }
+
+ @Test
+ public void throwIfInstanceIsManaged_whenCustomErrorMessageAndInstanceManaged_shouldNotThrow() {
+ when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(false);
+
+ String customErrorMessage = "Custom error message";
+
+ assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfInstanceIsManaged(customErrorMessage));
+ }
+
@Test
public void throwIfInstanceIsManaged_whenInstanceNotExternallyManaged_shouldNotThrow() {
when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(false);
@Override
public UserRestResponse updateUser(String id, UserUpdateRestRequest updateRequest) {
userSession.checkLoggedIn().checkIsSystemAdministrator();
+
UpdateUser update = toUpdateUser(updateRequest);
UserInformation updatedUser = userService.updateUser(id, update);
return usersSearchResponseGenerator.toRestUser(updatedUser);