From 50b630a089cd97721e88b737ef9079df70afaeec Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 9 Sep 2016 17:20:08 +0200 Subject: [PATCH] SONAR-8037 Validate settings of type USER_LOGIN --- .../server/setting/ws/SettingValidations.java | 21 ++++++-- .../server/setting/ws/SetActionTest.java | 49 ++++++++++++++++++- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingValidations.java b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingValidations.java index 6281ee259a0..89291bbc97b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingValidations.java +++ b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingValidations.java @@ -33,6 +33,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.db.metric.MetricDto; +import org.sonar.db.user.UserDto; import org.sonar.server.exceptions.BadRequestException; import static java.lang.String.format; @@ -103,13 +104,15 @@ public class SettingValidations { } if (definition.type() == PropertyType.METRIC) { - metric(data); + validateMetric(data); + } else if (definition.type() == PropertyType.USER_LOGIN) { + validateLogin(data); } else { - otherTypes(data, definition); + validateOtherTypes(data, definition); } } - private void otherTypes(SettingData data, PropertyDefinition definition) { + private void validateOtherTypes(SettingData data, PropertyDefinition definition) { data.values.stream() .map(definition::validate) .filter(result -> !result.isValid()) @@ -120,12 +123,20 @@ public class SettingValidations { }); } - private void metric(SettingData data) { + private void validateMetric(SettingData data) { try (DbSession dbSession = dbClient.openSession(false)) { - List metrics = dbClient.metricDao().selectByKeys(dbSession, data.values); + List metrics = dbClient.metricDao().selectByKeys(dbSession, data.values).stream().filter(MetricDto::isEnabled).collect(Collectors.toList()); checkRequest(data.values.size() == metrics.size(), "Error when validating metric setting with key '%s' and values [%s]. A value is not a valid metric key.", data.key, data.values.stream().collect(Collectors.joining(", "))); } } + + private void validateLogin(SettingData data) { + try (DbSession dbSession = dbClient.openSession(false)) { + List users = dbClient.userDao().selectByLogins(dbSession, data.values).stream().filter(UserDto::isActive).collect(Collectors.toList()); + checkRequest(data.values.size() == users.size(), "Error when validating login setting with key '%s' and values [%s]. A value is not a valid login.", + data.key, data.values.stream().collect(Collectors.joining(", "))); + } + } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java index ec4206d1796..e7eae206c3e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java @@ -46,6 +46,7 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.property.PropertyDbTester; import org.sonar.db.property.PropertyDto; import org.sonar.db.property.PropertyQuery; +import org.sonar.db.user.UserDbTester; import org.sonar.scanner.protocol.GsonHelper; import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.BadRequestException; @@ -65,6 +66,7 @@ import static org.sonar.db.component.ComponentTesting.newView; import static org.sonar.db.metric.MetricTesting.newMetricDto; import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto; import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto; +import static org.sonar.db.user.UserTesting.newUserDto; public class SetActionTest { @@ -78,6 +80,7 @@ public class SetActionTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); PropertyDbTester propertyDb = new PropertyDbTester(db); + UserDbTester userDb = new UserDbTester(db); ComponentDbTester componentDb = new ComponentDbTester(db); DbClient dbClient = db.getDbClient(); DbSession dbSession = db.getSession(); @@ -327,6 +330,26 @@ public class SetActionTest { assertGlobalSetting("my.key", "metric.key.1,metric.key.2"); } + @Test + public void persist_multi_value_with_type_login() { + definitions.addComponent(PropertyDefinition + .builder("my.key") + .name("foo") + .description("desc") + .category("cat") + .subCategory("subCat") + .type(PropertyType.USER_LOGIN) + .defaultValue("default") + .multiValues(true) + .build()); + userDb.insertUser(newUserDto().setLogin("login.1")); + userDb.insertUser(newUserDto().setLogin("login.2")); + + callForMultiValueGlobalSetting("my.key", newArrayList("login.1", "login.2")); + + assertGlobalSetting("my.key", "login.1,login.2"); + } + @Test public void user_setting_is_not_updated() { propertyDb.insertProperty(newGlobalPropertyDto("my.key", "my user value").setUserId(42L)); @@ -426,12 +449,34 @@ public class SetActionTest { .multiValues(true) .build()); dbClient.metricDao().insert(dbSession, newMetricDto().setKey("metric.key")); + dbClient.metricDao().insert(dbSession, newMetricDto().setKey("metric.disabled.key").setEnabled(false)); dbSession.commit(); expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Error when validating metric setting with key 'my.key' and values [metric.key, My Value]. A value is not a valid metric key."); + expectedException.expectMessage("Error when validating metric setting with key 'my.key' and values [metric.key, metric.disabled.key]. A value is not a valid metric key."); + + callForMultiValueGlobalSetting("my.key", newArrayList("metric.key", "metric.disabled.key")); + } + + @Test + public void fail_when_data_and_login_type_with_invalid_login() { + definitions.addComponent(PropertyDefinition + .builder("my.key") + .name("foo") + .description("desc") + .category("cat") + .subCategory("subCat") + .type(PropertyType.USER_LOGIN) + .defaultValue("default") + .multiValues(true) + .build()); + userDb.insertUser(newUserDto().setLogin("login.1")); + userDb.insertUser(newUserDto().setLogin("login.2").setActive(false)); + + expectedException.expect(BadRequestException.class); + expectedException.expectMessage("Error when validating login setting with key 'my.key' and values [login.1, login.2]. A value is not a valid login."); - callForMultiValueGlobalSetting("my.key", newArrayList("metric.key", "My Value")); + callForMultiValueGlobalSetting("my.key", newArrayList("login.1", "login.2")); } @Test -- 2.39.5