-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrunReindex.test.ts
97 lines (83 loc) · 2.84 KB
/
runReindex.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
import { describe, it, expect, vi, afterEach } from "vitest";
import { handler } from "./runReindex";
import { Context } from "aws-lambda";
import { CLOUDFORMATION_NOTIFICATION_DOMAIN } from "mocks";
import { SFNClient } from "@aws-sdk/client-sfn";
import * as cfn from "cfn-response-async";
describe("CloudFormation Custom Resource Handler", () => {
const mockEventBase = {
ResponseURL: CLOUDFORMATION_NOTIFICATION_DOMAIN,
ResourceProperties: {
stateMachine: "test-state-machine-arn",
},
};
const stepFunctionSpy = vi.spyOn(SFNClient.prototype, "send");
const cfnSpy = vi.spyOn(cfn, "send");
const callback = vi.fn();
afterEach(() => {
vi.clearAllMocks();
});
it("should start a state machine execution on Create request type", async () => {
const mockEvent = {
...mockEventBase,
RequestType: "Create",
};
await handler(mockEvent, {} as Context, callback);
expect(stepFunctionSpy).toHaveBeenCalledWith(
expect.objectContaining({
input: {
input: JSON.stringify({
cfnEvent: mockEvent,
cfnContext: {},
}),
stateMachineArn: "test-state-machine-arn",
},
}),
);
expect(cfnSpy).not.toHaveBeenCalled();
expect(callback).toHaveBeenCalledWith(null, { statusCode: 200 });
});
it("should send a SUCCESS response on Update request type", async () => {
const mockEvent = {
...mockEventBase,
RequestType: "Update",
};
await handler(mockEvent, {} as Context, callback);
expect(stepFunctionSpy).not.toHaveBeenCalled();
expect(cfnSpy).toHaveBeenCalledWith(mockEvent, {}, cfn.SUCCESS);
expect(callback).toHaveBeenCalledWith(null, { statusCode: 200 });
});
it("should send a SUCCESS response on Delete request type", async () => {
const mockEvent = {
...mockEventBase,
RequestType: "Delete",
};
await handler(mockEvent, {} as Context, callback);
expect(stepFunctionSpy).not.toHaveBeenCalled();
expect(cfnSpy).toHaveBeenCalledWith(mockEvent, {}, cfn.SUCCESS);
expect(callback).toHaveBeenCalledWith(null, { statusCode: 200 });
});
it("should send a FAILED response on error", async () => {
const mockEvent = {
...mockEventBase,
RequestType: "Create",
ResourceProperties: {
stateMachine: "error-test-state-machine-arn",
},
};
await handler(mockEvent, {} as Context, callback);
expect(stepFunctionSpy).toHaveBeenCalledWith(
expect.objectContaining({
input: {
input: JSON.stringify({
cfnEvent: mockEvent,
cfnContext: {},
}),
stateMachineArn: "error-test-state-machine-arn",
},
}),
);
expect(cfnSpy).toHaveBeenCalledWith(mockEvent, {}, cfn.FAILED);
expect(callback).toHaveBeenCalledWith(expect.any(Error), { statusCode: 500 });
});
});