-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.lambda.test.ts
169 lines (159 loc) · 4.43 KB
/
create.lambda.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
ConditionalCheckFailedException,
DynamoDBClient,
PutItemCommand,
} from '@aws-sdk/client-dynamodb';
import { marshall } from '@aws-sdk/util-dynamodb';
import { APIGatewayProxyEventV2, Context } from 'aws-lambda';
import { mockClient } from 'aws-sdk-client-mock';
import { handler } from '../../src/posts/create.lambda';
import { testCases } from '../components/test-cases';
const TABLE_NAME = 'test-table';
const ddbMock = mockClient(DynamoDBClient);
beforeEach(() => {
process.env.TABLE_NAME = TABLE_NAME;
jest.spyOn(console, 'info').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation(() => {});
jest.spyOn(console, 'log').mockImplementation(() => {});
});
afterEach(() => {
jest.clearAllMocks();
ddbMock.reset();
});
afterAll(() => {
process.env.TABLE_NAME = undefined;
jest.restoreAllMocks();
});
describe('create post', () => {
test.each([testCases])('successful', async (testCase) => {
// GIVEN
ddbMock.on(PutItemCommand).resolves({});
const body = JSON.stringify({
...testCase,
});
const req = createEvent(body);
// WHEN
const res = await handler(req.event, req.context);
// THEN
ddbMock.commandCalls(PutItemCommand, {
TableName: TABLE_NAME,
ConditionExpression: 'attribute_not_exists(pk)',
Item: marshall(body),
});
expect(res).toEqual({
statusCode: 200,
body: 'OK',
});
});
test.each([testCases])('failure', async (testCase) => {
// GIVEN
const body = JSON.stringify({
...testCase,
});
const req = createEvent(body);
// WHEN
ddbMock.on(PutItemCommand).rejects({});
// THEN
await expect(handler(req.event, req.context)).rejects.toThrow(
/failed creating post/,
);
ddbMock.commandCalls(PutItemCommand, {
TableName: TABLE_NAME,
ConditionExpression: 'attribute_not_exists(pk)',
Item: marshall(body),
});
});
test('item already exists', async () => {
// GIVEN
const body = JSON.stringify({
...testCases[0],
});
const req = createEvent(body);
// WHEN
ddbMock.on(PutItemCommand).rejects(
new ConditionalCheckFailedException({
message: 'error',
$metadata: {} as any,
}),
);
const res = await handler(req.event, req.context);
// THEN
expect(res).toEqual({
statusCode: 200,
body: 'OK',
});
ddbMock.commandCalls(PutItemCommand, {
TableName: TABLE_NAME,
ConditionExpression: 'attribute_not_exists(pk)',
Item: marshall(body),
});
});
});
function createEvent(body: string): {
event: APIGatewayProxyEventV2;
context: Context;
} {
return {
event: {
version: '2.0',
routeKey: 'POST /posts',
rawPath: '/posts',
rawQueryString: '',
headers: {
accept: '*/*',
'accept-encoding': 'gzip,deflate',
'content-length': '143',
'content-type': 'text/plain;charset=UTF-8',
host: 'hxzls0fu8c.execute-api.us-west-2.amazonaws.com',
'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
'x-amzn-trace-id': 'Root=1-64ad9aec-0afc754f0069c3f0151d05da',
'x-forwarded-for': '34.220.207.9',
'x-forwarded-port': '443',
'x-forwarded-proto': 'https',
},
requestContext: {
accountId: '12345678910',
apiId: 'hxzls0fu8c',
domainName: 'hxzls0fu8c.execute-api.us-west-2.amazonaws.com',
domainPrefix: 'hxzls0fu8c',
http: {
method: 'POST',
path: '/posts',
protocol: 'HTTP/1.1',
sourceIp: '34.220.207.9',
userAgent: 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
},
requestId: 'H6Uk_hCxvHcEMaw=',
routeKey: 'POST /posts',
stage: '$default',
time: '11/Jul/2023:18:09:48 +0000',
timeEpoch: 1689098988494,
},
body,
isBase64Encoded: false,
},
context: {
succeed(_message) {
return;
},
fail(_error) {
return;
},
done(_error, _result) {
return;
},
getRemainingTimeInMillis() {
return 1;
},
awsRequestId: '',
callbackWaitsForEmptyEventLoop: false,
functionName: 'test-function',
functionVersion: '1',
invokedFunctionArn: '',
logGroupName: 'loggroup',
logStreamName: 'logstream',
memoryLimitInMB: '125',
},
};
}