MimeMessage email = messages.get(0).getMimeMessage();
assertThat(email.getHeader("Content-Type", null)).isEqualTo("text/plain; charset=UTF-8");
- assertThat(email.getHeader("From", ",")).isEqualTo("SonarQube <server@nowhere>");
+ assertThat(email.getHeader("From", ",")).isEqualTo("SonarQube from NoWhere <server@nowhere>");
assertThat(email.getHeader("To", null)).isEqualTo("<user@nowhere>");
assertThat(email.getHeader("Subject", null)).isEqualTo("[SONARQUBE] Test Message from SonarQube");
assertThat((String) email.getContent()).startsWith("This is a test message from SonarQube.");
assertThat(email.getHeader("List-ID", null)).isEqualTo("SonarQube <sonar.nemo.sonarsource.org>");
assertThat(email.getHeader("List-Archive", null)).isEqualTo("http://nemo.sonarsource.org");
- assertThat(email.getHeader("From", ",")).isEqualTo("\"Full Username (SonarQube)\" <server@nowhere>");
+ assertThat(email.getHeader("From", ",")).isEqualTo("\"Full Username (SonarQube from NoWhere)\" <server@nowhere>");
assertThat(email.getHeader("To", null)).isEqualTo("<user@nowhere>");
assertThat(email.getHeader("Subject", null)).isEqualTo("[SONARQUBE] Review #3");
assertThat((String) email.getContent()).startsWith("I'll take care of this violation.");
assertThat(email.getHeader("List-ID", null)).isEqualTo("SonarQube <sonar.nemo.sonarsource.org>");
assertThat(email.getHeader("List-Archive", null)).isEqualTo("http://nemo.sonarsource.org");
- assertThat(email.getHeader("From", null)).isEqualTo("SonarQube <server@nowhere>");
+ assertThat(email.getHeader("From", null)).isEqualTo("SonarQube from NoWhere <server@nowhere>");
assertThat(email.getHeader("To", null)).isEqualTo("<user@nowhere>");
assertThat(email.getHeader("Subject", null)).isEqualTo("[SONARQUBE] Foo");
assertThat((String) email.getContent()).startsWith("Bar");
when(configuration.getSmtpHost()).thenReturn("localhost");
when(configuration.getSmtpPort()).thenReturn(smtpServer.getServer().getPort());
when(configuration.getFrom()).thenReturn("server@nowhere");
+ when(configuration.getFromName()).thenReturn("SonarQube from NoWhere");
when(configuration.getPrefix()).thenReturn("[SONARQUBE]");
when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
}
public static final String SMTP_PASSWORD_DEFAULT = "";
public static final String FROM = "email.from";
public static final String FROM_DEFAULT = "noreply@nowhere";
+ public static final String FROM_NAME = "email.fromName";
+ public static final String FROM_NAME_DEFAULT = "SonarQube";
public static final String PREFIX = "email.prefix";
public static final String PREFIX_DEFAULT = "[SONARQUBE]";
return get(FROM, FROM_DEFAULT);
}
+ public String getFromName() {
+ return get(FROM_NAME, FROM_NAME_DEFAULT);
+ }
+
public String getPrefix() {
return get(PREFIX, PREFIX_DEFAULT);
}
.category(CATEGORY_GENERAL)
.subCategory(SUBCATEGORY_EMAIL)
.build(),
+ PropertyDefinition.builder(FROM_NAME)
+ .name("From name")
+ .description("Emails will come from this address name. For example - \"SonarQube\". Note that server may ignore this setting.")
+ .defaultValue(FROM_NAME_DEFAULT)
+ .category(CATEGORY_GENERAL)
+ .subCategory(SUBCATEGORY_EMAIL)
+ .build(),
PropertyDefinition.builder(PREFIX)
.name("Email prefix")
.description("Prefix will be prepended to all outgoing email subjects.")
// Create quality gate with conditions on variations
WsQualityGates.CreateWsResponse simple = tester.qGates().generate();
- tester.qGates().service().createCondition(CreateConditionRequest.builder().setQualityGateId(simple.getId()).setMetricKey("ncloc").setPeriod(1).setOperator("EQ").setWarning("0").build());
+ tester.qGates().service()
+ .createCondition(CreateConditionRequest.builder().setQualityGateId(simple.getId()).setMetricKey("ncloc").setPeriod(1).setOperator("EQ").setWarning("0").build());
Project project = tester.projects().generate(null);
tester.qGates().associateProject(simple, project);
assertThat(emails.hasNext()).isTrue();
message = emails.next().getMimeMessage();
assertThat(message.getHeader("To", null)).isEqualTo("<tester@example.org>");
- assertThat((String) message.getContent()).contains("Quality gate status: Orange (was Green)");
- assertThat((String) message.getContent()).contains("Quality gate threshold: Lines of Code variation = 0 since previous analysis");
- assertThat((String) message.getContent()).contains("/dashboard?id=" + project.getKey());
+ assertThat((String) message.getContent())
+ .contains("Project: Sample")
+ .contains("Version: 1.0-SNAPSHOT")
+ .contains("Quality gate status: Orange (was Green)")
+ .contains("Quality gate threshold: Lines of Code variation = 0 since previous analysis")
+ .contains("/dashboard?id=" + project.getKey());
assertThat(emails.hasNext()).isFalse();
}
package org.sonarqube.tests.settings;
import com.sonar.orchestrator.Orchestrator;
+import java.util.Arrays;
import java.util.Iterator;
import javax.annotation.Nullable;
+import javax.mail.Address;
import javax.mail.internet.MimeMessage;
import org.junit.AfterClass;
import org.junit.Before;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
+import static java.lang.String.format;
import static junit.framework.TestCase.fail;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
@Test
public void update_email_settings() throws Exception {
- updateEmailSettings("localhost", "42", "noreply@email.com", "[EMAIL]", "ssl", "john", "123456");
+ updateEmailSettings("localhost", "42", "noreply@email.com", "The Devil", "[EMAIL]", "ssl", "john", "123456");
Settings.ValuesWsResponse response = tester.settings().service().values(ValuesRequest.builder()
.setKeys("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")
+ "email.from", "email.fromName", "email.prefix")
.build());
assertThat(response.getSettingsList()).extracting(Settings.Setting::getKey, Settings.Setting::getValue)
tuple("email.smtp_username.secured", "john"),
tuple("email.smtp_password.secured", "123456"),
tuple("email.from", "noreply@email.com"),
+ tuple("email.fromName", "The Devil"),
tuple("email.prefix", "[EMAIL]"));
}
@Test
public void send_test_email() throws Exception {
- updateEmailSettings("localhost", Integer.toString(SMTP_SERVER.getServer().getPort()), null, null, null, null, null);
+ updateEmailSettings("localhost", Integer.toString(SMTP_SERVER.getServer().getPort()), null, null, null, null, null, null);
sendEmail("test@example.org", "Test Message from SonarQube", "This is a test message from SonarQube");
waitUntilAllNotificationsAreDelivered(1);
Iterator<WiserMessage> emails = SMTP_SERVER.getMessages().iterator();
MimeMessage message = emails.next().getMimeMessage();
+ assertThat(Arrays.stream(message.getFrom()).map(Address::toString)).containsOnly("SonarQube <noreply@nowhere>");
assertThat(message.getHeader("To", null)).isEqualTo("<test@example.org>");
- assertThat(message.getSubject()).contains("Test Message from SonarQube");
+ assertThat(message.getSubject()).isEqualTo("[SONARQUBE] Test Message from SonarQube");
+ assertThat((String) message.getContent()).contains("This is a test message from SonarQube");
+ assertThat(emails.hasNext()).isFalse();
+ }
+
+ @Test
+ public void send_customized_test_email() throws Exception {
+ String from = randomAlphanumeric(4) + "@" + randomAlphabetic(5);
+ String fromName = randomAlphanumeric(5);
+ String prefix = randomAlphanumeric(6);
+ updateEmailSettings("localhost", Integer.toString(SMTP_SERVER.getServer().getPort()), from, fromName, prefix, null, null, null);
+
+ sendEmail("test@example.org", "Test Message from SonarQube", "This is a test message from SonarQube");
+
+ // We need to wait until all notifications will be delivered
+ waitUntilAllNotificationsAreDelivered(1);
+ Iterator<WiserMessage> emails = SMTP_SERVER.getMessages().iterator();
+ MimeMessage message = emails.next().getMimeMessage();
+ assertThat(Arrays.stream(message.getFrom()).map(Address::toString)).containsOnly(format("%s <%s>", fromName, from));
+ assertThat(message.getHeader("To", null)).isEqualTo("<test@example.org>");
+ assertThat(message.getSubject()).isEqualTo(prefix + " Test Message from SonarQube");
assertThat((String) message.getContent()).contains("This is a test message from SonarQube");
assertThat(emails.hasNext()).isFalse();
}
}
Thread.sleep(1_000);
}
- fail(String.format("Received %d emails, expected %d", SMTP_SERVER.getMessages().size(), expectedNumberOfEmails));
+ fail(format("Received %d emails, expected %d", SMTP_SERVER.getMessages().size(), expectedNumberOfEmails));
}
- private void updateEmailSettings(@Nullable String host, @Nullable String port, @Nullable String from, @Nullable String prefix, @Nullable String secure,
+ private void updateEmailSettings(@Nullable String host, @Nullable String port, @Nullable String from, @Nullable String fromName, @Nullable String prefix, @Nullable String secure,
@Nullable String username, @Nullable String password) {
tester.settings().setGlobalSettings(
"email.smtp_host.secured", host,
"email.smtp_username.secured", username,
"email.smtp_password.secured", password,
"email.from", from,
+ "email.fromName", fromName,
"email.prefix", prefix);
}