aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'tests/src/test')
-rw-r--r--tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateNotificationTest.java12
-rw-r--r--tests/src/test/java/org/sonarqube/tests/settings/EmailsTest.java40
2 files changed, 42 insertions, 10 deletions
diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateNotificationTest.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateNotificationTest.java
index 48b27237c12..d946a40e5f2 100644
--- a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateNotificationTest.java
+++ b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateNotificationTest.java
@@ -86,7 +86,8 @@ public class QualityGateNotificationTest {
// 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);
@@ -108,9 +109,12 @@ public class QualityGateNotificationTest {
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();
}
diff --git a/tests/src/test/java/org/sonarqube/tests/settings/EmailsTest.java b/tests/src/test/java/org/sonarqube/tests/settings/EmailsTest.java
index 50b895fbae2..8ce6439178b 100644
--- a/tests/src/test/java/org/sonarqube/tests/settings/EmailsTest.java
+++ b/tests/src/test/java/org/sonarqube/tests/settings/EmailsTest.java
@@ -20,8 +20,10 @@
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;
@@ -37,7 +39,10 @@ import org.sonarqube.ws.client.setting.ValuesRequest;
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;
@@ -72,11 +77,11 @@ public class EmailsTest {
@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)
@@ -87,12 +92,13 @@ public class EmailsTest {
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");
@@ -100,8 +106,29 @@ public class EmailsTest {
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();
}
@@ -113,10 +140,10 @@ public class EmailsTest {
}
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,
@@ -125,6 +152,7 @@ public class EmailsTest {
"email.smtp_username.secured", username,
"email.smtp_password.secured", password,
"email.from", from,
+ "email.fromName", fromName,
"email.prefix", prefix);
}