mirror of
https://github.com/google/nomulus.git
synced 2025-07-23 19:20:44 +02:00
Send Spec11 emails using Gmail (#2101)
First of a series of migrations to Gmail. This can only be verified in production.
This commit is contained in:
parent
45666773ee
commit
d7a3c0c439
2 changed files with 21 additions and 24 deletions
|
@ -31,12 +31,12 @@ import com.google.template.soy.tofu.SoyTofu;
|
||||||
import com.google.template.soy.tofu.SoyTofu.Renderer;
|
import com.google.template.soy.tofu.SoyTofu.Renderer;
|
||||||
import google.registry.beam.spec11.ThreatMatch;
|
import google.registry.beam.spec11.ThreatMatch;
|
||||||
import google.registry.config.RegistryConfig.Config;
|
import google.registry.config.RegistryConfig.Config;
|
||||||
|
import google.registry.groups.GmailClient;
|
||||||
import google.registry.model.domain.Domain;
|
import google.registry.model.domain.Domain;
|
||||||
import google.registry.model.registrar.Registrar;
|
import google.registry.model.registrar.Registrar;
|
||||||
import google.registry.model.registrar.RegistrarPoc;
|
import google.registry.model.registrar.RegistrarPoc;
|
||||||
import google.registry.reporting.spec11.soy.Spec11EmailSoyInfo;
|
import google.registry.reporting.spec11.soy.Spec11EmailSoyInfo;
|
||||||
import google.registry.util.EmailMessage;
|
import google.registry.util.EmailMessage;
|
||||||
import google.registry.util.SendEmailService;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
@ -56,8 +56,7 @@ public class Spec11EmailUtils {
|
||||||
Spec11EmailSoyInfo.getInstance().getFileName()))
|
Spec11EmailSoyInfo.getInstance().getFileName()))
|
||||||
.build()
|
.build()
|
||||||
.compileToTofu();
|
.compileToTofu();
|
||||||
|
private final GmailClient gmailClient;
|
||||||
private final SendEmailService emailService;
|
|
||||||
private final InternetAddress outgoingEmailAddress;
|
private final InternetAddress outgoingEmailAddress;
|
||||||
private final ImmutableList<InternetAddress> spec11BccEmailAddresses;
|
private final ImmutableList<InternetAddress> spec11BccEmailAddresses;
|
||||||
private final InternetAddress alertRecipientAddress;
|
private final InternetAddress alertRecipientAddress;
|
||||||
|
@ -66,13 +65,13 @@ public class Spec11EmailUtils {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
Spec11EmailUtils(
|
Spec11EmailUtils(
|
||||||
SendEmailService emailService,
|
GmailClient gmailClient,
|
||||||
@Config("alertRecipientEmailAddress") InternetAddress alertRecipientAddress,
|
@Config("alertRecipientEmailAddress") InternetAddress alertRecipientAddress,
|
||||||
@Config("spec11OutgoingEmailAddress") InternetAddress spec11OutgoingEmailAddress,
|
@Config("spec11OutgoingEmailAddress") InternetAddress spec11OutgoingEmailAddress,
|
||||||
@Config("spec11BccEmailAddresses") ImmutableList<InternetAddress> spec11BccEmailAddresses,
|
@Config("spec11BccEmailAddresses") ImmutableList<InternetAddress> spec11BccEmailAddresses,
|
||||||
@Config("spec11WebResources") ImmutableList<String> spec11WebResources,
|
@Config("spec11WebResources") ImmutableList<String> spec11WebResources,
|
||||||
@Config("registryName") String registryName) {
|
@Config("registryName") String registryName) {
|
||||||
this.emailService = emailService;
|
this.gmailClient = gmailClient;
|
||||||
this.outgoingEmailAddress = spec11OutgoingEmailAddress;
|
this.outgoingEmailAddress = spec11OutgoingEmailAddress;
|
||||||
this.spec11BccEmailAddresses = spec11BccEmailAddresses;
|
this.spec11BccEmailAddresses = spec11BccEmailAddresses;
|
||||||
this.alertRecipientAddress = alertRecipientAddress;
|
this.alertRecipientAddress = alertRecipientAddress;
|
||||||
|
@ -151,7 +150,7 @@ public class Spec11EmailUtils {
|
||||||
String subject,
|
String subject,
|
||||||
RegistrarThreatMatches registrarThreatMatches)
|
RegistrarThreatMatches registrarThreatMatches)
|
||||||
throws MessagingException {
|
throws MessagingException {
|
||||||
emailService.sendEmail(
|
gmailClient.sendEmail(
|
||||||
EmailMessage.newBuilder()
|
EmailMessage.newBuilder()
|
||||||
.setSubject(subject)
|
.setSubject(subject)
|
||||||
.setBody(getContent(date, soyTemplateInfo, registrarThreatMatches))
|
.setBody(getContent(date, soyTemplateInfo, registrarThreatMatches))
|
||||||
|
@ -191,7 +190,7 @@ public class Spec11EmailUtils {
|
||||||
/** Sends an e-mail indicating the state of the spec11 pipeline, with a given subject and body. */
|
/** Sends an e-mail indicating the state of the spec11 pipeline, with a given subject and body. */
|
||||||
void sendAlertEmail(String subject, String body) {
|
void sendAlertEmail(String subject, String body) {
|
||||||
try {
|
try {
|
||||||
emailService.sendEmail(
|
gmailClient.sendEmail(
|
||||||
EmailMessage.newBuilder()
|
EmailMessage.newBuilder()
|
||||||
.setFrom(outgoingEmailAddress)
|
.setFrom(outgoingEmailAddress)
|
||||||
.addRecipient(alertRecipientAddress)
|
.addRecipient(alertRecipientAddress)
|
||||||
|
|
|
@ -27,14 +27,13 @@ import static google.registry.testing.DatabaseHelper.persistActiveHost;
|
||||||
import static google.registry.testing.DatabaseHelper.persistResource;
|
import static google.registry.testing.DatabaseHelper.persistResource;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.mockito.Mockito.doThrow;
|
import static org.mockito.Mockito.doThrow;
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.net.MediaType;
|
import com.google.common.net.MediaType;
|
||||||
|
import google.registry.groups.GmailClient;
|
||||||
import google.registry.model.domain.Domain;
|
import google.registry.model.domain.Domain;
|
||||||
import google.registry.model.host.Host;
|
import google.registry.model.host.Host;
|
||||||
import google.registry.persistence.transaction.JpaTestExtensions;
|
import google.registry.persistence.transaction.JpaTestExtensions;
|
||||||
|
@ -42,7 +41,6 @@ import google.registry.persistence.transaction.JpaTestExtensions.JpaIntegrationT
|
||||||
import google.registry.reporting.spec11.soy.Spec11EmailSoyInfo;
|
import google.registry.reporting.spec11.soy.Spec11EmailSoyInfo;
|
||||||
import google.registry.testing.DatabaseHelper;
|
import google.registry.testing.DatabaseHelper;
|
||||||
import google.registry.util.EmailMessage;
|
import google.registry.util.EmailMessage;
|
||||||
import google.registry.util.SendEmailService;
|
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -51,10 +49,14 @@ import javax.mail.internet.InternetAddress;
|
||||||
import org.joda.time.LocalDate;
|
import org.joda.time.LocalDate;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
/** Unit tests for {@link Spec11EmailUtils}. */
|
/** Unit tests for {@link Spec11EmailUtils}. */
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
class Spec11EmailUtilsTest {
|
class Spec11EmailUtilsTest {
|
||||||
|
|
||||||
private static final ImmutableList<String> FAKE_RESOURCES = ImmutableList.of("foo");
|
private static final ImmutableList<String> FAKE_RESOURCES = ImmutableList.of("foo");
|
||||||
|
@ -98,9 +100,8 @@ class Spec11EmailUtilsTest {
|
||||||
final JpaIntegrationTestExtension jpa =
|
final JpaIntegrationTestExtension jpa =
|
||||||
new JpaTestExtensions.Builder().buildIntegrationTestExtension();
|
new JpaTestExtensions.Builder().buildIntegrationTestExtension();
|
||||||
|
|
||||||
private SendEmailService emailService;
|
@Mock private GmailClient gmailClient;
|
||||||
private Spec11EmailUtils emailUtils;
|
private Spec11EmailUtils emailUtils;
|
||||||
private Spec11RegistrarThreatMatchesParser parser;
|
|
||||||
private ArgumentCaptor<EmailMessage> contentCaptor;
|
private ArgumentCaptor<EmailMessage> contentCaptor;
|
||||||
private final LocalDate date = new LocalDate(2018, 7, 15);
|
private final LocalDate date = new LocalDate(2018, 7, 15);
|
||||||
|
|
||||||
|
@ -109,13 +110,10 @@ class Spec11EmailUtilsTest {
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void beforeEach() throws Exception {
|
void beforeEach() throws Exception {
|
||||||
emailService = mock(SendEmailService.class);
|
|
||||||
parser = mock(Spec11RegistrarThreatMatchesParser.class);
|
|
||||||
when(parser.getRegistrarThreatMatches(date)).thenReturn(sampleThreatMatches());
|
|
||||||
contentCaptor = ArgumentCaptor.forClass(EmailMessage.class);
|
contentCaptor = ArgumentCaptor.forClass(EmailMessage.class);
|
||||||
emailUtils =
|
emailUtils =
|
||||||
new Spec11EmailUtils(
|
new Spec11EmailUtils(
|
||||||
emailService,
|
gmailClient,
|
||||||
new InternetAddress("my-receiver@test.com"),
|
new InternetAddress("my-receiver@test.com"),
|
||||||
new InternetAddress("abuse@test.com"),
|
new InternetAddress("abuse@test.com"),
|
||||||
ImmutableList.of(
|
ImmutableList.of(
|
||||||
|
@ -138,7 +136,7 @@ class Spec11EmailUtilsTest {
|
||||||
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
||||||
sampleThreatMatches());
|
sampleThreatMatches());
|
||||||
// We inspect individual parameters because Message doesn't implement equals().
|
// We inspect individual parameters because Message doesn't implement equals().
|
||||||
verify(emailService, times(3)).sendEmail(contentCaptor.capture());
|
verify(gmailClient, times(3)).sendEmail(contentCaptor.capture());
|
||||||
List<EmailMessage> capturedContents = contentCaptor.getAllValues();
|
List<EmailMessage> capturedContents = contentCaptor.getAllValues();
|
||||||
validateMessage(
|
validateMessage(
|
||||||
capturedContents.get(0),
|
capturedContents.get(0),
|
||||||
|
@ -176,7 +174,7 @@ class Spec11EmailUtilsTest {
|
||||||
"Super Cool Registry Daily Threat Detector [2018-07-15]",
|
"Super Cool Registry Daily Threat Detector [2018-07-15]",
|
||||||
sampleThreatMatches());
|
sampleThreatMatches());
|
||||||
// We inspect individual parameters because Message doesn't implement equals().
|
// We inspect individual parameters because Message doesn't implement equals().
|
||||||
verify(emailService, times(3)).sendEmail(contentCaptor.capture());
|
verify(gmailClient, times(3)).sendEmail(contentCaptor.capture());
|
||||||
List<EmailMessage> capturedMessages = contentCaptor.getAllValues();
|
List<EmailMessage> capturedMessages = contentCaptor.getAllValues();
|
||||||
validateMessage(
|
validateMessage(
|
||||||
capturedMessages.get(0),
|
capturedMessages.get(0),
|
||||||
|
@ -217,7 +215,7 @@ class Spec11EmailUtilsTest {
|
||||||
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
||||||
sampleThreatMatches());
|
sampleThreatMatches());
|
||||||
// We inspect individual parameters because Message doesn't implement equals().
|
// We inspect individual parameters because Message doesn't implement equals().
|
||||||
verify(emailService, times(2)).sendEmail(contentCaptor.capture());
|
verify(gmailClient, times(2)).sendEmail(contentCaptor.capture());
|
||||||
List<EmailMessage> capturedContents = contentCaptor.getAllValues();
|
List<EmailMessage> capturedContents = contentCaptor.getAllValues();
|
||||||
validateMessage(
|
validateMessage(
|
||||||
capturedContents.get(0),
|
capturedContents.get(0),
|
||||||
|
@ -250,7 +248,7 @@ class Spec11EmailUtilsTest {
|
||||||
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
"Super Cool Registry Monthly Threat Detector [2018-07-15]",
|
||||||
sampleThreatMatches());
|
sampleThreatMatches());
|
||||||
// We inspect individual parameters because Message doesn't implement equals().
|
// We inspect individual parameters because Message doesn't implement equals().
|
||||||
verify(emailService, times(3)).sendEmail(contentCaptor.capture());
|
verify(gmailClient, times(3)).sendEmail(contentCaptor.capture());
|
||||||
List<EmailMessage> capturedContents = contentCaptor.getAllValues();
|
List<EmailMessage> capturedContents = contentCaptor.getAllValues();
|
||||||
validateMessage(
|
validateMessage(
|
||||||
capturedContents.get(0),
|
capturedContents.get(0),
|
||||||
|
@ -289,7 +287,7 @@ class Spec11EmailUtilsTest {
|
||||||
doThrow(new RuntimeException(new MessagingException("expected")))
|
doThrow(new RuntimeException(new MessagingException("expected")))
|
||||||
.doNothing()
|
.doNothing()
|
||||||
.doNothing()
|
.doNothing()
|
||||||
.when(emailService)
|
.when(gmailClient)
|
||||||
.sendEmail(contentCaptor.capture());
|
.sendEmail(contentCaptor.capture());
|
||||||
RuntimeException thrown =
|
RuntimeException thrown =
|
||||||
assertThrows(
|
assertThrows(
|
||||||
|
@ -305,7 +303,7 @@ class Spec11EmailUtilsTest {
|
||||||
.isEqualTo("Emailing Spec11 reports failed, first exception:");
|
.isEqualTo("Emailing Spec11 reports failed, first exception:");
|
||||||
assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("expected");
|
assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("expected");
|
||||||
// Verify we sent an e-mail alert
|
// Verify we sent an e-mail alert
|
||||||
verify(emailService, times(3)).sendEmail(contentCaptor.capture());
|
verify(gmailClient, times(3)).sendEmail(contentCaptor.capture());
|
||||||
List<EmailMessage> capturedMessages = contentCaptor.getAllValues();
|
List<EmailMessage> capturedMessages = contentCaptor.getAllValues();
|
||||||
validateMessage(
|
validateMessage(
|
||||||
capturedMessages.get(0),
|
capturedMessages.get(0),
|
||||||
|
@ -338,7 +336,7 @@ class Spec11EmailUtilsTest {
|
||||||
@Test
|
@Test
|
||||||
void testSuccess_sendAlertEmail() throws Exception {
|
void testSuccess_sendAlertEmail() throws Exception {
|
||||||
emailUtils.sendAlertEmail("Spec11 Pipeline Alert: 2018-07", "Alert!");
|
emailUtils.sendAlertEmail("Spec11 Pipeline Alert: 2018-07", "Alert!");
|
||||||
verify(emailService).sendEmail(contentCaptor.capture());
|
verify(gmailClient).sendEmail(contentCaptor.capture());
|
||||||
validateMessage(
|
validateMessage(
|
||||||
contentCaptor.getValue(),
|
contentCaptor.getValue(),
|
||||||
"abuse@test.com",
|
"abuse@test.com",
|
||||||
|
@ -363,7 +361,7 @@ class Spec11EmailUtilsTest {
|
||||||
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());
|
||||||
verify(emailService, times(3)).sendEmail(contentCaptor.capture());
|
verify(gmailClient, times(3)).sendEmail(contentCaptor.capture());
|
||||||
assertThat(contentCaptor.getAllValues().get(0).recipients())
|
assertThat(contentCaptor.getAllValues().get(0).recipients())
|
||||||
.containsExactly(new InternetAddress("johndoe@theregistrar.com"));
|
.containsExactly(new InternetAddress("johndoe@theregistrar.com"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue