From e2a19a2275611e8b1984576e4a048ca772ed10e1 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 8 Sep 2016 11:30:58 +0200 Subject: [PATCH] Sanitize usage of ItUtils.setServerProperty - Period4 and period5 settings should only be reset at component level - Replace empty string by null to remove setting when using ItUtils.setServerProperty --- .../test/java/it/settings/SettingsTest.java | 2 +- .../it/user/BaseIdentityProviderTest.java | 24 ++++++++----------- .../java/it/user/RealmAuthenticationTest.java | 5 ++++ it/it-tests/src/test/java/util/ItUtils.java | 7 +++--- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/it/it-tests/src/test/java/it/settings/SettingsTest.java b/it/it-tests/src/test/java/it/settings/SettingsTest.java index d44d6c3d923..64d6da45e20 100644 --- a/it/it-tests/src/test/java/it/settings/SettingsTest.java +++ b/it/it-tests/src/test/java/it/settings/SettingsTest.java @@ -76,7 +76,7 @@ public class SettingsTest { */ @Test public void global_property_change_extension_point() throws IOException { - orchestrator.getServer().adminWsClient().post("api/properties/create?id=globalPropertyChange.received&value=NEWVALUE"); + SETTINGS.set(SetRequest.builder().setKey("globalPropertyChange.received").setValue("NEWVALUE").build()); assertThat(FileUtils.readFileToString(orchestrator.getServer().getLogs())).contains("Received change: [key=globalPropertyChange.received, newValue=NEWVALUE]"); } diff --git a/it/it-tests/src/test/java/it/user/BaseIdentityProviderTest.java b/it/it-tests/src/test/java/it/user/BaseIdentityProviderTest.java index 2890fc95d97..fcccdd5545a 100644 --- a/it/it-tests/src/test/java/it/user/BaseIdentityProviderTest.java +++ b/it/it-tests/src/test/java/it/user/BaseIdentityProviderTest.java @@ -40,6 +40,7 @@ import util.user.Users; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.guava.api.Assertions.assertThat; import static util.ItUtils.newAdminWsClient; +import static util.ItUtils.resetSettings; import static util.ItUtils.setServerProperty; /** @@ -81,11 +82,8 @@ public class BaseIdentityProviderTest { public void cleanUpUsersAndGroupsAndProperties() throws Exception { userRule.deactivateUsers(USER_LOGIN); userRule.removeGroups(GROUP1, GROUP2, GROUP3); - setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.enabled", null); - setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.user", null); - setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.throwUnauthorizedMessage", null); - setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.enabledGroupsSync", null); - setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.groups", null); + resetSettings(ORCHESTRATOR, null, "sonar.auth.fake-base-id-provider.enabled", "sonar.auth.fake-base-id-provider.user", + "sonar.auth.fake-base-id-provider.throwUnauthorizedMessage", "sonar.auth.fake-base-id-provider.enabledGroupsSync", "sonar.auth.fake-base-id-provider.groups"); } @Test @@ -107,8 +105,7 @@ public class BaseIdentityProviderTest { setUserCreatedByAuthPlugin(USER_LOGIN, USER_PROVIDER_ID, USER_NAME, USER_EMAIL); new SeleneseTest(Selenese.builder().setHtmlTestsInClasspath("authenticate_through_ui", - "/user/BaseIdentityProviderTest/authenticate_user.html" - ).build()).runOn(ORCHESTRATOR); + "/user/BaseIdentityProviderTest/authenticate_user.html").build()).runOn(ORCHESTRATOR); userRule.verifyUserExists(USER_LOGIN, USER_NAME, USER_EMAIL); } @@ -120,8 +117,7 @@ public class BaseIdentityProviderTest { setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.user", null); new SeleneseTest(Selenese.builder().setHtmlTestsInClasspath("display_unauthorized_page_when_authentication_failed", - "/user/BaseIdentityProviderTest/display_unauthorized_page_when_authentication_failed.html" - ).build()).runOn(ORCHESTRATOR); + "/user/BaseIdentityProviderTest/display_unauthorized_page_when_authentication_failed.html").build()).runOn(ORCHESTRATOR); userRule.verifyUserDoesNotExist(USER_LOGIN); } @@ -133,8 +129,7 @@ public class BaseIdentityProviderTest { setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.allowsUsersToSignUp", "false"); new SeleneseTest(Selenese.builder().setHtmlTestsInClasspath("fail_to_authenticate_when_not_allowed_to_sign_up", - "/user/BaseIdentityProviderTest/fail_to_authenticate_when_not_allowed_to_sign_up.html" - ).build()).runOn(ORCHESTRATOR); + "/user/BaseIdentityProviderTest/fail_to_authenticate_when_not_allowed_to_sign_up.html").build()).runOn(ORCHESTRATOR); userRule.verifyUserDoesNotExist(USER_LOGIN); } @@ -196,8 +191,7 @@ public class BaseIdentityProviderTest { setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.throwUnauthorizedMessage", "true"); new SeleneseTest(Selenese.builder().setHtmlTestsInClasspath("fail_to_authenticate_when_not_allowed_to_sign_up", - "/user/BaseIdentityProviderTest/fail_to_authenticate_when_not_allowed_to_sign_up.html" - ).build()).runOn(ORCHESTRATOR); + "/user/BaseIdentityProviderTest/fail_to_authenticate_when_not_allowed_to_sign_up.html").build()).runOn(ORCHESTRATOR); File logFile = ORCHESTRATOR.getServer().getLogs(); assertThat(FileUtils.readFileToString(logFile)).doesNotContain("A functional error has happened"); @@ -263,7 +257,9 @@ public class BaseIdentityProviderTest { private static void setGroupsReturnedByAuthPlugin(String... groups) { setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.enabledGroupsSync", "true"); - setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.groups", Joiner.on(",").join(groups)); + if (groups.length > 0) { + setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.groups", Joiner.on(",").join(groups)); + } } private static void authenticateWithFakeAuthProvider() { diff --git a/it/it-tests/src/test/java/it/user/RealmAuthenticationTest.java b/it/it-tests/src/test/java/it/user/RealmAuthenticationTest.java index c61398ed44f..59863a364bf 100644 --- a/it/it-tests/src/test/java/it/user/RealmAuthenticationTest.java +++ b/it/it-tests/src/test/java/it/user/RealmAuthenticationTest.java @@ -25,6 +25,7 @@ import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.selenium.Selenese; import java.util.Map; import java.util.Objects; +import javax.annotation.CheckForNull; import org.apache.commons.lang.RandomStringUtils; import org.junit.After; import org.junit.Before; @@ -392,7 +393,11 @@ public class RealmAuthenticationTest { return new Sonar(new HttpClient4Connector(new Host(orchestrator.getServer().getUrl(), username, password))); } + @CheckForNull private static String format(Map map) { + if (map.isEmpty()) { + return null; + } StringBuilder sb = new StringBuilder(); for (Map.Entry entry : map.entrySet()) { sb.append(entry.getKey()).append('=').append(entry.getValue()).append('\n'); diff --git a/it/it-tests/src/test/java/util/ItUtils.java b/it/it-tests/src/test/java/util/ItUtils.java index 9d1241340b1..9c00d48c63b 100644 --- a/it/it-tests/src/test/java/util/ItUtils.java +++ b/it/it-tests/src/test/java/util/ItUtils.java @@ -36,7 +36,6 @@ import java.io.IOException; import java.lang.reflect.Type; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; @@ -56,6 +55,7 @@ import org.sonar.wsclient.services.PropertyUpdateQuery; import org.sonarqube.ws.client.HttpConnector; import org.sonarqube.ws.client.WsClient; import org.sonarqube.ws.client.WsClientFactories; +import org.sonarqube.ws.client.setting.ResetRequest; import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.FluentIterable.from; @@ -226,7 +226,7 @@ public class ItUtils { } public static void resetSettings(Orchestrator orchestrator, @Nullable String componentKey, String... keys) { - Arrays.stream(keys).forEach(key -> setServerProperty(orchestrator, componentKey, key, null)); + newAdminWsClient(orchestrator).settingsService().reset(ResetRequest.builder().setKeys(keys).setComponentKey(componentKey).build()); } public static void resetEmailSettings(Orchestrator orchestrator) { @@ -235,8 +235,7 @@ public class ItUtils { } public static void resetPeriods(Orchestrator orchestrator) { - resetSettings(orchestrator, null, "sonar.timemachine.period1", "sonar.timemachine.period2", "sonar.timemachine.period3", "sonar.timemachine.period4", - "sonar.timemachine.period5"); + resetSettings(orchestrator, null, "sonar.timemachine.period1", "sonar.timemachine.period2", "sonar.timemachine.period3"); } /** -- 2.39.5