1- import { KinesisClient } from ' @aws-sdk/client-kinesis' ;
1+ import { KinesisClient } from " @aws-sdk/client-kinesis" ;
22import * 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' ;
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" ;
88
9- describe ( ' letter-stream-forwarder Lambda' , ( ) => {
9+ describe ( " letter-stream-forwarder Lambda" , ( ) => {
1010
1111 const mockedDeps : jest . Mocked < Deps > = {
1212 kinesisClient : { send : jest . fn ( ) } as unknown as KinesisClient ,
@@ -21,15 +21,15 @@ describe('letter-stream-forwarder Lambda', () => {
2121 } ) ;
2222
2323
24- it ( ' forwards status changes to Kinesis' , async ( ) => {
24+ it ( " forwards status changes to Kinesis" , async ( ) => {
2525 const event : DynamoDBStreamEvent = {
2626 Records : [
2727 {
28- eventName : ' MODIFY' ,
28+ eventName : " MODIFY" ,
2929 dynamodb : {
30- Keys : { id : { S : ' 123' } } ,
31- OldImage : { status : { S : 'PENDING' } , id : { S : '123' } } ,
32- NewImage : { status : { S : ' ACCEPTED' } , id : { S : '123' } } ,
30+ Keys : { id : { S : " 123" } } ,
31+ OldImage : buildValidLetter ( ) ,
32+ NewImage : { ... buildValidLetter ( ) , status : { S : " ACCEPTED" } } ,
3333 } ,
3434 } ,
3535 ] ,
@@ -41,23 +41,43 @@ describe('letter-stream-forwarder Lambda', () => {
4141 expect ( mockedDeps . kinesisClient . send ) . toHaveBeenCalledWith (
4242 expect . objectContaining ( {
4343 input : expect . objectContaining ( {
44- StreamARN : ' test-stream.arn' ,
45- PartitionKey : ' 123' ,
44+ StreamARN : " test-stream.arn" ,
45+ PartitionKey : " 123" ,
4646 } ) ,
4747 } )
4848 ) ;
4949 } ) ;
5050
5151
52- it ( 'forwards to Kinesis if a reason code is added' , async ( ) => {
52+ it ( "does not forward invalid status changes" , async ( ) => {
5353 const event : DynamoDBStreamEvent = {
5454 Records : [
5555 {
56- eventName : ' MODIFY' ,
56+ eventName : " MODIFY" ,
5757 dynamodb : {
58- Keys : { id : { S : '123' } } ,
59- OldImage : { status : { S : 'PENDING' } , id : { S : '123' } } ,
60- NewImage : { status : { S : 'PENDING' } , id : { S : '123' } , reasonCode : { S : 'r1' } } ,
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" } } ,
6181 } ,
6282 } ,
6383 ] ,
@@ -69,23 +89,23 @@ describe('letter-stream-forwarder Lambda', () => {
6989 expect ( mockedDeps . kinesisClient . send ) . toHaveBeenCalledWith (
7090 expect . objectContaining ( {
7191 input : expect . objectContaining ( {
72- StreamARN : ' test-stream.arn' ,
73- PartitionKey : ' 123' ,
92+ StreamARN : " test-stream.arn" ,
93+ PartitionKey : " 123" ,
7494 } ) ,
7595 } )
7696 ) ;
7797 } ) ;
7898
7999
80- it ( ' forwards to Kinesis if a reason code is changed' , async ( ) => {
100+ it ( " forwards to Kinesis if a reason code is changed" , async ( ) => {
81101 const event : DynamoDBStreamEvent = {
82102 Records : [
83103 {
84- eventName : ' MODIFY' ,
104+ eventName : " MODIFY" ,
85105 dynamodb : {
86- Keys : { id : { S : ' 123' } } ,
87- OldImage : { status : { S : 'PENDING' } , id : { S : '123' } , reasonCode : { S : 'r1' } } ,
88- NewImage : { status : { S : 'PENDING' } , id : { S : '123' } , reasonCode : { S : 'r2' } } ,
106+ Keys : { id : { S : " 123" } } ,
107+ OldImage : { ... buildValidLetter ( ) , reasonCode : { S : "r1" } } ,
108+ NewImage : { ... buildValidLetter ( ) , reasonCode : { S : "r2" } } ,
89109 } ,
90110 } ,
91111 ] ,
@@ -97,22 +117,22 @@ describe('letter-stream-forwarder Lambda', () => {
97117 expect ( mockedDeps . kinesisClient . send ) . toHaveBeenCalledWith (
98118 expect . objectContaining ( {
99119 input : expect . objectContaining ( {
100- StreamARN : ' test-stream.arn' ,
101- PartitionKey : ' 123' ,
120+ StreamARN : " test-stream.arn" ,
121+ PartitionKey : " 123" ,
102122 } ) ,
103123 } )
104124 ) ;
105125 } ) ;
106126
107- it ( ' does not forward if neither status nor reason code changed' , async ( ) => {
127+ it ( " does not forward if neither status nor reason code changed" , async ( ) => {
108128 const event : DynamoDBStreamEvent = {
109129 Records : [
110130 {
111- eventName : ' MODIFY' ,
131+ eventName : " MODIFY" ,
112132 dynamodb : {
113- Keys : { id : { S : ' 123' } } ,
114- OldImage : { status : { S : 'PENDING' } , id : { S : '123' } } ,
115- NewImage : { status : { S : 'PENDING' } , id : { S : '123' } } ,
133+ Keys : { id : { S : " 123" } } ,
134+ OldImage : buildValidLetter ( ) ,
135+ NewImage : buildValidLetter ( ) ,
116136 } ,
117137 } ,
118138 ] ,
@@ -124,14 +144,14 @@ describe('letter-stream-forwarder Lambda', () => {
124144 expect ( mockedDeps . kinesisClient . send ) . not . toHaveBeenCalled ( ) ;
125145 } ) ;
126146
127- it ( ' does not forward non-MODIFY events' , async ( ) => {
147+ it ( " does not forward non-MODIFY events" , async ( ) => {
128148 const event : DynamoDBStreamEvent = {
129149 Records : [
130150 {
131- eventName : ' INSERT' ,
151+ eventName : " INSERT" ,
132152 dynamodb : {
133- Keys : { id : { S : ' 123' } } ,
134- NewImage : { status : { S : 'PENDING' } , id : { S : '123' } } ,
153+ Keys : { id : { S : " 123" } } ,
154+ NewImage : buildValidLetter ( ) ,
135155 } ,
136156 } ,
137157 ] ,
@@ -142,4 +162,41 @@ describe('letter-stream-forwarder Lambda', () => {
142162
143163 expect ( mockedDeps . kinesisClient . send ) . not . toHaveBeenCalled ( ) ;
144164 } ) ;
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+ }
145202} ) ;
0 commit comments