mirror of
https://github.com/google/nomulus.git
synced 2025-05-28 16:30:12 +02:00
Continue Spec11 emails even if one fails
One failure should not stop the rest from sending. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=241760092
This commit is contained in:
parent
bf70f83359
commit
3ad0d091f5
3 changed files with 148 additions and 132 deletions
|
@ -14,8 +14,9 @@
|
|||
|
||||
package google.registry.reporting.spec11;
|
||||
|
||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.reporting.spec11.Spec11RegistrarThreatMatchesParserTest.getMatchA;
|
||||
import static google.registry.reporting.spec11.Spec11RegistrarThreatMatchesParserTest.getMatchB;
|
||||
import static google.registry.reporting.spec11.Spec11RegistrarThreatMatchesParserTest.sampleThreatMatches;
|
||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
|
@ -25,11 +26,11 @@ import static org.mockito.Mockito.verify;
|
|||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.net.MediaType;
|
||||
import google.registry.reporting.spec11.soy.Spec11EmailSoyInfo;
|
||||
import google.registry.util.EmailMessage;
|
||||
import google.registry.util.SendEmailService;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import javax.mail.MessagingException;
|
||||
|
@ -46,6 +47,64 @@ import org.mockito.ArgumentCaptor;
|
|||
public class Spec11EmailUtilsTest {
|
||||
|
||||
private static final ImmutableList<String> FAKE_RESOURCES = ImmutableList.of("foo");
|
||||
private static final String DAILY_EMAIL_FORMAT =
|
||||
"Dear registrar partner,"
|
||||
+ ""
|
||||
+ "<p>Super Cool Registry conducts a daily analysis of all domains registered in its "
|
||||
+ "TLDs to identify potential security concerns. On 2018-07-15, the following domains "
|
||||
+ "that your registrar manages were flagged for potential security concerns:</p>"
|
||||
+ ""
|
||||
+ "<table>"
|
||||
+ "<tr><th>Domain Name</th><th>Threat Type</th></tr>"
|
||||
+ "%s"
|
||||
+ "</table>"
|
||||
+ "<p><b>Please communicate these findings to the registrant and work with the "
|
||||
+ "registrant to mitigate any security issues and have the domains delisted.</b></p>"
|
||||
+ ""
|
||||
+ "Some helpful resources for getting off a blocked list include:"
|
||||
+ "<ul><li>foo</li></ul><p>"
|
||||
+ ""
|
||||
+ "You will continue to receive daily notices when new domains managed by your registrar "
|
||||
+ "are flagged for abuse, as well as a monthly summary of all of your domains under "
|
||||
+ "management that remain flagged for abuse. Once the registrant has resolved the "
|
||||
+ "security issues and followed the steps to have his or her domain reviewed and "
|
||||
+ "delisted it will automatically be removed from our reporting.</p>"
|
||||
+ ""
|
||||
+ "<p>If you would like to change the email to which these notices are sent please "
|
||||
+ "update your abuse contact using your registrar portal account.</p>"
|
||||
+ ""
|
||||
+ "<p>If you have any questions regarding this notice, please contact "
|
||||
+ "my-reply-to@test.com.</p>";
|
||||
private static final String MONTHLY_EMAIL_FORMAT =
|
||||
"Dear registrar partner,"
|
||||
+ ""
|
||||
+ "<p>Super Cool Registry previously notified you when the following "
|
||||
+ "domains managed by your registrar were flagged for potential security concerns.</p>"
|
||||
+ "<p>The following domains that you manage continue to be flagged by our analysis "
|
||||
+ "for potential security concerns. This may be because the registrants have not "
|
||||
+ "completed the requisite steps to mitigate the potential security abuse and/or have "
|
||||
+ "it reviewed and delisted.</p>"
|
||||
+ ""
|
||||
+ "<table>"
|
||||
+ "<tr><th>Domain Name</th><th>Threat Type</th></tr>"
|
||||
+ "%s"
|
||||
+ "</table>"
|
||||
+ ""
|
||||
+ "<p>Please work with the registrant to mitigate any security issues "
|
||||
+ "and have the domains delisted.</p>"
|
||||
+ ""
|
||||
+ "Some helpful resources for getting off a blocked list include:"
|
||||
+ "<ul><li>foo</li></ul><p>"
|
||||
+ ""
|
||||
+ "You will continue to receive a monthly summary of all domains managed by your "
|
||||
+ "registrar that remain on our lists of potential security threats. You will "
|
||||
+ "additionally receive a daily notice when any new domains that are added to these "
|
||||
+ "lists. Once the registrant has resolved the security issues and followed the steps to "
|
||||
+ "have his or her domain reviewed and delisted it will automatically be removed from "
|
||||
+ "our monthly reporting.</p>"
|
||||
+ ""
|
||||
+ "<p>If you have any questions regarding this notice, please contact "
|
||||
+ "my-reply-to@test.com.</p>";
|
||||
|
||||
private SendEmailService emailService;
|
||||
private Spec11EmailUtils emailUtils;
|
||||
|
@ -79,30 +138,13 @@ public class Spec11EmailUtilsTest {
|
|||
// We inspect individual parameters because Message doesn't implement equals().
|
||||
verify(emailService, times(3)).sendEmail(contentCaptor.capture());
|
||||
List<EmailMessage> capturedContents = contentCaptor.getAllValues();
|
||||
String emailFormat =
|
||||
"Dear registrar partner,<p>Super Cool Registry previously notified you when the following "
|
||||
+ "domains managed by your registrar were flagged for potential security concerns."
|
||||
+ "</p><p>The following domains that you manage continue to be flagged by our analysis "
|
||||
+ "for potential security concerns. This may be because the registrants have not "
|
||||
+ "completed the requisite steps to mitigate the potential security abuse and/or have "
|
||||
+ "it reviewed and delisted.</p><table><tr><th>Domain Name</th><th>Threat Type</th>"
|
||||
+ "</tr>%s</table><p>Please work with the registrant to mitigate any security issues "
|
||||
+ "and have the domains delisted.</p>Some helpful resources for getting off a blocked "
|
||||
+ "list include:<ul><li>foo</li></ul><p>You will continue to receive a monthly summary "
|
||||
+ "of all domains managed by your registrar that remain on our lists of potential "
|
||||
+ "security threats. You will additionally receive a daily notice when any new domains "
|
||||
+ "that are added to these lists. Once the registrant has resolved the security issues "
|
||||
+ "and followed the steps to have his or her domain reviewed and delisted it will "
|
||||
+ "automatically be removed from our monthly reporting.</p><p>If you have any q"
|
||||
+ "uestions regarding this notice, please contact my-reply-to@test.com.</p>";
|
||||
|
||||
validateMessage(
|
||||
capturedContents.get(0),
|
||||
"my-sender@test.com",
|
||||
"a@fake.com",
|
||||
Optional.of("my-reply-to@test.com"),
|
||||
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
||||
String.format(emailFormat, "<tr><td>a.com</td><td>MALWARE</td></tr>"),
|
||||
String.format(MONTHLY_EMAIL_FORMAT, "<tr><td>a.com</td><td>MALWARE</td></tr>"),
|
||||
Optional.of(MediaType.HTML_UTF_8));
|
||||
validateMessage(
|
||||
capturedContents.get(1),
|
||||
|
@ -111,7 +153,7 @@ public class Spec11EmailUtilsTest {
|
|||
Optional.of("my-reply-to@test.com"),
|
||||
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
||||
String.format(
|
||||
emailFormat,
|
||||
MONTHLY_EMAIL_FORMAT,
|
||||
"<tr><td>b.com</td><td>MALWARE</td></tr><tr><td>c.com</td><td>MALWARE</td></tr>"),
|
||||
Optional.of(MediaType.HTML_UTF_8));
|
||||
validateMessage(
|
||||
|
@ -134,31 +176,13 @@ public class Spec11EmailUtilsTest {
|
|||
// We inspect individual parameters because Message doesn't implement equals().
|
||||
verify(emailService, times(3)).sendEmail(contentCaptor.capture());
|
||||
List<EmailMessage> capturedMessages = contentCaptor.getAllValues();
|
||||
String emailFormat =
|
||||
"Dear registrar partner,<p>"
|
||||
+ "Super Cool Registry conducts a daily analysis of all domains registered in its TLDs "
|
||||
+ "to identify potential security concerns. On 2018-07-15, the following domains that "
|
||||
+ "your registrar manages were flagged for potential security concerns:</p><table><tr>"
|
||||
+ "<th>Domain Name</th><th>Threat Type</th></tr>%s</table><p><b>Please communicate "
|
||||
+ "these findings to the registrant and work with the registrant to mitigate any "
|
||||
+ "security issues and have the domains delisted.</b></p>Some helpful resources for "
|
||||
+ "getting off a blocked list include:<ul><li>foo</li></ul><p>You will continue to "
|
||||
+ "receive daily notices when new domains managed by your registrar are flagged for "
|
||||
+ "abuse, as well as a monthly summary of all of your domains under management that "
|
||||
+ "remain flagged for abuse. Once the registrant has resolved the security issues and "
|
||||
+ "followed the steps to have his or her domain reviewed and delisted it will "
|
||||
+ "automatically be removed from our reporting.</p><p>If you would like to change "
|
||||
+ "the email to which these notices are sent please update your abuse contact using "
|
||||
+ "your registrar portal account.</p><p>If you have any questions regarding this "
|
||||
+ "notice, please contact my-reply-to@test.com.</p>";
|
||||
|
||||
validateMessage(
|
||||
capturedMessages.get(0),
|
||||
"my-sender@test.com",
|
||||
"a@fake.com",
|
||||
Optional.of("my-reply-to@test.com"),
|
||||
"Super Cool Registry Daily Threat Detector [2018-07-15]",
|
||||
String.format(emailFormat, "<tr><td>a.com</td><td>MALWARE</td></tr>"),
|
||||
String.format(DAILY_EMAIL_FORMAT, "<tr><td>a.com</td><td>MALWARE</td></tr>"),
|
||||
Optional.of(MediaType.HTML_UTF_8));
|
||||
validateMessage(
|
||||
capturedMessages.get(1),
|
||||
|
@ -167,7 +191,7 @@ public class Spec11EmailUtilsTest {
|
|||
Optional.of("my-reply-to@test.com"),
|
||||
"Super Cool Registry Daily Threat Detector [2018-07-15]",
|
||||
String.format(
|
||||
emailFormat,
|
||||
DAILY_EMAIL_FORMAT,
|
||||
"<tr><td>b.com</td><td>MALWARE</td></tr><tr><td>c.com</td><td>MALWARE</td></tr>"),
|
||||
Optional.of(MediaType.HTML_UTF_8));
|
||||
validateMessage(
|
||||
|
@ -181,8 +205,13 @@ public class Spec11EmailUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_emailsAlert() throws MessagingException {
|
||||
public void testOneFailure_sendsAlert() throws Exception {
|
||||
// If there is one failure, we should still send the other message and then an alert email
|
||||
LinkedHashSet<RegistrarThreatMatches> matches = new LinkedHashSet<>();
|
||||
matches.add(getMatchA());
|
||||
matches.add(getMatchB());
|
||||
doThrow(new RuntimeException(new MessagingException("expected")))
|
||||
.doNothing()
|
||||
.doNothing()
|
||||
.when(emailService)
|
||||
.sendEmail(contentCaptor.capture());
|
||||
|
@ -191,21 +220,42 @@ public class Spec11EmailUtilsTest {
|
|||
RuntimeException.class,
|
||||
() ->
|
||||
emailUtils.emailSpec11Reports(
|
||||
date, Spec11EmailSoyInfo.MONTHLY_SPEC_11_EMAIL, "bar", sampleThreatMatches()));
|
||||
assertThat(thrown).hasMessageThat().isEqualTo("Emailing spec11 report failed");
|
||||
date,
|
||||
Spec11EmailSoyInfo.MONTHLY_SPEC_11_EMAIL,
|
||||
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
||||
matches));
|
||||
assertThat(thrown)
|
||||
.hasCauseThat()
|
||||
.hasMessageThat()
|
||||
.isEqualTo("javax.mail.MessagingException: expected");
|
||||
.isEqualTo("Emailing Spec11 reports failed, first exception:");
|
||||
assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("expected");
|
||||
// Verify we sent an e-mail alert
|
||||
verify(emailService, times(2)).sendEmail(contentCaptor.capture());
|
||||
verify(emailService, times(3)).sendEmail(contentCaptor.capture());
|
||||
List<EmailMessage> capturedMessages = contentCaptor.getAllValues();
|
||||
validateMessage(
|
||||
contentCaptor.getValue(),
|
||||
capturedMessages.get(0),
|
||||
"my-sender@test.com",
|
||||
"a@fake.com",
|
||||
Optional.of("my-reply-to@test.com"),
|
||||
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
||||
String.format(MONTHLY_EMAIL_FORMAT, "<tr><td>a.com</td><td>MALWARE</td></tr>"),
|
||||
Optional.of(MediaType.HTML_UTF_8));
|
||||
validateMessage(
|
||||
capturedMessages.get(1),
|
||||
"my-sender@test.com",
|
||||
"b@fake.com",
|
||||
Optional.of("my-reply-to@test.com"),
|
||||
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
||||
String.format(
|
||||
MONTHLY_EMAIL_FORMAT,
|
||||
"<tr><td>b.com</td><td>MALWARE</td></tr><tr><td>c.com</td><td>MALWARE</td></tr>"),
|
||||
Optional.of(MediaType.HTML_UTF_8));
|
||||
validateMessage(
|
||||
capturedMessages.get(2),
|
||||
"my-sender@test.com",
|
||||
"my-receiver@test.com",
|
||||
Optional.empty(),
|
||||
"Spec11 Emailing Failure 2018-07-15",
|
||||
"Emailing spec11 reports failed due to expected",
|
||||
"Emailing Spec11 reports failed due to expected",
|
||||
Optional.empty());
|
||||
}
|
||||
|
||||
|
@ -223,36 +273,6 @@ public class Spec11EmailUtilsTest {
|
|||
Optional.empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWarning_emptyEmailAddressForRegistrars() throws Exception {
|
||||
ImmutableSet<RegistrarThreatMatches> matchesWithoutEmails =
|
||||
sampleThreatMatches().stream()
|
||||
.map(matches -> RegistrarThreatMatches.create("", matches.threatMatches()))
|
||||
.collect(toImmutableSet());
|
||||
emailUtils.emailSpec11Reports(
|
||||
date,
|
||||
Spec11EmailSoyInfo.DAILY_SPEC_11_EMAIL,
|
||||
"Super Cool Registry Daily Threat Detector [2018-07-15]",
|
||||
matchesWithoutEmails);
|
||||
verify(emailService).sendEmail(contentCaptor.capture());
|
||||
validateMessage(
|
||||
contentCaptor.getValue(),
|
||||
"my-sender@test.com",
|
||||
"my-receiver@test.com",
|
||||
Optional.empty(),
|
||||
"Spec11 Pipeline Warning 2018-07-15",
|
||||
"No errors occurred but the following matches had no associated email: \n"
|
||||
+ "[RegistrarThreatMatches{registrarEmailAddress=, threatMatches=[ThreatMatch"
|
||||
+ "{threatType=MALWARE, platformType=ANY_PLATFORM, metadata=NONE,"
|
||||
+ " fullyQualifiedDomainName=a.com}]}, RegistrarThreatMatches{registrarEmailAddress=,"
|
||||
+ " threatMatches=[ThreatMatch{threatType=MALWARE, platformType=ANY_PLATFORM,"
|
||||
+ " metadata=NONE, fullyQualifiedDomainName=b.com}, ThreatMatch{threatType=MALWARE,"
|
||||
+ " platformType=ANY_PLATFORM, metadata=NONE, fullyQualifiedDomainName=c.com}]}]\n\n"
|
||||
+ "This should not occur; please make sure we have email addresses for these"
|
||||
+ " registrar(s).",
|
||||
Optional.empty());
|
||||
}
|
||||
|
||||
private void validateMessage(
|
||||
EmailMessage message,
|
||||
String from,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue