Refactor threat emails for daily+monthly usage

Eventually the Publish action will control daily/monthly sending and provide
the correct threats to email. The goal of this PR is to entirely separate
the "sending email" functionality from the "parsing threat matches"
functionality.

The PublishAction will figure out if the monthly emails should be sent out,
then will ask the Spec11ThreatMatchesParser for the monthly threats (if
appropriate) and the new threat matches for today. It will then pass those
matches and the appropriate email subject+body to the email utils class,
whose only job is to format and send the emails.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=224869643
This commit is contained in:
jianglai 2018-12-10 13:15:09 -08:00
parent 4dad0a8a73
commit ea154a8378
4 changed files with 77 additions and 42 deletions

View file

@ -15,6 +15,7 @@
package google.registry.reporting.spec11;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.reporting.spec11.Spec11RegistrarThreatMatchesParserTest.sampleThreatMatches;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
@ -42,18 +43,22 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class PublishSpec11ReportActionTest {
private final String spec11BodyTemplate = "{LIST_OF_THREATS}\n{REPLY_TO_EMAIL}";
private final LocalDate date = new LocalDate(2018, 6, 5);
private Dataflow dataflow;
private Projects projects;
private Jobs jobs;
private Get get;
private Spec11EmailUtils emailUtils;
private Spec11RegistrarThreatMatchesParser parser;
private Job expectedJob;
private FakeResponse response;
private PublishSpec11ReportAction publishAction;
@Before
public void setUp() throws IOException {
public void setUp() throws Exception {
dataflow = mock(Dataflow.class);
projects = mock(Projects.class);
jobs = mock(Jobs.class);
@ -65,20 +70,40 @@ public class PublishSpec11ReportActionTest {
when(get.execute()).thenReturn(expectedJob);
emailUtils = mock(Spec11EmailUtils.class);
response = new FakeResponse();
parser = mock(Spec11RegistrarThreatMatchesParser.class);
when(parser.getRegistrarThreatMatches()).thenReturn(sampleThreatMatches());
publishAction =
new PublishSpec11ReportAction(
"test-project", "12345", emailUtils, dataflow, response, new LocalDate(2018, 6, 5));
"test-project",
spec11BodyTemplate,
"12345",
emailUtils,
mock(Spec11RegistrarThreatMatchesParser.class),
dataflow,
response,
date);
}
@Test
public void testJobDone_emailsResultsOnSecondOfMonth() {
public void testJobDone_emailsResultsOnSecondOfMonth() throws Exception {
expectedJob.setCurrentState("JOB_STATE_DONE");
publishAction =
new PublishSpec11ReportAction(
"test-project", "12345", emailUtils, dataflow, response, new LocalDate(2018, 6, 2));
"test-project",
spec11BodyTemplate,
"12345",
emailUtils,
parser,
dataflow,
response,
date.withDayOfMonth(2));
publishAction.run();
assertThat(response.getStatus()).isEqualTo(SC_OK);
verify(emailUtils).emailSpec11Reports();
verify(emailUtils)
.emailSpec11Reports(
spec11BodyTemplate,
"Google Registry Monthly Threat Detector [2018-06-02]",
sampleThreatMatches());
}
@Test