Last active
March 12, 2018 12:23
-
-
Save gcchaan/ac661da11ad5be0ef10fc5f67aa2711b to your computer and use it in GitHub Desktop.
Amazon Cognito PreSignUp DomainValidation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { event, context } from './data' | |
import { handler } from '../validation' | |
const event1 = event('PreSignUp_SignUp', { email: '[email protected]' }); | |
const event2 = event('PreSignUp_SignUp',{ email: '[email protected]' }); | |
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