Skip to content

Commit

Permalink
Add tests for filterSensitiveLog
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed May 21, 2020
1 parent 1bb22be commit 9a31c3d
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 9a31c3d

Please sign in to comment.