-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.spec.ts
257 lines (232 loc) · 6.66 KB
/
index.spec.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { expect } from "chai";
import createEvent from "./index";
describe("creating a new SNS event", () => {
it("should return a valid event", () => {
const event = createEvent("aws:sns", {
Records: [
{
Sns: {
Message: "trigger-email",
},
},
],
} as any);
expect(event.Records[0].Sns.Message).to.equal("trigger-email");
expect(event.Records[0].Sns.Type).to.equal("Notification");
});
});
describe("createSqsEvent()", () => {
it("should return SQS mocked event", () => {
const event = createEvent("aws:sqs", {
Records: [
{
body: JSON.stringify({
foo: "bar",
}),
},
],
} as any);
expect(event.Records[0].body).to.equal("{\"foo\":\"bar\"}");
expect(event.Records[0].eventSource).to.equal("aws:sqs");
});
});
describe("createApigEvent()", () => {
it("should return APIG mocked event", () => {
const event = createEvent("aws:apiGateway", {
body: JSON.stringify({
first_name: "Sam",
last_name: "Smith",
}),
} as any);
const parsedBody = JSON.parse(event.body || "");
expect(parsedBody.first_name).to.equal("Sam");
expect(parsedBody.last_name).to.equal("Smith");
expect(event.httpMethod).to.equal("GET");
});
});
it("should return APIG mocked event with custom authorizer", () => {
const event = createEvent("aws:apiGateway", {
requestContext: {
authorizer: {
user_info: JSON.stringify({
id: 1234,
name: "Sam Smith",
}),
},
},
} as any);
expect(!!event.requestContext.authorizer).to.be.true;
const authorizer = event.requestContext.authorizer || {};
const parsedAuthorizer = JSON.parse(authorizer.user_info || "");
expect(parsedAuthorizer.id).to.eql(1234);
expect(parsedAuthorizer.name).to.eql("Sam Smith");
});
describe("createWebsocketEvent()", () => {
it("should return websocket mocked event", () => {
const event = createEvent("aws:websocket", {
body: JSON.stringify({
first_name: "Sam",
last_name: "Smith",
}),
requestContext: {
connectedAt: 123,
connectionId: "abc123",
},
} as any);
const parsedBody = JSON.parse(event.body || "");
expect(parsedBody.first_name).to.equal("Sam");
expect(parsedBody.last_name).to.equal("Smith");
expect(event.requestContext.connectedAt).to.equal(123);
expect(event.requestContext.connectionId).to.equal("abc123");
});
});
describe("createS3Event()", () => {
it("should return S3 mocked event", () => {
const event = createEvent("aws:s3", {
Records: [
{
s3: {
bucket: {
name: "my-bucket-name",
},
object: {
key: "object-key",
},
},
},
],
} as any);
expect(event.Records[0].s3.bucket.name).to.equal("my-bucket-name");
expect(event.Records[0].s3.object.key).to.equal("object-key");
expect(event.Records[0].eventName).to.equal("ObjectCreated:Put");
});
it("should return S3 mocked event without side-effect", () => {
const event = createEvent("aws:s3", {
Records: [
{
s3: {
bucket: {
name: "my-bucket-name",
},
object: {
key: "object-key",
},
},
},
],
} as any);
const event2 = createEvent("aws:s3", {
Records: [
{
s3: {
bucket: {
name: "my-bucket-name",
},
object: {
key: "object-key-2",
},
},
},
],
} as any);
expect(event.Records[0].s3.bucket.name).to.equal("my-bucket-name");
expect(event.Records[0].s3.object.key).to.equal("object-key");
expect(event2.Records[0].s3.object.key).to.equal("object-key-2");
expect(event.Records[0].eventName).to.equal("ObjectCreated:Put");
});
});
describe("createScheduledEvent()", () => {
it("should return Scheduled mocked event", () => {
const event = createEvent("aws:scheduled", {
region: "us-west-2",
} as any);
expect(event.region).to.equal("us-west-2");
expect(event["detail-type"]).to.equal("Scheduled Event");
});
});
describe("createKinesisEvent()", () => {
it("should return Kinesis mocked event", () => {
const event = createEvent("aws:kinesis", {
Records: [
{
kinesis: {
data: Buffer.from("kinesis test").toString("base64"),
},
},
],
} as any);
expect(
Buffer.from(event.Records[0].kinesis.data, "base64").toString("ascii"),
).to.equal("kinesis test");
});
});
describe("createCloudWatchEvent()", () => {
it("should return a valid event", () => {
const event = createEvent("aws:cloudWatch", {
"detail-type": "Something has been deleted.",
"region": "us-east-1",
} as any);
expect(event["detail-type"]).to.equal("Something has been deleted.");
expect(event.region).to.equal("us-east-1");
});
});
describe("createCloudWatchLogEvent()", () => {
it("should return a valid event", () => {
const event = createEvent("aws:cloudWatchLog", {
awslogs: {
data: "Some gzipped, then base64 encoded data",
},
}) as any;
expect(event.awslogs.data).to.equal(
"Some gzipped, then base64 encoded data",
);
});
});
describe("createAlexaSkillEvent()", () => {
it("should return a valid event", () => {
const event = createEvent("aws:alexaSkill", {
request: {
type: "CanFulfillIntentRequest",
},
context: {
System: {
device: {
deviceId: "myDevice",
},
},
},
} as any);
expect(event.request.type).to.equal("CanFulfillIntentRequest");
expect(event.context.System.device.deviceId).to.equal("myDevice");
});
});
describe("createAlexaSmartHomeEvent()", () => {
it("should return a valid event", () => {
const event = createEvent("aws:alexaSmartHome", {
payload: {
switchControlAction: "TURN_OFF",
},
} as any);
expect(event.payload.switchControlAction).to.equal("TURN_OFF");
});
});
describe("createIotEvent()", () => {
it("should return a valid event", () => {
const event = createEvent("aws:iot", {
this: {
can: {
be: "anything I want",
},
},
} as any);
expect(event.this.can.be).to.equal("anything I want");
});
});
describe("createCognitoPoolEvent()", () => {
it("should return a valid event", () => {
const event = createEvent("aws:cognitoUserPool", {
userName: "notAJ",
} as any);
expect(event.userName).to.eql("notAJ");
});
});