Allow for removal of registry lock passwords in User objects (#2609)

This essentially enables the "forgot password" flow
This commit is contained in:
gbrodman
2024-11-14 21:01:17 +00:00
committed by GitHub
parent 78cc1b2937
commit e54075fea3
4 changed files with 107 additions and 5 deletions
@@ -91,10 +91,16 @@ public class CreateUserCommandTest extends CommandTestCase<CreateUserCommand> {
runCommandForced(
"--email",
"user@example.test",
"--registrar_roles",
"TheRegistrar=PRIMARY_CONTACT",
"--registry_lock_email_address",
"registrylockemail@otherexample.test");
assertThat(loadExistingUser("user@example.test").getRegistryLockEmailAddress())
.hasValue("registrylockemail@otherexample.test");
"registrylockemail@otherexample.test",
"--registry_lock_password",
"password");
User user = loadExistingUser("user@example.test");
assertThat(user.getRegistryLockEmailAddress()).hasValue("registrylockemail@otherexample.test");
assertThat(user.verifyRegistryLockPassword("password")).isTrue();
assertThat(user.verifyRegistryLockPassword("foobar")).isFalse();
}
@Test
@@ -174,4 +180,18 @@ public class CreateUserCommandTest extends CommandTestCase<CreateUserCommand> {
.hasMessageThat()
.isEqualTo("Provided email this is not valid is not a valid email address");
}
@Test
void testFailure_registryLockPassword_withoutEmail() {
assertThat(
assertThrows(
IllegalArgumentException.class,
() ->
runCommandForced(
"--email", "user@example.test", "--registry_lock_password", "password")))
.hasMessageThat()
.isEqualTo(
"Cannot set/remove registry lock password on a user without a registry lock email"
+ " address");
}
}
@@ -16,6 +16,7 @@ package google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatabaseHelper.loadExistingUser;
import static google.registry.testing.DatabaseHelper.persistResource;
import static google.registry.testing.DatabaseHelper.putInDb;
import static org.junit.Assert.assertThrows;
@@ -98,6 +99,44 @@ public class UpdateUserCommandTest extends CommandTestCase<UpdateUserCommand> {
.isEqualTo(GlobalRole.FTE);
}
@Test
void testSuccess_removePassword() throws Exception {
// Empty password value removes the password
persistResource(
loadExistingUser("user@example.test")
.asBuilder()
.setUserRoles(
new UserRoles.Builder()
.setRegistrarRoles(ImmutableMap.of("TheRegistrar", RegistrarRole.TECH_CONTACT))
.build())
.setRegistryLockEmailAddress("registrylock@example.test")
.setRegistryLockPassword("password")
.build());
assertThat(loadExistingUser("user@example.test").hasRegistryLockPassword()).isTrue();
runCommandForced("--email", "user@example.test", "--registry_lock_password", "");
assertThat(loadExistingUser("user@example.test").hasRegistryLockPassword()).isFalse();
}
@Test
void testSuccess_setsPassword() throws Exception {
persistResource(
loadExistingUser("user@example.test")
.asBuilder()
.setUserRoles(
new UserRoles.Builder()
.setRegistrarRoles(ImmutableMap.of("TheRegistrar", RegistrarRole.TECH_CONTACT))
.build())
.setRegistryLockEmailAddress("registrylock@example.test")
.setRegistryLockPassword("password")
.build());
assertThat(loadExistingUser("user@example.test").verifyRegistryLockPassword("password"))
.isTrue();
runCommandForced("--email", "user@example.test", "--registry_lock_password", "foobar");
assertThat(loadExistingUser("user@example.test").verifyRegistryLockPassword("password"))
.isFalse();
assertThat(loadExistingUser("user@example.test").verifyRegistryLockPassword("foobar")).isTrue();
}
@Test
void testFailure_doesntExist() {
assertThat(
@@ -122,4 +161,18 @@ public class UpdateUserCommandTest extends CommandTestCase<UpdateUserCommand> {
.hasMessageThat()
.isEqualTo("Provided email this is not valid is not a valid email address");
}
@Test
void testFailure_setPassword_noEmail() {
assertThat(
assertThrows(
IllegalArgumentException.class,
() ->
runCommandForced(
"--email", "user@example.test", "--registry_lock_password", "foobar")))
.hasMessageThat()
.isEqualTo(
"Cannot set/remove registry lock password on a user without a registry lock email"
+ " address");
}
}