From e31ea464ac678e6f5c2d0197ae29e68d0c04e7a7 Mon Sep 17 00:00:00 2001 From: Matthew Bonig Date: Thu, 27 Feb 2020 14:55:59 -0700 Subject: [PATCH] adding additional tests --- packages/@aws-cdk/aws-iam/lib/policy-statement.ts | 3 +++ .../aws-iam/test/policy-statement.test.ts | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/packages/@aws-cdk/aws-iam/lib/policy-statement.ts b/packages/@aws-cdk/aws-iam/lib/policy-statement.ts index 3d47948998bbc..83c223ddbd957 100644 --- a/packages/@aws-cdk/aws-iam/lib/policy-statement.ts +++ b/packages/@aws-cdk/aws-iam/lib/policy-statement.ts @@ -20,6 +20,9 @@ export class PolicyStatement { if (typeof (field) !== "string" && !Array.isArray(field)) { throw new Error("Fields must be either a string or an array of strings"); } + if (Array.isArray(field) && !field.reduce((acc: boolean, f: any) => (acc && typeof (f) === "string"), true)) { + throw new Error("Fields must be either a string or an array of strings"); + } return Array.isArray(field) ? field : [field]; }; diff --git a/packages/@aws-cdk/aws-iam/test/policy-statement.test.ts b/packages/@aws-cdk/aws-iam/test/policy-statement.test.ts index 34035cda07290..8496a865f228f 100644 --- a/packages/@aws-cdk/aws-iam/test/policy-statement.test.ts +++ b/packages/@aws-cdk/aws-iam/test/policy-statement.test.ts @@ -237,6 +237,21 @@ describe('IAM policy statement', () => { expect(stack.resolve(doc)).toEqual(policyDocument); }); + test('throws error with field data being object', () => { + expect(() => { + PolicyStatement.fromJson({ + Action: {} + }); + }).toThrow(/Fields must be either a string or an array of strings/); + }); + + test('throws error with field data being object', () => { + expect(() => { + PolicyStatement.fromJson({ + Action: [{}] + }); + }).toThrow(/Fields must be either a string or an array of strings/); + }); }); });