Skip to content

Commit 6d1728d

Browse files
committed
Email service tests
1 parent e601fcd commit 6d1728d

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ static boolean recipientAddressInAllowList(Email email, Set<String> allowedRecip
365365
}
366366

367367
final Set<String> recipients = getRecipients(email, false);
368-
final Predicate<String> matchesAnyAllowedRecipient = recipient -> recipients.stream()
369-
.anyMatch(allowedDomain -> Regex.simpleMatch(allowedDomain, recipient, true));
368+
final Predicate<String> matchesAnyAllowedRecipient = recipient -> allowedRecipientPatterns.stream()
369+
.anyMatch(pattern -> Regex.simpleMatch(pattern, recipient, true));
370370
return recipients.stream().allMatch(matchesAnyAllowedRecipient);
371371
}
372372

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,103 @@ public void testChangeDomainAllowListSetting() throws UnsupportedEncodingExcepti
321321
assertThat(e2.getMessage(), containsString("port out of range"));
322322
}
323323

324+
public void testRecipientAddressInAllowList_EmptyAllowedPatterns() throws UnsupportedEncodingException {
325+
Email email = createTestEmail("[email protected]", "[email protected]");
326+
Set<String> allowedPatterns = Set.of();
327+
assertThat(EmailService.recipientAddressInAllowList(email, allowedPatterns), is(false));
328+
}
329+
330+
public void testRecipientAddressInAllowList_WildcardPattern() throws UnsupportedEncodingException {
331+
Email email = createTestEmail("[email protected]", "[email protected]");
332+
Set<String> allowedPatterns = Set.of("*");
333+
assertThat(EmailService.recipientAddressInAllowList(email, allowedPatterns), is(true));
334+
}
335+
336+
public void testRecipientAddressInAllowList_SpecificPattern() throws UnsupportedEncodingException {
337+
Email email = createTestEmail("[email protected]", "[email protected]");
338+
Set<String> allowedPatterns = Set.of("[email protected]");
339+
assertThat(EmailService.recipientAddressInAllowList(email, allowedPatterns), is(false));
340+
}
341+
342+
public void testRecipientAddressInAllowList_MultiplePatterns() throws UnsupportedEncodingException {
343+
Email email = createTestEmail("[email protected]", "[email protected]");
344+
Set<String> allowedPatterns = Set.of("[email protected]", "[email protected]");
345+
assertThat(EmailService.recipientAddressInAllowList(email, allowedPatterns), is(true));
346+
}
347+
348+
public void testRecipientAddressInAllowList_MixedCasePatterns() throws UnsupportedEncodingException {
349+
Email email = createTestEmail("[email protected]", "[email protected]");
350+
Set<String> allowedPatterns = Set.of("[email protected]", "[email protected]");
351+
assertThat(EmailService.recipientAddressInAllowList(email, allowedPatterns), is(true));
352+
}
353+
354+
public void testRecipientAddressInAllowList_PartialWildcardPrefixPattern() throws UnsupportedEncodingException {
355+
Email email = createTestEmail("[email protected]", "[email protected]");
356+
Set<String> allowedPatterns = Set.of("foo@*", "baz@*");
357+
assertThat(EmailService.recipientAddressInAllowList(email, allowedPatterns), is(true));
358+
}
359+
360+
public void testRecipientAddressInAllowList_PartialWildcardSuffixPattern() throws UnsupportedEncodingException {
361+
Email email = createTestEmail("[email protected]", "[email protected]");
362+
Set<String> allowedPatterns = Set.of("*@bar.com", "*@potato.com");
363+
assertThat(EmailService.recipientAddressInAllowList(email, allowedPatterns), is(true));
364+
}
365+
366+
public void testRecipientAddressInAllowList_DisallowedCCAddressesFails() throws UnsupportedEncodingException {
367+
Email email = new Email(
368+
"id",
369+
new Email.Address("[email protected]", "Sender"),
370+
createAddressList("[email protected]"),
371+
randomFrom(Email.Priority.values()),
372+
ZonedDateTime.now(),
373+
createAddressList("[email protected]"),
374+
createAddressList("[email protected]", "[email protected]"),
375+
null,
376+
"subject",
377+
"body",
378+
"htmlbody",
379+
Collections.emptyMap()
380+
);
381+
Set<String> allowedPatterns = Set.of("[email protected]", "[email protected]");
382+
assertThat(EmailService.recipientAddressInAllowList(email, allowedPatterns), is(false));
383+
}
384+
385+
public void testRecipientAddressInAllowList_DisallowedBCCAddressesFails() throws UnsupportedEncodingException {
386+
Email email = new Email(
387+
"id",
388+
new Email.Address("[email protected]", "Sender"),
389+
createAddressList("[email protected]"),
390+
randomFrom(Email.Priority.values()),
391+
ZonedDateTime.now(),
392+
createAddressList("[email protected]"),
393+
null,
394+
createAddressList("[email protected]", "[email protected]"),
395+
"subject",
396+
"body",
397+
"htmlbody",
398+
Collections.emptyMap()
399+
);
400+
Set<String> allowedPatterns = Set.of("[email protected]", "[email protected]");
401+
assertThat(EmailService.recipientAddressInAllowList(email, allowedPatterns), is(false));
402+
}
403+
404+
private Email createTestEmail(String... recipients) throws UnsupportedEncodingException {
405+
return new Email(
406+
"id",
407+
new Email.Address("[email protected]", "Sender"),
408+
createAddressList(recipients),
409+
randomFrom(Email.Priority.values()),
410+
ZonedDateTime.now(),
411+
createAddressList(recipients),
412+
null,
413+
null,
414+
"subject",
415+
"body",
416+
"htmlbody",
417+
Collections.emptyMap()
418+
);
419+
}
420+
324421
private static Email.AddressList createAddressList(String... emails) throws UnsupportedEncodingException {
325422
List<Email.Address> addresses = new ArrayList<>();
326423
for (String email : emails) {

0 commit comments

Comments
 (0)