mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
Change SendEmailService to an instance field.
This allows us to inject it with Dagger and avoid using InjectRule to set it in unit tests. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=217571475
This commit is contained in:
parent
06ce429c5a
commit
84c3544097
5 changed files with 14 additions and 20 deletions
|
@ -25,7 +25,6 @@ import dagger.Provides;
|
||||||
import google.registry.bigquery.BigqueryConnection;
|
import google.registry.bigquery.BigqueryConnection;
|
||||||
import google.registry.request.HttpException.BadRequestException;
|
import google.registry.request.HttpException.BadRequestException;
|
||||||
import google.registry.request.Parameter;
|
import google.registry.request.Parameter;
|
||||||
import google.registry.util.SendEmailService;
|
|
||||||
import java.lang.annotation.Documented;
|
import java.lang.annotation.Documented;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -115,11 +114,6 @@ public final class IcannReportingModule {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
|
||||||
static SendEmailService provideSendEmailService() {
|
|
||||||
return new SendEmailService();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Dagger qualifier for the subdirectory we stage to/upload from. */
|
/** Dagger qualifier for the subdirectory we stage to/upload from. */
|
||||||
@Qualifier
|
@Qualifier
|
||||||
@Documented
|
@Documented
|
||||||
|
|
|
@ -21,7 +21,6 @@ import com.google.common.base.Joiner;
|
||||||
import com.google.common.collect.Streams;
|
import com.google.common.collect.Streams;
|
||||||
import com.google.common.flogger.FluentLogger;
|
import com.google.common.flogger.FluentLogger;
|
||||||
import google.registry.config.RegistryConfig.Config;
|
import google.registry.config.RegistryConfig.Config;
|
||||||
import google.registry.util.NonFinalForTesting;
|
|
||||||
import google.registry.util.SendEmailService;
|
import google.registry.util.SendEmailService;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -39,18 +38,18 @@ public class SendEmailUtils {
|
||||||
|
|
||||||
private final String gSuiteOutgoingEmailAddress;
|
private final String gSuiteOutgoingEmailAddress;
|
||||||
private final String gSuiteOutoingEmailDisplayName;
|
private final String gSuiteOutoingEmailDisplayName;
|
||||||
|
private final SendEmailService emailService;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public SendEmailUtils(
|
public SendEmailUtils(
|
||||||
@Config("gSuiteOutgoingEmailAddress") String gSuiteOutgoingEmailAddress,
|
@Config("gSuiteOutgoingEmailAddress") String gSuiteOutgoingEmailAddress,
|
||||||
@Config("gSuiteOutoingEmailDisplayName") String gSuiteOutoingEmailDisplayName) {
|
@Config("gSuiteOutoingEmailDisplayName") String gSuiteOutoingEmailDisplayName,
|
||||||
|
SendEmailService emailService) {
|
||||||
this.gSuiteOutgoingEmailAddress = gSuiteOutgoingEmailAddress;
|
this.gSuiteOutgoingEmailAddress = gSuiteOutgoingEmailAddress;
|
||||||
this.gSuiteOutoingEmailDisplayName = gSuiteOutoingEmailDisplayName;
|
this.gSuiteOutoingEmailDisplayName = gSuiteOutoingEmailDisplayName;
|
||||||
|
this.emailService = emailService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonFinalForTesting
|
|
||||||
private static SendEmailService emailService = new SendEmailService();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends an email from Nomulus to the specified recipient(s). Returns true iff sending was
|
* Sends an email from Nomulus to the specified recipient(s). Returns true iff sending was
|
||||||
* successful.
|
* successful.
|
||||||
|
|
|
@ -15,6 +15,8 @@
|
||||||
package google.registry.util;
|
package google.registry.util;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Singleton;
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
import javax.mail.Session;
|
import javax.mail.Session;
|
||||||
|
@ -22,8 +24,12 @@ import javax.mail.Transport;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
|
|
||||||
/** Wrapper around javax.mail's Transport.send that can be mocked for testing purposes. */
|
/** Wrapper around javax.mail's Transport.send that can be mocked for testing purposes. */
|
||||||
|
@Singleton
|
||||||
public class SendEmailService {
|
public class SendEmailService {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
SendEmailService() {};
|
||||||
|
|
||||||
/** Returns a new MimeMessage using default App Engine transport settings. */
|
/** Returns a new MimeMessage using default App Engine transport settings. */
|
||||||
public Message createMessage() {
|
public Message createMessage() {
|
||||||
return new MimeMessage(Session.getDefaultInstance(new Properties(), null));
|
return new MimeMessage(Session.getDefaultInstance(new Properties(), null));
|
||||||
|
|
|
@ -91,14 +91,14 @@ public class RegistrarSettingsActionTestCase {
|
||||||
action.registrarChangesNotificationEmailAddresses = ImmutableList.of(
|
action.registrarChangesNotificationEmailAddresses = ImmutableList.of(
|
||||||
"notification@test.example", "notification2@test.example");
|
"notification@test.example", "notification2@test.example");
|
||||||
action.sendEmailUtils =
|
action.sendEmailUtils =
|
||||||
new SendEmailUtils(getGSuiteOutgoingEmailAddress(), getGSuiteOutgoingEmailDisplayName());
|
new SendEmailUtils(
|
||||||
|
getGSuiteOutgoingEmailAddress(), getGSuiteOutgoingEmailDisplayName(), emailService);
|
||||||
action.registryEnvironment = RegistryEnvironment.get();
|
action.registryEnvironment = RegistryEnvironment.get();
|
||||||
action.authResult =
|
action.authResult =
|
||||||
AuthResult.create(
|
AuthResult.create(
|
||||||
AuthLevel.USER,
|
AuthLevel.USER,
|
||||||
UserAuthInfo.create(new User("user@email.com", "email.com", "12345"), false));
|
UserAuthInfo.create(new User("user@email.com", "email.com", "12345"), false));
|
||||||
inject.setStaticField(Ofy.class, "clock", clock);
|
inject.setStaticField(Ofy.class, "clock", clock);
|
||||||
inject.setStaticField(SendEmailUtils.class, "emailService", emailService);
|
|
||||||
message = new MimeMessage(Session.getDefaultInstance(new Properties(), null));
|
message = new MimeMessage(Session.getDefaultInstance(new Properties(), null));
|
||||||
when(emailService.createMessage()).thenReturn(message);
|
when(emailService.createMessage()).thenReturn(message);
|
||||||
when(req.getMethod()).thenReturn("POST");
|
when(req.getMethod()).thenReturn("POST");
|
||||||
|
|
|
@ -25,7 +25,6 @@ import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import google.registry.testing.InjectRule;
|
|
||||||
import google.registry.util.SendEmailService;
|
import google.registry.util.SendEmailService;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
|
@ -35,7 +34,6 @@ import javax.mail.Session;
|
||||||
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.InternetAddress;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
@ -44,9 +42,6 @@ import org.junit.runners.JUnit4;
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class SendEmailUtilsTest {
|
public class SendEmailUtilsTest {
|
||||||
|
|
||||||
@Rule
|
|
||||||
public final InjectRule inject = new InjectRule();
|
|
||||||
|
|
||||||
private final SendEmailService emailService = mock(SendEmailService.class);
|
private final SendEmailService emailService = mock(SendEmailService.class);
|
||||||
|
|
||||||
private Message message;
|
private Message message;
|
||||||
|
@ -54,11 +49,11 @@ public class SendEmailUtilsTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
inject.setStaticField(SendEmailUtils.class, "emailService", emailService);
|
|
||||||
message = new MimeMessage(Session.getDefaultInstance(new Properties(), null));
|
message = new MimeMessage(Session.getDefaultInstance(new Properties(), null));
|
||||||
when(emailService.createMessage()).thenReturn(message);
|
when(emailService.createMessage()).thenReturn(message);
|
||||||
sendEmailUtils =
|
sendEmailUtils =
|
||||||
new SendEmailUtils(getGSuiteOutgoingEmailAddress(), getGSuiteOutgoingEmailDisplayName());
|
new SendEmailUtils(
|
||||||
|
getGSuiteOutgoingEmailAddress(), getGSuiteOutgoingEmailDisplayName(), emailService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue