Skip to content

Commit 5a2c153

Browse files
committed
add tests for redaction reports
1 parent 669cb2a commit 5a2c153

File tree

3 files changed

+215
-16
lines changed

3 files changed

+215
-16
lines changed

pkg/redact/redact_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,9 @@ func Test_Redactors(t *testing.T) {
16201620
}
16211621
]`
16221622

1623+
wantRedactionsLen := 38
1624+
wantRedactionsCount := 25
1625+
16231626
t.Run("test default redactors", func(t *testing.T) {
16241627
scopetest := scopeagent.StartTest(t)
16251628
defer scopetest.End()
@@ -1636,6 +1639,11 @@ func Test_Redactors(t *testing.T) {
16361639
req.NoError(err)
16371640

16381641
req.JSONEq(expected, string(redacted))
1642+
1643+
actualRedactions := GetRedactionList()
1644+
ResetRedactionList()
1645+
req.Len(actualRedactions.ByFile["testpath"], wantRedactionsLen)
1646+
req.Len(actualRedactions.ByRedactor, wantRedactionsCount)
16391647
})
16401648
}
16411649

pkg/redact/single_line_test.go

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,95 @@ import (
1111

1212
func TestNewSingleLineRedactor(t *testing.T) {
1313
tests := []struct {
14-
name string
15-
re string
16-
inputString string
17-
wantString string
14+
name string
15+
re string
16+
inputString string
17+
wantString string
18+
wantRedactions RedactionList
1819
}{
1920
{
2021
name: "copied from default redactors",
2122
re: `(?i)(Pwd *= *)(?P<mask>[^\;]+)(;)`,
2223
inputString: `pwd = abcdef;`,
2324
wantString: "pwd = ***HIDDEN***;\n",
25+
wantRedactions: RedactionList{
26+
ByRedactor: map[string][]Redaction{
27+
"copied from default redactors": []Redaction{
28+
{
29+
RedactorName: "copied from default redactors",
30+
CharactersRemoved: -6,
31+
Line: 1,
32+
File: "testfile",
33+
},
34+
},
35+
},
36+
ByFile: map[string][]Redaction{
37+
"testfile": []Redaction{
38+
{
39+
RedactorName: "copied from default redactors",
40+
CharactersRemoved: -6,
41+
Line: 1,
42+
File: "testfile",
43+
},
44+
},
45+
},
46+
},
2447
},
2548
{
2649
name: "no leading matching group", // this is not the ideal behavior - why are we dropping ungrouped match components?
2750
re: `(?i)Pwd *= *(?P<mask>[^\;]+)(;)`,
2851
inputString: `pwd = abcdef;`,
2952
wantString: "***HIDDEN***;\n",
53+
wantRedactions: RedactionList{
54+
ByRedactor: map[string][]Redaction{
55+
"no leading matching group": []Redaction{
56+
{
57+
RedactorName: "no leading matching group",
58+
CharactersRemoved: 0,
59+
Line: 1,
60+
File: "testfile",
61+
},
62+
},
63+
},
64+
ByFile: map[string][]Redaction{
65+
"testfile": []Redaction{
66+
{
67+
RedactorName: "no leading matching group",
68+
CharactersRemoved: 0,
69+
Line: 1,
70+
File: "testfile",
71+
},
72+
},
73+
},
74+
},
3075
},
3176
{
3277
name: "multiple matching literals",
3378
re: `(?i)(Pwd *= *)(?P<mask>[^\;]+)(;)`,
3479
inputString: `pwd = abcdef;abcdef`,
3580
wantString: "pwd = ***HIDDEN***;abcdef\n",
81+
wantRedactions: RedactionList{
82+
ByRedactor: map[string][]Redaction{
83+
"multiple matching literals": []Redaction{
84+
{
85+
RedactorName: "multiple matching literals",
86+
CharactersRemoved: -6,
87+
Line: 1,
88+
File: "testfile",
89+
},
90+
},
91+
},
92+
ByFile: map[string][]Redaction{
93+
"testfile": []Redaction{
94+
{
95+
RedactorName: "multiple matching literals",
96+
CharactersRemoved: -6,
97+
Line: 1,
98+
File: "testfile",
99+
},
100+
},
101+
},
102+
},
36103
},
37104
}
38105
for _, tt := range tests {
@@ -48,6 +115,10 @@ func TestNewSingleLineRedactor(t *testing.T) {
48115
gotBytes, err := ioutil.ReadAll(outReader)
49116
req.NoError(err)
50117
req.Equal(tt.wantString, string(gotBytes))
118+
119+
actualRedactions := GetRedactionList()
120+
ResetRedactionList()
121+
req.Equal(tt.wantRedactions, actualRedactions)
51122
})
52123
}
53124
}

pkg/redact/yaml_test.go

Lines changed: 132 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import (
1212

1313
func TestNewYamlRedactor(t *testing.T) {
1414
tests := []struct {
15-
name string
16-
path []string
17-
inputString string
18-
wantString string
15+
name string
16+
path []string
17+
inputString string
18+
wantString string
19+
wantRedactions RedactionList
1920
}{
2021
{
2122
name: "object paths",
@@ -32,6 +33,28 @@ xyz:
3233
xyz:
3334
hello: {}
3435
`,
36+
wantRedactions: RedactionList{
37+
ByRedactor: map[string][]Redaction{
38+
"object paths": []Redaction{
39+
{
40+
RedactorName: "object paths",
41+
CharactersRemoved: -3,
42+
Line: 0,
43+
File: "testfile",
44+
},
45+
},
46+
},
47+
ByFile: map[string][]Redaction{
48+
"testfile": []Redaction{
49+
{
50+
RedactorName: "object paths",
51+
CharactersRemoved: -3,
52+
Line: 0,
53+
File: "testfile",
54+
},
55+
},
56+
},
57+
},
3558
},
3659
{
3760
name: "one index in array",
@@ -50,6 +73,28 @@ xyz:
5073
xyz:
5174
hello: {}
5275
`,
76+
wantRedactions: RedactionList{
77+
ByRedactor: map[string][]Redaction{
78+
"one index in array": []Redaction{
79+
{
80+
RedactorName: "one index in array",
81+
CharactersRemoved: -13,
82+
Line: 0,
83+
File: "testfile",
84+
},
85+
},
86+
},
87+
ByFile: map[string][]Redaction{
88+
"testfile": []Redaction{
89+
{
90+
RedactorName: "one index in array",
91+
CharactersRemoved: -13,
92+
Line: 0,
93+
File: "testfile",
94+
},
95+
},
96+
},
97+
},
5398
},
5499
{
55100
name: "index after end of array",
@@ -68,6 +113,7 @@ abc:
68113
- b
69114
xyz:
70115
hello: {}`,
116+
wantRedactions: RedactionList{ByRedactor: map[string][]Redaction{}, ByFile: map[string][]Redaction{}},
71117
},
72118
{
73119
name: "non-integer index",
@@ -86,6 +132,7 @@ abc:
86132
- b
87133
xyz:
88134
hello: {}`,
135+
wantRedactions: RedactionList{ByRedactor: map[string][]Redaction{}, ByFile: map[string][]Redaction{}},
89136
},
90137
{
91138
name: "object paths, no matches",
@@ -104,6 +151,7 @@ abc:
104151
- b
105152
xyz:
106153
hello: {}`,
154+
wantRedactions: RedactionList{ByRedactor: map[string][]Redaction{}, ByFile: map[string][]Redaction{}},
107155
},
108156
{
109157
name: "star index in array",
@@ -122,6 +170,28 @@ xyz:
122170
xyz:
123171
hello: {}
124172
`,
173+
wantRedactions: RedactionList{
174+
ByRedactor: map[string][]Redaction{
175+
"star index in array": []Redaction{
176+
{
177+
RedactorName: "star index in array",
178+
CharactersRemoved: -26,
179+
Line: 0,
180+
File: "testfile",
181+
},
182+
},
183+
},
184+
ByFile: map[string][]Redaction{
185+
"testfile": []Redaction{
186+
{
187+
RedactorName: "star index in array",
188+
CharactersRemoved: -26,
189+
Line: 0,
190+
File: "testfile",
191+
},
192+
},
193+
},
194+
},
125195
},
126196
{
127197
name: "objects within array index in array",
@@ -140,18 +210,42 @@ xyz:
140210
xyz:
141211
hello: {}
142212
`,
213+
wantRedactions: RedactionList{
214+
ByRedactor: map[string][]Redaction{
215+
"objects within array index in array": []Redaction{
216+
{
217+
RedactorName: "objects within array index in array",
218+
CharactersRemoved: -9,
219+
Line: 0,
220+
File: "testfile",
221+
},
222+
},
223+
},
224+
ByFile: map[string][]Redaction{
225+
"testfile": []Redaction{
226+
{
227+
RedactorName: "objects within array index in array",
228+
CharactersRemoved: -9,
229+
Line: 0,
230+
File: "testfile",
231+
},
232+
},
233+
},
234+
},
143235
},
144236
{
145-
name: "non-yaml file",
146-
path: []string{""},
147-
inputString: `hello world, this is not valid yaml: {`,
148-
wantString: `hello world, this is not valid yaml: {`,
237+
name: "non-yaml file",
238+
path: []string{""},
239+
inputString: `hello world, this is not valid yaml: {`,
240+
wantString: `hello world, this is not valid yaml: {`,
241+
wantRedactions: RedactionList{ByRedactor: map[string][]Redaction{}, ByFile: map[string][]Redaction{}},
149242
},
150243
{
151-
name: "no matches",
152-
path: []string{"abc"},
153-
inputString: `improperly-formatted: yaml`,
154-
wantString: `improperly-formatted: yaml`,
244+
name: "no matches",
245+
path: []string{"abc"},
246+
inputString: `improperly-formatted: yaml`,
247+
wantString: `improperly-formatted: yaml`,
248+
wantRedactions: RedactionList{ByRedactor: map[string][]Redaction{}, ByFile: map[string][]Redaction{}},
155249
},
156250
{
157251
name: "star index in map",
@@ -172,6 +266,28 @@ xyz:
172266
xyz:
173267
hello: {}
174268
`,
269+
wantRedactions: RedactionList{
270+
ByRedactor: map[string][]Redaction{
271+
"star index in map": []Redaction{
272+
{
273+
RedactorName: "star index in map",
274+
CharactersRemoved: -39,
275+
Line: 0,
276+
File: "testfile",
277+
},
278+
},
279+
},
280+
ByFile: map[string][]Redaction{
281+
"testfile": []Redaction{
282+
{
283+
RedactorName: "star index in map",
284+
CharactersRemoved: -39,
285+
Line: 0,
286+
File: "testfile",
287+
},
288+
},
289+
},
290+
},
175291
},
176292
}
177293
for _, tt := range tests {
@@ -186,6 +302,10 @@ xyz:
186302
gotBytes, err := ioutil.ReadAll(outReader)
187303
req.NoError(err)
188304
req.Equal(tt.wantString, string(gotBytes))
305+
306+
actualRedactions := GetRedactionList()
307+
ResetRedactionList()
308+
req.Equal(tt.wantRedactions, actualRedactions)
189309
})
190310
}
191311
}

0 commit comments

Comments
 (0)