Move SendEmailUtils to the /ui/server directory

SendEmailUtils is a general utility of the web console, and not specifically "only"
to the Registrar console.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=226187094
This commit is contained in:
guyben 2018-12-19 09:42:22 -08:00 committed by Michael Muller
parent 56b61ad5a2
commit 51f22a15ed
8 changed files with 78 additions and 32 deletions

View file

@ -13,12 +13,15 @@ java_library(
deps = [
"//java/google/registry/ui/forms",
"//java/google/registry/ui/server",
"//java/google/registry/util",
"//javatests/google/registry/testing",
"@com_google_appengine_api_1_0_sdk",
"@com_google_guava",
"@com_google_truth",
"@com_google_truth_extensions_truth_java8_extension",
"@junit",
"@org_hamcrest_library",
"@org_mockito_all",
],
)

View file

@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.ui.server.registrar;
package google.registry.ui.server;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.config.RegistryConfig.getGSuiteOutgoingEmailAddress;
import static google.registry.config.RegistryConfig.getGSuiteOutgoingEmailDisplayName;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
@ -51,16 +49,23 @@ public class SendEmailUtilsTest {
public void init() {
message = new MimeMessage(Session.getDefaultInstance(new Properties(), null));
when(emailService.createMessage()).thenReturn(message);
}
private void setRecepients(ImmutableList<String> recepients) {
sendEmailUtils =
new SendEmailUtils(
getGSuiteOutgoingEmailAddress(), getGSuiteOutgoingEmailDisplayName(), emailService);
"outgoing@registry.example",
"outgoing display name",
recepients,
emailService);
}
@Test
public void testSuccess_sendToOneAddress() throws Exception {
setRecepients(ImmutableList.of("johnny@fakesite.tld"));
assertThat(sendEmailUtils.hasRecepients()).isTrue();
assertThat(
sendEmailUtils.sendEmail(
ImmutableList.of("johnny@fakesite.tld"),
"Welcome to the Internet",
"It is a dark and scary place."))
.isTrue();
@ -73,9 +78,10 @@ public class SendEmailUtilsTest {
@Test
public void testSuccess_sendToMultipleAddresses() throws Exception {
setRecepients(ImmutableList.of("foo@example.com", "bar@example.com"));
assertThat(sendEmailUtils.hasRecepients()).isTrue();
assertThat(
sendEmailUtils.sendEmail(
ImmutableList.of("foo@example.com", "bar@example.com"),
"Welcome to the Internet",
"It is a dark and scary place."))
.isTrue();
@ -87,9 +93,10 @@ public class SendEmailUtilsTest {
@Test
public void testSuccess_ignoresMalformedEmailAddress() throws Exception {
setRecepients(ImmutableList.of("foo@example.com", "1iñvalidemail"));
assertThat(sendEmailUtils.hasRecepients()).isTrue();
assertThat(
sendEmailUtils.sendEmail(
ImmutableList.of("foo@example.com", "1iñvalidemail"),
"Welcome to the Internet",
"It is a dark and scary place."))
.isTrue();
@ -99,10 +106,23 @@ public class SendEmailUtilsTest {
}
@Test
public void testFailure_onlyGivenMalformedAddress() throws Exception {
public void testFailure_noAddresses() throws Exception {
setRecepients(ImmutableList.of());
assertThat(sendEmailUtils.hasRecepients()).isFalse();
assertThat(
sendEmailUtils.sendEmail(
"Welcome to the Internet",
"It is a dark and scary place."))
.isFalse();
verify(emailService, never()).sendMessage(any(Message.class));
}
@Test
public void testFailure_onlyGivenMalformedAddress() throws Exception {
setRecepients(ImmutableList.of("1iñvalidemail"));
assertThat(sendEmailUtils.hasRecepients()).isTrue();
assertThat(
sendEmailUtils.sendEmail(
ImmutableList.of("1iñvalidemail"),
"Welcome to the Internet",
"It is a dark and scary place."))
.isFalse();
@ -111,10 +131,11 @@ public class SendEmailUtilsTest {
@Test
public void testFailure_exceptionThrownDuringSend() throws Exception {
setRecepients(ImmutableList.of("foo@example.com"));
assertThat(sendEmailUtils.hasRecepients()).isTrue();
doThrow(new MessagingException()).when(emailService).sendMessage(any(Message.class));
assertThat(
sendEmailUtils.sendEmail(
ImmutableList.of("foo@example.com"),
"Welcome to the Internet",
"It is a dark and scary place."))
.isFalse();

View file

@ -19,6 +19,7 @@ java_library(
"//java/google/registry/request",
"//java/google/registry/request/auth",
"//java/google/registry/security",
"//java/google/registry/ui/server",
"//java/google/registry/ui/server/registrar",
"//java/google/registry/util",
"//javatests/google/registry/security",

View file

@ -41,6 +41,7 @@ import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeClock;
import google.registry.testing.InjectRule;
import google.registry.testing.MockitoJUnitRule;
import google.registry.ui.server.SendEmailUtils;
import google.registry.util.AppEngineServiceUtils;
import google.registry.util.SendEmailService;
import java.io.PrintWriter;
@ -97,11 +98,12 @@ public class RegistrarSettingsActionTestCase {
when(appEngineServiceUtils.getCurrentVersionHostname("backend")).thenReturn("backend.hostname");
action.jsonActionRunner = new JsonActionRunner(
ImmutableMap.of(), new JsonResponse(new ResponseImpl(rsp)));
action.registrarChangesNotificationEmailAddresses = ImmutableList.of(
"notification@test.example", "notification2@test.example");
action.sendEmailUtils =
new SendEmailUtils(
getGSuiteOutgoingEmailAddress(), getGSuiteOutgoingEmailDisplayName(), emailService);
getGSuiteOutgoingEmailAddress(),
getGSuiteOutgoingEmailDisplayName(),
ImmutableList.of("notification@test.example", "notification2@test.example"),
emailService);
action.registryEnvironment = RegistryEnvironment.get();
action.registrarConsoleMetrics = new RegistrarConsoleMetrics();
action.authResult =