google-nomulus/javatests/google/registry/ui/server/otesetup/ConsoleOteSetupActionTest.java
guyben 2777018d6a Add the ability to setup OT&E from the web console
We create a new endpoint with a simple form that will let admins (including
support) setup OT&E for registrars.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=226570568
2019-01-02 11:56:59 -05:00

173 lines
7.1 KiB
Java

// 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("<h1>OT&E Setup Page</h1>");
}
@Test
public void testGet_authorized_onProduction() {
action.registryEnvironment = RegistryEnvironment.PRODUCTION;
assertThrows(IllegalStateException.class, action::run);
}
@Test
public void testGet_unauthorized() {
action.registrarAccessor =
AuthenticatedRegistrarAccessor.createForTesting(ImmutableSetMultimap.of());
action.run();
assertThat(response.getPayload()).contains("<h1>You need permission</h1>");
}
@Test
public void testPost_authorized() throws Exception {
action.clientId = Optional.of("myclientid");
action.email = Optional.of("contact@registry.example");
action.method = Method.POST;
action.run();
// We just check some samples to make sure OteAccountBuilder was called successfully. We aren't
// checking that all the entities are there or that they have the correct values.
assertThat(loadByClientId("myclientid-3")).isPresent();
assertThat(Registry.get("myclientid-ga")).isNotNull();
assertThat(loadByClientId("myclientid-5").get().getContacts().asList().get(0).getEmailAddress())
.isEqualTo("contact@registry.example");
assertThat(response.getPayload())
.contains("<h1>OT&E successfully created for registrar myclientid!</h1>");
assertThat(message.getSubject()).isEqualTo("OT&E for registrar myclientid created in unittest");
assertThat(message.getContent())
.isEqualTo(
""
+ "The following entities were created in unittest by TestUserId:\n"
+ " Registrar myclientid-1 with access to TLD myclientid-sunrise\n"
+ " Registrar myclientid-2 with access to TLD myclientid-landrush\n"
+ " Registrar myclientid-3 with access to TLD myclientid-ga\n"
+ " Registrar myclientid-4 with access to TLD myclientid-ga\n"
+ " Registrar myclientid-5 with access to TLD myclientid-eap\n"
+ "Gave user contact@registry.example web access to these Registrars\n");
}
@Test
public void testPost_unauthorized() {
action.registrarAccessor =
AuthenticatedRegistrarAccessor.createForTesting(ImmutableSetMultimap.of());
action.clientId = Optional.of("myclientid");
action.email = Optional.of("contact@registry.example");
action.method = Method.POST;
action.run();
assertThat(response.getPayload()).contains("<h1>You need permission</h1>");
}
}