Use gmail to send invoices (#2130)

This commit is contained in:
Weimin Yu
2023-08-29 14:25:54 -04:00
committed by GitHub
parent 57592d787c
commit ee3ece8c56
8 changed files with 91 additions and 26 deletions
@@ -125,6 +125,9 @@ public class GmailClientTest {
assertThat(mimeMessage.getRecipients(RecipientType.TO)).asList().containsExactly(toAddr);
assertThat(mimeMessage.getRecipients(RecipientType.CC)).asList().containsExactly(ccAddr);
assertThat(mimeMessage.getRecipients(RecipientType.BCC)).asList().containsExactly(bccAddr);
assertThat(mimeMessage.getReplyTo())
.asList()
.containsExactly(new InternetAddress("replyTo@example.com"));
assertThat(mimeMessage.getSubject()).isEqualTo("My subject");
assertThat(mimeMessage.getContent()).isInstanceOf(MimeMultipart.class);
MimeMultipart parts = (MimeMultipart) mimeMessage.getContent();
@@ -137,6 +140,23 @@ public class GmailClientTest {
assertThat(attachment.getContent()).isEqualTo("foo,bar\nbaz,qux");
}
@Test
public void toMimeMessage_overrideReplyToAddr() throws Exception {
InternetAddress fromAddr = new InternetAddress("from@example.com", "My sender");
InternetAddress toAddr = new InternetAddress("to@example.com");
InternetAddress replyToAddr = new InternetAddress("some-addr@another.com");
EmailMessage emailMessage =
EmailMessage.newBuilder()
.setFrom(fromAddr)
.setRecipients(ImmutableList.of(toAddr))
.setReplyToEmailAddress(replyToAddr)
.setSubject("My subject")
.setBody("My body")
.build();
MimeMessage mimeMessage = getGmailClient(true).toMimeMessage(emailMessage);
assertThat(mimeMessage.getReplyTo()).asList().containsExactly(replyToAddr);
}
@Test
public void toGmailMessage() throws Exception {
MimeMessage mimeMessage = mock(MimeMessage.class);
@@ -27,11 +27,12 @@ import com.google.cloud.storage.BlobId;
import com.google.common.collect.ImmutableList;
import com.google.common.net.MediaType;
import google.registry.gcs.GcsUtils;
import google.registry.groups.GmailClient;
import google.registry.util.EmailMessage;
import google.registry.util.EmailMessage.Attachment;
import google.registry.util.SendEmailService;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import org.joda.time.YearMonth;
@@ -42,39 +43,44 @@ import org.mockito.ArgumentCaptor;
/** Unit tests for {@link google.registry.reporting.billing.BillingEmailUtils}. */
class BillingEmailUtilsTest {
private SendEmailService emailService;
private GmailClient gmailClient;
private BillingEmailUtils emailUtils;
private GcsUtils gcsUtils;
private ArgumentCaptor<EmailMessage> contentCaptor;
@BeforeEach
void beforeEach() throws Exception {
emailService = mock(SendEmailService.class);
gmailClient = mock(GmailClient.class);
gcsUtils = mock(GcsUtils.class);
when(gcsUtils.openInputStream(BlobId.of("test-bucket", "results/REG-INV-2017-10.csv")))
.thenReturn(
new ByteArrayInputStream("test,data\nhello,world".getBytes(StandardCharsets.UTF_8)));
contentCaptor = ArgumentCaptor.forClass(EmailMessage.class);
emailUtils =
new BillingEmailUtils(
emailService,
new YearMonth(2017, 10),
new InternetAddress("my-sender@test.com"),
new InternetAddress("my-receiver@test.com"),
ImmutableList.of(
new InternetAddress("hello@world.com"), new InternetAddress("hola@mundo.com")),
"test-bucket",
"REG-INV",
"results/",
gcsUtils);
emailUtils = getEmailUtils(Optional.of(new InternetAddress("reply-to@test.com")));
}
private BillingEmailUtils getEmailUtils(Optional<InternetAddress> replyToAddress)
throws Exception {
return new BillingEmailUtils(
gmailClient,
new YearMonth(2017, 10),
new InternetAddress("my-sender@test.com"),
new InternetAddress("my-receiver@test.com"),
ImmutableList.of(
new InternetAddress("hello@world.com"), new InternetAddress("hola@mundo.com")),
replyToAddress,
"test-bucket",
"REG-INV",
"results/",
gcsUtils);
}
@Test
void testSuccess_emailOverallInvoice() throws MessagingException {
emailUtils.emailOverallInvoice();
verify(emailService).sendEmail(contentCaptor.capture());
verify(gmailClient).sendEmail(contentCaptor.capture());
EmailMessage emailMessage = contentCaptor.getValue();
EmailMessage expectedContent =
EmailMessage.newBuilder()
@@ -84,6 +90,7 @@ class BillingEmailUtilsTest {
new InternetAddress("hello@world.com"), new InternetAddress("hola@mundo.com")))
.setSubject("Domain Registry invoice data 2017-10")
.setBody("Attached is the 2017-10 invoice for the domain registry.")
.setReplyToEmailAddress(new InternetAddress("reply-to@test.com"))
.setAttachment(
Attachment.newBuilder()
.setContent("test,data\nhello,world")
@@ -94,11 +101,21 @@ class BillingEmailUtilsTest {
assertThat(emailMessage).isEqualTo(expectedContent);
}
@Test
void testSuccess_emailOverallInvoiceNoReplyOverride() throws Exception {
emailUtils = getEmailUtils(Optional.empty());
emailUtils.emailOverallInvoice();
verify(gmailClient).sendEmail(contentCaptor.capture());
EmailMessage emailMessage = contentCaptor.getValue();
assertThat(emailMessage.replyToEmailAddress()).isEmpty();
}
@Test
void testFailure_emailsAlert() throws MessagingException {
doThrow(new RuntimeException(new MessagingException("expected")))
.doNothing()
.when(emailService)
.when(gmailClient)
.sendEmail(contentCaptor.capture());
RuntimeException thrown =
assertThrows(RuntimeException.class, () -> emailUtils.emailOverallInvoice());
@@ -108,14 +125,14 @@ class BillingEmailUtilsTest {
.hasMessageThat()
.isEqualTo("javax.mail.MessagingException: expected");
// Verify we sent an e-mail alert
verify(emailService, times(2)).sendEmail(contentCaptor.capture());
verify(gmailClient, times(2)).sendEmail(contentCaptor.capture());
validateAlertMessage(contentCaptor.getValue(), "Emailing invoice failed due to expected");
}
@Test
void testSuccess_sendAlertEmail() throws MessagingException {
emailUtils.sendAlertEmail("Alert!");
verify(emailService).sendEmail(contentCaptor.capture());
verify(gmailClient).sendEmail(contentCaptor.capture());
validateAlertMessage(contentCaptor.getValue(), "Alert!");
}