forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredentials_test.go
133 lines (126 loc) · 2.62 KB
/
credentials_test.go
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
124
125
126
127
128
129
130
131
132
133
package runner
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestParseCredentialOverrides(t *testing.T) {
cases := []struct {
name string
envs map[string]string
in []string
out map[string]map[string]string
expectErr bool
}{
{
name: "nil",
in: nil,
out: map[string]map[string]string{},
},
{
name: "empty",
in: []string{""},
expectErr: true,
},
{
name: "single cred, single env",
envs: map[string]string{
"ENV1": "VALUE1",
},
in: []string{"cred1:ENV1"},
out: map[string]map[string]string{
"cred1": {
"ENV1": "VALUE1",
},
},
},
{
name: "single cred, multiple envs",
envs: map[string]string{
"ENV1": "VALUE1",
"ENV2": "VALUE2",
},
in: []string{"cred1:ENV1,ENV2"},
out: map[string]map[string]string{
"cred1": {
"ENV1": "VALUE1",
"ENV2": "VALUE2",
},
},
},
{
name: "single cred, key value pairs",
envs: map[string]string{
"ENV1": "VALUE1",
"ENV2": "VALUE2",
},
in: []string{"cred1:ENV1=OTHERVALUE1,ENV2=OTHERVALUE2"},
out: map[string]map[string]string{
"cred1": {
"ENV1": "OTHERVALUE1",
"ENV2": "OTHERVALUE2",
},
},
},
{
name: "multiple creds, multiple envs",
envs: map[string]string{
"ENV1": "VALUE1",
"ENV2": "VALUE2",
},
in: []string{"cred1:ENV1,ENV2", "cred2:ENV1,ENV2"},
out: map[string]map[string]string{
"cred1": {
"ENV1": "VALUE1",
"ENV2": "VALUE2",
},
"cred2": {
"ENV1": "VALUE1",
"ENV2": "VALUE2",
},
},
},
{
name: "multiple creds, key value pairs",
envs: map[string]string{
"ENV1": "VALUE1",
"ENV2": "VALUE2",
},
in: []string{"cred1:ENV1=OTHERVALUE1,ENV2=OTHERVALUE2", "cred2:ENV1=OTHERVALUE3,ENV2=OTHERVALUE4"},
out: map[string]map[string]string{
"cred1": {
"ENV1": "OTHERVALUE1",
"ENV2": "OTHERVALUE2",
},
"cred2": {
"ENV1": "OTHERVALUE3",
"ENV2": "OTHERVALUE4",
},
},
},
{
name: "invalid format",
in: []string{"cred1=ENV1,ENV2"},
expectErr: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
envs := tc.envs
if envs == nil {
envs = map[string]string{}
}
for k, v := range envs {
_ = os.Setenv(k, v)
}
out, err := parseCredentialOverrides(tc.in)
if tc.expectErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, len(tc.out), len(out), "expected %d creds, but got %d", len(tc.out), len(out))
require.Equal(t, tc.out, out, "expected output %v, but got %v", tc.out, out)
})
}
}