fix(cognito): enrich User Pool responses and implement MfaConfig stub (#198)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-03 18:21:29 -05:00
1 parent 9d6181c547
commit 441d9f179e
5 files changed
+337 -21

No files matched your search

@@ -0,0 +1,70 @@
package io.github.hectorvent.floci.services.cognito;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.cognito.model.UserPool;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
class CognitoJsonHandlerTest {
private CognitoJsonHandler handler;
private ObjectMapper mapper = new ObjectMapper();
@BeforeEach
void setUp() {
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
CognitoService service = new CognitoService(
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new InMemoryStorage<>(),
"http://localhost:4566",
regionResolver
);
handler = new CognitoJsonHandler(service, mapper);
}
@Test
void createUserPoolReturnsRichResponse() {
ObjectNode request = mapper.createObjectNode();
request.put("PoolName", "test-pool");
ArrayNode schema = request.putArray("Schema");
ObjectNode attr = schema.addObject();
attr.put("Name", "email");
attr.put("AttributeDataType", "String");
Response response = handler.handle("CreateUserPool", request, "us-east-1");
assertEquals(200, response.getStatus());
JsonNode body = (JsonNode) response.getEntity();
JsonNode pool = body.get("UserPool");
assertNotNull(pool.get("Id"));
assertEquals("test-pool", pool.get("Name").asText());
assertTrue(pool.get("Arn").asText().contains("arn:aws:cognito-idp:us-east-1:000000000000:userpool/"));
assertEquals("Enabled", pool.get("Status").asText());
// Check mandatory blocks for Terraform
assertNotNull(pool.get("SchemaAttributes"));
assertEquals(1, pool.get("SchemaAttributes").size());
assertEquals("email", pool.get("SchemaAttributes").get(0).get("Name").asText());
assertNotNull(pool.get("Policies"));
assertNotNull(pool.get("LambdaConfig"));
assertNotNull(pool.get("AdminCreateUserConfig"));
assertNotNull(pool.get("AccountRecoverySetting"));
assertEquals("ESSENTIALS", pool.get("UserPoolTier").asText());
}
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.cognito;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.cognito.model.CognitoGroup;
import io.github.hectorvent.floci.services.cognito.model.CognitoUser;
@@ -11,6 +12,7 @@ import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@@ -21,34 +23,62 @@ class CognitoServiceTest {
private CognitoService service;
private InMemoryStorage<String, CognitoGroup> groupStore;
private RegionResolver regionResolver;
@BeforeEach
void setUp() {
groupStore = new InMemoryStorage<>();
regionResolver = new RegionResolver("us-east-1", "000000000000");
service = new CognitoService(
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new InMemoryStorage<>(),
groupStore,
"http://localhost:4566"
"http://localhost:4566",
regionResolver
);
}
private UserPool createPoolAndUser() {
UserPool pool = service.createUserPool("TestPool", "us-east-1");
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
service.adminCreateUser(pool.getId(), "alice", Map.of("email", "alice@example.com"), "TempPass1!");
service.adminSetUserPassword(pool.getId(), "alice", "Perm1234!", true);
return pool;
}
@Test
void createUserPoolWithFullConfig() {
List<Map<String, Object>> schema = List.of(
Map.of("Name", "my-attr", "AttributeDataType", "String")
);
Map<String, Object> policies = Map.of(
"PasswordPolicy", Map.of("MinimumLength", 12)
);
Map<String, Object> request = new HashMap<>();
request.put("PoolName", "FullConfigPool");
request.put("Schema", schema);
request.put("Policies", policies);
request.put("UsernameAttributes", List.of("email"));
UserPool pool = service.createUserPool(request, "us-east-1");
assertNotNull(pool.getId());
assertEquals("FullConfigPool", pool.getName());
assertEquals("arn:aws:cognito-idp:us-east-1:000000000000:userpool/" + pool.getId(), pool.getArn());
assertEquals(schema, pool.getSchemaAttributes());
assertEquals(policies, pool.getPolicies());
assertEquals(List.of("email"), pool.getUsernameAttributes());
}
// =========================================================================
// Groups
// =========================================================================
@Test
void createGroup() {
UserPool pool = service.createUserPool("TestPool", "us-east-1");
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
CognitoGroup group = service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
assertEquals("admins", group.getGroupName());
@@ -62,7 +92,7 @@ class CognitoServiceTest {
@Test
void createGroupDuplicateThrows() {
UserPool pool = service.createUserPool("TestPool", "us-east-1");
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
assertThrows(AwsException.class, () ->
@@ -71,7 +101,7 @@ class CognitoServiceTest {
@Test
void getGroup() {
UserPool pool = service.createUserPool("TestPool", "us-east-1");
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
CognitoGroup fetched = service.getGroup(pool.getId(), "admins");
@@ -83,7 +113,7 @@ class CognitoServiceTest {
@Test
void getGroupNotFoundThrows() {
UserPool pool = service.createUserPool("TestPool", "us-east-1");
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
assertThrows(AwsException.class, () ->
service.getGroup(pool.getId(), "nonexistent"));
@@ -91,7 +121,7 @@ class CognitoServiceTest {
@Test
void listGroups() {
UserPool pool = service.createUserPool("TestPool", "us-east-1");
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
service.createGroup(pool.getId(), "editors", "Editor group", 2, null);
@@ -101,7 +131,7 @@ class CognitoServiceTest {
@Test
void deleteGroup() {
UserPool pool = service.createUserPool("TestPool", "us-east-1");
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
service.deleteGroup(pool.getId(), "admins");
@@ -201,7 +231,7 @@ class CognitoServiceTest {
@Test
void adminAddUserToGroupNonexistentUserThrows() {
UserPool pool = service.createUserPool("TestPool", "us-east-1");
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
assertThrows(AwsException.class, () ->
@@ -373,7 +403,7 @@ class CognitoServiceTest {
@Test
void deleteUserPoolCascadesGroups() {
UserPool pool = service.createUserPool("TestPool", "us-east-1");
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
service.createGroup(pool.getId(), "editors", "Editor group", 2, null);