@@ -2,21 +2,31 @@ import { PiiRedactHandler } from "../src/pii-redact-handler";
22import * as zlib from "node:zlib" ;
33import { DynamoDBClient } from "@aws-sdk/client-dynamodb" ;
44import mocked = jest . mocked ;
5+ import { ResourceAlreadyExistsException } from "@aws-sdk/client-cloudwatch-logs" ;
6+ import { mockLogger } from "./utils" ;
57
6- let shouldMockThrowError = false ;
8+ let throwMockError : null | "Error" | "ResourceAlreadyExistsException" = null ;
79
810jest . mock ( "@aws-sdk/client-cloudwatch-logs" , ( ) => {
911 const actual = jest . requireActual ( "@aws-sdk/client-cloudwatch-logs" ) ;
1012 return {
1113 ...actual ,
1214 CloudWatchLogsClient : jest . fn ( ( ) => ( {
1315 send : jest . fn ( ) . mockImplementation ( ( command ) => {
14- if (
15- shouldMockThrowError &&
16- command instanceof actual . CreateLogStreamCommand
17- ) {
18- throw new Error ( "Error creating log stream" ) ;
16+ if ( command instanceof actual . CreateLogStreamCommand ) {
17+ switch ( throwMockError ) {
18+ case "ResourceAlreadyExistsException" : {
19+ throw new ResourceAlreadyExistsException ( {
20+ $metadata : { } ,
21+ message : "Resource already exists!" ,
22+ } ) ;
23+ }
24+ case "Error" : {
25+ throw new Error ( "Error creating log stream" ) ;
26+ }
27+ }
1928 }
29+
2030 return {
2131 message : "" ,
2232 } ;
@@ -44,6 +54,10 @@ describe("pii-redact-handler", () => {
4454 } ) ;
4555 }
4656
57+ beforeEach ( ( ) => {
58+ throwMockError = null ;
59+ } ) ;
60+
4761 it ( "should not fail when running the handler" , async ( ) => {
4862 const mockDynamoDbClient = mocked ( DynamoDBClient ) ;
4963 mockDynamoDbClient . prototype . send = jest . fn ( ) . mockReturnValue ( {
@@ -71,7 +85,10 @@ describe("pii-redact-handler", () => {
7185 data : compressedData ,
7286 } ,
7387 } ;
74- const handler = new PiiRedactHandler ( mockDynamoDbClient . prototype ) ;
88+ const handler = new PiiRedactHandler (
89+ mockDynamoDbClient . prototype ,
90+ mockLogger
91+ ) ;
7592 const result = await handler . handler ( event , { } ) ;
7693 expect ( result ) . toStrictEqual ( { } ) ;
7794 } ) ;
@@ -81,7 +98,7 @@ describe("pii-redact-handler", () => {
8198 mockDynamoDbClient . prototype . send = jest . fn ( ) . mockReturnValue ( {
8299 Count : 0 ,
83100 } ) ;
84- shouldMockThrowError = true ;
101+ throwMockError = "Error" ;
85102 const piiData = {
86103 owner : undefined ,
87104 logGroup : "dummy-log-group" ,
@@ -103,12 +120,50 @@ describe("pii-redact-handler", () => {
103120 data : compressedData ,
104121 } ,
105122 } ;
106- const handler = new PiiRedactHandler ( mockDynamoDbClient . prototype ) ;
123+ const handler = new PiiRedactHandler (
124+ mockDynamoDbClient . prototype ,
125+ mockLogger
126+ ) ;
107127
108128 await expect ( handler . handler ( event , { } ) ) . rejects . toThrow (
109129 new Error ( "Error creating log stream" )
110130 ) ;
111- shouldMockThrowError = false ;
131+ } ) ;
132+
133+ it ( "should not throw when a ResourceAlreadyExistsException error occurs creating a log stream" , async ( ) => {
134+ const mockDynamoDbClient = mocked ( DynamoDBClient ) ;
135+ mockDynamoDbClient . prototype . send = jest . fn ( ) . mockReturnValue ( {
136+ Count : 0 ,
137+ } ) ;
138+ throwMockError = "ResourceAlreadyExistsException" ;
139+ const piiData = {
140+ owner : undefined ,
141+ logGroup : "dummy-log-group" ,
142+ logStream : "dummy-log-stream" ,
143+ subscriptionFilters : undefined ,
144+ messageType : undefined ,
145+ logEvents : [
146+ {
147+ id : undefined ,
148+ timestamp : undefined ,
149+ message : "{}" ,
150+ extractedFields : undefined ,
151+ } ,
152+ ] ,
153+ } ;
154+ const compressedData = await encode ( JSON . stringify ( piiData ) ) ;
155+ const event = {
156+ awslogs : {
157+ data : compressedData ,
158+ } ,
159+ } ;
160+ const handler = new PiiRedactHandler (
161+ mockDynamoDbClient . prototype ,
162+ mockLogger
163+ ) ;
164+
165+ const result = await handler . handler ( event , { } ) ;
166+ expect ( result ) . toStrictEqual ( { } ) ;
112167 } ) ;
113168
114169 it ( "should create log stream when does not exist" , async ( ) => {
@@ -138,7 +193,10 @@ describe("pii-redact-handler", () => {
138193 data : compressedData ,
139194 } ,
140195 } ;
141- const handler = new PiiRedactHandler ( mockDynamoDbClient . prototype ) ;
196+ const handler = new PiiRedactHandler (
197+ mockDynamoDbClient . prototype ,
198+ mockLogger
199+ ) ;
142200 const result = await handler . handler ( event , { } ) ;
143201
144202 expect ( result ) . toStrictEqual ( { } ) ;
@@ -171,7 +229,10 @@ describe("pii-redact-handler", () => {
171229 data : compressedData ,
172230 } ,
173231 } ;
174- const handler = new PiiRedactHandler ( mockDynamoDbClient . prototype ) ;
232+ const handler = new PiiRedactHandler (
233+ mockDynamoDbClient . prototype ,
234+ mockLogger
235+ ) ;
175236 const result = await handler . handler ( event , { } ) ;
176237
177238 expect ( result ) . toStrictEqual ( { } ) ;
0 commit comments