Skip to content

Commit 9a31c3d

Browse files
committed
Add tests for filterSensitiveLog
1 parent 1bb22be commit 9a31c3d

File tree

9 files changed

+255
-7
lines changed

9 files changed

+255
-7
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private void writeStructureFilterSensitiveLog(
109109
return;
110110
}
111111
// Call filterSensitiveLog on Structure.
112-
writer.openBlock("$T.filterSensitiveLog($L)", symbolProvider.toSymbol(structureTarget), structureParam);
112+
writer.write("$T.filterSensitiveLog($L)", symbolProvider.toSymbol(structureTarget), structureParam);
113113
}
114114

115115
/**

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,92 @@ public void properlyGeneratesRequiredMessageMemberOfException() {
4343
+ "}");
4444
}
4545

46-
public void testErrorStructureCodegen(String file, String expectedType) {
46+
@Test
47+
public void filtersSensitiveSimpleShape() {
48+
testStructureCodegen("test-sensitive-simple-shape.smithy",
49+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
50+
+ " ...obj,\n"
51+
+ " ...(obj.password && { password:\n"
52+
+ " SENSITIVE_STRING\n"
53+
+ " }),\n"
54+
+ " })\n");
55+
}
56+
57+
@Test
58+
public void callsFilterForStructureWithSensitiveData() {
59+
testStructureCodegen("test-structure-with-sensitive-data.smithy",
60+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
61+
+ " ...obj,\n"
62+
+ " ...(obj.foo && { foo:\n"
63+
+ " User.filterSensitiveLog(obj.foo)\n"
64+
+ " }),\n"
65+
+ " })\n");
66+
}
67+
68+
@Test
69+
public void filtersSensitiveStructure() {
70+
testStructureCodegen("test-sensitive-structure.smithy",
71+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
72+
+ " ...obj,\n"
73+
+ " ...(obj.foo && { foo:\n"
74+
+ " SENSITIVE_STRING\n"
75+
+ " }),\n"
76+
+ " })\n");
77+
}
78+
79+
@Test
80+
public void callsFilterForListWithSensitiveData() {
81+
testStructureCodegen("test-list-with-sensitive-data.smithy",
82+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
83+
+ " ...obj,\n"
84+
+ " ...(obj.foo && { foo:\n"
85+
+ " obj.foo.map(\n"
86+
+ " item =>\n"
87+
+ " User.filterSensitiveLog(item)\n"
88+
+ " )\n"
89+
+ " }),\n"
90+
+ " })\n");
91+
}
92+
93+
@Test
94+
public void filtersSensitiveList() {
95+
testStructureCodegen("test-sensitive-list.smithy",
96+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
97+
+ " ...obj,\n"
98+
+ " ...(obj.foo && { foo:\n"
99+
+ " SENSITIVE_STRING\n"
100+
+ " }),\n"
101+
+ " })\n");
102+
}
103+
104+
@Test
105+
public void callsFilterForMapWithSensitiveData() {
106+
testStructureCodegen("test-map-with-sensitive-data.smithy",
107+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
108+
+ " ...obj,\n"
109+
+ " ...(obj.foo && { foo:\n"
110+
+ " Object.entries(obj.foo).reduce((acc: any, [key, value]: [string, User]) => ({\n"
111+
+ " ...acc,\n"
112+
+ " [key]:\n"
113+
+ " User.filterSensitiveLog(value)\n"
114+
+ " ,\n"
115+
+ " }), {})\n"
116+
+ " }),\n"
117+
+ " })\n");
118+
}
119+
120+
@Test
121+
public void filtersSensitiveMap() {
122+
testStructureCodegen("test-sensitive-map.smithy",
123+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
124+
+ " ...obj,\n"
125+
+ " ...(obj.foo && { foo:\n"
126+
+ " SENSITIVE_STRING\n"
127+
+ " }),\n"
128+
+ " })\n");
129+
}
130+
131+
private String testStructureCodegen(String file, String expectedType) {
47132
Model model = Model.assembler()
48133
.addImport(getClass().getResource(file))
49134
.assemble()
@@ -53,18 +138,24 @@ public void testErrorStructureCodegen(String file, String expectedType) {
53138
.model(model)
54139
.fileManifest(manifest)
55140
.settings(Node.objectNodeBuilder()
56-
.withMember("service", Node.from("smithy.example#Example"))
57-
.withMember("package", Node.from("example"))
58-
.withMember("packageVersion", Node.from("1.0.0"))
59-
.build())
141+
.withMember("service", Node.from("smithy.example#Example"))
142+
.withMember("package", Node.from("example"))
143+
.withMember("packageVersion", Node.from("1.0.0"))
144+
.build())
60145
.build();
61146

62147
new TypeScriptCodegenPlugin().execute(context);
63148
String contents = manifest.getFileString("/models/index.ts").get();
64149

150+
assertThat(contents, containsString(expectedType));
151+
return contents;
152+
}
153+
154+
private void testErrorStructureCodegen(String file, String expectedType) {
155+
String contents = testStructureCodegen(file, expectedType);
156+
65157
assertThat(contents, containsString("as __isa"));
66158
assertThat(contents, containsString("as __SmithyException"));
67-
assertThat(contents, containsString(expectedType));
68159
assertThat(contents, containsString("namespace Err {"));
69160
assertThat(contents, containsString(" export const isa = (o: any): o is Err => "
70161
+ "__isa(o, \"Err\");\n"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
foo: UserList
13+
}
14+
15+
list UserList {
16+
member: User
17+
}
18+
19+
structure User {
20+
username: String,
21+
22+
@sensitive
23+
password: String
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
foo: UserMap
13+
}
14+
15+
map UserMap {
16+
key: String,
17+
value: User
18+
}
19+
20+
structure User {
21+
username: String,
22+
23+
@sensitive
24+
password: String
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
@sensitive
13+
foo: UserList
14+
}
15+
16+
list UserList {
17+
member: User
18+
}
19+
20+
structure User {
21+
username: String,
22+
23+
@sensitive
24+
password: String
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
@sensitive
13+
foo: UserMap
14+
}
15+
16+
map UserMap {
17+
key: String,
18+
value: User
19+
}
20+
21+
structure User {
22+
username: String,
23+
24+
@sensitive
25+
password: String
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
username: String,
13+
14+
@sensitive
15+
password: String
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
@sensitive
13+
foo: User
14+
}
15+
16+
structure User {
17+
username: String,
18+
19+
@sensitive
20+
password: String
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
foo: User
13+
}
14+
15+
structure User {
16+
username: String,
17+
18+
@sensitive
19+
password: String
20+
}

0 commit comments

Comments
 (0)