|
| 1 | +import { APIGatewayEvent, Context } from 'aws-lambda'; |
| 2 | +import { mocked } from 'ts-jest/utils'; |
| 3 | +import { githubWebhook } from './lambda'; |
| 4 | +import { handle } from './webhook/handler'; |
| 5 | +import { logger } from './webhook/logger'; |
| 6 | + |
| 7 | +const event: APIGatewayEvent = { |
| 8 | + body: JSON.stringify(''), |
| 9 | + headers: { abc: undefined }, |
| 10 | + httpMethod: '', |
| 11 | + isBase64Encoded: false, |
| 12 | + multiValueHeaders: { abc: undefined }, |
| 13 | + multiValueQueryStringParameters: null, |
| 14 | + path: '', |
| 15 | + pathParameters: null, |
| 16 | + queryStringParameters: null, |
| 17 | + stageVariables: null, |
| 18 | + resource: '', |
| 19 | + requestContext: { |
| 20 | + authorizer: null, |
| 21 | + accountId: '123456789012', |
| 22 | + resourceId: '123456', |
| 23 | + stage: 'prod', |
| 24 | + requestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef', |
| 25 | + requestTime: '09/Apr/2015:12:34:56 +0000', |
| 26 | + requestTimeEpoch: 1428582896000, |
| 27 | + identity: { |
| 28 | + cognitoIdentityPoolId: null, |
| 29 | + accountId: null, |
| 30 | + cognitoIdentityId: null, |
| 31 | + caller: null, |
| 32 | + accessKey: null, |
| 33 | + sourceIp: '127.0.0.1', |
| 34 | + cognitoAuthenticationType: null, |
| 35 | + cognitoAuthenticationProvider: null, |
| 36 | + userArn: null, |
| 37 | + userAgent: 'Custom User Agent String', |
| 38 | + user: null, |
| 39 | + clientCert: null, |
| 40 | + apiKey: null, |
| 41 | + apiKeyId: null, |
| 42 | + principalOrgId: null, |
| 43 | + }, |
| 44 | + path: '/prod/path/to/resource', |
| 45 | + resourcePath: '/{proxy+}', |
| 46 | + httpMethod: 'POST', |
| 47 | + apiId: '1234567890', |
| 48 | + protocol: 'HTTP/1.1', |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +const context: Context = { |
| 53 | + awsRequestId: '1', |
| 54 | + callbackWaitsForEmptyEventLoop: false, |
| 55 | + functionName: '', |
| 56 | + functionVersion: '', |
| 57 | + getRemainingTimeInMillis: () => 0, |
| 58 | + invokedFunctionArn: '', |
| 59 | + logGroupName: '', |
| 60 | + logStreamName: '', |
| 61 | + memoryLimitInMB: '', |
| 62 | + done: () => { |
| 63 | + return; |
| 64 | + }, |
| 65 | + fail: () => { |
| 66 | + return; |
| 67 | + }, |
| 68 | + succeed: () => { |
| 69 | + return; |
| 70 | + }, |
| 71 | +}; |
| 72 | + |
| 73 | +jest.mock('./webhook/handler'); |
| 74 | + |
| 75 | +describe('Test scale up lambda wrapper.', () => { |
| 76 | + it('Happy flow, resolve.', async () => { |
| 77 | + const mock = mocked(handle); |
| 78 | + mock.mockImplementation(() => { |
| 79 | + return new Promise((resolve) => { |
| 80 | + resolve({ statusCode: 200 }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + const result = await githubWebhook(event, context); |
| 85 | + expect(result).toEqual({ statusCode: 200 }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('An expected error, resolve.', async () => { |
| 89 | + const mock = mocked(handle); |
| 90 | + mock.mockImplementation(() => { |
| 91 | + return new Promise((resolve) => { |
| 92 | + resolve({ statusCode: 400 }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + const result = await githubWebhook(event, context); |
| 97 | + expect(result).toEqual({ statusCode: 400 }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('Errors are not thrown.', async () => { |
| 101 | + const mock = mocked(handle); |
| 102 | + const logSpy = jest.spyOn(logger, 'error'); |
| 103 | + mock.mockRejectedValue(new Error('some error')); |
| 104 | + const result = await githubWebhook(event, context); |
| 105 | + expect(result).toMatchObject({ statusCode: 500 }); |
| 106 | + expect(logSpy).toBeCalledTimes(1); |
| 107 | + }); |
| 108 | +}); |
0 commit comments