mirror of
https://github.com/google/nomulus.git
synced 2025-07-22 18:55:58 +02:00
Centralize RDAP test initialization and login
All RDAP actions inherit from a common RdapActionBase class. All RDAP search actions inherit from a comman RdapSearchActionBase class, which inherits from RdapActionBase Each of the base classes has @Before initialization needed for the tests, as well as utility functions (such as login and logout). Currently, these were copied in all test classes. Instead, we created a similar test inheritance tree to centralize the initialization and place common utility functions. This way, the @Before of every test only needs to initialize the variables new to the specific action, making the code somewhat clearer. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=217856844
This commit is contained in:
parent
b254269d2f
commit
3a3b0b738a
45 changed files with 456 additions and 758 deletions
|
@ -20,72 +20,36 @@ import static google.registry.testing.DatastoreHelper.persistResource;
|
|||
import static google.registry.testing.FullFieldsTestEntityHelper.makeAndPersistHostResource;
|
||||
import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrar;
|
||||
import static google.registry.testing.TestDataHelper.loadFile;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.appengine.api.users.User;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import google.registry.model.ofy.Ofy;
|
||||
import google.registry.model.registrar.Registrar;
|
||||
import google.registry.rdap.RdapMetrics.EndpointType;
|
||||
import google.registry.rdap.RdapMetrics.SearchType;
|
||||
import google.registry.rdap.RdapMetrics.WildcardType;
|
||||
import google.registry.rdap.RdapSearchResults.IncompletenessWarningType;
|
||||
import google.registry.request.Action;
|
||||
import google.registry.request.HttpException.ForbiddenException;
|
||||
import google.registry.request.auth.AuthLevel;
|
||||
import google.registry.request.auth.AuthResult;
|
||||
import google.registry.request.auth.UserAuthInfo;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.FakeResponse;
|
||||
import google.registry.testing.InjectRule;
|
||||
import google.registry.ui.server.registrar.AuthenticatedRegistrarAccessor;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import javax.annotation.Nullable;
|
||||
import org.joda.time.DateTime;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link RdapNameserverAction}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class RdapNameserverActionTest {
|
||||
public class RdapNameserverActionTest extends RdapActionBaseTestCase<RdapNameserverAction> {
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine = AppEngineRule.builder()
|
||||
.withDatastore()
|
||||
.build();
|
||||
|
||||
@Rule
|
||||
public final InjectRule inject = new InjectRule();
|
||||
|
||||
private final FakeResponse response = new FakeResponse();
|
||||
private final FakeClock clock = new FakeClock(DateTime.parse("2000-01-01TZ"));
|
||||
private final AuthenticatedRegistrarAccessor registrarAccessor =
|
||||
mock(AuthenticatedRegistrarAccessor.class);
|
||||
private final RdapMetrics rdapMetrics = mock(RdapMetrics.class);
|
||||
|
||||
private static final AuthResult AUTH_RESULT =
|
||||
AuthResult.create(
|
||||
AuthLevel.USER,
|
||||
UserAuthInfo.create(new User("rdap.user@user.com", "gmail.com", "12345"), false));
|
||||
|
||||
private static final AuthResult AUTH_RESULT_ADMIN =
|
||||
AuthResult.create(
|
||||
AuthLevel.USER,
|
||||
UserAuthInfo.create(new User("rdap.user@google.com", "gmail.com", "12345"), true));
|
||||
public RdapNameserverActionTest() {
|
||||
super(RdapNameserverAction.class, RdapNameserverAction.PATH);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
inject.setStaticField(Ofy.class, "clock", clock);
|
||||
// normal
|
||||
createTld("lol");
|
||||
makeAndPersistHostResource(
|
||||
|
@ -111,43 +75,6 @@ public class RdapNameserverActionTest {
|
|||
makeAndPersistHostResource("ns1.domain.external", "9.10.11.12", clock.nowUtc().minusYears(1));
|
||||
}
|
||||
|
||||
private RdapNameserverAction newRdapNameserverAction(
|
||||
String input, Optional<String> desiredRegistrar, Optional<Boolean> includeDeleted) {
|
||||
return newRdapNameserverAction(input, desiredRegistrar, includeDeleted, AUTH_RESULT);
|
||||
}
|
||||
|
||||
private RdapNameserverAction newRdapNameserverAction(
|
||||
String input,
|
||||
Optional<String> desiredRegistrar,
|
||||
Optional<Boolean> includeDeleted,
|
||||
AuthResult authResult) {
|
||||
RdapNameserverAction action = new RdapNameserverAction();
|
||||
action.clock = clock;
|
||||
action.requestMethod = Action.Method.GET;
|
||||
action.fullServletPath = "https://example.tld/rdap";
|
||||
action.response = response;
|
||||
action.requestPath = RdapNameserverAction.PATH.concat(input);
|
||||
action.registrarParam = desiredRegistrar;
|
||||
action.includeDeletedParam = includeDeleted;
|
||||
action.formatOutputParam = Optional.empty();
|
||||
action.rdapJsonFormatter = RdapTestHelper.getTestRdapJsonFormatter();
|
||||
action.rdapWhoisServer = null;
|
||||
action.authResult = authResult;
|
||||
action.registrarAccessor = registrarAccessor;
|
||||
action.rdapMetrics = rdapMetrics;
|
||||
return action;
|
||||
}
|
||||
|
||||
private Object generateActualJson(String name) {
|
||||
return generateActualJson(name, Optional.empty(), Optional.empty());
|
||||
}
|
||||
|
||||
private Object generateActualJson(
|
||||
String name, Optional<String> desiredRegistrar, Optional<Boolean> includeDeleted) {
|
||||
newRdapNameserverAction(name, desiredRegistrar, includeDeleted).run();
|
||||
return JSONValue.parse(response.getPayload());
|
||||
}
|
||||
|
||||
private Object generateExpectedJson(
|
||||
String name,
|
||||
@Nullable ImmutableMap<String, String> otherSubstitutions,
|
||||
|
@ -328,9 +255,8 @@ public class RdapNameserverActionTest {
|
|||
|
||||
@Test
|
||||
public void testNameserver_found_sameRegistrarRequested() {
|
||||
assertThat(
|
||||
generateActualJson(
|
||||
"ns1.cat.lol", Optional.of("TheRegistrar"), Optional.empty()))
|
||||
action.registrarParam = Optional.of("TheRegistrar");
|
||||
assertThat(generateActualJson("ns1.cat.lol"))
|
||||
.isEqualTo(
|
||||
generateExpectedJsonWithTopLevelEntries(
|
||||
"ns1.cat.lol",
|
||||
|
@ -345,7 +271,9 @@ public class RdapNameserverActionTest {
|
|||
|
||||
@Test
|
||||
public void testNameserver_notFound_differentRegistrarRequested() {
|
||||
generateActualJson("ns1.cat.lol", Optional.of("otherregistrar"), Optional.of(false));
|
||||
action.registrarParam = Optional.of("otherregistrar");
|
||||
action.includeDeletedParam = Optional.of(false);
|
||||
generateActualJson("ns1.cat.lol");
|
||||
assertThat(response.getStatus()).isEqualTo(404);
|
||||
}
|
||||
|
||||
|
@ -357,29 +285,32 @@ public class RdapNameserverActionTest {
|
|||
|
||||
@Test
|
||||
public void testDeletedNameserver_notFound_includeDeletedSetFalse() {
|
||||
generateActualJson("nsdeleted.cat.lol", Optional.empty(), Optional.of(false));
|
||||
action.includeDeletedParam = Optional.of(false);
|
||||
generateActualJson("nsdeleted.cat.lol");
|
||||
assertThat(response.getStatus()).isEqualTo(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletedNameserver_notFound_notLoggedIn() {
|
||||
when(registrarAccessor.guessClientId()).thenThrow(new ForbiddenException("blah"));
|
||||
generateActualJson("nsdeleted.cat.lol", Optional.empty(), Optional.of(true));
|
||||
logout();
|
||||
action.includeDeletedParam = Optional.of(true);
|
||||
generateActualJson("nsdeleted.cat.lol");
|
||||
assertThat(response.getStatus()).isEqualTo(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletedNameserver_notFound_loggedInAsDifferentRegistrar() {
|
||||
when(registrarAccessor.guessClientId()).thenReturn("otherregistrar");
|
||||
generateActualJson("nsdeleted.cat.lol", Optional.empty(), Optional.of(true));
|
||||
login("otherregistrar");
|
||||
action.includeDeletedParam = Optional.of(true);
|
||||
generateActualJson("nsdeleted.cat.lol");
|
||||
assertThat(response.getStatus()).isEqualTo(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletedNameserver_found_loggedInAsCorrectRegistrar() {
|
||||
when(registrarAccessor.guessClientId()).thenReturn("TheRegistrar");
|
||||
assertThat(
|
||||
generateActualJson("nsdeleted.cat.lol", Optional.empty(), Optional.of(true)))
|
||||
login("TheRegistrar");
|
||||
action.includeDeletedParam = Optional.of(true);
|
||||
assertThat(generateActualJson("nsdeleted.cat.lol"))
|
||||
.isEqualTo(
|
||||
generateExpectedJsonWithTopLevelEntries(
|
||||
"nsdeleted.cat.lol",
|
||||
|
@ -394,14 +325,9 @@ public class RdapNameserverActionTest {
|
|||
|
||||
@Test
|
||||
public void testDeletedNameserver_found_loggedInAsAdmin() {
|
||||
when(registrarAccessor.guessClientId()).thenReturn("irrelevant");
|
||||
newRdapNameserverAction(
|
||||
"nsdeleted.cat.lol",
|
||||
Optional.empty(),
|
||||
Optional.of(true),
|
||||
AUTH_RESULT_ADMIN)
|
||||
.run();
|
||||
assertThat(JSONValue.parse(response.getPayload()))
|
||||
loginAsAdmin();
|
||||
action.includeDeletedParam = Optional.of(true);
|
||||
assertThat(generateActualJson("nsdeleted.cat.lol"))
|
||||
.isEqualTo(
|
||||
generateExpectedJsonWithTopLevelEntries(
|
||||
"nsdeleted.cat.lol",
|
||||
|
@ -416,9 +342,10 @@ public class RdapNameserverActionTest {
|
|||
|
||||
@Test
|
||||
public void testDeletedNameserver_found_sameRegistrarRequested() {
|
||||
when(registrarAccessor.guessClientId()).thenReturn("TheRegistrar");
|
||||
assertThat(
|
||||
generateActualJson("nsdeleted.cat.lol", Optional.of("TheRegistrar"), Optional.of(true)))
|
||||
login("TheRegistrar");
|
||||
action.registrarParam = Optional.of("TheRegistrar");
|
||||
action.includeDeletedParam = Optional.of(true);
|
||||
assertThat(generateActualJson("nsdeleted.cat.lol"))
|
||||
.isEqualTo(
|
||||
generateExpectedJsonWithTopLevelEntries(
|
||||
"nsdeleted.cat.lol",
|
||||
|
@ -433,8 +360,10 @@ public class RdapNameserverActionTest {
|
|||
|
||||
@Test
|
||||
public void testDeletedNameserver_notFound_differentRegistrarRequested() {
|
||||
when(registrarAccessor.guessClientId()).thenReturn("TheRegistrar");
|
||||
generateActualJson("ns1.cat.lol", Optional.of("otherregistrar"), Optional.of(false));
|
||||
login("TheRegistrar");
|
||||
action.registrarParam = Optional.of("otherregistrar");
|
||||
action.includeDeletedParam = Optional.of(false);
|
||||
generateActualJson("ns1.cat.lol");
|
||||
assertThat(response.getStatus()).isEqualTo(404);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue