-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsecret_test.go
More file actions
123 lines (115 loc) · 2.85 KB
/
secret_test.go
File metadata and controls
123 lines (115 loc) · 2.85 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package flags
import (
"io"
"testing"
"testing/fstest"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
)
type testFile struct {
path, content string
}
func TestSecretFlag(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
want *string
file *testFile
stdin string
wantErr bool
wantStdErr string
}{
{
name: "no value: prompts",
value: "",
want: utils.Ptr("from stdin"),
stdin: "from stdin",
},
{
name: "a value: prints deprecation",
value: "a value",
want: utils.Ptr("a value"),
wantStdErr: "Warning: Passing a secret value on the command line is insecure and deprecated. This usage will stop working October 2026.\n",
},
{
name: "from an existing file",
value: "@some-file.txt",
want: utils.Ptr("from file"),
file: &testFile{
path: "some-file.txt",
content: "from file",
},
},
{
name: "from a non-existing file",
value: "@some-file-with-typo.txt",
wantErr: true,
file: &testFile{
path: "some-file.txt",
content: "from file",
},
},
{
name: "from an existing double-quoted file",
value: `@"some-file.txt"`,
want: utils.Ptr("from file"),
file: &testFile{
path: "some-file.txt",
content: "from file",
},
},
{
name: "from an existing single-quoted file",
value: "@'some-file.txt'",
want: utils.Ptr("from file"),
file: &testFile{
path: "some-file.txt",
content: "from file",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
params := testparams.NewTestParams()
if tt.file != nil {
params.CmdParams.Fs = fstest.MapFS{
tt.file.path: &fstest.MapFile{
Data: []byte(tt.file.content),
},
}
}
flag := SecretFlag("test", params.CmdParams)
cmd := cobra.Command{}
cmd.Flags().Var(flag, "test", flag.Usage())
if tt.stdin != "" {
params.In.WriteString(tt.stdin)
params.In.WriteString("\n")
}
if tt.value != "" { // emulate pflag only calling set when flag is specified on the command line
err := cmd.Flags().Set("test", tt.value)
if err != nil && !tt.wantErr {
t.Fatalf("unexpected error: %v", err)
}
if err == nil && tt.wantErr {
t.Fatalf("expected error, got none")
}
}
got := SecretFlagToStringPointer(params.Printer, &cmd, "test")
if got != tt.want && *got != *tt.want {
t.Fatalf("unexpected value: got %q, want %q", *got, *tt.want)
}
if tt.wantStdErr != "" {
message, err := params.Err.ReadString('\n')
if err != nil && err != io.EOF {
t.Fatalf("reading stderr: %v", err)
}
if message != tt.wantStdErr {
t.Fatalf("unexpected stderr: got %q, want %q", message, tt.wantStdErr)
}
}
})
}
}