]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8055 Remove /api/emails/update_configuration WS
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 5 Sep 2016 15:19:49 +0000 (17:19 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 6 Sep 2016 09:23:20 +0000 (11:23 +0200)
it/it-tests/src/test/java/it/settings/EmailsTest.java
it/it-tests/src/test/java/util/ItUtils.java
server/sonar-server/src/main/java/org/sonar/server/email/ws/EmailsWsModule.java
server/sonar-server/src/main/java/org/sonar/server/email/ws/UpdateConfigurationAction.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/email/ws/EmailsWsModuleTest.java
server/sonar-server/src/test/java/org/sonar/server/email/ws/UpdateConfigurationActionTest.java [deleted file]

index 329d17483a881ccd610b2584b0f66077a9e8b563..8533f89ff1b301512ac6c5b73934007a51af2ed7 100644 (file)
@@ -41,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.groups.Tuple.tuple;
 import static util.ItUtils.newAdminWsClient;
 import static util.ItUtils.resetEmailSettings;
+import static util.ItUtils.setServerProperty;
 
 public class EmailsTest {
 
@@ -55,7 +56,6 @@ public class EmailsTest {
   public static void before() throws Exception {
     ADMIN_WS_CLIENT = newAdminWsClient(orchestrator);
     SETTINGS = ADMIN_WS_CLIENT.settingsService();
-    resetEmailSettings(orchestrator);
 
     SMTP_SERVER = new Wiser(0);
     SMTP_SERVER.start();
@@ -74,6 +74,7 @@ public class EmailsTest {
   public void prepare() {
     orchestrator.resetData();
     SMTP_SERVER.getMessages().clear();
+    resetEmailSettings(orchestrator);
   }
 
   @Test
@@ -118,16 +119,13 @@ public class EmailsTest {
 
   private static void updateEmailSettings(@Nullable String host, @Nullable String port, @Nullable String from, @Nullable String prefix, @Nullable String secure,
     @Nullable String username, @Nullable String password) {
-    ADMIN_WS_CLIENT.wsConnector().call(
-      new PostRequest("/api/emails/update_configuration")
-        .setParam("host", host)
-        .setParam("port", port)
-        .setParam("from", from)
-        .setParam("prefix", prefix)
-        .setParam("secure", secure)
-        .setParam("username", username)
-        .setParam("password", password))
-      .failIfNotSuccessful();
+    setServerProperty(orchestrator, "email.smtp_host.secured", host);
+    setServerProperty(orchestrator, "email.smtp_port.secured", port);
+    setServerProperty(orchestrator, "email.smtp_secure_connection.secured", secure);
+    setServerProperty(orchestrator, "email.smtp_username.secured", username);
+    setServerProperty(orchestrator, "email.smtp_password.secured", password);
+    setServerProperty(orchestrator, "email.from", from);
+    setServerProperty(orchestrator, "email.prefix", prefix);
   }
 
   private static void sendEmail(String to, String subject, String message) {
index 61207522e0a91831010b1af6871b3e096a2ba97c..9d1241340b156a045b26982b5736208979d9738d 100644 (file)
@@ -36,6 +36,7 @@ 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;
@@ -53,10 +54,8 @@ import org.sonar.wsclient.issue.IssueQuery;
 import org.sonar.wsclient.services.PropertyDeleteQuery;
 import org.sonar.wsclient.services.PropertyUpdateQuery;
 import org.sonarqube.ws.client.HttpConnector;
-import org.sonarqube.ws.client.PostRequest;
 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;
@@ -227,19 +226,17 @@ public class ItUtils {
   }
 
   public static void resetSettings(Orchestrator orchestrator, @Nullable String componentKey, String... keys) {
-    newAdminWsClient(orchestrator).settingsService().reset(ResetRequest.builder().setKeys(keys).setComponentKey(componentKey).build());
+    Arrays.stream(keys).forEach(key -> setServerProperty(orchestrator, componentKey, key, null));
   }
 
   public static void resetEmailSettings(Orchestrator orchestrator) {
-    newAdminWsClient(orchestrator).wsConnector().call(new PostRequest("/api/emails/update_configuration")).failIfNotSuccessful();
+    resetSettings(orchestrator, null, "email.smtp_host.secured", "email.smtp_port.secured", "email.smtp_secure_connection.secured", "email.smtp_username.secured",
+      "email.smtp_password.secured", "email.from", "email.prefix");
   }
 
   public static void resetPeriods(Orchestrator orchestrator) {
-    setServerProperty(orchestrator, "sonar.timemachine.period1", null);
-    setServerProperty(orchestrator, "sonar.timemachine.period2", null);
-    setServerProperty(orchestrator, "sonar.timemachine.period3", null);
-    setServerProperty(orchestrator, "sonar.timemachine.period4", null);
-    setServerProperty(orchestrator, "sonar.timemachine.period5", null);
+    resetSettings(orchestrator, null, "sonar.timemachine.period1", "sonar.timemachine.period2", "sonar.timemachine.period3", "sonar.timemachine.period4",
+      "sonar.timemachine.period5");
   }
 
   /**
index f1e9afda074ea6770eb8e75b707ad492c3d67e42..586787290f41b644eb1585a43b82fb3d93c80a7a 100644 (file)
@@ -26,7 +26,6 @@ public class EmailsWsModule extends Module {
   protected void configureModule() {
     add(
       EmailsWs.class,
-      UpdateConfigurationAction.class,
       SendAction.class);
   }
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/email/ws/UpdateConfigurationAction.java b/server/sonar-server/src/main/java/org/sonar/server/email/ws/UpdateConfigurationAction.java
deleted file mode 100644 (file)
index 524efb8..0000000
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.server.email.ws;
-
-import javax.annotation.Nullable;
-import org.sonar.api.config.Settings;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.property.PropertyDto;
-import org.sonar.server.user.UserSession;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static org.sonar.api.config.EmailSettings.FROM;
-import static org.sonar.api.config.EmailSettings.FROM_DEFAULT;
-import static org.sonar.api.config.EmailSettings.PREFIX;
-import static org.sonar.api.config.EmailSettings.PREFIX_DEFAULT;
-import static org.sonar.api.config.EmailSettings.SMTP_HOST;
-import static org.sonar.api.config.EmailSettings.SMTP_PASSWORD;
-import static org.sonar.api.config.EmailSettings.SMTP_PORT;
-import static org.sonar.api.config.EmailSettings.SMTP_PORT_DEFAULT;
-import static org.sonar.api.config.EmailSettings.SMTP_SECURE_CONNECTION;
-import static org.sonar.api.config.EmailSettings.SMTP_USERNAME;
-
-public class UpdateConfigurationAction implements EmailsWsAction {
-
-  private static final String PARAM_HOST = "host";
-  private static final String PARAM_PORT = "port";
-  private static final String PARAM_SECURE = "secure";
-  private static final String PARAM_USERNAME = "username";
-  private static final String PARAM_PASSWORD = "password";
-  private static final String PARAM_FROM = "from";
-  private static final String PARAM_PREFIX = "prefix";
-
-  private static final String SSL_VALUE = "ssl";
-  private static final String STARTTLS_VALUE = "starttls";
-
-  private final DbClient dbClient;
-  private final UserSession userSession;
-
-  // TODO remove this when EmailSettings won't use Settings in memory to get settings
-  private final Settings settings;
-
-  public UpdateConfigurationAction(DbClient dbClient, UserSession userSession, Settings settings) {
-    this.dbClient = dbClient;
-    this.userSession = userSession;
-    this.settings = settings;
-  }
-
-  @Override
-  public void define(WebService.NewController context) {
-    WebService.NewAction action = context.createAction("update_configuration")
-      .setDescription("Update email configuration<br/>" +
-        "If a parameter is not set, it will be reset<br>" +
-        "Requires 'Administer System' permission.")
-      .setSince("6.1")
-      .setInternal(true)
-      .setPost(true)
-      .setHandler(this);
-
-    action.createParam(PARAM_HOST)
-      .setDescription("SMTP host.")
-      .setExampleValue("smtp.gmail.com");
-
-    action.createParam(PARAM_PORT)
-      .setDescription("Port number to connect with SMTP server.")
-      .setExampleValue(SMTP_PORT_DEFAULT);
-
-    action.createParam(PARAM_SECURE)
-      .setDescription("Whether to use secure connection and its type.")
-      .setPossibleValues(SSL_VALUE, STARTTLS_VALUE)
-      .setExampleValue(SSL_VALUE);
-
-    action.createParam(PARAM_USERNAME)
-      .setDescription("Username to use with authenticated SMTP.")
-      .setExampleValue("my_username");
-
-    action.createParam(PARAM_PASSWORD)
-      .setDescription("Username to use with authenticated SMTP.")
-      .setExampleValue("my_password");
-
-    action.createParam(PARAM_FROM)
-      .setDescription("Emails will come from this address. For example - \"noreply@sonarsource.com\". Note that server may ignore this setting.")
-      .setExampleValue(FROM_DEFAULT);
-
-    action.createParam(PARAM_PREFIX)
-      .setDescription("Prefix will be prepended to all outgoing email subjects.")
-      .setExampleValue(PREFIX_DEFAULT);
-  }
-
-  @Override
-  public void handle(Request request, Response response) throws Exception {
-    userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN);
-
-    DbSession dbSession = dbClient.openSession(false);
-    try {
-      save(dbSession, SMTP_HOST, request.param(PARAM_HOST), PARAM_HOST);
-      save(dbSession, SMTP_PORT, request.hasParam(PARAM_PORT) ? Integer.toString(request.paramAsInt(PARAM_PORT)) : null, PARAM_PORT);
-      save(dbSession, SMTP_SECURE_CONNECTION, request.param(PARAM_SECURE), PARAM_SECURE);
-      save(dbSession, SMTP_USERNAME, request.param(PARAM_USERNAME), PARAM_USERNAME);
-      save(dbSession, SMTP_PASSWORD, request.param(PARAM_PASSWORD), PARAM_PASSWORD);
-      save(dbSession, FROM, request.param(PARAM_FROM), PARAM_FROM);
-      save(dbSession, PREFIX, request.param(PARAM_PREFIX), PARAM_PREFIX);
-      dbSession.commit();
-    } finally {
-      dbClient.closeSession(dbSession);
-    }
-    response.noContent();
-  }
-
-  private void save(DbSession dbSession, String settingKey, @Nullable String value, String requestKey) {
-    checkArgument(value == null || !value.isEmpty(), "Parameter '%s' cannot have an empty value", requestKey);
-    settings.setProperty(settingKey, value);
-    if (value != null) {
-      dbClient.propertiesDao().insertProperty(dbSession, new PropertyDto().setKey(settingKey).setValue(value));
-    } else {
-      dbClient.propertiesDao().deleteGlobalProperty(settingKey, dbSession);
-    }
-  }
-
-}
index 63fed89a4c35f7c7f31b8efabe1d108450485dc6..e42ea79fe6843f9b924cc7237f63958263a0e226 100644 (file)
@@ -29,6 +29,6 @@ public class EmailsWsModuleTest {
   public void verify_count_of_added_components() {
     ComponentContainer container = new ComponentContainer();
     new EmailsWsModule().configure(container);
-    assertThat(container.size()).isEqualTo(3 + 2);
+    assertThat(container.size()).isEqualTo(2 + 2);
   }
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/email/ws/UpdateConfigurationActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/email/ws/UpdateConfigurationActionTest.java
deleted file mode 100644 (file)
index a03ad95..0000000
+++ /dev/null
@@ -1,184 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.server.email.ws;
-
-import javax.annotation.Nullable;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.config.MapSettings;
-import org.sonar.api.config.Settings;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.System2;
-import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbTester;
-import org.sonar.db.property.PropertyDbTester;
-import org.sonar.db.property.PropertyDto;
-import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.ws.TestRequest;
-import org.sonar.server.ws.WsActionTester;
-
-import static org.assertj.core.api.Java6Assertions.assertThat;
-import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
-import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto;
-
-public class UpdateConfigurationActionTest {
-
-  @Rule
-  public ExpectedException expectedException = ExpectedException.none();
-
-  @Rule
-  public UserSessionRule userSession = UserSessionRule.standalone();
-
-  @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
-
-  private DbClient dbClient = db.getDbClient();
-  private PropertyDbTester propertyDb = new PropertyDbTester(db);
-  private Settings settings = new MapSettings();
-
-  private WsActionTester ws = new WsActionTester(new UpdateConfigurationAction(dbClient, userSession, settings));
-
-  @Test
-  public void update_email_settings() throws Exception {
-    setUserAsSystemAdmin();
-
-    executeRequest("smtp.gmail.com", 25, "starttls", "john", "12345", "noreply@email.com", "[EMAIL]");
-
-    assertSetting("email.smtp_host.secured", "smtp.gmail.com");
-    assertSetting("email.smtp_port.secured", "25");
-    assertSetting("email.smtp_secure_connection.secured", "starttls");
-    assertSetting("email.smtp_username.secured", "john");
-    assertSetting("email.smtp_password.secured", "12345");
-    assertSetting("email.from", "noreply@email.com");
-    assertSetting("email.prefix", "[EMAIL]");
-  }
-
-  @Test
-  public void does_not_save_settings_when_no_value_sent() throws Exception {
-    setUserAsSystemAdmin();
-
-    executeRequest(null, null, null, null, null, null, null);
-
-    assertThat(db.countRowsOfTable(db.getSession(), "properties")).isZero();
-  }
-
-  @Test
-  public void remove_existing_settings_when_no_value_sent() throws Exception {
-    setUserAsSystemAdmin();
-    addSetting("email.smtp_host.secured", "smtp.gmail.com");
-    addSetting("email.smtp_port.secured", "25");
-    addSetting("email.smtp_secure_connection.secured", "starttls");
-    addSetting("email.smtp_username.secured", "john");
-    addSetting("email.smtp_password.secured", "12345");
-    addSetting("email.from", "noreply@email.com");
-    addSetting("email.prefix", "[EMAIL]");
-
-    executeRequest(null, null, null, null, null, null, null);
-
-    assertThat(db.countRowsOfTable(db.getSession(), "properties")).isZero();
-  }
-
-  @Test
-  public void fail_when_secure_param_is_invalid() {
-    setUserAsSystemAdmin();
-
-    expectedException.expect(IllegalArgumentException.class);
-
-    executeRequest(null, null, "unknown", null, null, null, null);
-  }
-
-  @Test
-  public void fail_when_insufficient_privileges() {
-    userSession.anonymous().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
-    expectedException.expect(ForbiddenException.class);
-
-    ws.newRequest().execute();
-  }
-
-  @Test
-  public void fail_when_parameter_is_empty() {
-    setUserAsSystemAdmin();
-    expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("Parameter 'host' cannot have an empty value");
-
-    executeRequest("", null, null, null, null, null, null);
-  }
-
-  @Test
-  public void test_ws_definition() {
-    WebService.Action action = ws.getDef();
-    assertThat(action).isNotNull();
-    assertThat(action.isInternal()).isTrue();
-    assertThat(action.isPost()).isTrue();
-    assertThat(action.responseExampleAsString()).isNull();
-    assertThat(action.params()).hasSize(7);
-  }
-
-  private void assertSetting(String key, String value) {
-    PropertyDto result = dbClient.propertiesDao().selectGlobalProperty(key);
-
-    assertThat(result)
-      .extracting(PropertyDto::getKey, PropertyDto::getValue, PropertyDto::getResourceId)
-      .containsExactly(key, value, null);
-
-    assertThat(settings.getString(key)).isEqualTo(value);
-  }
-
-  private void executeRequest(@Nullable String host, @Nullable Integer port, @Nullable String secure,
-    @Nullable String username, @Nullable String password, @Nullable String from, @Nullable String prefix) {
-    TestRequest request = ws.newRequest();
-    if (host != null) {
-      request.setParam("host", host);
-    }
-    if (port != null) {
-      request.setParam("port", Integer.toString(port));
-    }
-    if (secure != null) {
-      request.setParam("secure", secure);
-    }
-    if (username != null) {
-      request.setParam("username", username);
-    }
-    if (password != null) {
-      request.setParam("password", password);
-    }
-    if (from != null) {
-      request.setParam("from", from);
-    }
-    if (prefix != null) {
-      request.setParam("prefix", prefix);
-    }
-    request.execute();
-  }
-
-  private void addSetting(String key, String value) {
-    propertyDb.insertProperty(newGlobalPropertyDto(key, value));
-    settings.setProperty(key, value);
-  }
-
-  private void setUserAsSystemAdmin() {
-    userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN);
-  }
-
-}