mirror of
https://github.com/google/nomulus.git
synced 2025-07-22 02:36:03 +02:00
TESTED=For all tests, I added @Cache to DomainBase because otherwise the tests will fail. We aren't ready to do this in prod yet, which is why the tests have a TODO in them. The new tests fail if you change line 134 in Ofy to not use memcache and either use the unchanged original flow code, or use the new inlined code and change loadWithMemcache() to load(). They pass with the new inlined code that calls loadWithMemcache(), as long as the @Cache is added to DomainResource. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=154457655
389 lines
17 KiB
Java
389 lines
17 KiB
Java
// Copyright 2017 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.flows.domain;
|
|
|
|
import static com.google.common.base.Predicates.equalTo;
|
|
import static com.google.common.io.BaseEncoding.base16;
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
import static google.registry.model.ofy.ObjectifyService.ofy;
|
|
import static google.registry.testing.DatastoreHelper.assertNoBillingEvents;
|
|
import static google.registry.testing.DatastoreHelper.createTld;
|
|
import static google.registry.testing.DatastoreHelper.persistActiveContact;
|
|
import static google.registry.testing.DatastoreHelper.persistActiveHost;
|
|
import static google.registry.testing.DatastoreHelper.persistResource;
|
|
import static google.registry.testing.MemcacheHelper.clearMemcache;
|
|
import static google.registry.testing.MemcacheHelper.setMemcacheContents;
|
|
import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions;
|
|
import static google.registry.util.DatastoreServiceUtils.KEY_TO_KIND_FUNCTION;
|
|
|
|
import com.google.common.base.Predicate;
|
|
import com.google.common.base.Predicates;
|
|
import com.google.common.collect.FluentIterable;
|
|
import com.google.common.collect.ImmutableList;
|
|
import com.google.common.collect.ImmutableMap;
|
|
import com.google.common.collect.ImmutableSet;
|
|
import com.googlecode.objectify.Key;
|
|
import google.registry.flows.ResourceFlowTestCase;
|
|
import google.registry.flows.ResourceFlowUtils.ResourceDoesNotExistException;
|
|
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;
|
|
import google.registry.flows.domain.DomainApplicationInfoFlow.ApplicationLaunchPhaseMismatchException;
|
|
import google.registry.flows.domain.DomainApplicationInfoFlow.MissingApplicationIdException;
|
|
import google.registry.flows.domain.DomainFlowUtils.ApplicationDomainNameMismatchException;
|
|
import google.registry.model.contact.ContactResource;
|
|
import google.registry.model.domain.DesignatedContact;
|
|
import google.registry.model.domain.DesignatedContact.Type;
|
|
import google.registry.model.domain.DomainApplication;
|
|
import google.registry.model.domain.DomainAuthInfo;
|
|
import google.registry.model.domain.launch.ApplicationStatus;
|
|
import google.registry.model.domain.launch.LaunchCreateExtension;
|
|
import google.registry.model.domain.launch.LaunchPhase;
|
|
import google.registry.model.domain.secdns.DelegationSignerData;
|
|
import google.registry.model.eppcommon.AuthInfo.PasswordAuth;
|
|
import google.registry.model.eppcommon.StatusValue;
|
|
import google.registry.model.host.HostResource;
|
|
import google.registry.model.index.DomainApplicationIndex;
|
|
import google.registry.model.ofy.RequestCapturingAsyncDatastoreService;
|
|
import google.registry.model.registry.Registry.TldState;
|
|
import google.registry.model.smd.EncodedSignedMark;
|
|
import google.registry.testing.AppEngineRule;
|
|
import google.registry.testing.EppLoader;
|
|
import java.util.List;
|
|
import org.joda.time.DateTime;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
|
|
/** Unit tests for {@link DomainApplicationInfoFlow}. */
|
|
public class DomainApplicationInfoFlowTest
|
|
extends ResourceFlowTestCase<DomainApplicationInfoFlow, DomainApplication> {
|
|
|
|
private ContactResource registrant;
|
|
private ContactResource contact;
|
|
private HostResource host1;
|
|
private HostResource host2;
|
|
private DomainApplication application;
|
|
|
|
private enum MarksState { MARKS_EXIST, NO_MARKS_EXIST }
|
|
private enum HostsState { HOSTS_EXIST, NO_HOSTS_EXIST }
|
|
|
|
@Before
|
|
public void resetClientId() {
|
|
setEppInput("domain_info_sunrise.xml");
|
|
sessionMetadata.setClientId("NewRegistrar");
|
|
createTld("tld", TldState.SUNRUSH);
|
|
}
|
|
|
|
private void persistTestEntities(HostsState hostsState, MarksState marksState) throws Exception {
|
|
registrant = persistActiveContact("jd1234");
|
|
contact = persistActiveContact("sh8013");
|
|
host1 = persistActiveHost("ns1.example.net");
|
|
host2 = persistActiveHost("ns1.example.tld");
|
|
application = persistResource(new DomainApplication.Builder()
|
|
.setRepoId("123-TLD")
|
|
.setFullyQualifiedDomainName("example.tld")
|
|
.setPhase(LaunchPhase.SUNRUSH)
|
|
.setPersistedCurrentSponsorClientId("NewRegistrar")
|
|
.setCreationClientId("TheRegistrar")
|
|
.setLastEppUpdateClientId("NewRegistrar")
|
|
.setCreationTimeForTest(DateTime.parse("1999-04-03T22:00:00.0Z"))
|
|
.setLastEppUpdateTime(DateTime.parse("1999-12-03T09:00:00.0Z"))
|
|
.setRegistrant(Key.create(registrant))
|
|
.setContacts(ImmutableSet.of(
|
|
DesignatedContact.create(Type.ADMIN, Key.create(contact)),
|
|
DesignatedContact.create(Type.TECH, Key.create(contact))))
|
|
.setNameservers(hostsState.equals(HostsState.HOSTS_EXIST) ? ImmutableSet.of(
|
|
Key.create(host1), Key.create(host2)) : null)
|
|
.setAuthInfo(DomainAuthInfo.create(PasswordAuth.create("2fooBAR")))
|
|
.addStatusValue(StatusValue.PENDING_CREATE)
|
|
.setApplicationStatus(ApplicationStatus.PENDING_VALIDATION)
|
|
.setEncodedSignedMarks(marksState.equals(MarksState.MARKS_EXIST)
|
|
// If we need to include an encoded signed mark, pull it out of the create xml.
|
|
? ImmutableList.of((EncodedSignedMark)
|
|
new EppLoader(this, "domain_create_sunrise_encoded_signed_mark.xml")
|
|
.getEpp()
|
|
.getSingleExtension(LaunchCreateExtension.class)
|
|
.getSignedMarks().get(0))
|
|
: null)
|
|
.build());
|
|
}
|
|
|
|
private void doSuccessfulTest(String expectedXmlFilename, HostsState hostsState)
|
|
throws Exception {
|
|
assertTransactionalFlow(false);
|
|
String expected = loadFileWithSubstitutions(
|
|
getClass(), expectedXmlFilename, ImmutableMap.of("ROID", "123-TLD"));
|
|
if (hostsState.equals(HostsState.NO_HOSTS_EXIST)) {
|
|
expected = expected.replaceAll("\"ok\"", "\"inactive\"");
|
|
}
|
|
runFlowAssertResponse(expected);
|
|
assertNoHistory();
|
|
assertNoBillingEvents();
|
|
}
|
|
|
|
private void doSuccessfulTest(String expectedXmlFilename) throws Exception {
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
doSuccessfulTest(expectedXmlFilename, HostsState.HOSTS_EXIST);
|
|
}
|
|
|
|
private void doSuccessfulTestNoNameservers(String expectedXmlFilename) throws Exception {
|
|
persistTestEntities(HostsState.NO_HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
doSuccessfulTest(expectedXmlFilename, HostsState.NO_HOSTS_EXIST);
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_quietPeriod() throws Exception {
|
|
createTld("tld", TldState.QUIET_PERIOD);
|
|
doSuccessfulTest("domain_info_sunrise_response.xml");
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_generalAvailability() throws Exception {
|
|
createTld("tld", TldState.GENERAL_AVAILABILITY);
|
|
doSuccessfulTest("domain_info_sunrise_response.xml");
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedDefaultHosts_nameserversExist() throws Exception {
|
|
// Default is "all", which means nameservers since there can't be subordinates.
|
|
doSuccessfulTest("domain_info_sunrise_response.xml");
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedDefaultHosts_noNameserversExist() throws Exception {
|
|
// Default is "all", which means nameservers since there can't be subordinates.
|
|
doSuccessfulTestNoNameservers("domain_info_sunrise_response_no_nameservers.xml");
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedAllHosts_nameserversExist() throws Exception {
|
|
// "All" means nameservers since there can't be subordinates (same as "delegated").
|
|
setEppInput("domain_info_sunrise_all_hosts.xml");
|
|
doSuccessfulTest("domain_info_sunrise_response.xml");
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedAllHosts_noNameserversExist() throws Exception {
|
|
// "All" means nameservers since there can't be subordinates (same as "delegated").
|
|
setEppInput("domain_info_sunrise_all_hosts.xml");
|
|
doSuccessfulTestNoNameservers("domain_info_sunrise_response_no_nameservers.xml");
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedDelegatedHosts_nameserversExist() throws Exception {
|
|
// "Delegated" means nameservers since there can't be subordinates (same as "all").
|
|
setEppInput("domain_info_sunrise_delegated_hosts.xml");
|
|
doSuccessfulTest("domain_info_sunrise_response.xml");
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedDelegatedHosts_noNameserversExist() throws Exception {
|
|
// "Delegated" means nameservers since there can't be subordinates (same as "all").
|
|
setEppInput("domain_info_sunrise_delegated_hosts.xml");
|
|
doSuccessfulTestNoNameservers("domain_info_sunrise_response_no_nameservers.xml");
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedNoneHosts_nameserversExist() throws Exception {
|
|
setEppInput("domain_info_sunrise_none_hosts.xml");
|
|
doSuccessfulTestNoNameservers("domain_info_sunrise_response_no_nameservers.xml");
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedNoneHosts_noNameserversExist() throws Exception {
|
|
setEppInput("domain_info_sunrise_none_hosts.xml");
|
|
doSuccessfulTestNoNameservers("domain_info_sunrise_response_no_nameservers.xml");
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedDefaultMarks_noMarksExist() throws Exception {
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
doSuccessfulTest("domain_info_sunrise_response.xml", HostsState.HOSTS_EXIST);
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedDefaultMarks_marksExist() throws Exception {
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.MARKS_EXIST);
|
|
doSuccessfulTest("domain_info_sunrise_response.xml", HostsState.HOSTS_EXIST);
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedNoMarks_marksExist() throws Exception {
|
|
setEppInput("domain_info_sunrise_no_marks.xml");
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.MARKS_EXIST);
|
|
doSuccessfulTest("domain_info_sunrise_response.xml", HostsState.HOSTS_EXIST);
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedNoMarks_noMarksExist() throws Exception {
|
|
setEppInput("domain_info_sunrise_no_marks.xml");
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
doSuccessfulTest("domain_info_sunrise_response.xml", HostsState.HOSTS_EXIST);
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedIncludeMarks_marksExist() throws Exception {
|
|
setEppInput("domain_info_sunrise_include_marks.xml");
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.MARKS_EXIST);
|
|
doSuccessfulTest("domain_info_sunrise_response_with_mark.xml", HostsState.HOSTS_EXIST);
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_requestedIncludeMarks_noMarksExist() throws Exception {
|
|
setEppInput("domain_info_sunrise_include_marks.xml");
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
doSuccessfulTest("domain_info_sunrise_response.xml", HostsState.HOSTS_EXIST);
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_secDns() throws Exception {
|
|
persistTestEntities(HostsState.NO_HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
// Add the dsData to the saved resource and change the nameservers to match the sample xml.
|
|
persistResource(application.asBuilder()
|
|
.setDsData(ImmutableSet.of(DelegationSignerData.create(
|
|
12345, 3, 1, base16().decode("49FD46E6C4B45C55D4AC"))))
|
|
.setNameservers(ImmutableSet.of(
|
|
Key.create(host1), Key.create(host2)))
|
|
.build());
|
|
doSuccessfulTest("domain_info_sunrise_response_dsdata.xml", HostsState.NO_HOSTS_EXIST);
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_allocated() throws Exception {
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
// Update the application status of the saved resource.
|
|
persistResource(application.asBuilder()
|
|
.removeStatusValue(StatusValue.PENDING_CREATE)
|
|
.setApplicationStatus(ApplicationStatus.ALLOCATED)
|
|
.build());
|
|
doSuccessfulTest("domain_info_sunrise_allocated.xml", HostsState.HOSTS_EXIST);
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_neverExisted() throws Exception {
|
|
thrown.expect(
|
|
ResourceDoesNotExistException.class,
|
|
String.format("(%s)", getUniqueIdFromCommand()));
|
|
runFlow();
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_existedButWasDeleted() throws Exception {
|
|
persistResource(new DomainApplication.Builder()
|
|
.setRepoId("123-COM")
|
|
.setFullyQualifiedDomainName("timber.com")
|
|
.setDeletionTime(clock.nowUtc().minusDays(1))
|
|
.setRegistrant(Key.create(persistActiveContact("jd1234")))
|
|
.build());
|
|
thrown.expect(
|
|
ResourceDoesNotExistException.class,
|
|
String.format("(%s)", getUniqueIdFromCommand()));
|
|
runFlow();
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_unauthorized() throws Exception {
|
|
persistResource(
|
|
AppEngineRule.makeRegistrar1().asBuilder().setClientId("ClientZ").build());
|
|
sessionMetadata.setClientId("ClientZ");
|
|
persistTestEntities(HostsState.NO_HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
thrown.expect(ResourceNotOwnedException.class);
|
|
runFlow();
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_applicationIdForDifferentDomain() throws Exception {
|
|
persistResource(new DomainApplication.Builder()
|
|
.setRepoId("123-TLD")
|
|
.setFullyQualifiedDomainName("invalid.tld")
|
|
.setRegistrant(Key.create(persistActiveContact("jd1234")))
|
|
.setPhase(LaunchPhase.SUNRUSH)
|
|
.build());
|
|
thrown.expect(ApplicationDomainNameMismatchException.class);
|
|
runFlow();
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_noApplicationId() throws Exception {
|
|
setEppInput("domain_info_sunrise_no_application_id.xml");
|
|
persistTestEntities(HostsState.NO_HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
thrown.expect(MissingApplicationIdException.class);
|
|
runFlow();
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_mismatchedLaunchPhase() throws Exception {
|
|
persistTestEntities(HostsState.NO_HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
application = persistResource(
|
|
application.asBuilder().setPhase(LaunchPhase.SUNRISE).build());
|
|
thrown.expect(ApplicationLaunchPhaseMismatchException.class);
|
|
runFlow();
|
|
}
|
|
|
|
@Test
|
|
public void testLoadsComeFromMemcache() throws Exception {
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
setMemcacheContents(
|
|
Key.create(application),
|
|
DomainApplicationIndex.createKey(application),
|
|
Key.create(registrant),
|
|
Key.create(contact),
|
|
Key.create(host1),
|
|
Key.create(host2));
|
|
int numPreviousReads = RequestCapturingAsyncDatastoreService.getReads().size();
|
|
doSuccessfulTest("domain_info_sunrise_response.xml", HostsState.HOSTS_EXIST);
|
|
|
|
// Everything should be in memcache so nothing should hit datastore.
|
|
int numReadsInFlow = RequestCapturingAsyncDatastoreService.getReads().size() - numPreviousReads;
|
|
// TODO(b/27424173): This is 1 because there is no @Cache annotation on DomainBase, and we
|
|
// don't want to blindly add it because that's a production change that adds potentially
|
|
// dangerous caching. When the recommendations from the audit in b/27424173 are done and we've
|
|
// tested the new safer caching this should be set to 0.
|
|
assertThat(numReadsInFlow).isEqualTo(1);
|
|
}
|
|
|
|
/** Test that we load contacts and hosts as a batch rather than individually. */
|
|
@Test
|
|
public void testBatchLoadingOfReferences() throws Exception {
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
// Clear out memcache and session cache so that we count actual Datastore calls.
|
|
ofy().clearSessionCache();
|
|
clearMemcache();
|
|
int numPreviousReads = RequestCapturingAsyncDatastoreService.getReads().size();
|
|
doSuccessfulTest("domain_info_sunrise_response.xml", HostsState.HOSTS_EXIST);
|
|
// Get all of the keys loaded in the flow, with each distinct load() call as a list of keys.
|
|
int numReadsWithContactsOrHosts = FluentIterable
|
|
.from(RequestCapturingAsyncDatastoreService.getReads())
|
|
.skip(numPreviousReads)
|
|
.filter(
|
|
new Predicate<List<com.google.appengine.api.datastore.Key>>() {
|
|
@Override
|
|
public boolean apply(List<com.google.appengine.api.datastore.Key> keys) {
|
|
return FluentIterable.from(keys)
|
|
.transform(KEY_TO_KIND_FUNCTION)
|
|
.anyMatch(Predicates.or(
|
|
equalTo(Key.getKind(ContactResource.class)),
|
|
equalTo(Key.getKind(HostResource.class))));
|
|
}})
|
|
.size();
|
|
assertThat(numReadsWithContactsOrHosts).isEqualTo(1);
|
|
}
|
|
|
|
@Test
|
|
public void testIcannActivityReportField_getsLogged() throws Exception {
|
|
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
|
|
runFlow();
|
|
assertIcannReportingActivityFieldLogged("srs-dom-info");
|
|
assertTldsFieldLogged("tld");
|
|
}
|
|
}
|