1
0
mirror of https://github.com/google/nomulus synced 2026-01-07 14:05:44 +00:00

Add a GetUserCommand (#2435)

This is fairly simple, similar to most of the other Get*Command classes
This commit is contained in:
gbrodman
2024-05-10 17:25:52 -04:00
committed by GitHub
parent ae14e35df7
commit fd21fcdb84
3 changed files with 178 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
// Copyright 2024 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.tools;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import google.registry.model.console.User;
import google.registry.model.console.UserDao;
import java.util.List;
/** Command to display one or more users. */
@Parameters(separators = " =", commandDescription = "Show user(s)")
public class GetUserCommand implements Command {
@Parameter(description = "Email address(es) of the user(s) to display", required = true)
private List<String> mainParameters;
@Override
public void run() throws Exception {
for (String emailAddress : mainParameters) {
System.out.println(
UserDao.loadUser(emailAddress)
.map(User::toString)
.orElse(String.format("No user with email address %s", emailAddress)));
}
}
}

View File

@@ -80,6 +80,7 @@ public final class RegistryTool {
.put("get_routing_map", GetRoutingMapCommand.class) .put("get_routing_map", GetRoutingMapCommand.class)
.put("get_sql_credential", GetSqlCredentialCommand.class) .put("get_sql_credential", GetSqlCredentialCommand.class)
.put("get_tld", GetTldCommand.class) .put("get_tld", GetTldCommand.class)
.put("get_user", GetUserCommand.class)
.put("ghostryde", GhostrydeCommand.class) .put("ghostryde", GhostrydeCommand.class)
.put("hash_certificate", HashCertificateCommand.class) .put("hash_certificate", HashCertificateCommand.class)
.put("list_cursors", ListCursorsCommand.class) .put("list_cursors", ListCursorsCommand.class)

View File

@@ -0,0 +1,138 @@
// Copyright 2024 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.tools;
import com.google.common.collect.ImmutableMap;
import google.registry.model.console.GlobalRole;
import google.registry.model.console.RegistrarRole;
import google.registry.model.console.User;
import google.registry.model.console.UserDao;
import google.registry.model.console.UserRoles;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** Tests for {@link GetUserCommand}. */
public class GetUserCommandTest extends CommandTestCase<GetUserCommand> {
@BeforeEach
void beforeEach() {
UserDao.saveUser(
new User.Builder()
.setEmailAddress("johndoe@theregistrar.com")
.setUserRoles(
new UserRoles.Builder()
.setRegistrarRoles(
ImmutableMap.of("TheRegistrar", RegistrarRole.PRIMARY_CONTACT))
.build())
.build());
UserDao.saveUser(
new User.Builder()
.setEmailAddress("fte@google.com")
.setUserRoles(
new UserRoles.Builder().setIsAdmin(true).setGlobalRole(GlobalRole.FTE).build())
.build());
}
@Test
void testSuccess() throws Exception {
runCommand("fte@google.com");
assertStdoutIs(
"""
User: {
emailAddress=fte@google.com
id=2
registryLockEmailAddress=null
registryLockPasswordHash=null
registryLockPasswordSalt=null
updateTimestamp=UpdateAutoTimestamp: {
lastUpdateTime=2022-09-01T00:00:00.000Z
}
userRoles=UserRoles: {
globalRole=FTE
isAdmin=true
registrarRoles={}
}
}
""");
}
@Test
void testSuccess_multipleUsers() throws Exception {
runCommand("johndoe@theregistrar.com", "fte@google.com");
assertStdoutIs(
"""
User: {
emailAddress=johndoe@theregistrar.com
id=1
registryLockEmailAddress=null
registryLockPasswordHash=null
registryLockPasswordSalt=null
updateTimestamp=UpdateAutoTimestamp: {
lastUpdateTime=2022-09-01T00:00:00.000Z
}
userRoles=UserRoles: {
globalRole=NONE
isAdmin=false
registrarRoles={TheRegistrar=PRIMARY_CONTACT}
}
}
User: {
emailAddress=fte@google.com
id=2
registryLockEmailAddress=null
registryLockPasswordHash=null
registryLockPasswordSalt=null
updateTimestamp=UpdateAutoTimestamp: {
lastUpdateTime=2022-09-01T00:00:00.000Z
}
userRoles=UserRoles: {
globalRole=FTE
isAdmin=true
registrarRoles={}
}
}
""");
}
@Test
void testPartialSuccess_partiallyUnknown() throws Exception {
runCommand("johndoe@theregistrar.com", "asdf@asdf.com");
assertStdoutIs(
"""
User: {
emailAddress=johndoe@theregistrar.com
id=1
registryLockEmailAddress=null
registryLockPasswordHash=null
registryLockPasswordSalt=null
updateTimestamp=UpdateAutoTimestamp: {
lastUpdateTime=2022-09-01T00:00:00.000Z
}
userRoles=UserRoles: {
globalRole=NONE
isAdmin=false
registrarRoles={TheRegistrar=PRIMARY_CONTACT}
}
}
No user with email address asdf@asdf.com
""");
}
@Test
void testFailure_unknownUser() throws Exception {
runCommand("asdf@asdf.com");
assertStdoutIs("No user with email address asdf@asdf.com\n");
}
}