Skip to content

Commit 3235b37

Browse files
authored
Changing EmailServiceTests to use an impossible SMTP server to avoid accidentally sending email (#90469) (#90476)
If the build machine happened to be running an SMTP server with no authentication required on port 25, then `EmailServiceTests::testChangeDomainAllowListSetting` could fail because it asserts that sending an email does not work. Relates #90426
1 parent 2e24da1 commit 3235b37

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailServiceTests.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ public void testSendEmailWithDomainNotInAllowList() throws Exception {
269269
public void testChangeDomainAllowListSetting() throws UnsupportedEncodingException, MessagingException {
270270
Settings settings = Settings.builder()
271271
.put("xpack.notification.email.account.account1.foo", "bar")
272-
.put("xpack.notification.email.account.account1.smtp.host", "localhost")
272+
// Setting a random SMTP server name and an invalid port so that sending emails is guaranteed to fail:
273+
.put("xpack.notification.email.account.account1.smtp.host", randomAlphaOfLength(10))
274+
.put("xpack.notification.email.account.account1.smtp.port", -100)
273275
.putList("xpack.notification.email.account.domain_allowlist", "bar.com")
274276
.build();
275277
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()));
@@ -293,12 +295,12 @@ public void testChangeDomainAllowListSetting() throws UnsupportedEncodingExcepti
293295
Profile profile = randomFrom(Profile.values());
294296

295297
// This send will fail because one of the recipients ("[email protected]") is in a domain that is not in the allowed list
296-
IllegalArgumentException e = expectThrows(
298+
IllegalArgumentException e1 = expectThrows(
297299
IllegalArgumentException.class,
298300
() -> emailService.send(email, auth, profile, "account1")
299301
);
300302
assertThat(
301-
e.getMessage(),
303+
e1.getMessage(),
302304
containsString(
303305
"failed to send email with subject [subject] and recipient domains "
304306
+ "[bar.com, invalid.com], one or more recipients is not specified in the domain allow list setting "
@@ -312,7 +314,11 @@ public void testChangeDomainAllowListSetting() throws UnsupportedEncodingExcepti
312314
.build();
313315
clusterSettings.applySettings(newSettings);
314316
// Still expect an exception because we're not actually sending the email, but it's no longer because the domain isn't allowed:
315-
expectThrows(MessagingException.class, () -> emailService.send(email, auth, profile, "account1"));
317+
IllegalArgumentException e2 = expectThrows(
318+
IllegalArgumentException.class,
319+
() -> emailService.send(email, auth, profile, "account1")
320+
);
321+
assertThat(e2.getMessage(), containsString("port out of range"));
316322
}
317323

318324
private static Email.AddressList createAddressList(String... emails) throws UnsupportedEncodingException {

0 commit comments

Comments
 (0)