Automatically create mailing lists for registrars

This improves one of our gTech processes so they no longer have to remember to create Google Groups for a registrar after creating that registrar.

The ConfirmingCommand.verify() method is renamed to be more general purpose, so that it can do anything that follows naturally after a successful execution, such as creating Google Groups groups.

Minor refactoring is done around RegistryToolEnvironment handling for tests to make it more bullet-proof and general case.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=117494277
This commit is contained in:
mcilwain 2016-03-17 15:50:11 -07:00 committed by Justine Tunney
parent 5174c1c63f
commit 2293be4079
6 changed files with 135 additions and 5 deletions

View file

@ -68,7 +68,7 @@ final class AllocateDomainCommand extends MutatingEppToolCommand {
private final List<Key<DomainApplication>> applicationKeys = new ArrayList<>(); private final List<Key<DomainApplication>> applicationKeys = new ArrayList<>();
@Override @Override
protected String verify() throws Exception { protected String postExecute() throws Exception {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
// Check to see that we allocated everything. // Check to see that we allocated everything.
return builder.append(ofy().transactNewReadOnly(new Work<String>() { return builder.append(ofy().transactNewReadOnly(new Work<String>() {

View file

@ -34,7 +34,7 @@ public abstract class ConfirmingCommand implements Command {
printLineIfNotEmpty(prompt()); printLineIfNotEmpty(prompt());
if (force || promptForYes("Perform this command?")) { if (force || promptForYes("Perform this command?")) {
System.out.println(execute()); System.out.println(execute());
printLineIfNotEmpty(verify()); printLineIfNotEmpty(postExecute());
} else { } else {
System.out.println("Command aborted."); System.out.println("Command aborted.");
} }
@ -57,8 +57,11 @@ public abstract class ConfirmingCommand implements Command {
/** Perform the command and return a result description. */ /** Perform the command and return a result description. */
protected abstract String execute() throws Exception; protected abstract String execute() throws Exception;
/** Verify result and/or perform any post-execution steps, and return optional description. */ /**
protected String verify() throws Exception { * Perform any post-execution steps (e.g. verifying the result), and return a description String
* to be printed if non-empty.
*/
protected String postExecute() throws Exception {
return ""; return "";
} }
} }

View file

@ -19,16 +19,22 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.emptyToNull; import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Lists.newArrayList;
import static com.google.domain.registry.model.registrar.Registrar.State.ACTIVE; import static com.google.domain.registry.model.registrar.Registrar.State.ACTIVE;
import static com.google.domain.registry.tools.RegistryToolEnvironment.PRODUCTION;
import static com.google.domain.registry.tools.RegistryToolEnvironment.SANDBOX;
import static com.google.domain.registry.tools.RegistryToolEnvironment.UNITTEST;
import static com.google.domain.registry.util.RegistrarUtils.normalizeClientId; import static com.google.domain.registry.util.RegistrarUtils.normalizeClientId;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.domain.registry.model.registrar.Registrar; import com.google.domain.registry.model.registrar.Registrar;
import com.google.domain.registry.tools.Command.GtechCommand; import com.google.domain.registry.tools.Command.GtechCommand;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import java.util.List; import java.util.List;
@ -37,12 +43,29 @@ import javax.annotation.Nullable;
/** Command to create a Registrar. */ /** Command to create a Registrar. */
@Parameters(separators = " =", commandDescription = "Create new registrar account(s)") @Parameters(separators = " =", commandDescription = "Create new registrar account(s)")
final class CreateRegistrarCommand extends CreateOrUpdateRegistrarCommand implements GtechCommand { final class CreateRegistrarCommand extends CreateOrUpdateRegistrarCommand
implements GtechCommand, ServerSideCommand {
private static final ImmutableSet<RegistryToolEnvironment> ENVIRONMENTS_ALLOWING_GROUP_CREATION =
ImmutableSet.of(PRODUCTION, SANDBOX, UNITTEST);
// Allows test cases to be cleaner. // Allows test cases to be cleaner.
@VisibleForTesting @VisibleForTesting
static boolean requireAddress = true; static boolean requireAddress = true;
@Parameter(
names = "--create_groups",
description = "Whether the Google Groups for this registrar should be created",
arity = 1)
boolean createGoogleGroups = true;
private Connection connection;
@Override
public void setConnection(Connection connection) {
this.connection = connection;
}
@Override @Override
protected void initRegistrarCommand() throws Exception { protected void initRegistrarCommand() throws Exception {
checkArgument(mainParameters.size() == 1, "Must specify exactly one client identifier."); checkArgument(mainParameters.size() == 1, "Must specify exactly one client identifier.");
@ -85,4 +108,24 @@ final class CreateRegistrarCommand extends CreateOrUpdateRegistrarCommand implem
} }
return null; return null;
} }
@Override
protected String postExecute() throws Exception {
if (!createGoogleGroups) {
return "";
}
// Allow prod and sandbox because they actually have Groups, and UNITTEST for testing.
if (!ENVIRONMENTS_ALLOWING_GROUP_CREATION.contains(RegistryToolEnvironment.get())) {
return "\nSkipping registrar groups creation because only production and sandbox support it.";
}
try {
// We know it is safe to use the only main parameter here because initRegistrarCommand has
// already verified that there is only one, and getOldRegistrar has already verified that a
// registrar with this clientIdentifier doesn't already exist.
CreateRegistrarGroupsCommand.executeOnServer(connection, getOnlyElement(mainParameters));
} catch (Exception e) {
return "\nRegistrar created, but groups creation failed with error:\n" + e;
}
return "\nRegistrar groups created successfully.";
}
} }

View file

@ -73,6 +73,15 @@ public class CreateRegistrarGroupsCommand extends ConfirmingCommand
}}))); }})));
} }
/** Calls the server endpoint to create groups for the specified registrar client id. */
static void executeOnServer(Connection connection, String clientIdentifier) throws IOException {
connection.send(
CreateGroupsAction.PATH,
ImmutableMap.of(CreateGroupsAction.CLIENT_ID_PARAM, clientIdentifier),
MediaType.PLAIN_TEXT_UTF_8,
new byte[0]);
}
@Override @Override
protected String execute() throws IOException { protected String execute() throws IOException {
for (Registrar registrar : registrars) { for (Registrar registrar : registrars) {

View file

@ -117,6 +117,7 @@ final class SetupOteCommand extends ConfirmingCommand implements RemoteApiComman
private void createRegistrar(String registrarName, String password, String tld) throws Exception { private void createRegistrar(String registrarName, String password, String tld) throws Exception {
CreateRegistrarCommand command = new CreateRegistrarCommand(); CreateRegistrarCommand command = new CreateRegistrarCommand();
command.mainParameters = ImmutableList.of(registrarName); command.mainParameters = ImmutableList.of(registrarName);
command.createGoogleGroups = false; // Don't create Google Groups for OT&E registrars.
command.allowedTlds = ImmutableList.of(tld); command.allowedTlds = ImmutableList.of(tld);
command.registrarName = registrarName; command.registrarName = registrarName;
command.registrarType = Registrar.Type.OTE; command.registrarType = Registrar.Type.OTE;

View file

@ -21,23 +21,38 @@ import static com.google.domain.registry.testing.CertificateSamples.SAMPLE_CERT_
import static com.google.domain.registry.testing.DatastoreHelper.createTlds; import static com.google.domain.registry.testing.DatastoreHelper.createTlds;
import static com.google.domain.registry.testing.DatastoreHelper.persistResource; import static com.google.domain.registry.testing.DatastoreHelper.persistResource;
import static org.joda.time.DateTimeZone.UTC; import static org.joda.time.DateTimeZone.UTC;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Range; import com.google.common.collect.Range;
import com.google.common.net.MediaType;
import com.google.domain.registry.model.registrar.Registrar; import com.google.domain.registry.model.registrar.Registrar;
import com.google.domain.registry.testing.CertificateSamples; import com.google.domain.registry.testing.CertificateSamples;
import com.google.domain.registry.tools.ServerSideCommand.Connection;
import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParameterException;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import java.io.IOException;
/** Unit tests for {@link CreateRegistrarCommand}. */ /** Unit tests for {@link CreateRegistrarCommand}. */
public class CreateRegistrarCommandTest extends CommandTestCase<CreateRegistrarCommand> { public class CreateRegistrarCommandTest extends CommandTestCase<CreateRegistrarCommand> {
@Mock
private Connection connection;
@Before @Before
public void init() { public void init() {
CreateRegistrarCommand.requireAddress = false; CreateRegistrarCommand.requireAddress = false;
command.setConnection(connection);
} }
@Test @Test
@ -70,6 +85,12 @@ public class CreateRegistrarCommandTest extends CommandTestCase<CreateRegistrarC
assertThat(registrar.getCreationTime()).isIn(Range.closed(before, after)); assertThat(registrar.getCreationTime()).isIn(Range.closed(before, after));
assertThat(registrar.getLastUpdateTime()).isEqualTo(registrar.getCreationTime()); assertThat(registrar.getLastUpdateTime()).isEqualTo(registrar.getCreationTime());
assertThat(registrar.getBlockPremiumNames()).isFalse(); assertThat(registrar.getBlockPremiumNames()).isFalse();
verify(connection).send(
eq("/_dr/admin/createGroups"),
eq(ImmutableMap.of("clientId", "clientz")),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
} }
@Test @Test
@ -143,6 +164,59 @@ public class CreateRegistrarCommandTest extends CommandTestCase<CreateRegistrarC
assertThat(registrar.getAllowedTlds()).containsExactly("xn--q9jyb4c", "foobar"); assertThat(registrar.getAllowedTlds()).containsExactly("xn--q9jyb4c", "foobar");
} }
@Test
public void testSuccess_groupCreationCanBeDisabled() throws Exception {
runCommandForced(
"--name=blobio",
"--password=some_password",
"--registrar_type=TEST",
"--passcode=01234",
"--icann_referral_email=foo@bar.test",
"--create_groups=false",
"clientz");
verifyZeroInteractions(connection);
}
@SuppressWarnings("unchecked")
@Test
public void testFailure_groupCreationFails() throws Exception {
when(
connection.send(
Mockito.anyString(),
Mockito.anyMapOf(String.class, String.class),
Mockito.any(MediaType.class),
Mockito.any(byte[].class)))
.thenThrow(new IOException("BAD ROBOT NO COOKIE"));
runCommandForced(
"--name=blobio",
"--password=some_password",
"--registrar_type=TEST",
"--passcode=01234",
"--icann_referral_email=foo@bar.test",
"clientz");
Registrar registrar = Registrar.loadByClientId("clientz");
assertThat(registrar).isNotNull();
assertInStdout("Registrar created, but groups creation failed with error");
assertInStdout("BAD ROBOT NO COOKIE");
}
@Test
public void testSuccess_groupCreationDoesntOccurOnAlphaEnv() throws Exception {
runCommandInEnvironment(
RegistryToolEnvironment.ALPHA,
"--name=blobio",
"--password=some_password",
"--registrar_type=TEST",
"--passcode=01234",
"--icann_referral_email=foo@bar.test",
"--force",
"clientz");
verifyZeroInteractions(connection);
}
@Test @Test
public void testSuccess_ipWhitelistFlag() throws Exception { public void testSuccess_ipWhitelistFlag() throws Exception {
runCommand( runCommand(