Add a parameter to prevent spec11 from sending emails (#1407)

This commit is contained in:
sarahcaseybot 2021-11-05 13:02:59 -04:00 committed by GitHub
parent 421ed0a8d5
commit f729e8c117
4 changed files with 50 additions and 1 deletions

View file

@ -14,6 +14,7 @@
package google.registry.reporting;
import static google.registry.request.RequestParameters.extractOptionalBooleanParameter;
import static google.registry.request.RequestParameters.extractOptionalParameter;
import static google.registry.request.RequestParameters.extractRequiredParameter;
@ -55,6 +56,12 @@ public class ReportingModule {
/** The request parameter specifying the jobId for a running Dataflow pipeline. */
public static final String PARAM_JOB_ID = "jobId";
/**
* The request parameter name used by actions to indicate if an email should be sent. This
* parameter defaults to true if not specified.
*/
public static final String SEND_EMAIL = "email";
/** Provides the Cloud Dataflow jobId for a pipeline. */
@Provides
@Parameter(PARAM_JOB_ID)
@ -62,6 +69,13 @@ public class ReportingModule {
return extractRequiredParameter(req, PARAM_JOB_ID);
}
/** Provides the boolean value indicating if emails should be sent. */
@Provides
@Parameter(SEND_EMAIL)
static boolean provideSendEmail(HttpServletRequest req) {
return extractOptionalBooleanParameter(req, SEND_EMAIL).orElse(true);
}
/** Extracts an optional YearMonth in yyyy-MM format from the request. */
@Provides
@Parameter(PARAM_YEAR_MONTH)

View file

@ -72,6 +72,7 @@ public class GenerateSpec11ReportAction implements Runnable {
private final Response response;
private final Dataflow dataflow;
private final PrimaryDatabase database;
private final boolean sendEmail;
@Inject
GenerateSpec11ReportAction(
@ -82,6 +83,7 @@ public class GenerateSpec11ReportAction implements Runnable {
@Key("safeBrowsingAPIKey") String apiKey,
@Parameter(ReportingModule.PARAM_DATE) LocalDate date,
@Parameter(RequestParameters.PARAM_DATABASE) PrimaryDatabase database,
@Parameter(ReportingModule.SEND_EMAIL) boolean sendEmail,
Clock clock,
Response response,
Dataflow dataflow) {
@ -98,6 +100,7 @@ public class GenerateSpec11ReportAction implements Runnable {
this.clock = clock;
this.response = response;
this.dataflow = dataflow;
this.sendEmail = sendEmail;
}
@Override
@ -136,7 +139,9 @@ public class GenerateSpec11ReportAction implements Runnable {
Map<String, String> beamTaskParameters =
ImmutableMap.of(
ReportingModule.PARAM_JOB_ID, jobId, ReportingModule.PARAM_DATE, date.toString());
enqueueBeamReportingTask(PublishSpec11ReportAction.PATH, beamTaskParameters);
if (sendEmail) {
enqueueBeamReportingTask(PublishSpec11ReportAction.PATH, beamTaskParameters);
}
response.setStatus(SC_OK);
response.setPayload(String.format("Launched Spec11 pipeline: %s", jobId));
} catch (IOException e) {

View file

@ -114,4 +114,10 @@ class ReportingModuleTest {
when(req.getParameter("date")).thenReturn("2017-07-02");
assertThat(ReportingModule.provideDate(req, clock)).isEqualTo(new LocalDate(2017, 7, 2));
}
@Test
void testEmptyEmail_returnsTrue() {
when(req.getParameter("email")).thenReturn(null);
assertThat(ReportingModule.provideSendEmail(req)).isTrue();
}
}

View file

@ -53,6 +53,7 @@ class GenerateSpec11ReportActionTest extends BeamActionTestBase {
"api_key/a",
clock.nowUtc().toLocalDate(),
PrimaryDatabase.DATASTORE,
true,
clock,
response,
dataflow);
@ -75,6 +76,7 @@ class GenerateSpec11ReportActionTest extends BeamActionTestBase {
"api_key/a",
clock.nowUtc().toLocalDate(),
PrimaryDatabase.DATASTORE,
true,
clock,
response,
dataflow);
@ -90,4 +92,26 @@ class GenerateSpec11ReportActionTest extends BeamActionTestBase {
.param("date", "2018-06-11");
assertTasksEnqueued("beam-reporting", matcher);
}
@Test
void testSuccess_noEmail() throws IOException {
action =
new GenerateSpec11ReportAction(
"test-project",
"us-east1-c",
"gs://staging-project/staging-bucket/",
"gs://reporting-project/reporting-bucket/",
"api_key/a",
clock.nowUtc().toLocalDate(),
PrimaryDatabase.DATASTORE,
false,
clock,
response,
dataflow);
action.run();
assertThat(response.getStatus()).isEqualTo(SC_OK);
assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
assertThat(response.getPayload()).isEqualTo("Launched Spec11 pipeline: jobid");
assertNoTasksEnqueued("beam-reporting");
}
}