Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2025 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.ui.server.console;

import static google.registry.testing.DatabaseHelper.createAdminUser;
import static google.registry.testing.DatabaseHelper.createTld;

import com.google.gson.Gson;
import google.registry.model.console.User;
import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.request.auth.AuthResult;
import google.registry.testing.ConsoleApiParamsUtils;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeResponse;
import google.registry.tools.GsonUtils;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;

public abstract class ConsoleActionBaseTestCase {

protected static final Gson GSON = GsonUtils.provideGson();

protected final FakeClock clock = new FakeClock(DateTime.parse("2024-04-15T00:00:00.000Z"));

@RegisterExtension
final JpaTestExtensions.JpaIntegrationTestExtension jpa =
new JpaTestExtensions.Builder().withClock(clock).buildIntegrationTestExtension();

protected ConsoleApiParams consoleApiParams;
protected FakeResponse response;
protected User fteUser;

@BeforeEach
void beforeEachBaseTestCase() {
createTld("tld");
fteUser = createAdminUser("fte@email.tld");
AuthResult authResult = AuthResult.createUser(fteUser);
consoleApiParams = ConsoleApiParamsUtils.createFake(authResult);
response = (FakeResponse) consoleApiParams.response();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package google.registry.ui.server.console;

import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatabaseHelper.createTld;
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import static jakarta.servlet.http.HttpServletResponse.SC_OK;
import static jakarta.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
Expand All @@ -25,28 +24,19 @@
import google.registry.model.console.RegistrarRole;
import google.registry.model.console.User;
import google.registry.model.console.UserRoles;
import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.request.Action;
import google.registry.request.auth.AuthResult;
import google.registry.testing.ConsoleApiParamsUtils;
import google.registry.testing.DatabaseHelper;
import google.registry.testing.FakeResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

/** Tests for {@link google.registry.ui.server.console.ConsoleDomainGetAction}. */
public class ConsoleDomainGetActionTest {

private ConsoleApiParams consoleApiParams;

@RegisterExtension
final JpaTestExtensions.JpaIntegrationTestExtension jpa =
new JpaTestExtensions.Builder().buildIntegrationTestExtension();
public class ConsoleDomainGetActionTest extends ConsoleActionBaseTestCase {

@BeforeEach
void beforeEach() {
createTld("tld");
DatabaseHelper.persistActiveDomain("exists.tld");
}

Expand All @@ -62,8 +52,8 @@ void testSuccess_fullJsonRepresentation() {
.build())),
"exists.tld");
action.run();
assertThat(((FakeResponse) consoleApiParams.response()).getStatus()).isEqualTo(SC_OK);
assertThat(((FakeResponse) consoleApiParams.response()).getPayload())
assertThat(response.getStatus()).isEqualTo(SC_OK);
assertThat(response.getPayload())
.isEqualTo(
"{\"domainName\":\"exists.tld\",\"adminContact\":{\"key\":\"3-ROID\",\"kind\":"
+ "\"google.registry.model.contact.Contact\"},\"techContact\":{\"key\":\"3-ROID\","
Expand All @@ -81,15 +71,15 @@ void testSuccess_fullJsonRepresentation() {
void testFailure_emptyAuth() {
ConsoleDomainGetAction action = createAction(AuthResult.NOT_AUTHENTICATED, "exists.tld");
action.run();
assertThat(((FakeResponse) consoleApiParams.response()).getStatus()).isEqualTo(SC_UNAUTHORIZED);
assertThat(response.getStatus()).isEqualTo(SC_UNAUTHORIZED);
}

@Test
void testFailure_appAuth() {
ConsoleDomainGetAction action =
createAction(AuthResult.createApp("service@registry.example"), "exists.tld");
action.run();
assertThat(((FakeResponse) consoleApiParams.response()).getStatus()).isEqualTo(SC_UNAUTHORIZED);
assertThat(response.getStatus()).isEqualTo(SC_UNAUTHORIZED);
}

@Test
Expand All @@ -98,17 +88,14 @@ void testFailure_noAccessToRegistrar() {
createAction(
AuthResult.createUser(createUser(new UserRoles.Builder().build())), "exists.tld");
action.run();
assertThat(((FakeResponse) consoleApiParams.response()).getStatus()).isEqualTo(SC_NOT_FOUND);
assertThat(response.getStatus()).isEqualTo(SC_NOT_FOUND);
}

@Test
void testFailure_nonexistentDomain() {
ConsoleDomainGetAction action =
createAction(
AuthResult.createUser(createUser(new UserRoles.Builder().setIsAdmin(true).build())),
"nonexistent.tld");
ConsoleDomainGetAction action = createAction(AuthResult.createUser(fteUser), "nonexistent.tld");
action.run();
assertThat(((FakeResponse) consoleApiParams.response()).getStatus()).isEqualTo(SC_NOT_FOUND);
assertThat(response.getStatus()).isEqualTo(SC_NOT_FOUND);
}

private User createUser(UserRoles userRoles) {
Expand All @@ -120,6 +107,7 @@ private User createUser(UserRoles userRoles) {

private ConsoleDomainGetAction createAction(AuthResult authResult, String domain) {
consoleApiParams = ConsoleApiParamsUtils.createFake(authResult);
response = (FakeResponse) consoleApiParams.response();
when(consoleApiParams.request().getMethod()).thenReturn(Action.Method.GET.toString());
return new ConsoleDomainGetAction(consoleApiParams, domain);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,31 @@

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatabaseHelper.createAdminUser;
import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.persistActiveDomain;
import static google.registry.testing.DatabaseHelper.persistDomainAsDeleted;
import static jakarta.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static org.mockito.Mockito.when;

import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import google.registry.model.EppResourceUtils;
import google.registry.model.domain.Domain;
import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.request.Action;
import google.registry.request.auth.AuthResult;
import google.registry.testing.ConsoleApiParamsUtils;
import google.registry.testing.DatabaseHelper;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeResponse;
import google.registry.tools.GsonUtils;
import google.registry.ui.server.console.ConsoleDomainListAction.DomainListResult;
import java.util.Optional;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

/** Tests for {@link ConsoleDomainListAction}. */
public class ConsoleDomainListActionTest {

private static final Gson GSON = GsonUtils.provideGson();

private final FakeClock clock = new FakeClock(DateTime.parse("2023-10-20T00:00:00.000Z"));

private ConsoleApiParams consoleApiParams;

@RegisterExtension
final JpaTestExtensions.JpaIntegrationTestExtension jpa =
new JpaTestExtensions.Builder().withClock(clock).buildIntegrationTestExtension();
public class ConsoleDomainListActionTest extends ConsoleActionBaseTestCase {

@BeforeEach
void beforeEach() {
createTld("tld");
for (int i = 0; i < 10; i++) {
DatabaseHelper.persistActiveDomain(i + "exists.tld", clock.nowUtc());
clock.advanceOneMilli();
Expand All @@ -70,9 +52,7 @@ void beforeEach() {
void testSuccess_allDomains() {
ConsoleDomainListAction action = createAction("TheRegistrar");
action.run();
DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains).hasSize(10);
assertThat(result.totalResults).isEqualTo(10);
assertThat(result.checkpointTime).isEqualTo(clock.nowUtc());
Expand All @@ -84,9 +64,7 @@ void testSuccess_allDomains() {
void testSuccess_noDomains() {
ConsoleDomainListAction action = createAction("NewRegistrar");
action.run();
DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains).hasSize(0);
assertThat(result.totalResults).isEqualTo(0);
assertThat(result.checkpointTime).isEqualTo(clock.nowUtc());
Expand All @@ -97,19 +75,15 @@ void testSuccess_pages() {
// Two pages of results should go in reverse chronological order
ConsoleDomainListAction action = createAction("TheRegistrar", null, 0, 5, null, null);
action.run();
DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains.stream().map(Domain::getDomainName).collect(toImmutableList()))
.containsExactly("9exists.tld", "8exists.tld", "7exists.tld", "6exists.tld", "5exists.tld");
assertThat(result.totalResults).isEqualTo(10);

// Now do the second page
action = createAction("TheRegistrar", result.checkpointTime, 1, 5, 10L, null);
action.run();
result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains.stream().map(Domain::getDomainName).collect(toImmutableList()))
.containsExactly("4exists.tld", "3exists.tld", "2exists.tld", "1exists.tld", "0exists.tld");
}
Expand All @@ -118,9 +92,7 @@ void testSuccess_pages() {
void testSuccess_partialPage() {
ConsoleDomainListAction action = createAction("TheRegistrar", null, 1, 8, null, null);
action.run();
DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains.stream().map(Domain::getDomainName).collect(toImmutableList()))
.containsExactly("1exists.tld", "0exists.tld");
}
Expand All @@ -130,9 +102,7 @@ void testSuccess_checkpointTime_createdBefore() {
ConsoleDomainListAction action = createAction("TheRegistrar", null, 0, 10, null, null);
action.run();

DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains).hasSize(10);
assertThat(result.totalResults).isEqualTo(10);

Expand All @@ -142,9 +112,7 @@ void testSuccess_checkpointTime_createdBefore() {
// Even though we persisted a new domain, the old checkpoint should return no more results
action = createAction("TheRegistrar", result.checkpointTime, 1, 10, null, null);
action.run();
result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains).isEmpty();
assertThat(result.totalResults).isEqualTo(10);
}
Expand All @@ -153,9 +121,7 @@ void testSuccess_checkpointTime_createdBefore() {
void testSuccess_checkpointTime_deletion() {
ConsoleDomainListAction action = createAction("TheRegistrar", null, 0, 5, null, null);
action.run();
DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);

clock.advanceOneMilli();
Domain toDelete =
Expand All @@ -165,9 +131,7 @@ void testSuccess_checkpointTime_deletion() {
// Second page should include the domain that is now deleted due to the checkpoint time
action = createAction("TheRegistrar", result.checkpointTime, 1, 5, null, null);
action.run();
result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains.stream().map(Domain::getDomainName).collect(toImmutableList()))
.containsExactly("4exists.tld", "3exists.tld", "2exists.tld", "1exists.tld", "0exists.tld");
}
Expand All @@ -176,29 +140,23 @@ void testSuccess_checkpointTime_deletion() {
void testSuccess_searchTerm_oneMatch() {
ConsoleDomainListAction action = createAction("TheRegistrar", null, 0, 5, null, "0");
action.run();
DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(Iterables.getOnlyElement(result.domains).getDomainName()).isEqualTo("0exists.tld");
}

@Test
void testSuccess_searchTerm_returnsNone() {
ConsoleDomainListAction action = createAction("TheRegistrar", null, 0, 5, null, "deleted");
action.run();
DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains).isEmpty();
}

@Test
void testSuccess_searchTerm_caseInsensitive() {
ConsoleDomainListAction action = createAction("TheRegistrar", null, 0, 5, null, "eXiStS");
action.run();
DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains).hasSize(5);
assertThat(result.totalResults).isEqualTo(10);
}
Expand All @@ -207,9 +165,7 @@ void testSuccess_searchTerm_caseInsensitive() {
void testSuccess_searchTerm_tld() {
ConsoleDomainListAction action = createAction("TheRegistrar", null, 0, 5, null, "tld");
action.run();
DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains).hasSize(5);
assertThat(result.totalResults).isEqualTo(10);
}
Expand All @@ -218,34 +174,31 @@ void testSuccess_searchTerm_tld() {
void testPartialSuccess_pastEnd() {
ConsoleDomainListAction action = createAction("TheRegistrar", null, 5, 5, null, null);
action.run();
DomainListResult result =
GSON.fromJson(
((FakeResponse) consoleApiParams.response()).getPayload(), DomainListResult.class);
DomainListResult result = GSON.fromJson(response.getPayload(), DomainListResult.class);
assertThat(result.domains).isEmpty();
}

@Test
void testFailure_invalidResultsPerPage() {
ConsoleDomainListAction action = createAction("TheRegistrar", null, 0, 0, null, null);
action.run();
assertThat(((FakeResponse) consoleApiParams.response()).getStatus()).isEqualTo(SC_BAD_REQUEST);
assertThat(((FakeResponse) consoleApiParams.response()).getPayload())
assertThat(response.getStatus()).isEqualTo(SC_BAD_REQUEST);
assertThat(response.getPayload())
.isEqualTo("Results per page must be between 1 and 500 inclusive");

action = createAction("TheRegistrar", null, 0, 501, null, null);
action.run();
assertThat(((FakeResponse) consoleApiParams.response()).getStatus()).isEqualTo(SC_BAD_REQUEST);
assertThat(((FakeResponse) consoleApiParams.response()).getPayload())
assertThat(response.getStatus()).isEqualTo(SC_BAD_REQUEST);
assertThat(response.getPayload())
.isEqualTo("Results per page must be between 1 and 500 inclusive");
}

@Test
void testFailure_invalidPageNumber() {
ConsoleDomainListAction action = createAction("TheRegistrar", null, -1, 10, null, null);
action.run();
assertThat(((FakeResponse) consoleApiParams.response()).getStatus()).isEqualTo(SC_BAD_REQUEST);
assertThat(((FakeResponse) consoleApiParams.response()).getPayload())
.isEqualTo("Page number must be non-negative");
assertThat(response.getStatus()).isEqualTo(SC_BAD_REQUEST);
assertThat(response.getPayload()).isEqualTo("Page number must be non-negative");
}

private ConsoleDomainListAction createAction(String registrarId) {
Expand All @@ -259,9 +212,10 @@ private ConsoleDomainListAction createAction(
@Nullable Integer resultsPerPage,
@Nullable Long totalResults,
@Nullable String searchTerm) {
AuthResult authResult = AuthResult.createUser(createAdminUser("email@email.example"));
AuthResult authResult = AuthResult.createUser(fteUser);
consoleApiParams = ConsoleApiParamsUtils.createFake(authResult);
when(consoleApiParams.request().getMethod()).thenReturn(Action.Method.GET.toString());
response = (FakeResponse) consoleApiParams.response();
return new ConsoleDomainListAction(
consoleApiParams,
registrarId,
Expand Down
Loading