Skip to content

Commit 6b3c8e0

Browse files
committed
add tests to generic and cloudformation wrappers
1 parent 9ede516 commit 6b3c8e0

File tree

10 files changed

+145
-29
lines changed

10 files changed

+145
-29
lines changed

src/cloudformation/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './cloudformation';
1+
export * from './wrapper';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Context, CloudFormationCustomResourceEvent } from 'aws-lambda';
2+
import { send } from 'cfn-response';
3+
import { successWrapper, failureWrapper } from './responses';
4+
5+
jest.mock('cfn-response');
6+
7+
describe('Cloudformation responses', () => {
8+
const event: CloudFormationCustomResourceEvent = {
9+
RequestType: 'Create',
10+
ServiceToken: 'token',
11+
ResponseURL: 'url',
12+
StackId: 'stackId',
13+
RequestId: 'requestId',
14+
LogicalResourceId: 'resourceId',
15+
ResourceType: 'resourceType',
16+
ResourceProperties: {
17+
ServiceToken: 'serviceToken'
18+
}
19+
};
20+
const context: Context = {
21+
callbackWaitsForEmptyEventLoop: false,
22+
functionName: 'function-name',
23+
functionVersion: '$LATEST',
24+
invokedFunctionArn: 'arn:',
25+
memoryLimitInMB: 128,
26+
awsRequestId: 'request',
27+
logGroupName: 'group',
28+
logStreamName: 'stream',
29+
getRemainingTimeInMillis: () => 2,
30+
done: () => {},
31+
fail: () => {},
32+
succeed: () => {}
33+
};
34+
35+
it('Handles successWrapper response', () => {
36+
const success = successWrapper(event, context);
37+
expect(success).toBeInstanceOf(Function);
38+
success('success');
39+
expect(send).toBeCalled();
40+
});
41+
42+
it('Handles failureWrapper response', () => {
43+
const failure = failureWrapper(event, context);
44+
expect(failure).toBeInstanceOf(Function);
45+
failure('failure');
46+
expect(send).toBeCalled();
47+
});
48+
});

src/cloudformation/responses.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Context, CloudFormationCustomResourceEvent } from 'aws-lambda';
2+
import { send, SUCCESS, FAILED } from 'cfn-response';
3+
import { Metrics } from '../common';
4+
5+
const metrics = new Metrics('Cloudformation');
6+
7+
export function successWrapper(event: CloudFormationCustomResourceEvent, context: Context) {
8+
return function success(message?: any): void {
9+
metrics.success(message);
10+
return send(event, context, SUCCESS);
11+
};
12+
}
13+
export function failureWrapper(event: CloudFormationCustomResourceEvent, context: Context) {
14+
return function failure(message: any): void {
15+
metrics.failure(message);
16+
return send(event, context, FAILED);
17+
};
18+
}

src/cloudformation/wrapper.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { cloudFormationWrapper, CloudFormationSignature } from './wrapper';
2+
import { CloudFormationCustomResourceEvent } from 'aws-lambda';
3+
4+
describe('Stream wrapper', () => {
5+
const requestEvent: CloudFormationCustomResourceEvent = {
6+
RequestType: 'Create',
7+
ServiceToken: 'token',
8+
ResponseURL: 'url',
9+
StackId: 'stackId',
10+
RequestId: 'requestId',
11+
LogicalResourceId: 'resourceId',
12+
ResourceType: 'resourceType',
13+
ResourceProperties: {
14+
ServiceToken: 'serviceToken'
15+
}
16+
};
17+
18+
it('Has expected properties and response funtions', () => {
19+
function mockHandler({ event, success, failure }: CloudFormationSignature) {
20+
expect(event).toEqual(requestEvent);
21+
expect(success).toBeInstanceOf(Function);
22+
expect(failure).toBeInstanceOf(Function);
23+
}
24+
// @ts-ignore
25+
cloudFormationWrapper(mockHandler)(requestEvent);
26+
});
27+
});

src/cloudformation/cloudformation.ts renamed to src/cloudformation/wrapper.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
import { CloudFormationCustomResourceEvent, Context, Callback } from 'aws-lambda';
2-
import { send, SUCCESS, FAILED } from 'cfn-response';
1+
import { CloudFormationCustomResourceEvent, Context } from 'aws-lambda';
32
import { Metrics } from '../common';
3+
import { successWrapper, failureWrapper } from './responses';
44

55
const metrics = new Metrics('CloudFormation');
66

77
export function cloudFormationWrapper<T extends Function>(fn: T): T {
8-
return <any>function(event: CloudFormationCustomResourceEvent, context: Context, callback: Callback) {
8+
return <any>function(event: CloudFormationCustomResourceEvent, context: Context) {
99
metrics.common(event);
1010

11-
function success(message?: any): void {
12-
metrics.success(message);
13-
return send(event, context, SUCCESS);
14-
}
15-
16-
function failure(message: any): void {
17-
metrics.failure(message);
18-
return send(event, context, FAILED);
19-
}
20-
21-
const signature: CloudFormationSignature = { event, success, failure };
11+
const signature: CloudFormationSignature = {
12+
event,
13+
// We need to call these response wrappers so that we can pass in context (necessary for CloudFormation responses) so that
14+
success: successWrapper(event, context),
15+
failure: failureWrapper(event, context)
16+
};
2217
return fn(signature);
2318
};
2419
}

src/generic/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './generic';
1+
export * from './wrapper';

src/generic/responses.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { success, error } from './responses';
2+
3+
describe('Generic responses', () => {
4+
it('Handles success response', () => {
5+
expect(success('success')).toEqual('success');
6+
});
7+
8+
it('Handles error response', () => {
9+
expect(() => error('error')).toThrow('error');
10+
});
11+
});

src/generic/responses.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Metrics } from '../common';
2+
3+
const metrics = new Metrics('Generic');
4+
5+
export function success(message: any): void {
6+
metrics.success(message);
7+
return message;
8+
}
9+
10+
export function error(error: any): void {
11+
metrics.error(error);
12+
throw new Error(error);
13+
}

src/generic/wrapper.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { wrapper, WrapperSignature } from './wrapper';
2+
3+
describe('Stream wrapper', () => {
4+
const requestEvent = { hello: 'world' };
5+
6+
it('Has expected properties and response funtions', () => {
7+
function mockHandler({ event, success, error }: WrapperSignature) {
8+
expect(event).toEqual(requestEvent);
9+
expect(success).toBeInstanceOf(Function);
10+
expect(error).toBeInstanceOf(Function);
11+
}
12+
// @ts-ignore
13+
wrapper(mockHandler)(requestEvent);
14+
});
15+
});

src/generic/generic.ts renamed to src/generic/wrapper.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
import { Context, Callback } from 'aws-lambda';
21
import { Metrics } from '../common';
2+
import { success, error } from './responses';
33

44
const metrics = new Metrics('Generic');
55

66
export function wrapper<T extends Function>(fn: T): T {
7-
return <any>function(event, context: Context, callback: Callback) {
7+
return <any>function(event) {
88
metrics.common(event);
9-
10-
function success(message: any = ''): void {
11-
metrics.success(message);
12-
return callback(null, message);
13-
}
14-
15-
function error(error: any = ''): void {
16-
metrics.error(error);
17-
return callback(error);
18-
}
19-
209
const signature: WrapperSignature = { event, success, error };
2110
return fn(signature);
2211
};

0 commit comments

Comments
 (0)