|
| 1 | +import { KinesisClient } from "@aws-sdk/client-kinesis"; |
| 2 | +import * as pino from "pino"; |
| 3 | +import { mockDeep } from "jest-mock-extended"; |
| 4 | +import { DynamoDBStreamEvent, Context } from "aws-lambda"; |
| 5 | +import { Deps } from "../deps"; |
| 6 | +import { EnvVars } from "../env"; |
| 7 | +import { createHandler } from "../letter-stream-forwarder"; |
| 8 | + |
| 9 | +describe("letter-stream-forwarder Lambda", () => { |
| 10 | + |
| 11 | + const mockedDeps: jest.Mocked<Deps> = { |
| 12 | + kinesisClient: { send: jest.fn()} as unknown as KinesisClient, |
| 13 | + logger: { info: jest.fn(), warn: jest.fn(), error: jest.fn() } as unknown as pino.Logger, |
| 14 | + env: { |
| 15 | + LETTER_CHANGE_STREAM_ARN: "test-stream.arn", |
| 16 | + } as unknown as EnvVars |
| 17 | + } as Deps; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + jest.clearAllMocks(); |
| 21 | + }); |
| 22 | + |
| 23 | + |
| 24 | + it("forwards status changes to Kinesis", async () => { |
| 25 | + const event: DynamoDBStreamEvent = { |
| 26 | + Records: [ |
| 27 | + { |
| 28 | + eventName: "MODIFY", |
| 29 | + dynamodb: { |
| 30 | + Keys: { id: { S: "123" } }, |
| 31 | + OldImage: buildValidLetter(), |
| 32 | + NewImage: {...buildValidLetter(), status: { S: "ACCEPTED" } }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + ], |
| 36 | + }; |
| 37 | + |
| 38 | + const handler = createHandler(mockedDeps); |
| 39 | + await handler(event, mockDeep<Context>(), jest.fn()); |
| 40 | + |
| 41 | + expect(mockedDeps.kinesisClient.send).toHaveBeenCalledWith( |
| 42 | + expect.objectContaining({ |
| 43 | + input: expect.objectContaining({ |
| 44 | + StreamARN: "test-stream.arn", |
| 45 | + PartitionKey: "123", |
| 46 | + }), |
| 47 | + }) |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + |
| 52 | + it("does not forward invalid status changes", async () => { |
| 53 | + const event: DynamoDBStreamEvent = { |
| 54 | + Records: [ |
| 55 | + { |
| 56 | + eventName: "MODIFY", |
| 57 | + dynamodb: { |
| 58 | + Keys: { id: { S: "123" } }, |
| 59 | + OldImage: {...buildValidLetter(), status: { S: "CANCELLED" } }, |
| 60 | + NewImage: {...buildValidLetter(), status: { S: "PRINTED" } }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + ], |
| 64 | + }; |
| 65 | + |
| 66 | + const handler = createHandler(mockedDeps); |
| 67 | + await handler(event, mockDeep<Context>(), jest.fn()); |
| 68 | + |
| 69 | + expect(mockedDeps.kinesisClient.send).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("forwards to Kinesis if a reason code is added", async () => { |
| 73 | + const event: DynamoDBStreamEvent = { |
| 74 | + Records: [ |
| 75 | + { |
| 76 | + eventName: "MODIFY", |
| 77 | + dynamodb: { |
| 78 | + Keys: { id: { S: "123" } }, |
| 79 | + OldImage: buildValidLetter(), |
| 80 | + NewImage: {...buildValidLetter(), reasonCode: {S: "r1"} }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + ], |
| 84 | + }; |
| 85 | + |
| 86 | + const handler = createHandler(mockedDeps); |
| 87 | + await handler(event, mockDeep<Context>(), jest.fn()); |
| 88 | + |
| 89 | + expect(mockedDeps.kinesisClient.send).toHaveBeenCalledWith( |
| 90 | + expect.objectContaining({ |
| 91 | + input: expect.objectContaining({ |
| 92 | + StreamARN: "test-stream.arn", |
| 93 | + PartitionKey: "123", |
| 94 | + }), |
| 95 | + }) |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + |
| 100 | + it("forwards to Kinesis if a reason code is changed", async () => { |
| 101 | + const event: DynamoDBStreamEvent = { |
| 102 | + Records: [ |
| 103 | + { |
| 104 | + eventName: "MODIFY", |
| 105 | + dynamodb: { |
| 106 | + Keys: { id: { S: "123" } }, |
| 107 | + OldImage: {...buildValidLetter(), reasonCode: {S: "r1"} }, |
| 108 | + NewImage: {...buildValidLetter(), reasonCode: {S: "r2"} }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + ], |
| 112 | + }; |
| 113 | + |
| 114 | + const handler = createHandler(mockedDeps); |
| 115 | + await handler(event, mockDeep<Context>(), jest.fn()); |
| 116 | + |
| 117 | + expect(mockedDeps.kinesisClient.send).toHaveBeenCalledWith( |
| 118 | + expect.objectContaining({ |
| 119 | + input: expect.objectContaining({ |
| 120 | + StreamARN: "test-stream.arn", |
| 121 | + PartitionKey: "123", |
| 122 | + }), |
| 123 | + }) |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it("does not forward if neither status nor reason code changed", async () => { |
| 128 | + const event: DynamoDBStreamEvent = { |
| 129 | + Records: [ |
| 130 | + { |
| 131 | + eventName: "MODIFY", |
| 132 | + dynamodb: { |
| 133 | + Keys: { id: { S: "123" } }, |
| 134 | + OldImage: buildValidLetter(), |
| 135 | + NewImage: buildValidLetter(), |
| 136 | + }, |
| 137 | + }, |
| 138 | + ], |
| 139 | + }; |
| 140 | + |
| 141 | + const handler = createHandler(mockedDeps); |
| 142 | + await handler(event, mockDeep<Context>(), jest.fn()); |
| 143 | + |
| 144 | + expect(mockedDeps.kinesisClient.send).not.toHaveBeenCalled(); |
| 145 | + }); |
| 146 | + |
| 147 | + it("does not forward non-MODIFY events", async () => { |
| 148 | + const event: DynamoDBStreamEvent = { |
| 149 | + Records: [ |
| 150 | + { |
| 151 | + eventName: "INSERT", |
| 152 | + dynamodb: { |
| 153 | + Keys: { id: { S: "123" } }, |
| 154 | + NewImage: buildValidLetter(), |
| 155 | + }, |
| 156 | + }, |
| 157 | + ], |
| 158 | + }; |
| 159 | + |
| 160 | + const handler = createHandler(mockedDeps); |
| 161 | + await handler(event, mockDeep<Context>(), jest.fn()); |
| 162 | + |
| 163 | + expect(mockedDeps.kinesisClient.send).not.toHaveBeenCalled(); |
| 164 | + }); |
| 165 | + |
| 166 | + |
| 167 | + it("does not forward invalid letter data", async () => { |
| 168 | + const event: DynamoDBStreamEvent = { |
| 169 | + Records: [ |
| 170 | + { |
| 171 | + eventName: "MODIFY", |
| 172 | + dynamodb: { |
| 173 | + Keys: { id: { S: "123" } }, |
| 174 | + OldImage: buildInvalidLetter(), |
| 175 | + NewImage: {...buildInvalidLetter(), status: { S: "ACCEPTED" } }, |
| 176 | + }, |
| 177 | + } |
| 178 | + ], |
| 179 | + }; |
| 180 | + |
| 181 | + const handler = createHandler(mockedDeps); |
| 182 | + await expect(handler(event, mockDeep<Context>(), jest.fn())).rejects.toThrow(); |
| 183 | + |
| 184 | + expect(mockedDeps.kinesisClient.send).not.toHaveBeenCalled(); |
| 185 | + }); |
| 186 | + |
| 187 | + function buildValidLetter() { |
| 188 | + return { |
| 189 | + id: {S: "123"}, |
| 190 | + status: {S: "PENDING"}, |
| 191 | + specificationId: {S: "spec1"}, |
| 192 | + groupId: {S: "group1"}, |
| 193 | + }; |
| 194 | + } |
| 195 | + |
| 196 | + function buildInvalidLetter() { |
| 197 | + return { |
| 198 | + id: {S: "123"}, |
| 199 | + status: {S: "PENDING"}, |
| 200 | + }; |
| 201 | + } |
| 202 | +}); |
0 commit comments