-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathredactor_add_test.go
More file actions
108 lines (97 loc) · 2.48 KB
/
redactor_add_test.go
File metadata and controls
108 lines (97 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package clicommand_test
import (
"encoding/json"
"errors"
"slices"
"strings"
"testing"
"github.com/buildkite/agent/v3/clicommand"
"github.com/buildkite/agent/v3/logger"
"github.com/google/go-cmp/cmp"
)
func TestParseSecrets(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
inputData string
formatString string
applyVarsFilter bool
wantSecrets []string
}{
{
name: "json",
inputData: `{"hello": "world", "password": "hunter2"}`,
formatString: clicommand.FormatStringJSON,
wantSecrets: []string{"world", "hunter2"},
},
{
name: "plaintext",
inputData: "hunter2\n",
formatString: clicommand.FormatStringNone,
wantSecrets: []string{"hunter2"},
},
{
name: "vars filter",
inputData: `{"HELLO": "1", "MY_PASSWORD": "hunter2"}`,
applyVarsFilter: true,
formatString: clicommand.FormatStringJSON,
wantSecrets: []string{"hunter2"},
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
input := strings.NewReader(tc.inputData)
secrets, err := clicommand.ParseSecrets(
logger.Discard,
clicommand.RedactorAddConfig{
Format: tc.formatString,
ApplyVarsFilter: tc.applyVarsFilter,
RedactedVars: *clicommand.RedactedVars.Value,
},
input,
)
if err != nil {
t.Errorf("clicommand.ParseSecrets(logger, cfg, %q) error = %v", input, err)
}
slices.Sort(secrets)
slices.Sort(tc.wantSecrets)
if diff := cmp.Diff(secrets, tc.wantSecrets); diff != "" {
t.Errorf("clicommand.ParseSecrets(logger, cfg, %q) secrets diff (-got +want):\n%s", input, diff)
}
})
}
}
func TestParseSecrets_JSONErrors(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
inputData string
wantError any
}{
{
name: "type mismatch",
inputData: `{"hello": 1, "password": "hunter2"}`,
wantError: new(*json.UnmarshalTypeError),
},
{
name: "syntax error",
inputData: `}}{"hello": , "pas: "hun'}`,
wantError: new(*json.SyntaxError),
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
input := strings.NewReader(tc.inputData)
_, err := clicommand.ParseSecrets(
logger.Discard,
clicommand.RedactorAddConfig{
Format: clicommand.FormatStringJSON,
},
input,
)
if !errors.As(err, tc.wantError) {
t.Errorf("clicommand.ParseSecrets(logger, cfg, %q) error = %v, want error wrapping %T", input, err, tc.wantError)
}
})
}
}