Skip to content

Commit

Permalink
Retain existing Cognito User Pool config
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuens committed Aug 8, 2019
1 parent c8fa1d3 commit f2a3fc9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@

const CognitoIdentityServiceProvider = require('aws-sdk/clients/cognitoidentityserviceprovider');

function getUpdateConfigFromCurrentSetup(currentSetup) {
const updatedConfig = Object.assign({}, currentSetup);
delete updatedConfig.Id;
delete updatedConfig.Name;
delete updatedConfig.Status;
delete updatedConfig.LastModifiedDate;
delete updatedConfig.CreationDate;
delete updatedConfig.SchemaAttributes;
delete updatedConfig.AliasAttributes;
delete updatedConfig.UsernameAttributes;
delete updatedConfig.EstimatedNumberOfUsers;
delete updatedConfig.SmsConfigurationFailure;
delete updatedConfig.EmailConfigurationFailure;
delete updatedConfig.Domain;
delete updatedConfig.CustomDomain;
delete updatedConfig.Arn;
// necessary reassignments
updatedConfig.Policies.PasswordPolicy.TemporaryPasswordValidityDays =
updatedConfig.AdminCreateUserConfig.UnusedAccountValidityDays;
delete updatedConfig.AdminCreateUserConfig.UnusedAccountValidityDays;
return updatedConfig;
}

function findUserPoolByName(config) {
const { userPoolName, region } = config;
const cognito = new CognitoIdentityServiceProvider({ region });
Expand Down Expand Up @@ -58,7 +81,13 @@ function updateConfiguration(config) {
LambdaConfig[poolConfig.Trigger] = lambdaArn;
});

return cognito.updateUserPool({ UserPoolId, LambdaConfig }).promise();
const updatedConfig = getUpdateConfigFromCurrentSetup(res.UserPool);
Object.assign(updatedConfig, {
UserPoolId,
LambdaConfig,
});

return cognito.updateUserPool(updatedConfig).promise();
});
}

Expand All @@ -75,7 +104,13 @@ function removeConfiguration(config) {
return accum;
}, LambdaConfig);

return cognito.updateUserPool({ UserPoolId, LambdaConfig }).promise();
const updatedConfig = getUpdateConfigFromCurrentSetup(res.UserPool);
Object.assign(updatedConfig, {
UserPoolId,
LambdaConfig,
});

return cognito.updateUserPool(updatedConfig).promise();
});
}

Expand Down
22 changes: 21 additions & 1 deletion tests/integration-all/cognito-user-pool/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
createUserPool,
deleteUserPool,
findUserPoolByName,
describeUserPool,
createUser,
createUserPoolClient,
setUserPassword,
Expand All @@ -29,6 +30,7 @@ describe('AWS - Cognito User Pool Integration Test', () => {
let poolBasicSetup;
let poolExistingSimpleSetup;
let poolExistingMultiSetup;
let poolExistingSimpleSetupConfig;
const stage = 'dev';

beforeAll(() => {
Expand All @@ -52,10 +54,15 @@ describe('AWS - Cognito User Pool Integration Test', () => {
serviceName = serverlessConfig.service;
stackName = `${serviceName}-${stage}`;
// create external Cognito User Pools
// the simple pool setup has some additional configuration when we set it up
poolExistingSimpleSetupConfig = {
EmailVerificationMessage: 'email{####}message',
EmailVerificationSubject: 'email{####}subject',
};
// NOTE: deployment can only be done once the Cognito User Pools are created
console.info('Creating Cognito User Pools');
return BbPromise.all([
createUserPool(poolExistingSimpleSetup),
createUserPool(poolExistingSimpleSetup, poolExistingSimpleSetupConfig),
createUserPool(poolExistingMultiSetup),
]).then(() => {
console.info(`Deploying "${stackName}" service...`);
Expand Down Expand Up @@ -112,6 +119,19 @@ describe('AWS - Cognito User Pool Integration Test', () => {
expect(logs).to.include('"triggerSource":"PreSignUp_AdminCreateUser"');
});
});

it('should not overwrite existing User Pool configurations', () => {
return findUserPoolByName(poolExistingSimpleSetup).then(pool => {
return describeUserPool(pool.Id).then(config => {
expect(config.UserPool.EmailVerificationMessage).to.equal(
poolExistingSimpleSetupConfig.EmailVerificationMessage
);
expect(config.UserPool.EmailVerificationSubject).to.equal(
poolExistingSimpleSetupConfig.EmailVerificationSubject
);
});
});
});
});

describe('single function / multi pool setup', () => {
Expand Down
12 changes: 10 additions & 2 deletions tests/utils/cognito/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
const AWS = require('aws-sdk');
const { region, persistentRequest } = require('../misc');

function createUserPool(name) {
function createUserPool(name, config = {}) {
const cognito = new AWS.CognitoIdentityServiceProvider({ region });

return cognito.createUserPool({ PoolName: name }).promise();
const params = Object.assign({}, { PoolName: name }, config);
return cognito.createUserPool(params).promise();
}

function createUserPoolClient(name, userPoolId) {
Expand Down Expand Up @@ -53,6 +54,12 @@ function findUserPoolByName(name) {
return recursiveFind();
}

function describeUserPool(userPoolId) {
const cognito = new AWS.CognitoIdentityServiceProvider({ region });

return cognito.describeUserPool({ UserPoolId: userPoolId }).promise();
}

function createUser(userPoolId, username, password) {
const cognito = new AWS.CognitoIdentityServiceProvider({ region });

Expand Down Expand Up @@ -94,6 +101,7 @@ module.exports = {
createUserPool: persistentRequest.bind(this, createUserPool),
deleteUserPool: persistentRequest.bind(this, deleteUserPool),
findUserPoolByName: persistentRequest.bind(this, findUserPoolByName),
describeUserPool: persistentRequest.bind(this, describeUserPool),
createUserPoolClient: persistentRequest.bind(this, createUserPoolClient),
createUser: persistentRequest.bind(this, createUser),
setUserPassword: persistentRequest.bind(this, setUserPassword),
Expand Down

0 comments on commit f2a3fc9

Please sign in to comment.