]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7973 Create /api/emails/send WS to test email configuration
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 1 Sep 2016 15:24:32 +0000 (17:24 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 2 Sep 2016 09:46:28 +0000 (11:46 +0200)
server/sonar-server/src/main/java/org/sonar/server/email/ws/EmailsWsModule.java
server/sonar-server/src/main/java/org/sonar/server/email/ws/SendAction.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/email/ws/EmailsWsModuleTest.java
server/sonar-server/src/test/java/org/sonar/server/email/ws/SendActionTest.java [new file with mode: 0644]

index 9db36399e9732d438a50d646f08f14e71b66819a..f1e9afda074ea6770eb8e75b707ad492c3d67e42 100644 (file)
@@ -26,6 +26,7 @@ public class EmailsWsModule extends Module {
   protected void configureModule() {
     add(
       EmailsWs.class,
-      UpdateConfigurationAction.class);
+      UpdateConfigurationAction.class,
+      SendAction.class);
   }
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/email/ws/SendAction.java b/server/sonar-server/src/main/java/org/sonar/server/email/ws/SendAction.java
new file mode 100644 (file)
index 0000000..a7b866a
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * 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 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.server.notification.email.EmailNotificationChannel;
+import org.sonar.server.user.UserSession;
+
+public class SendAction implements EmailsWsAction {
+
+  private static final String PARAM_TO = "to";
+  private static final String PARAM_SUBJECT = "subject";
+  private static final String PARAM_MESSAGE = "message";
+
+  private final UserSession userSession;
+  private final EmailNotificationChannel emailNotificationChannel;
+
+  public SendAction(UserSession userSession, EmailNotificationChannel emailNotificationChannel) {
+    this.userSession = userSession;
+    this.emailNotificationChannel = emailNotificationChannel;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    WebService.NewAction action = context.createAction("send")
+      .setDescription("Test email configuration by sending an email<br>" +
+        "Requires 'Administer System' permission.")
+      .setSince("6.1")
+      .setInternal(true)
+      .setPost(true)
+      .setHandler(this);
+
+    action.createParam(PARAM_TO)
+      .setDescription("Email address")
+      .setExampleValue("john@doo.com")
+      .setRequired(true);
+
+    action.createParam(PARAM_SUBJECT)
+      .setDescription("Subject of the email")
+      .setExampleValue("Test Message from SonarQube");
+
+    action.createParam(PARAM_MESSAGE)
+      .setDescription("Content of the email")
+      .setRequired(true);
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN);
+    emailNotificationChannel.sendTestEmail(request.mandatoryParam(PARAM_TO), request.param(PARAM_SUBJECT), request.mandatoryParam(PARAM_MESSAGE));
+    response.noContent();
+  }
+
+}
index e42ea79fe6843f9b924cc7237f63958263a0e226..63fed89a4c35f7c7f31b8efabe1d108450485dc6 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(2 + 2);
+    assertThat(container.size()).isEqualTo(3 + 2);
   }
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/email/ws/SendActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/email/ws/SendActionTest.java
new file mode 100644 (file)
index 0000000..e90cda1
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * 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.server.ws.WebService;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.notification.email.EmailNotificationChannel;
+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.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
+
+public class SendActionTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+
+  EmailNotificationChannel emailNotificationChannel = mock(EmailNotificationChannel.class);
+
+  WsActionTester ws = new WsActionTester(new SendAction(userSession, emailNotificationChannel));
+
+  @Test
+  public void send_test_email() throws Exception {
+    setUserAsSystemAdmin();
+
+    executeRequest("john@doo.com", "Test Message from SonarQube", "This is a test message from SonarQube at http://localhost:9000");
+
+    verify(emailNotificationChannel).sendTestEmail("john@doo.com", "Test Message from SonarQube", "This is a test message from SonarQube at http://localhost:9000");
+  }
+
+  @Test
+  public void does_not_fail_when_subject_param_is_missing() throws Exception {
+    setUserAsSystemAdmin();
+
+    executeRequest("john@doo.com", null, "This is a test message from SonarQube at http://localhost:9000");
+
+    verify(emailNotificationChannel).sendTestEmail("john@doo.com", null, "This is a test message from SonarQube at http://localhost:9000");
+  }
+
+  @Test
+  public void fail_when_to_param_is_missing() throws Exception {
+    setUserAsSystemAdmin();
+    expectedException.expect(IllegalArgumentException.class);
+
+    executeRequest(null, "Test Message from SonarQube", "This is a test message from SonarQube at http://localhost:9000");
+  }
+
+  @Test
+  public void fail_when_message_param_is_missing() throws Exception {
+    setUserAsSystemAdmin();
+    expectedException.expect(IllegalArgumentException.class);
+
+    executeRequest("john@doo.com", "Test Message from SonarQube", 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 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(3);
+  }
+
+  private void executeRequest(@Nullable String to, @Nullable String subject, @Nullable String message) {
+    TestRequest request = ws.newRequest();
+    if (to != null) {
+      request.setParam("to", to);
+    }
+    if (subject != null) {
+      request.setParam("subject", subject);
+    }
+    if (message != null) {
+      request.setParam("message", message);
+    }
+    request.execute();
+  }
+
+  private void setUserAsSystemAdmin() {
+    userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN);
+  }
+
+}