Skip to content

Instantly share code, notes, and snippets.

@gcchaan
Last active March 12, 2018 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcchaan/ac661da11ad5be0ef10fc5f67aa2711b to your computer and use it in GitHub Desktop.
Save gcchaan/ac661da11ad5be0ef10fc5f67aa2711b to your computer and use it in GitHub Desktop.
Amazon Cognito PreSignUp DomainValidation
export function event(triggerSource = '', userAttributes = null) {
return {
triggerSource: triggerSource,
request: {
userAttributes: userAttributes,
validationData: null
},
response: {
autoConfirmUser: false,
autoVerifyEmail: false,
autoVerifyPhone: false
}
}
}
export const context = {
callbackWaitsForEmptyEventLoop: false,// should nullable
done: jest.fn(),
succeed: () => {},
fail: () => {},
logGroupName: '/aws/lambda/validation',
logStreamName: 'yyyy/mm/dd/[$LATEST]hash',
functionName: 'validation',
memoryLimitInMB: 128,
functionVersion: '$LATEST',
getRemainingTimeInMillis: () => 1,
awsRequestId: '',
invokedFunctionArn: 'arn:aws:lambda:ap-northeast-1:xxxx:function:validation'
}
import { CognitoUserPoolTriggerEvent, Handler, Context, Callback } from 'aws-lambda';
const allowedDomains = ['hoge.ne.jp']
export const handler: Handler = (
event: CognitoUserPoolTriggerEvent,
context: Context,
callback: Callback) => {
// PreSignUp_SignUp
if (event.triggerSource == 'PreSignUp_SignUp') {
if (event.request.userAttributes &&
event.request.userAttributes.email &&
allowedDomains.some((_) =>
event.request.userAttributes.email.endsWith('@' + _))) {
context.done(undefined, event);
} else {
var error = new Error('domain validation error');
context.done(error, event);
}
}
};
import { event, context } from './data'
import { handler } from '../validation'
const event1 = event('PreSignUp_SignUp', { email: 'aaa@hoge.ne.jp' });
const event2 = event('PreSignUp_SignUp',{ email: 'aaa@should.rejected.com' });
describe('mytest', () => {
it('登録できる', () => {
handler(event1, context, () => {});
expect(context.done).toHaveBeenCalledWith(undefined, event1);
});
it('ドメインバリデーション', () => {
handler(event2, context, () => {});
expect(context.done).toHaveBeenCalledWith(Error('domain validation error'), event2);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment