Nullify contact fields in setContactFields (#1533)

When setting contact fields from a set of DesignatedContact's, nullify the
existing fields so they don't stick around if they're not in the new set.
This commit is contained in:
Michael Muller 2022-02-28 10:57:35 -05:00 committed by GitHub
parent fbdee623de
commit 55c368cf13
3 changed files with 64 additions and 5 deletions

View file

@ -779,6 +779,10 @@ public class DomainContent extends EppResource
*/ */
void setContactFields(Set<DesignatedContact> contacts, boolean includeRegistrant) { void setContactFields(Set<DesignatedContact> contacts, boolean includeRegistrant) {
// Set the individual contact fields. // Set the individual contact fields.
billingContact = techContact = adminContact = null;
if (includeRegistrant) {
registrantContact = null;
}
for (DesignatedContact contact : contacts) { for (DesignatedContact contact : contacts) {
switch (contact.getType()) { switch (contact.getType()) {
case BILLING: case BILLING:

View file

@ -109,7 +109,6 @@ import google.registry.persistence.VKey;
import google.registry.testing.DatabaseHelper; import google.registry.testing.DatabaseHelper;
import google.registry.testing.DualDatabaseTest; import google.registry.testing.DualDatabaseTest;
import google.registry.testing.ReplayExtension; import google.registry.testing.ReplayExtension;
import google.registry.testing.ReplayExtension.NoDatabaseCompare;
import google.registry.testing.TestOfyAndSql; import google.registry.testing.TestOfyAndSql;
import google.registry.testing.TestOfyOnly; import google.registry.testing.TestOfyOnly;
import java.util.Optional; import java.util.Optional;
@ -955,7 +954,6 @@ class DomainUpdateFlowTest extends ResourceFlowTestCase<DomainUpdateFlow, Domain
assertThat(thrown).hasMessageThat().contains("(sh8013)"); assertThat(thrown).hasMessageThat().contains("(sh8013)");
} }
@NoDatabaseCompare
@TestOfyAndSql @TestOfyAndSql
void testFailure_addingDuplicateContact() throws Exception { void testFailure_addingDuplicateContact() throws Exception {
persistReferencedEntities(); persistReferencedEntities();
@ -1284,7 +1282,6 @@ class DomainUpdateFlowTest extends ResourceFlowTestCase<DomainUpdateFlow, Domain
} }
// Contacts mismatch. // Contacts mismatch.
@NoDatabaseCompare
@TestOfyAndSql @TestOfyAndSql
void testFailure_sameContactAddedAndRemoved() throws Exception { void testFailure_sameContactAddedAndRemoved() throws Exception {
setEppInput("domain_update_add_remove_same_contact.xml"); setEppInput("domain_update_add_remove_same_contact.xml");

View file

@ -75,6 +75,7 @@ public class DomainBaseTest extends EntityTestCase {
private VKey<BillingEvent.OneTime> oneTimeBillKey; private VKey<BillingEvent.OneTime> oneTimeBillKey;
private VKey<BillingEvent.Recurring> recurringBillKey; private VKey<BillingEvent.Recurring> recurringBillKey;
private Key<HistoryEntry> historyEntryKey; private Key<HistoryEntry> historyEntryKey;
private VKey<ContactResource> contact1Key, contact2Key;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
@ -88,14 +89,14 @@ public class DomainBaseTest extends EntityTestCase {
.setRepoId("1-COM") .setRepoId("1-COM")
.build()) .build())
.createVKey(); .createVKey();
VKey<ContactResource> contact1Key = contact1Key =
persistResource( persistResource(
new ContactResource.Builder() new ContactResource.Builder()
.setContactId("contact_id1") .setContactId("contact_id1")
.setRepoId("2-COM") .setRepoId("2-COM")
.build()) .build())
.createVKey(); .createVKey();
VKey<ContactResource> contact2Key = contact2Key =
persistResource( persistResource(
new ContactResource.Builder() new ContactResource.Builder()
.setContactId("contact_id2") .setContactId("contact_id2")
@ -947,4 +948,61 @@ public class DomainBaseTest extends EntityTestCase {
this.billingEventOneTime = billingEventOneTime; this.billingEventOneTime = billingEventOneTime;
} }
} }
@Test
void testContactFields() {
VKey<ContactResource> contact3Key =
persistResource(
new ContactResource.Builder()
.setContactId("contact_id3")
.setRepoId("4-COM")
.build())
.createVKey();
VKey<ContactResource> contact4Key =
persistResource(
new ContactResource.Builder()
.setContactId("contact_id4")
.setRepoId("5-COM")
.build())
.createVKey();
// Set all of the contacts.
domain.setContactFields(
ImmutableSet.of(
DesignatedContact.create(Type.REGISTRANT, contact1Key),
DesignatedContact.create(Type.ADMIN, contact2Key),
DesignatedContact.create(Type.BILLING, contact3Key),
DesignatedContact.create(Type.TECH, contact4Key)),
true);
assertThat(domain.getRegistrant()).isEqualTo(contact1Key);
assertThat(domain.getAdminContact()).isEqualTo(contact2Key);
assertThat(domain.getBillingContact()).isEqualTo(contact3Key);
assertThat(domain.getTechContact()).isEqualTo(contact4Key);
// Make sure everything gets nulled out.
domain.setContactFields(ImmutableSet.of(), true);
assertThat(domain.getRegistrant()).isNull();
assertThat(domain.getAdminContact()).isNull();
assertThat(domain.getBillingContact()).isNull();
assertThat(domain.getTechContact()).isNull();
// Make sure that changes don't affect the registrant unless requested.
domain.setContactFields(
ImmutableSet.of(
DesignatedContact.create(Type.REGISTRANT, contact1Key),
DesignatedContact.create(Type.ADMIN, contact2Key),
DesignatedContact.create(Type.BILLING, contact3Key),
DesignatedContact.create(Type.TECH, contact4Key)),
false);
assertThat(domain.getRegistrant()).isNull();
assertThat(domain.getAdminContact()).isEqualTo(contact2Key);
assertThat(domain.getBillingContact()).isEqualTo(contact3Key);
assertThat(domain.getTechContact()).isEqualTo(contact4Key);
domain = domain.asBuilder().setRegistrant(contact1Key).build();
domain.setContactFields(ImmutableSet.of(), false);
assertThat(domain.getRegistrant()).isEqualTo(contact1Key);
assertThat(domain.getAdminContact()).isNull();
assertThat(domain.getBillingContact()).isNull();
assertThat(domain.getTechContact()).isNull();
}
} }