From: Julien Lancelot Date: Thu, 1 Sep 2016 15:24:32 +0000 (+0200) Subject: SONAR-7973 Create /api/emails/send WS to test email configuration X-Git-Tag: 6.1-RC1~238 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=27ae635f118b7b07309ab13469f35a541eb08794;p=sonarqube.git SONAR-7973 Create /api/emails/send WS to test email configuration --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/email/ws/EmailsWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/email/ws/EmailsWsModule.java index 9db36399e97..f1e9afda074 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/email/ws/EmailsWsModule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/email/ws/EmailsWsModule.java @@ -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 index 00000000000..a7b866acfe1 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/email/ws/SendAction.java @@ -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
" + + "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(); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/email/ws/EmailsWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/email/ws/EmailsWsModuleTest.java index e42ea79fe68..63fed89a4c3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/email/ws/EmailsWsModuleTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/email/ws/EmailsWsModuleTest.java @@ -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 index 00000000000..e90cda1057b --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/email/ws/SendActionTest.java @@ -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); + } + +}