Better handle syncing contacts when creating RegistrarContacts

This also adds a test that should've been there already.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=149583500
This commit is contained in:
mcilwain 2017-03-08 14:29:10 -08:00 committed by Ben McIlwain
parent 27b9244126
commit ad840ad4d6
2 changed files with 48 additions and 30 deletions

View file

@ -54,8 +54,6 @@ import javax.annotation.Nullable;
commandDescription = "Create/read/update/delete the various contact lists for a Registrar.") commandDescription = "Create/read/update/delete the various contact lists for a Registrar.")
final class RegistrarContactCommand extends MutatingCommand { final class RegistrarContactCommand extends MutatingCommand {
private enum Mode { LIST, CREATE, UPDATE, DELETE }
@Parameter( @Parameter(
description = "Client identifier of the registrar account.", description = "Client identifier of the registrar account.",
required = true) required = true)
@ -131,6 +129,11 @@ final class RegistrarContactCommand extends MutatingCommand {
validateWith = PathParameter.OutputFile.class) validateWith = PathParameter.OutputFile.class)
private Path output = Paths.get("/dev/stdout"); private Path output = Paths.get("/dev/stdout");
private enum Mode { LIST, CREATE, UPDATE, DELETE }
private static final ImmutableSet<Mode> MODES_REQUIRING_CONTACT_SYNC =
ImmutableSet.of(Mode.CREATE, Mode.UPDATE, Mode.DELETE);
@Nullable @Nullable
private Set<RegistrarContact.Type> contactTypes; private Set<RegistrarContact.Type> contactTypes;
@ -178,7 +181,7 @@ final class RegistrarContactCommand extends MutatingCommand {
default: default:
throw new AssertionError(); throw new AssertionError();
} }
if (mode == Mode.CREATE || mode == Mode.UPDATE || mode == Mode.DELETE) { if (MODES_REQUIRING_CONTACT_SYNC.contains(mode)) {
stageEntityChange(registrar, registrar.asBuilder().setContactsRequireSyncing(true).build()); stageEntityChange(registrar, registrar.asBuilder().setContactsRequireSyncing(true).build());
} }
} }

View file

@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.registrar.RegistrarContact.Type.ABUSE; import static google.registry.model.registrar.RegistrarContact.Type.ABUSE;
import static google.registry.model.registrar.RegistrarContact.Type.ADMIN; import static google.registry.model.registrar.RegistrarContact.Type.ADMIN;
import static google.registry.model.registrar.RegistrarContact.Type.WHOIS; import static google.registry.model.registrar.RegistrarContact.Type.WHOIS;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.DatastoreHelper.persistSimpleResource; import static google.registry.testing.DatastoreHelper.persistSimpleResource;
import static google.registry.testing.DatastoreHelper.persistSimpleResources; import static google.registry.testing.DatastoreHelper.persistSimpleResources;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
@ -44,21 +45,24 @@ public class RegistrarContactCommandTest extends CommandTestCase<RegistrarContac
@Test @Test
public void testList() throws Exception { public void testList() throws Exception {
Registrar registrar = Registrar.loadByClientId("NewRegistrar"); Registrar registrar = Registrar.loadByClientId("NewRegistrar");
RegistrarContact.updateContacts(registrar, ImmutableSet.of( RegistrarContact.updateContacts(
new RegistrarContact.Builder() registrar,
.setParent(registrar) ImmutableSet.of(
.setName("John Doe") new RegistrarContact.Builder()
.setEmailAddress("john.doe@example.com") .setParent(registrar)
.setTypes(ImmutableSet.of(ADMIN)) .setName("John Doe")
.setVisibleInWhoisAsAdmin(true) .setEmailAddress("john.doe@example.com")
.build())); .setTypes(ImmutableSet.of(ADMIN))
runCommand("-f", "--mode=LIST", "--output=" + output, "NewRegistrar"); .setVisibleInWhoisAsAdmin(true)
assertThat(Files.readAllLines(Paths.get(output), UTF_8)).containsExactly( .build()));
"John Doe", runCommandForced("--mode=LIST", "--output=" + output, "NewRegistrar");
"john.doe@example.com", assertThat(Files.readAllLines(Paths.get(output), UTF_8))
"Types: [ADMIN]", .containsExactly(
"Visible in WHOIS as Admin contact: Yes", "John Doe",
"Visible in WHOIS as Technical contact: No"); "john.doe@example.com",
"Types: [ADMIN]",
"Visible in WHOIS as Admin contact: Yes",
"Visible in WHOIS as Technical contact: No");
} }
@Test @Test
@ -74,8 +78,7 @@ public class RegistrarContactCommandTest extends CommandTestCase<RegistrarContac
.setVisibleInWhoisAsTech(true) .setVisibleInWhoisAsTech(true)
.build()); .build());
persistSimpleResources(contacts); persistSimpleResources(contacts);
runCommand( runCommandForced(
"--force",
"--mode=UPDATE", "--mode=UPDATE",
"--name=Judith Registrar", "--name=Judith Registrar",
"--email=judith.doe@example.com", "--email=judith.doe@example.com",
@ -109,8 +112,7 @@ public class RegistrarContactCommandTest extends CommandTestCase<RegistrarContac
.setName("Jane Doe") .setName("Jane Doe")
.setEmailAddress("jane.doe@example.com") .setEmailAddress("jane.doe@example.com")
.build()); .build());
runCommand( runCommandForced(
"--force",
"--mode=UPDATE", "--mode=UPDATE",
"--email=jane.doe@example.com", "--email=jane.doe@example.com",
"--allow_console_access=true", "--allow_console_access=true",
@ -130,8 +132,7 @@ public class RegistrarContactCommandTest extends CommandTestCase<RegistrarContac
.setEmailAddress("judith.doe@example.com") .setEmailAddress("judith.doe@example.com")
.setGaeUserId("11111") .setGaeUserId("11111")
.build()); .build());
runCommand( runCommandForced(
"--force",
"--mode=UPDATE", "--mode=UPDATE",
"--email=judith.doe@example.com", "--email=judith.doe@example.com",
"--allow_console_access=false", "--allow_console_access=false",
@ -144,8 +145,7 @@ public class RegistrarContactCommandTest extends CommandTestCase<RegistrarContac
@Test @Test
public void testCreate_withAdminType() throws Exception { public void testCreate_withAdminType() throws Exception {
Registrar registrar = Registrar.loadByClientId("NewRegistrar"); Registrar registrar = Registrar.loadByClientId("NewRegistrar");
runCommand( runCommandForced(
"--force",
"--mode=CREATE", "--mode=CREATE",
"--name=Jim Doe", "--name=Jim Doe",
"--email=jim.doe@example.com", "--email=jim.doe@example.com",
@ -169,8 +169,7 @@ public class RegistrarContactCommandTest extends CommandTestCase<RegistrarContac
@Test @Test
public void testDelete() throws Exception { public void testDelete() throws Exception {
runCommand( runCommandForced(
"--force",
"--mode=DELETE", "--mode=DELETE",
"--email=janedoe@theregistrar.com", "--email=janedoe@theregistrar.com",
"NewRegistrar"); "NewRegistrar");
@ -179,8 +178,7 @@ public class RegistrarContactCommandTest extends CommandTestCase<RegistrarContac
@Test @Test
public void testCreate_withConsoleAccessEnabled() throws Exception { public void testCreate_withConsoleAccessEnabled() throws Exception {
runCommand( runCommandForced(
"--force",
"--mode=CREATE", "--mode=CREATE",
"--name=Jim Doe", "--name=Jim Doe",
"--email=jim.doe@example.com", "--email=jim.doe@example.com",
@ -191,4 +189,21 @@ public class RegistrarContactCommandTest extends CommandTestCase<RegistrarContac
Registrar.loadByClientId("NewRegistrar").getContacts().asList().get(1); Registrar.loadByClientId("NewRegistrar").getContacts().asList().get(1);
assertThat(registrarContact.getGaeUserId()).matches("-?[0-9]+"); assertThat(registrarContact.getGaeUserId()).matches("-?[0-9]+");
} }
@Test
public void testCreate_syncingRequiredSetToTrue() throws Exception {
persistResource(
Registrar.loadByClientId("NewRegistrar")
.asBuilder()
.setContactsRequireSyncing(false)
.build());
assertThat(Registrar.loadByClientId("NewRegistrar").getContactsRequireSyncing()).isFalse();
runCommandForced(
"--mode=CREATE",
"--name=Jim Doe",
"--email=jim.doe@example.com",
"NewRegistrar");
assertThat(Registrar.loadByClientId("NewRegistrar").getContactsRequireSyncing()).isTrue();
}
} }