// Copyright 2018 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.otesetup; import static com.google.common.net.HttpHeaders.LOCATION; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth8.assertThat; import static google.registry.model.registrar.Registrar.loadByClientId; import static google.registry.testing.DatastoreHelper.persistPremiumList; import static google.registry.testing.JUnitBackports.assertThrows; import static javax.servlet.http.HttpServletResponse.SC_MOVED_TEMPORARILY; import static org.mockito.Mockito.when; import com.google.appengine.api.users.User; import com.google.appengine.api.users.UserServiceFactory; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSetMultimap; import google.registry.config.RegistryEnvironment; import google.registry.model.registry.Registry; import google.registry.request.Action.Method; import google.registry.request.auth.AuthLevel; import google.registry.request.auth.AuthResult; import google.registry.request.auth.AuthenticatedRegistrarAccessor; import google.registry.request.auth.UserAuthInfo; import google.registry.security.XsrfTokenManager; import google.registry.testing.AppEngineRule; import google.registry.testing.DeterministicStringGenerator; import google.registry.testing.FakeClock; import google.registry.testing.FakeResponse; import google.registry.testing.MockitoJUnitRule; import google.registry.ui.server.SendEmailUtils; import google.registry.util.SendEmailService; import java.util.Optional; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.internet.MimeMessage; import javax.servlet.http.HttpServletRequest; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Mock; @RunWith(JUnit4.class) public final class ConsoleOteSetupActionTest { @Rule public final AppEngineRule appEngineRule = AppEngineRule.builder() .withDatastore() .build(); @Rule public final MockitoJUnitRule mocks = MockitoJUnitRule.create(); private final FakeResponse response = new FakeResponse(); private final ConsoleOteSetupAction action = new ConsoleOteSetupAction(); private final User user = new User("marla.singer@example.com", "gmail.com", "12345"); @Mock HttpServletRequest request; @Mock SendEmailService emailService; Message message; @Before public void setUp() { persistPremiumList("default_sandbox_list", "sandbox,USD 1000"); action.req = request; action.method = Method.GET; action.response = response; action.registrarAccessor = AuthenticatedRegistrarAccessor.createForTesting( ImmutableSetMultimap.of("unused", AuthenticatedRegistrarAccessor.Role.ADMIN)); action.userService = UserServiceFactory.getUserService(); action.xsrfTokenManager = new XsrfTokenManager(new FakeClock(), action.userService); action.authResult = AuthResult.create(AuthLevel.USER, UserAuthInfo.create(user, false)); action.registryEnvironment = RegistryEnvironment.UNITTEST; action.sendEmailUtils = new SendEmailUtils( "outgoing@registry.example", "UnitTest Registry", ImmutableList.of("notification@test.example", "notification2@test.example"), emailService); action.logoFilename = "logo.png"; action.productName = "Nomulus"; action.clientId = Optional.empty(); action.email = Optional.empty(); action.passwordGenerator = new DeterministicStringGenerator("abcdefghijklmnopqrstuvwxyz"); message = new MimeMessage(Session.getDefaultInstance(new Properties(), null)); when(emailService.createMessage()).thenReturn(message); } @Test public void testNoUser_redirect() { when(request.getRequestURI()).thenReturn("/test"); action.authResult = AuthResult.NOT_AUTHENTICATED; action.run(); assertThat(response.getStatus()).isEqualTo(SC_MOVED_TEMPORARILY); assertThat(response.getHeaders().get(LOCATION)).isEqualTo("/_ah/login?continue=%2Ftest"); } @Test public void testGet_authorized() { action.run(); assertThat(response.getPayload()).contains("