Skip to content

Commit

Permalink
fix(api): createUserPool saves SchemaAttributes
Browse files Browse the repository at this point in the history
Fixes #93
  • Loading branch information
jagregory committed Jan 15, 2022
1 parent d5b8cd5 commit 3301878
Show file tree
Hide file tree
Showing 4 changed files with 373 additions and 7 deletions.
2 changes: 2 additions & 0 deletions integration-tests/aws-sdk/createUserPool.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Pino from "pino";
import { ClockFake } from "../../src/__tests__/clockFake";
import { UUID } from "../../src/__tests__/patterns";
import { USER_POOL_AWS_DEFAULTS } from "../../src/services/cognitoService";
Expand Down Expand Up @@ -38,6 +39,7 @@ describe(
},
{
clock,
logger: Pino(),
}
)
);
12 changes: 7 additions & 5 deletions src/services/cognitoService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mergeWith from "lodash.mergewith";
import * as path from "path";
import { ResourceNotFoundError } from "../errors";
import { UserPoolDefaults } from "../server/config";
Expand Down Expand Up @@ -309,11 +310,12 @@ export class CognitoServiceImpl implements CognitoService {
const service = await this.userPoolServiceFactory.create(
ctx,
this.clients,
{
...USER_POOL_AWS_DEFAULTS,
...this.userPoolDefaultConfig,
...userPool,
}
mergeWith(
{},
USER_POOL_AWS_DEFAULTS,
this.userPoolDefaultConfig,
userPool
)
);

return service.config;
Expand Down
292 changes: 291 additions & 1 deletion src/targets/createUserPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { newMockUserPoolService } from "../__tests__/mockUserPoolService";
import { TestContext } from "../__tests__/testContext";
import * as TDB from "../__tests__/testDataBuilder";
import { CognitoService } from "../services";
import { USER_POOL_AWS_DEFAULTS } from "../services/cognitoService";
import { CreateUserPool, CreateUserPoolTarget } from "./createUserPool";

const originalDate = new Date();
Expand Down Expand Up @@ -38,7 +39,296 @@ describe("CreateUserPool target", () => {
Id: expect.stringMatching(/^local_[\w\d]{8}$/),
LastModifiedDate: originalDate,
Name: "test-pool",
PoolName: "test-pool",
SchemaAttributes: USER_POOL_AWS_DEFAULTS.SchemaAttributes,
}
);

expect(result).toEqual({
UserPool: createdUserPool,
});
});

it("creates a new user pool with a custom attribute", async () => {
const createdUserPool = TDB.userPool();
mockCognitoService.createUserPool.mockResolvedValue(createdUserPool);

const result = await createUserPool(TestContext, {
PoolName: "test-pool",
Schema: [
{
Name: "my_attribute",
AttributeDataType: "String",
},
],
});

expect(mockCognitoService.createUserPool).toHaveBeenCalledWith(
TestContext,
{
Arn: expect.stringMatching(
/^arn:aws:cognito-idp:local:local:userpool\/local_[\w\d]{8}$/
),
CreationDate: originalDate,
Id: expect.stringMatching(/^local_[\w\d]{8}$/),
LastModifiedDate: originalDate,
Name: "test-pool",
SchemaAttributes: [
...(USER_POOL_AWS_DEFAULTS.SchemaAttributes ?? []),
{
Name: "custom:my_attribute",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {},
},
],
}
);

expect(result).toEqual({
UserPool: createdUserPool,
});
});

it("creates a new user pool with an overridden attribute", async () => {
const createdUserPool = TDB.userPool();
mockCognitoService.createUserPool.mockResolvedValue(createdUserPool);

const result = await createUserPool(TestContext, {
PoolName: "test-pool",
Schema: [
{
Name: "email",
AttributeDataType: "String",
Required: true,
},
],
});

expect(mockCognitoService.createUserPool).toHaveBeenCalledWith(
TestContext,
{
Arn: expect.stringMatching(
/^arn:aws:cognito-idp:local:local:userpool\/local_[\w\d]{8}$/
),
CreationDate: originalDate,
Id: expect.stringMatching(/^local_[\w\d]{8}$/),
LastModifiedDate: originalDate,
Name: "test-pool",
SchemaAttributes: [
{
Name: "sub",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: false,
Required: true,
StringAttributeConstraints: {
MinLength: "1",
MaxLength: "2048",
},
},
{
Name: "name",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "given_name",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "family_name",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "middle_name",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "nickname",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "preferred_username",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "profile",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "picture",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "website",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "email",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: true,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "email_verified",
AttributeDataType: "Boolean",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
},
{
Name: "gender",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "birthdate",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "10",
MaxLength: "10",
},
},
{
Name: "zoneinfo",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "locale",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "phone_number",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "phone_number_verified",
AttributeDataType: "Boolean",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
},
{
Name: "address",
AttributeDataType: "String",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
StringAttributeConstraints: {
MinLength: "0",
MaxLength: "2048",
},
},
{
Name: "updated_at",
AttributeDataType: "Number",
DeveloperOnlyAttribute: false,
Mutable: true,
Required: false,
NumberAttributeConstraints: {
MinValue: "0",
},
},
],
}
);

Expand Down
Loading

0 comments on commit 3301878

Please sign in to comment.