mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 16:07:15 +02:00
Move LocalDate injection to the Actions themselves
We want to make it clear what query (or POST) inputs the user needs to / can give for each Action. That means moving all the @Injects of these parameters to the Actions themselves instead of injecting them in "hidden" indirect dependencies. This has the extra benefit of allowing these indirect dependencies to work for JSON Actions as well, since the "regular" way we @Inject parameters can corrupt the POST JSON data. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=232540758
This commit is contained in:
parent
89802329b3
commit
927e8bbd73
7 changed files with 30 additions and 20 deletions
|
@ -83,7 +83,8 @@ public class ReportingModule {
|
||||||
*/
|
*/
|
||||||
@Provides
|
@Provides
|
||||||
static YearMonth provideYearMonth(
|
static YearMonth provideYearMonth(
|
||||||
@Parameter(PARAM_YEAR_MONTH) Optional<YearMonth> yearMonthOptional, LocalDate date) {
|
@Parameter(PARAM_YEAR_MONTH) Optional<YearMonth> yearMonthOptional,
|
||||||
|
@Parameter(PARAM_DATE) LocalDate date) {
|
||||||
return yearMonthOptional.orElseGet(() -> new YearMonth(date.minusMonths(1)));
|
return yearMonthOptional.orElseGet(() -> new YearMonth(date.minusMonths(1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,9 +109,10 @@ public class ReportingModule {
|
||||||
* current date.
|
* current date.
|
||||||
*/
|
*/
|
||||||
@Provides
|
@Provides
|
||||||
static LocalDate provideDate(
|
@Parameter(PARAM_DATE)
|
||||||
@Parameter(PARAM_DATE) Optional<LocalDate> dateOptional, Clock clock) {
|
static LocalDate provideDate(HttpServletRequest req, Clock clock) {
|
||||||
return dateOptional.orElseGet(() -> new LocalDate(clock.nowUtc(), DateTimeZone.UTC));
|
return provideDateOptional(req)
|
||||||
|
.orElseGet(() -> new LocalDate(clock.nowUtc(), DateTimeZone.UTC));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Constructs a {@link Dataflow} API client with default settings. */
|
/** Constructs a {@link Dataflow} API client with default settings. */
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
package google.registry.reporting.spec11;
|
package google.registry.reporting.spec11;
|
||||||
|
|
||||||
|
import static google.registry.reporting.ReportingModule.PARAM_DATE;
|
||||||
import static google.registry.reporting.ReportingUtils.enqueueBeamReportingTask;
|
import static google.registry.reporting.ReportingUtils.enqueueBeamReportingTask;
|
||||||
import static google.registry.request.Action.Method.POST;
|
import static google.registry.request.Action.Method.POST;
|
||||||
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
||||||
|
@ -30,6 +31,7 @@ import google.registry.config.RegistryConfig.Config;
|
||||||
import google.registry.keyring.api.KeyModule.Key;
|
import google.registry.keyring.api.KeyModule.Key;
|
||||||
import google.registry.reporting.ReportingModule;
|
import google.registry.reporting.ReportingModule;
|
||||||
import google.registry.request.Action;
|
import google.registry.request.Action;
|
||||||
|
import google.registry.request.Parameter;
|
||||||
import google.registry.request.Response;
|
import google.registry.request.Response;
|
||||||
import google.registry.request.auth.Auth;
|
import google.registry.request.auth.Auth;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -70,7 +72,7 @@ public class GenerateSpec11ReportAction implements Runnable {
|
||||||
@Config("spec11TemplateUrl") String spec11TemplateUrl,
|
@Config("spec11TemplateUrl") String spec11TemplateUrl,
|
||||||
@Config("defaultJobZone") String jobZone,
|
@Config("defaultJobZone") String jobZone,
|
||||||
@Key("safeBrowsingAPIKey") String apiKey,
|
@Key("safeBrowsingAPIKey") String apiKey,
|
||||||
LocalDate date,
|
@Parameter(PARAM_DATE) LocalDate date,
|
||||||
Response response,
|
Response response,
|
||||||
Dataflow dataflow) {
|
Dataflow dataflow) {
|
||||||
this.projectId = projectId;
|
this.projectId = projectId;
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
package google.registry.reporting.spec11;
|
package google.registry.reporting.spec11;
|
||||||
|
|
||||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||||
|
import static google.registry.reporting.ReportingModule.PARAM_DATE;
|
||||||
import static google.registry.request.Action.Method.POST;
|
import static google.registry.request.Action.Method.POST;
|
||||||
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
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_NOT_MODIFIED;
|
||||||
|
@ -83,7 +84,7 @@ public class PublishSpec11ReportAction implements Runnable {
|
||||||
Spec11RegistrarThreatMatchesParser spec11RegistrarThreatMatchesParser,
|
Spec11RegistrarThreatMatchesParser spec11RegistrarThreatMatchesParser,
|
||||||
Dataflow dataflow,
|
Dataflow dataflow,
|
||||||
Response response,
|
Response response,
|
||||||
LocalDate date) {
|
@Parameter(PARAM_DATE) LocalDate date) {
|
||||||
this.projectId = projectId;
|
this.projectId = projectId;
|
||||||
this.registryName = registryName;
|
this.registryName = registryName;
|
||||||
this.jobId = jobId;
|
this.jobId = jobId;
|
||||||
|
@ -148,7 +149,7 @@ public class PublishSpec11ReportAction implements Runnable {
|
||||||
spec11RegistrarThreatMatchesParser.getRegistrarThreatMatches(date);
|
spec11RegistrarThreatMatchesParser.getRegistrarThreatMatches(date);
|
||||||
String subject = String.format("%s Monthly Threat Detector [%s]", registryName, date);
|
String subject = String.format("%s Monthly Threat Detector [%s]", registryName, date);
|
||||||
emailUtils.emailSpec11Reports(
|
emailUtils.emailSpec11Reports(
|
||||||
Spec11EmailSoyInfo.MONTHLY_SPEC_11_EMAIL, subject, monthlyMatchesSet);
|
date, Spec11EmailSoyInfo.MONTHLY_SPEC_11_EMAIL, subject, monthlyMatchesSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processDailyDiff(LocalDate previousDate) throws IOException, JSONException {
|
private void processDailyDiff(LocalDate previousDate) throws IOException, JSONException {
|
||||||
|
@ -158,6 +159,7 @@ public class PublishSpec11ReportAction implements Runnable {
|
||||||
spec11RegistrarThreatMatchesParser.getRegistrarThreatMatches(date);
|
spec11RegistrarThreatMatchesParser.getRegistrarThreatMatches(date);
|
||||||
String dailySubject = String.format("%s Daily Threat Detector [%s]", registryName, date);
|
String dailySubject = String.format("%s Daily Threat Detector [%s]", registryName, date);
|
||||||
emailUtils.emailSpec11Reports(
|
emailUtils.emailSpec11Reports(
|
||||||
|
date,
|
||||||
Spec11EmailSoyInfo.DAILY_SPEC_11_EMAIL,
|
Spec11EmailSoyInfo.DAILY_SPEC_11_EMAIL,
|
||||||
dailySubject,
|
dailySubject,
|
||||||
getNewMatches(previousMatches, currentMatches));
|
getNewMatches(previousMatches, currentMatches));
|
||||||
|
|
|
@ -52,7 +52,6 @@ public class Spec11EmailUtils {
|
||||||
.compileToTofu();
|
.compileToTofu();
|
||||||
|
|
||||||
private final SendEmailService emailService;
|
private final SendEmailService emailService;
|
||||||
private final LocalDate date;
|
|
||||||
private final String outgoingEmailAddress;
|
private final String outgoingEmailAddress;
|
||||||
private final String alertRecipientAddress;
|
private final String alertRecipientAddress;
|
||||||
private final String spec11ReplyToAddress;
|
private final String spec11ReplyToAddress;
|
||||||
|
@ -63,7 +62,6 @@ public class Spec11EmailUtils {
|
||||||
@Inject
|
@Inject
|
||||||
Spec11EmailUtils(
|
Spec11EmailUtils(
|
||||||
SendEmailService emailService,
|
SendEmailService emailService,
|
||||||
LocalDate date,
|
|
||||||
@Config("gSuiteOutgoingEmailAddress") String outgoingEmailAddress,
|
@Config("gSuiteOutgoingEmailAddress") String outgoingEmailAddress,
|
||||||
@Config("alertRecipientEmailAddress") String alertRecipientAddress,
|
@Config("alertRecipientEmailAddress") String alertRecipientAddress,
|
||||||
@Config("spec11ReplyToEmailAddress") String spec11ReplyToAddress,
|
@Config("spec11ReplyToEmailAddress") String spec11ReplyToAddress,
|
||||||
|
@ -71,7 +69,6 @@ public class Spec11EmailUtils {
|
||||||
@Config("registryName") String registryName,
|
@Config("registryName") String registryName,
|
||||||
Retrier retrier) {
|
Retrier retrier) {
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
this.date = date;
|
|
||||||
this.outgoingEmailAddress = outgoingEmailAddress;
|
this.outgoingEmailAddress = outgoingEmailAddress;
|
||||||
this.alertRecipientAddress = alertRecipientAddress;
|
this.alertRecipientAddress = alertRecipientAddress;
|
||||||
this.spec11ReplyToAddress = spec11ReplyToAddress;
|
this.spec11ReplyToAddress = spec11ReplyToAddress;
|
||||||
|
@ -85,6 +82,7 @@ public class Spec11EmailUtils {
|
||||||
* appropriate address.
|
* appropriate address.
|
||||||
*/
|
*/
|
||||||
void emailSpec11Reports(
|
void emailSpec11Reports(
|
||||||
|
LocalDate date,
|
||||||
SoyTemplateInfo soyTemplateInfo,
|
SoyTemplateInfo soyTemplateInfo,
|
||||||
String subject,
|
String subject,
|
||||||
Set<RegistrarThreatMatches> registrarThreatMatchesSet) {
|
Set<RegistrarThreatMatches> registrarThreatMatchesSet) {
|
||||||
|
@ -92,7 +90,7 @@ public class Spec11EmailUtils {
|
||||||
retrier.callWithRetry(
|
retrier.callWithRetry(
|
||||||
() -> {
|
() -> {
|
||||||
for (RegistrarThreatMatches registrarThreatMatches : registrarThreatMatchesSet) {
|
for (RegistrarThreatMatches registrarThreatMatches : registrarThreatMatchesSet) {
|
||||||
emailRegistrar(soyTemplateInfo, subject, registrarThreatMatches);
|
emailRegistrar(date, soyTemplateInfo, subject, registrarThreatMatches);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
IOException.class,
|
IOException.class,
|
||||||
|
@ -110,13 +108,14 @@ public class Spec11EmailUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void emailRegistrar(
|
private void emailRegistrar(
|
||||||
|
LocalDate date,
|
||||||
SoyTemplateInfo soyTemplateInfo,
|
SoyTemplateInfo soyTemplateInfo,
|
||||||
String subject,
|
String subject,
|
||||||
RegistrarThreatMatches registrarThreatMatches)
|
RegistrarThreatMatches registrarThreatMatches)
|
||||||
throws MessagingException {
|
throws MessagingException {
|
||||||
Message msg = emailService.createMessage();
|
Message msg = emailService.createMessage();
|
||||||
msg.setSubject(subject);
|
msg.setSubject(subject);
|
||||||
String content = getContent(soyTemplateInfo, registrarThreatMatches);
|
String content = getContent(date, soyTemplateInfo, registrarThreatMatches);
|
||||||
msg.setContent(content, "text/html");
|
msg.setContent(content, "text/html");
|
||||||
msg.setHeader("Content-Type", "text/html");
|
msg.setHeader("Content-Type", "text/html");
|
||||||
msg.setFrom(new InternetAddress(outgoingEmailAddress));
|
msg.setFrom(new InternetAddress(outgoingEmailAddress));
|
||||||
|
@ -127,7 +126,9 @@ public class Spec11EmailUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getContent(
|
private String getContent(
|
||||||
SoyTemplateInfo soyTemplateInfo, RegistrarThreatMatches registrarThreatMatches) {
|
LocalDate date,
|
||||||
|
SoyTemplateInfo soyTemplateInfo,
|
||||||
|
RegistrarThreatMatches registrarThreatMatches) {
|
||||||
Renderer renderer = SOY_SAUCE.newRenderer(soyTemplateInfo);
|
Renderer renderer = SOY_SAUCE.newRenderer(soyTemplateInfo);
|
||||||
// Soy templates require that data be in raw map/list form.
|
// Soy templates require that data be in raw map/list form.
|
||||||
List<Map<String, String>> threatMatchMap =
|
List<Map<String, String>> threatMatchMap =
|
||||||
|
|
|
@ -108,13 +108,13 @@ public class ReportingModuleTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyDate_returnsToday() {
|
public void testEmptyDate_returnsToday() {
|
||||||
assertThat(ReportingModule.provideDate(Optional.empty(), clock))
|
when(req.getParameter("date")).thenReturn(null);
|
||||||
.isEqualTo(new LocalDate(2017, 7, 1));
|
assertThat(ReportingModule.provideDate(req, clock)).isEqualTo(new LocalDate(2017, 7, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGivenDate_returnsThatDate() {
|
public void testGivenDate_returnsThatDate() {
|
||||||
assertThat(ReportingModule.provideDate(Optional.of(new LocalDate(2017, 7, 2)), clock))
|
when(req.getParameter("date")).thenReturn("2017-07-02");
|
||||||
.isEqualTo(new LocalDate(2017, 7, 2));
|
assertThat(ReportingModule.provideDate(req, clock)).isEqualTo(new LocalDate(2017, 7, 2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,6 +105,7 @@ public class PublishSpec11ReportActionTest {
|
||||||
assertThat(response.getStatus()).isEqualTo(SC_OK);
|
assertThat(response.getStatus()).isEqualTo(SC_OK);
|
||||||
verify(emailUtils)
|
verify(emailUtils)
|
||||||
.emailSpec11Reports(
|
.emailSpec11Reports(
|
||||||
|
secondOfMonth,
|
||||||
Spec11EmailSoyInfo.MONTHLY_SPEC_11_EMAIL,
|
Spec11EmailSoyInfo.MONTHLY_SPEC_11_EMAIL,
|
||||||
"Super Cool Registry Monthly Threat Detector [2018-06-02]",
|
"Super Cool Registry Monthly Threat Detector [2018-06-02]",
|
||||||
sampleThreatMatches());
|
sampleThreatMatches());
|
||||||
|
@ -154,6 +155,7 @@ public class PublishSpec11ReportActionTest {
|
||||||
assertThat(response.getStatus()).isEqualTo(SC_OK);
|
assertThat(response.getStatus()).isEqualTo(SC_OK);
|
||||||
verify(emailUtils)
|
verify(emailUtils)
|
||||||
.emailSpec11Reports(
|
.emailSpec11Reports(
|
||||||
|
date,
|
||||||
Spec11EmailSoyInfo.DAILY_SPEC_11_EMAIL,
|
Spec11EmailSoyInfo.DAILY_SPEC_11_EMAIL,
|
||||||
"Super Cool Registry Daily Threat Detector [2018-06-05]",
|
"Super Cool Registry Daily Threat Detector [2018-06-05]",
|
||||||
sampleThreatMatches());
|
sampleThreatMatches());
|
||||||
|
|
|
@ -61,6 +61,7 @@ public class Spec11EmailUtilsTest {
|
||||||
private Spec11EmailUtils emailUtils;
|
private Spec11EmailUtils emailUtils;
|
||||||
private Spec11RegistrarThreatMatchesParser parser;
|
private Spec11RegistrarThreatMatchesParser parser;
|
||||||
private ArgumentCaptor<Message> gotMessage;
|
private ArgumentCaptor<Message> gotMessage;
|
||||||
|
private final LocalDate date = new LocalDate(2018, 7, 15);
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
|
@ -69,7 +70,6 @@ public class Spec11EmailUtilsTest {
|
||||||
.thenAnswer((args) -> new MimeMessage(Session.getInstance(new Properties(), null)));
|
.thenAnswer((args) -> new MimeMessage(Session.getInstance(new Properties(), null)));
|
||||||
|
|
||||||
parser = mock(Spec11RegistrarThreatMatchesParser.class);
|
parser = mock(Spec11RegistrarThreatMatchesParser.class);
|
||||||
LocalDate date = new LocalDate(2018, 7, 15);
|
|
||||||
when(parser.getRegistrarThreatMatches(date)).thenReturn(sampleThreatMatches());
|
when(parser.getRegistrarThreatMatches(date)).thenReturn(sampleThreatMatches());
|
||||||
|
|
||||||
gotMessage = ArgumentCaptor.forClass(Message.class);
|
gotMessage = ArgumentCaptor.forClass(Message.class);
|
||||||
|
@ -77,7 +77,6 @@ public class Spec11EmailUtilsTest {
|
||||||
emailUtils =
|
emailUtils =
|
||||||
new Spec11EmailUtils(
|
new Spec11EmailUtils(
|
||||||
emailService,
|
emailService,
|
||||||
date,
|
|
||||||
"my-sender@test.com",
|
"my-sender@test.com",
|
||||||
"my-receiver@test.com",
|
"my-receiver@test.com",
|
||||||
"my-reply-to@test.com",
|
"my-reply-to@test.com",
|
||||||
|
@ -89,6 +88,7 @@ public class Spec11EmailUtilsTest {
|
||||||
@Test
|
@Test
|
||||||
public void testSuccess_emailMonthlySpec11Reports() throws Exception {
|
public void testSuccess_emailMonthlySpec11Reports() throws Exception {
|
||||||
emailUtils.emailSpec11Reports(
|
emailUtils.emailSpec11Reports(
|
||||||
|
date,
|
||||||
Spec11EmailSoyInfo.MONTHLY_SPEC_11_EMAIL,
|
Spec11EmailSoyInfo.MONTHLY_SPEC_11_EMAIL,
|
||||||
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
||||||
sampleThreatMatches());
|
sampleThreatMatches());
|
||||||
|
@ -143,6 +143,7 @@ public class Spec11EmailUtilsTest {
|
||||||
@Test
|
@Test
|
||||||
public void testSuccess_emailDailySpec11Reports() throws Exception {
|
public void testSuccess_emailDailySpec11Reports() throws Exception {
|
||||||
emailUtils.emailSpec11Reports(
|
emailUtils.emailSpec11Reports(
|
||||||
|
date,
|
||||||
Spec11EmailSoyInfo.DAILY_SPEC_11_EMAIL,
|
Spec11EmailSoyInfo.DAILY_SPEC_11_EMAIL,
|
||||||
"Super Cool Registry Daily Threat Detector [2018-07-15]",
|
"Super Cool Registry Daily Threat Detector [2018-07-15]",
|
||||||
sampleThreatMatches());
|
sampleThreatMatches());
|
||||||
|
@ -224,7 +225,7 @@ public class Spec11EmailUtilsTest {
|
||||||
RuntimeException.class,
|
RuntimeException.class,
|
||||||
() ->
|
() ->
|
||||||
emailUtils.emailSpec11Reports(
|
emailUtils.emailSpec11Reports(
|
||||||
Spec11EmailSoyInfo.MONTHLY_SPEC_11_EMAIL, "bar", sampleThreatMatches()));
|
date, Spec11EmailSoyInfo.MONTHLY_SPEC_11_EMAIL, "bar", sampleThreatMatches()));
|
||||||
assertThat(thrown).hasMessageThat().isEqualTo("Emailing spec11 report failed");
|
assertThat(thrown).hasMessageThat().isEqualTo("Emailing spec11 report failed");
|
||||||
assertThat(thrown)
|
assertThat(thrown)
|
||||||
.hasCauseThat()
|
.hasCauseThat()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue