import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import org.sonar.api.internal.google.common.base.Preconditions;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
UserDto user = getUser(dbSession, login);
UpdateUser updateUser = new UpdateUser();
if (request.getName() != null) {
+ Preconditions.checkArgument(user.isLocal(), "Name cannot be updated for a non-local user");
updateUser.setName(request.getName());
}
if (request.getEmail() != null) {
+ Preconditions.checkArgument(user.isLocal(), "Email cannot be updated for a non-local user");
updateUser.setEmail(emptyToNull(request.getEmail()));
}
if (!request.getScmAccounts().isEmpty()) {
.assertJson(getClass(), "update_user.json");
}
+ @Test
+ public void fail_on_update_name_non_local_user() {
+ createUser(false);
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Name cannot be updated for a non-local user");
+
+ ws.newRequest()
+ .setParam("login", "john")
+ .setParam("name", "Jean Neige")
+ .execute();
+ }
+
+ @Test
+ public void fail_on_update_email_non_local_user() {
+ createUser(false);
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Email cannot be updated for a non-local user");
+
+ ws.newRequest()
+ .setParam("login", "john")
+ .setParam("email", "jean.neige@thegreatw.all")
+ .execute();
+ }
+
@Test
public void update_only_name() {
createUser();
}
private void createUser() {
+ createUser(true);
+ }
+
+ private void createUser(boolean local) {
UserDto userDto = newUserDto()
.setEmail("john@email.com")
.setLogin("john")
.setName("John")
.setScmAccounts(newArrayList("jn"))
.setActive(true)
- .setLocal(true)
+ .setLocal(local)
.setExternalLogin("jo")
.setExternalIdentityProvider("sonarqube");
dbClient.userDao().insert(dbSession, userDto);