From 9a31c3d5931fb12e8dfaab85d363e78d6e9e548b Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Thu, 21 May 2020 02:27:25 +0000 Subject: [PATCH] Add tests for filterSensitiveLog --- .../codegen/StructuredMemberWriter.java | 2 +- .../codegen/StructureGeneratorTest.java | 103 +++++++++++++++++- .../test-list-with-sensitive-data.smithy | 24 ++++ .../test-map-with-sensitive-data.smithy | 25 +++++ .../codegen/test-sensitive-list.smithy | 25 +++++ .../codegen/test-sensitive-map.smithy | 26 +++++ .../test-sensitive-simple-shape.smithy | 16 +++ .../codegen/test-sensitive-structure.smithy | 21 ++++ .../test-structure-with-sensitive-data.smithy | 20 ++++ 9 files changed, 255 insertions(+), 7 deletions(-) create mode 100644 smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-list-with-sensitive-data.smithy create mode 100644 smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-map-with-sensitive-data.smithy create mode 100644 smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-list.smithy create mode 100644 smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-map.smithy create mode 100644 smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-simple-shape.smithy create mode 100644 smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-structure.smithy create mode 100644 smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-structure-with-sensitive-data.smithy diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java index e6fbccc7412..2160267a053 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java @@ -109,7 +109,7 @@ private void writeStructureFilterSensitiveLog( return; } // Call filterSensitiveLog on Structure. - writer.openBlock("$T.filterSensitiveLog($L)", symbolProvider.toSymbol(structureTarget), structureParam); + writer.write("$T.filterSensitiveLog($L)", symbolProvider.toSymbol(structureTarget), structureParam); } /** diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java index 82553ac64f1..8d206d34367 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java @@ -43,7 +43,92 @@ public void properlyGeneratesRequiredMessageMemberOfException() { + "}"); } - public void testErrorStructureCodegen(String file, String expectedType) { + @Test + public void filtersSensitiveSimpleShape() { + testStructureCodegen("test-sensitive-simple-shape.smithy", + " export const filterSensitiveLog = (obj: GetFooInput): any => ({\n" + + " ...obj,\n" + + " ...(obj.password && { password:\n" + + " SENSITIVE_STRING\n" + + " }),\n" + + " })\n"); + } + + @Test + public void callsFilterForStructureWithSensitiveData() { + testStructureCodegen("test-structure-with-sensitive-data.smithy", + " export const filterSensitiveLog = (obj: GetFooInput): any => ({\n" + + " ...obj,\n" + + " ...(obj.foo && { foo:\n" + + " User.filterSensitiveLog(obj.foo)\n" + + " }),\n" + + " })\n"); + } + + @Test + public void filtersSensitiveStructure() { + testStructureCodegen("test-sensitive-structure.smithy", + " export const filterSensitiveLog = (obj: GetFooInput): any => ({\n" + + " ...obj,\n" + + " ...(obj.foo && { foo:\n" + + " SENSITIVE_STRING\n" + + " }),\n" + + " })\n"); + } + + @Test + public void callsFilterForListWithSensitiveData() { + testStructureCodegen("test-list-with-sensitive-data.smithy", + " export const filterSensitiveLog = (obj: GetFooInput): any => ({\n" + + " ...obj,\n" + + " ...(obj.foo && { foo:\n" + + " obj.foo.map(\n" + + " item =>\n" + + " User.filterSensitiveLog(item)\n" + + " )\n" + + " }),\n" + + " })\n"); + } + + @Test + public void filtersSensitiveList() { + testStructureCodegen("test-sensitive-list.smithy", + " export const filterSensitiveLog = (obj: GetFooInput): any => ({\n" + + " ...obj,\n" + + " ...(obj.foo && { foo:\n" + + " SENSITIVE_STRING\n" + + " }),\n" + + " })\n"); + } + + @Test + public void callsFilterForMapWithSensitiveData() { + testStructureCodegen("test-map-with-sensitive-data.smithy", + " export const filterSensitiveLog = (obj: GetFooInput): any => ({\n" + + " ...obj,\n" + + " ...(obj.foo && { foo:\n" + + " Object.entries(obj.foo).reduce((acc: any, [key, value]: [string, User]) => ({\n" + + " ...acc,\n" + + " [key]:\n" + + " User.filterSensitiveLog(value)\n" + + " ,\n" + + " }), {})\n" + + " }),\n" + + " })\n"); + } + + @Test + public void filtersSensitiveMap() { + testStructureCodegen("test-sensitive-map.smithy", + " export const filterSensitiveLog = (obj: GetFooInput): any => ({\n" + + " ...obj,\n" + + " ...(obj.foo && { foo:\n" + + " SENSITIVE_STRING\n" + + " }),\n" + + " })\n"); + } + + private String testStructureCodegen(String file, String expectedType) { Model model = Model.assembler() .addImport(getClass().getResource(file)) .assemble() @@ -53,18 +138,24 @@ public void testErrorStructureCodegen(String file, String expectedType) { .model(model) .fileManifest(manifest) .settings(Node.objectNodeBuilder() - .withMember("service", Node.from("smithy.example#Example")) - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()) + .withMember("service", Node.from("smithy.example#Example")) + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()) .build(); new TypeScriptCodegenPlugin().execute(context); String contents = manifest.getFileString("/models/index.ts").get(); + assertThat(contents, containsString(expectedType)); + return contents; + } + + private void testErrorStructureCodegen(String file, String expectedType) { + String contents = testStructureCodegen(file, expectedType); + assertThat(contents, containsString("as __isa")); assertThat(contents, containsString("as __SmithyException")); - assertThat(contents, containsString(expectedType)); assertThat(contents, containsString("namespace Err {")); assertThat(contents, containsString(" export const isa = (o: any): o is Err => " + "__isa(o, \"Err\");\n")); diff --git a/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-list-with-sensitive-data.smithy b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-list-with-sensitive-data.smithy new file mode 100644 index 00000000000..3b9507d796e --- /dev/null +++ b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-list-with-sensitive-data.smithy @@ -0,0 +1,24 @@ +namespace smithy.example + +@protocols([{name: "aws.rest-json-1.1"}]) +service Example { + version: "1.0.0", + operations: [GetFoo] +} + +operation GetFoo(GetFooInput) + +structure GetFooInput { + foo: UserList +} + +list UserList { + member: User +} + +structure User { + username: String, + + @sensitive + password: String +} \ No newline at end of file diff --git a/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-map-with-sensitive-data.smithy b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-map-with-sensitive-data.smithy new file mode 100644 index 00000000000..e678ed6cceb --- /dev/null +++ b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-map-with-sensitive-data.smithy @@ -0,0 +1,25 @@ +namespace smithy.example + +@protocols([{name: "aws.rest-json-1.1"}]) +service Example { + version: "1.0.0", + operations: [GetFoo] +} + +operation GetFoo(GetFooInput) + +structure GetFooInput { + foo: UserMap +} + +map UserMap { + key: String, + value: User +} + +structure User { + username: String, + + @sensitive + password: String +} \ No newline at end of file diff --git a/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-list.smithy b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-list.smithy new file mode 100644 index 00000000000..a484f206665 --- /dev/null +++ b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-list.smithy @@ -0,0 +1,25 @@ +namespace smithy.example + +@protocols([{name: "aws.rest-json-1.1"}]) +service Example { + version: "1.0.0", + operations: [GetFoo] +} + +operation GetFoo(GetFooInput) + +structure GetFooInput { + @sensitive + foo: UserList +} + +list UserList { + member: User +} + +structure User { + username: String, + + @sensitive + password: String +} \ No newline at end of file diff --git a/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-map.smithy b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-map.smithy new file mode 100644 index 00000000000..9c4a49696ec --- /dev/null +++ b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-map.smithy @@ -0,0 +1,26 @@ +namespace smithy.example + +@protocols([{name: "aws.rest-json-1.1"}]) +service Example { + version: "1.0.0", + operations: [GetFoo] +} + +operation GetFoo(GetFooInput) + +structure GetFooInput { + @sensitive + foo: UserMap +} + +map UserMap { + key: String, + value: User +} + +structure User { + username: String, + + @sensitive + password: String +} \ No newline at end of file diff --git a/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-simple-shape.smithy b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-simple-shape.smithy new file mode 100644 index 00000000000..dd7072376a6 --- /dev/null +++ b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-simple-shape.smithy @@ -0,0 +1,16 @@ +namespace smithy.example + +@protocols([{name: "aws.rest-json-1.1"}]) +service Example { + version: "1.0.0", + operations: [GetFoo] +} + +operation GetFoo(GetFooInput) + +structure GetFooInput { + username: String, + + @sensitive + password: String +} \ No newline at end of file diff --git a/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-structure.smithy b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-structure.smithy new file mode 100644 index 00000000000..ad0e2495f25 --- /dev/null +++ b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-sensitive-structure.smithy @@ -0,0 +1,21 @@ +namespace smithy.example + +@protocols([{name: "aws.rest-json-1.1"}]) +service Example { + version: "1.0.0", + operations: [GetFoo] +} + +operation GetFoo(GetFooInput) + +structure GetFooInput { + @sensitive + foo: User +} + +structure User { + username: String, + + @sensitive + password: String +} \ No newline at end of file diff --git a/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-structure-with-sensitive-data.smithy b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-structure-with-sensitive-data.smithy new file mode 100644 index 00000000000..a5f2f1501d5 --- /dev/null +++ b/smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/test-structure-with-sensitive-data.smithy @@ -0,0 +1,20 @@ +namespace smithy.example + +@protocols([{name: "aws.rest-json-1.1"}]) +service Example { + version: "1.0.0", + operations: [GetFoo] +} + +operation GetFoo(GetFooInput) + +structure GetFooInput { + foo: User +} + +structure User { + username: String, + + @sensitive + password: String +} \ No newline at end of file