mirror of
https://github.com/google/nomulus.git
synced 2025-05-21 03:39:36 +02:00
This allows us to use util methods from within config, which is a useful thing to be able to do for, e.g., being able to log errors while loading configuration. It makes sense that the util package should be at the very base of the class inheritance hierarchy; config seems logically higher than it. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=144324273
142 lines
5 KiB
Java
142 lines
5 KiB
Java
// Copyright 2016 The Nomulus Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package google.registry.ui.server.registrar;
|
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
import static org.mockito.Matchers.any;
|
|
import static org.mockito.Mockito.doThrow;
|
|
import static org.mockito.Mockito.never;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
import google.registry.config.RegistryConfig.LocalTestConfig;
|
|
import google.registry.testing.ExceptionRule;
|
|
import google.registry.testing.InjectRule;
|
|
import google.registry.util.SendEmailService;
|
|
import java.util.Properties;
|
|
import javax.mail.Message;
|
|
import javax.mail.Message.RecipientType;
|
|
import javax.mail.MessagingException;
|
|
import javax.mail.Session;
|
|
import javax.mail.internet.InternetAddress;
|
|
import javax.mail.internet.MimeMessage;
|
|
import org.junit.Before;
|
|
import org.junit.Rule;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.mockito.Mock;
|
|
import org.mockito.runners.MockitoJUnitRunner;
|
|
|
|
/** Unit tests for {@link SendEmailUtils}. */
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
public class SendEmailUtilsTest {
|
|
|
|
@Rule
|
|
public final ExceptionRule thrown = new ExceptionRule();
|
|
|
|
@Rule
|
|
public final InjectRule inject = new InjectRule();
|
|
|
|
@Mock
|
|
private SendEmailService emailService;
|
|
|
|
private Message message;
|
|
private SendEmailUtils sendEmailUtils;
|
|
|
|
@Before
|
|
public void init() throws Exception {
|
|
inject.setStaticField(SendEmailUtils.class, "emailService", emailService);
|
|
message = new MimeMessage(Session.getDefaultInstance(new Properties(), null));
|
|
when(emailService.createMessage()).thenReturn(message);
|
|
sendEmailUtils = new SendEmailUtils(
|
|
LocalTestConfig.GOOGLE_APPS_SEND_FROM_EMAIL_ADDRESS,
|
|
LocalTestConfig.GOOGLE_APPS_ADMIN_EMAIL_DISPLAY_NAME);
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_sendToOneAddress() throws Exception {
|
|
assertThat(
|
|
sendEmailUtils.sendEmail(
|
|
ImmutableList.of("johnny@fakesite.tld"),
|
|
"Welcome to the Internet",
|
|
"It is a dark and scary place."))
|
|
.isTrue();
|
|
verifyMessageSent();
|
|
assertThat(message.getRecipients(RecipientType.TO)).asList()
|
|
.containsExactly(new InternetAddress("johnny@fakesite.tld"));
|
|
assertThat(message.getAllRecipients()).asList()
|
|
.containsExactly(new InternetAddress("johnny@fakesite.tld"));
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_sendToMultipleAddresses() throws Exception {
|
|
assertThat(
|
|
sendEmailUtils.sendEmail(
|
|
ImmutableList.of("foo@example.com", "bar@example.com"),
|
|
"Welcome to the Internet",
|
|
"It is a dark and scary place."))
|
|
.isTrue();
|
|
verifyMessageSent();
|
|
assertThat(message.getAllRecipients()).asList().containsExactly(
|
|
new InternetAddress("foo@example.com"),
|
|
new InternetAddress("bar@example.com"));
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_ignoresMalformedEmailAddress() throws Exception {
|
|
assertThat(
|
|
sendEmailUtils.sendEmail(
|
|
ImmutableList.of("foo@example.com", "1iñvalidemail"),
|
|
"Welcome to the Internet",
|
|
"It is a dark and scary place."))
|
|
.isTrue();
|
|
verifyMessageSent();
|
|
assertThat(message.getAllRecipients()).asList()
|
|
.containsExactly(new InternetAddress("foo@example.com"));
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_onlyGivenMalformedAddress() throws Exception {
|
|
assertThat(
|
|
sendEmailUtils.sendEmail(
|
|
ImmutableList.of("1iñvalidemail"),
|
|
"Welcome to the Internet",
|
|
"It is a dark and scary place."))
|
|
.isFalse();
|
|
verify(emailService, never()).sendMessage(any(Message.class));
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_exceptionThrownDuringSend() throws Exception {
|
|
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();
|
|
verifyMessageSent();
|
|
assertThat(message.getAllRecipients())
|
|
.asList()
|
|
.containsExactly(new InternetAddress("foo@example.com"));
|
|
}
|
|
|
|
private void verifyMessageSent() throws Exception {
|
|
verify(emailService).sendMessage(message);
|
|
assertThat(message.getSubject()).isEqualTo("Welcome to the Internet");
|
|
assertThat(message.getContent()).isEqualTo("It is a dark and scary place.");
|
|
}
|
|
}
|