Rename and reorder RegistrarAction

Rename RegistrarAction to RegistrarSettingsAction and reorder class contents
according to local style.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=137554457
This commit is contained in:
mmuller 2016-10-28 14:37:42 -07:00 committed by Ben McIlwain
parent b3a166b0c4
commit 3928ccf03f
7 changed files with 129 additions and 128 deletions

View file

@ -36,9 +36,9 @@ import google.registry.rdap.RdapNameserverSearchAction;
import google.registry.request.RequestModule; import google.registry.request.RequestModule;
import google.registry.request.RequestScope; import google.registry.request.RequestScope;
import google.registry.ui.server.registrar.ConsoleUiAction; import google.registry.ui.server.registrar.ConsoleUiAction;
import google.registry.ui.server.registrar.RegistrarAction;
import google.registry.ui.server.registrar.RegistrarPaymentAction; import google.registry.ui.server.registrar.RegistrarPaymentAction;
import google.registry.ui.server.registrar.RegistrarPaymentSetupAction; import google.registry.ui.server.registrar.RegistrarPaymentSetupAction;
import google.registry.ui.server.registrar.RegistrarSettingsAction;
import google.registry.ui.server.registrar.RegistrarUserModule; import google.registry.ui.server.registrar.RegistrarUserModule;
import google.registry.whois.WhoisHttpServer; import google.registry.whois.WhoisHttpServer;
import google.registry.whois.WhoisModule; import google.registry.whois.WhoisModule;
@ -66,7 +66,7 @@ interface FrontendRequestComponent {
RdapAutnumAction rdapAutnumAction(); RdapAutnumAction rdapAutnumAction();
RegistrarPaymentAction registrarPaymentAction(); RegistrarPaymentAction registrarPaymentAction();
RegistrarPaymentSetupAction registrarPaymentSetupAction(); RegistrarPaymentSetupAction registrarPaymentSetupAction();
RegistrarAction registrarAction(); RegistrarSettingsAction registrarAction();
RdapDomainAction rdapDomainAction(); RdapDomainAction rdapDomainAction();
RdapDomainSearchAction rdapDomainSearchAction(); RdapDomainSearchAction rdapDomainSearchAction();
RdapEntityAction rdapEntityAction(); RdapEntityAction rdapEntityAction();

View file

@ -58,12 +58,12 @@ import javax.servlet.http.HttpServletRequest;
* preserve history. * preserve history.
*/ */
@Action( @Action(
path = RegistrarAction.PATH, path = RegistrarSettingsAction.PATH,
requireLogin = true, requireLogin = true,
xsrfProtection = true, xsrfProtection = true,
xsrfScope = "console", xsrfScope = "console",
method = Action.Method.POST) method = Action.Method.POST)
public class RegistrarAction implements Runnable, JsonActionRunner.JsonAction { public class RegistrarSettingsAction implements Runnable, JsonActionRunner.JsonAction {
public static final String PATH = "/registrar-settings"; public static final String PATH = "/registrar-settings";
@ -76,7 +76,7 @@ public class RegistrarAction implements Runnable, JsonActionRunner.JsonAction {
@Inject Registrar initialRegistrar; @Inject Registrar initialRegistrar;
@Inject @Config("registrarChangesNotificationEmailAddresses") ImmutableList<String> @Inject @Config("registrarChangesNotificationEmailAddresses") ImmutableList<String>
registrarChangesNotificationEmailAddresses; registrarChangesNotificationEmailAddresses;
@Inject RegistrarAction() {} @Inject RegistrarSettingsAction() {}
private static final Predicate<RegistrarContact> HAS_PHONE = new Predicate<RegistrarContact>() { private static final Predicate<RegistrarContact> HAS_PHONE = new Predicate<RegistrarContact>() {
@Override @Override
@ -84,19 +84,42 @@ public class RegistrarAction implements Runnable, JsonActionRunner.JsonAction {
return contact.getPhoneNumber() != null; return contact.getPhoneNumber() != null;
}}; }};
/** Thrown when a set of contacts doesn't meet certain constraints. */ @Override
private static class ContactRequirementException extends FormException { public void run() {
ContactRequirementException(String msg) { jsonActionRunner.run(this);
super(msg);
}
ContactRequirementException(RegistrarContact.Type type) {
super("Must have at least one " + type.getDisplayName() + " contact");
}
} }
public Map<String, Object> read(Map<String, ?> args, Registrar registrar) { @Override
return JsonResponseHelper.create(SUCCESS, "Success", registrar.toJsonMap()); public Map<String, Object> handleJsonRequest(Map<String, ?> input) {
if (input == null) {
throw new BadRequestException("Malformed JSON");
}
if (!sessionUtils.checkRegistrarConsoleLogin(request)) {
return JsonResponseHelper.create(ERROR, "Not authorized to access Registrar Console");
}
// Process the operation. Though originally derived from a CRUD
// handlder, registrar-settings really only supports read and update.
String op = Optional.fromNullable((String) input.get(OP_PARAM)).or("read");
@SuppressWarnings("unchecked")
Map<String, ?> args = (Map<String, Object>)
Optional.<Object>fromNullable(input.get(ARGS_PARAM)).or(ImmutableMap.of());
try {
switch (op) {
case "update":
return update(args, initialRegistrar);
case "read":
return read(args, initialRegistrar);
default:
return JsonResponseHelper.create(ERROR, "Unknown or unsupported operation: " + op);
}
} catch (FormFieldException e) {
return JsonResponseHelper.createFormFieldError(e.getMessage(), e.getFieldName());
} catch (FormException ee) {
return JsonResponseHelper.create(ERROR, ee.getMessage());
}
} }
Map<String, Object> update(final Map<String, ?> args, final Registrar registrar) { Map<String, Object> update(final Map<String, ?> args, final Registrar registrar) {
@ -108,7 +131,8 @@ public class RegistrarAction implements Runnable, JsonActionRunner.JsonAction {
Map<String, Object> existingRegistrarMap = Map<String, Object> existingRegistrarMap =
expandRegistrarWithContacts(oldContacts, registrar); expandRegistrarWithContacts(oldContacts, registrar);
Registrar.Builder builder = registrar.asBuilder(); Registrar.Builder builder = registrar.asBuilder();
ImmutableSet<RegistrarContact> updatedContacts = update(registrar, builder, args); ImmutableSet<RegistrarContact> updatedContacts =
changeRegistrarFields(registrar, builder, args);
if (!updatedContacts.isEmpty()) { if (!updatedContacts.isEmpty()) {
builder.setContactsRequireSyncing(true); builder.setContactsRequireSyncing(true);
} }
@ -150,28 +174,54 @@ public class RegistrarAction implements Runnable, JsonActionRunner.JsonAction {
} }
/** /**
* Determines if any changes were made to the registrar besides the lastUpdateTime, and if so, * Updates a registrar builder with the supplied args from the http request, and returns a list of
* sends an email with a diff of the changes to the configured notification email address and * the new registrar contacts.
* enqueues a task to re-sync the registrar sheet.
*/ */
private void sendExternalUpdatesIfNecessary( public static ImmutableSet<RegistrarContact> changeRegistrarFields(
String registrarName, Registrar existingRegistrarObj, Registrar.Builder builder, Map<String, ?> args) {
Map<String, Object> existingRegistrar,
Map<String, Object> updatedRegistrar) { // WHOIS
Map<?, ?> diffs = DiffUtils.deepDiff(existingRegistrar, updatedRegistrar, true); builder.setWhoisServer(
@SuppressWarnings("unchecked") RegistrarFormFields.WHOIS_SERVER_FIELD.extractUntyped(args).orNull());
Set<String> changedKeys = (Set<String>) diffs.keySet(); builder.setReferralUrl(
if (CollectionUtils.difference(changedKeys, "lastUpdateTime").isEmpty()) { RegistrarFormFields.REFERRAL_URL_FIELD.extractUntyped(args).orNull());
return; for (String email :
RegistrarFormFields.EMAIL_ADDRESS_FIELD.extractUntyped(args).asSet()) {
builder.setEmailAddress(email);
} }
SyncRegistrarsSheetAction.enqueueBackendTask(); builder.setPhoneNumber(
if (!registrarChangesNotificationEmailAddresses.isEmpty()) { RegistrarFormFields.PHONE_NUMBER_FIELD.extractUntyped(args).orNull());
SendEmailUtils.sendEmail( builder.setFaxNumber(
registrarChangesNotificationEmailAddresses, RegistrarFormFields.FAX_NUMBER_FIELD.extractUntyped(args).orNull());
String.format("Registrar %s updated", registrarName), builder.setLocalizedAddress(
"The following changes were made to the registrar:\n" RegistrarFormFields.L10N_ADDRESS_FIELD.extractUntyped(args).orNull());
+ DiffUtils.prettyPrintDiffedMap(diffs, null));
// Security
builder.setIpAddressWhitelist(
RegistrarFormFields.IP_ADDRESS_WHITELIST_FIELD.extractUntyped(args).or(
ImmutableList.<CidrAddressBlock>of()));
for (String certificate
: RegistrarFormFields.CLIENT_CERTIFICATE_FIELD.extractUntyped(args).asSet()) {
builder.setClientCertificate(certificate, ofy().getTransactionTime());
} }
for (String certificate
: RegistrarFormFields.FAILOVER_CLIENT_CERTIFICATE_FIELD.extractUntyped(args).asSet()) {
builder.setFailoverClientCertificate(certificate, ofy().getTransactionTime());
}
builder.setUrl(
RegistrarFormFields.URL_FIELD.extractUntyped(args).orNull());
builder.setReferralUrl(
RegistrarFormFields.REFERRAL_URL_FIELD.extractUntyped(args).orNull());
// Contact
ImmutableSet.Builder<RegistrarContact> contacts = new ImmutableSet.Builder<>();
for (RegistrarContact.Builder contactBuilder
: concat(RegistrarFormFields.CONTACTS_FIELD.extractUntyped(args).asSet())) {
contacts.add(contactBuilder.setParent(existingRegistrarObj).build());
}
return contacts.build();
} }
/** /**
@ -219,91 +269,42 @@ public class RegistrarAction implements Runnable, JsonActionRunner.JsonAction {
} }
/** /**
* Updates a registrar builder with the supplied args from the http request, and returns a list of * Determines if any changes were made to the registrar besides the lastUpdateTime, and if so,
* the new registrar contacts. * sends an email with a diff of the changes to the configured notification email address and
* enqueues a task to re-sync the registrar sheet.
*/ */
public static ImmutableSet<RegistrarContact> update( private void sendExternalUpdatesIfNecessary(
Registrar existingRegistrarObj, Registrar.Builder builder, Map<String, ?> args) { String registrarName,
Map<String, Object> existingRegistrar,
// WHOIS Map<String, Object> updatedRegistrar) {
builder.setWhoisServer( Map<?, ?> diffs = DiffUtils.deepDiff(existingRegistrar, updatedRegistrar, true);
RegistrarFormFields.WHOIS_SERVER_FIELD.extractUntyped(args).orNull());
builder.setReferralUrl(
RegistrarFormFields.REFERRAL_URL_FIELD.extractUntyped(args).orNull());
for (String email :
RegistrarFormFields.EMAIL_ADDRESS_FIELD.extractUntyped(args).asSet()) {
builder.setEmailAddress(email);
}
builder.setPhoneNumber(
RegistrarFormFields.PHONE_NUMBER_FIELD.extractUntyped(args).orNull());
builder.setFaxNumber(
RegistrarFormFields.FAX_NUMBER_FIELD.extractUntyped(args).orNull());
builder.setLocalizedAddress(
RegistrarFormFields.L10N_ADDRESS_FIELD.extractUntyped(args).orNull());
// Security
builder.setIpAddressWhitelist(
RegistrarFormFields.IP_ADDRESS_WHITELIST_FIELD.extractUntyped(args).or(
ImmutableList.<CidrAddressBlock>of()));
for (String certificate
: RegistrarFormFields.CLIENT_CERTIFICATE_FIELD.extractUntyped(args).asSet()) {
builder.setClientCertificate(certificate, ofy().getTransactionTime());
}
for (String certificate
: RegistrarFormFields.FAILOVER_CLIENT_CERTIFICATE_FIELD.extractUntyped(args).asSet()) {
builder.setFailoverClientCertificate(certificate, ofy().getTransactionTime());
}
builder.setUrl(
RegistrarFormFields.URL_FIELD.extractUntyped(args).orNull());
builder.setReferralUrl(
RegistrarFormFields.REFERRAL_URL_FIELD.extractUntyped(args).orNull());
// Contact
ImmutableSet.Builder<RegistrarContact> contacts = new ImmutableSet.Builder<>();
for (RegistrarContact.Builder contactBuilder
: concat(RegistrarFormFields.CONTACTS_FIELD.extractUntyped(args).asSet())) {
contacts.add(contactBuilder.setParent(existingRegistrarObj).build());
}
return contacts.build();
}
@Override
public Map<String, Object> handleJsonRequest(Map<String, ?> input) {
if (input == null) {
throw new BadRequestException("Malformed JSON");
}
if (!sessionUtils.checkRegistrarConsoleLogin(request)) {
return JsonResponseHelper.create(ERROR, "Not authorized to access Registrar Console");
}
// Process the operation. Though originally derived from a CRUD
// handlder, registrar-settings really only supports read and update.
String op = Optional.fromNullable((String) input.get(OP_PARAM)).or("read");
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, ?> args = (Map<String, Object>) Set<String> changedKeys = (Set<String>) diffs.keySet();
Optional.<Object>fromNullable(input.get(ARGS_PARAM)).or(ImmutableMap.of()); if (CollectionUtils.difference(changedKeys, "lastUpdateTime").isEmpty()) {
try { return;
switch (op) { }
case "update": SyncRegistrarsSheetAction.enqueueBackendTask();
return update(args, initialRegistrar); if (!registrarChangesNotificationEmailAddresses.isEmpty()) {
case "read": SendEmailUtils.sendEmail(
return read(args, initialRegistrar); registrarChangesNotificationEmailAddresses,
default: String.format("Registrar %s updated", registrarName),
return JsonResponseHelper.create(ERROR, "Unknown or unsupported operation: " + op); "The following changes were made to the registrar:\n"
} + DiffUtils.prettyPrintDiffedMap(diffs, null));
} catch (FormFieldException e) {
return JsonResponseHelper.createFormFieldError(e.getMessage(), e.getFieldName());
} catch (FormException ee) {
return JsonResponseHelper.create(ERROR, ee.getMessage());
} }
} }
@Override public Map<String, Object> read(Map<String, ?> args, Registrar registrar) {
public void run() { return JsonResponseHelper.create(SUCCESS, "Success", registrar.toJsonMap());
jsonActionRunner.run(this); }
/** Thrown when a set of contacts doesn't meet certain constraints. */
private static class ContactRequirementException extends FormException {
ContactRequirementException(String msg) {
super(msg);
}
ContactRequirementException(RegistrarContact.Type type) {
super("Must have at least one " + type.getDisplayName() + " contact");
}
} }
} }

View file

@ -32,13 +32,13 @@ import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
/** /**
* Unit tests for contact_settings.js use of {@link RegistrarAction}. * Unit tests for contact_settings.js use of {@link RegistrarSettingsAction}.
* *
* <p>The default read and session validation tests are handled by the * <p>The default read and session validation tests are handled by the
* superclass. * superclass.
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class ContactSettingsTest extends RegistrarActionTestCase { public class ContactSettingsTest extends RegistrarSettingsActionTestCase {
@Test @Test
public void testPost_readContacts_success() throws Exception { public void testPost_readContacts_success() throws Exception {

View file

@ -36,9 +36,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
/** Tests for {@link RegistrarAction}. */ /** Tests for {@link RegistrarSettingsAction}. */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class RegistrarActionTest extends RegistrarActionTestCase { public class RegistrarSettingsActionTest extends RegistrarSettingsActionTestCase {
@Rule @Rule
public final ExceptionRule thrown = new ExceptionRule(); public final ExceptionRule thrown = new ExceptionRule();

View file

@ -55,9 +55,9 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
/** Base class for tests using {@link RegistrarAction}. */ /** Base class for tests using {@link RegistrarSettingsAction}. */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class RegistrarActionTestCase { public class RegistrarSettingsActionTestCase {
static final String CLIENT_ID = "TheRegistrar"; static final String CLIENT_ID = "TheRegistrar";
@ -90,7 +90,7 @@ public class RegistrarActionTestCase {
Message message; Message message;
final RegistrarAction action = new RegistrarAction(); final RegistrarSettingsAction action = new RegistrarSettingsAction();
final StringWriter writer = new StringWriter(); final StringWriter writer = new StringWriter();
final Supplier<Map<String, Object>> json = createJsonResponseSupplier(writer); final Supplier<Map<String, Object>> json = createJsonResponseSupplier(writer);
final FakeClock clock = new FakeClock(DateTime.parse("2014-01-01T00:00:00Z")); final FakeClock clock = new FakeClock(DateTime.parse("2014-01-01T00:00:00Z"));

View file

@ -31,13 +31,13 @@ import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
/** /**
* Unit tests for security_settings.js use of {@link RegistrarAction}. * Unit tests for security_settings.js use of {@link RegistrarSettingsAction}.
* *
* <p>The default read and session validation tests are handled by the * <p>The default read and session validation tests are handled by the
* superclass. * superclass.
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class SecuritySettingsTest extends RegistrarActionTestCase { public class SecuritySettingsTest extends RegistrarSettingsActionTestCase {
@Test @Test
public void testPost_updateCert_success() throws Exception { public void testPost_updateCert_success() throws Exception {

View file

@ -28,12 +28,12 @@ import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
/** /**
* Unit tests for security_settings.js use of {@link RegistrarAction}. * Unit tests for security_settings.js use of {@link RegistrarSettingsAction}.
* *
* <p>The default read and session validation tests are handled by the superclass. * <p>The default read and session validation tests are handled by the superclass.
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class WhoisSettingsTest extends RegistrarActionTestCase { public class WhoisSettingsTest extends RegistrarSettingsActionTestCase {
@Test @Test
public void testPost_update_success() throws Exception { public void testPost_update_success() throws Exception {