feat(cognito): support AdminCreateUser MessageAction=RESEND (#755)

- Parse MessageAction in CognitoJsonHandler#handleAdminCreateUser.
- Add a new 5-arg CognitoService#adminCreateUser overload that accepts MessageAction.
- Keep the existing 4-arg adminCreateUser signature and delegate it to the new overload to preserve existing callers.
- Handle RESEND for FORCE_CHANGE_PASSWORD users by refreshing lastModifiedDate and returning the existing user instead of throwing UsernameExistsException.
- Treat RESEND as a no-op invitation refresh because Floci does not have email transport.
- Keep RESEND behavior consistent with other notification-bearing APIs in the emulator.
- Throw UserNotFoundException when RESEND is requested for a missing user.
- Throw UnsupportedUserStateException when RESEND is requested for a CONFIRMED user.
- Align RESEND error behavior with real AWS Cognito.
- Add 3 unit tests in CognitoServiceTest.
- Add 1 wire-level test in CognitoIntegrationTest.
This commit is contained in:
Sai Manohar Veerubhotla authored and GitHub committed 2026-05-10 10:21:40 -05:00
1 parent 5befe8b619
commit bb810a1c64
4 files changed
+114 -4

No files matched your search

@@ -5,8 +5,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
@@ -15,7 +13,6 @@ import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
@@ -1094,6 +1091,39 @@ class CognitoIntegrationTest {
.post("/cognito-idp/oauth2/token");
}
@Test
@Order(91)
void adminCreateUserWithMessageActionResendRefreshesExistingUser() throws Exception {
JsonNode poolResponse = cognitoJson("CreateUserPool", """
{ "PoolName": "ResendPool" }
""");
String resendPoolId = poolResponse.path("UserPool").path("Id").asText();
String resendUser = "resend-" + UUID.randomUUID();
cognitoAction("AdminCreateUser", """
{
"UserPoolId": "%s",
"Username": "%s",
"TemporaryPassword": "TempPass1!",
"UserAttributes": [ { "Name": "email", "Value": "resend@example.com" } ]
}
""".formatted(resendPoolId, resendUser))
.then()
.statusCode(200);
JsonNode resent = cognitoJson("AdminCreateUser", """
{
"UserPoolId": "%s",
"Username": "%s",
"MessageAction": "RESEND",
"UserAttributes": [ { "Name": "email", "Value": "resend@example.com" } ]
}
""".formatted(resendPoolId, resendUser));
assertEquals("FORCE_CHANGE_PASSWORD",
resent.path("User").path("UserStatus").asText());
}
private static Response cognitoAction(String action, String body) {
return given()
.header("X-Amz-Target", "AWSCognitoIdentityProviderService." + action)
@@ -524,6 +524,50 @@ class CognitoServiceTest {
"adminCreateUser should not overwrite an explicitly provided sub");
}
@Test
void adminCreateUserResendRefreshesExistingForceChangePasswordUser() {
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
CognitoUser created = service.adminCreateUser(pool.getId(),
"alice",
Map.of("email", "alice@example.com"),
"TempPass1!");
// Backdate lastModifiedDate so RESEND's refresh is unambiguously observable
// without relying on wall-clock sleep (lastModifiedDate has 1s precision).
long backdated = (System.currentTimeMillis() / 1000L) - 60;
created.setLastModifiedDate(backdated);
CognitoUser resent = service.adminCreateUser(pool.getId(), "alice",
Map.of("email", "alice@example.com"), null, "RESEND");
assertEquals(created.getAttributes().get("sub"), resent.getAttributes().get("sub"),
"RESEND must not recreate the user");
assertEquals("FORCE_CHANGE_PASSWORD", resent.getUserStatus());
assertTrue(resent.getLastModifiedDate() > backdated,
"RESEND should refresh lastModifiedDate");
}
@Test
void adminCreateUserResendThrowsUserNotFoundForMissingUser() {
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
AwsException ex = assertThrows(AwsException.class, () ->
service.adminCreateUser(pool.getId(), "ghost",
Map.of("email", "g@example.com"), null, "RESEND"));
assertEquals("UserNotFoundException", ex.getErrorCode());
}
@Test
void adminCreateUserResendThrowsUnsupportedStateForConfirmedUser() {
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
service.adminCreateUser(pool.getId(), "bob", Map.of("email", "bob@example.com"), "TempPass1!");
service.adminSetUserPassword(pool.getId(), "bob", "Permanent1!", true);
AwsException ex = assertThrows(AwsException.class, () ->
service.adminCreateUser(pool.getId(), "bob",
Map.of("email", "bob@example.com"), null, "RESEND"));
assertEquals("UnsupportedUserStateException", ex.getErrorCode());
}
@Test
void signUpAutoGeneratesSub() {
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");