-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
368 lines (361 loc) · 9.19 KB
/
main_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"reflect"
"testing"
)
type MockHttpClient struct {
response *http.Response
err error
}
func (m *MockHttpClient) Do(req *http.Request) (*http.Response, error) {
return m.response, m.err
}
func TestParseRetrieveFlag(t *testing.T) {
cases := []struct {
name string
retrieve string
want map[string]map[string]string
wantErr error
}{
{
name: "empty string",
retrieve: "",
want: make(map[string]map[string]string),
wantErr: nil,
},
{
name: "happy path",
retrieve: `
folder1/folder2/secret1 mykey1 as key1
folder1/folder2/secret1 mykey2 as key2
folder1/folder2/secret2 mykey as key3
`,
want: map[string]map[string]string{
"folder1/folder2/secret1": {
"mykey1": "key1",
"mykey2": "key2",
},
"folder1/folder2/secret2": {
"mykey": "key3",
},
},
wantErr: nil,
},
{
name: "secret path validation",
retrieve: `
folder@/folder-/_secret_ mykey1 as key1
secret$ mykey2 as key2
`,
want: nil,
wantErr: fmt.Errorf("invalid path: 'secret$'. Secret path may contain only letters, numbers, underscores, dashes, @, pluses and periods separated by colon or slash"),
},
{
name: "too many args",
retrieve: `arg1 arg2 as arg3 arg4`,
want: nil,
wantErr: fmt.Errorf("invalid row: 'arg1 arg2 as arg3 arg4'. Expected format: '<secret path> <secret data key> as <output key>'"),
},
{
name: "less args",
retrieve: `arg1 arg2`,
want: nil,
wantErr: fmt.Errorf("invalid row: 'arg1 arg2'. Expected format: '<secret path> <secret data key> as <output key>'"),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result, err := parseRetrieve(tc.retrieve)
if (tc.wantErr != nil && tc.wantErr.Error() != err.Error()) || (tc.wantErr == nil && err != nil) {
t.Errorf("want error:\n\t%v\ngot:\n\t%v", tc.wantErr, err)
}
if !reflect.DeepEqual(tc.want, result) {
t.Errorf("want:\n\t%v\ngot:\n\t%v", tc.want, result)
}
})
}
}
func TestDsvGetToken(t *testing.T) {
cases := []struct {
name string
apiEndpoint string
cid string
csecret string
client httpClient
want string
wantErr error
}{
{
name: "happy path",
apiEndpoint: "test.example.com",
cid: "client_id",
csecret: "client_secret",
client: &MockHttpClient{
response: &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader([]byte(`{
"accessToken": "token"
}`))),
},
err: nil,
},
want: "token",
wantErr: nil,
},
{
name: "bad request",
apiEndpoint: "test.example.com",
cid: "client_id",
csecret: "client_secret",
client: &MockHttpClient{
response: &http.Response{
Status: "400 Bad Request",
StatusCode: 400,
Body: io.NopCloser(bytes.NewReader([]byte(nil))),
},
err: nil,
},
want: "",
wantErr: fmt.Errorf("API call failed: POST test.example.com/token: 400 Bad Request"),
},
{
name: "empty endpoint",
apiEndpoint: "",
cid: "client_id",
csecret: "client_secret",
client: &MockHttpClient{
response: &http.Response{
Status: "400 Bad Request",
StatusCode: 400,
Body: io.NopCloser(bytes.NewReader([]byte(nil))),
},
err: nil,
},
want: "",
wantErr: fmt.Errorf("API call failed: POST /token: 400 Bad Request"),
},
{
name: "http error",
apiEndpoint: "test.example.com",
cid: "client_id",
csecret: "client_secret",
client: &MockHttpClient{
response: nil,
err: fmt.Errorf("error"),
},
want: "",
wantErr: fmt.Errorf("API call failed: error"),
},
{
name: "nil body",
apiEndpoint: "test.example.com",
cid: "client_id",
csecret: "client_secret",
client: &MockHttpClient{
response: &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader([]byte(nil))),
},
err: nil,
},
want: "",
wantErr: fmt.Errorf("API call failed: could not unmarshal response body: unexpected end of JSON input"),
},
{
name: "no access token",
apiEndpoint: "test.example.com",
cid: "client_id",
csecret: "client_secret",
client: &MockHttpClient{
response: &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader([]byte(`{
"test": "token"
}`))),
},
err: nil,
},
want: "",
wantErr: fmt.Errorf("could not read access token from response"),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result, err := dsvGetToken(tc.client, tc.apiEndpoint, tc.cid, tc.csecret)
if (tc.wantErr != nil && tc.wantErr.Error() != err.Error()) || (tc.wantErr == nil && err != nil) {
t.Errorf("want error:\n\t%v\ngot:\n\t%v", tc.wantErr, err)
}
if tc.want != result {
t.Errorf("want:\n\t%v\ngot:\n\t%v", tc.want, result)
}
})
}
}
func TestDsvGetSecret(t *testing.T) {
cases := []struct {
name string
client httpClient
apiEndpoint string
accessToken string
secretPath string
want map[string]interface{}
wantErr error
}{
{
name: "happy path",
client: &MockHttpClient{
response: &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader([]byte(`{
"key": "val"
}`))),
},
err: nil,
},
apiEndpoint: "test.example.com",
accessToken: "token",
secretPath: "folder1/secret1",
want: map[string]interface{}{
"key": "val",
},
wantErr: nil,
},
{
name: "bad request",
client: &MockHttpClient{
response: &http.Response{
Status: "400 Bad Request",
StatusCode: 400,
Body: io.NopCloser(bytes.NewReader([]byte(nil))),
},
err: nil,
},
apiEndpoint: "test.example.com",
accessToken: "token",
secretPath: "folder1/secret1",
want: nil,
wantErr: fmt.Errorf("API call failed: GET test.example.com/secrets/folder1/secret1: 400 Bad Request"),
},
{
name: "http error",
client: &MockHttpClient{
response: nil,
err: fmt.Errorf("error"),
},
apiEndpoint: "test.example.com",
accessToken: "token",
secretPath: "folder1/secret1",
want: nil,
wantErr: fmt.Errorf("API call failed: error"),
},
{
name: "nil body",
client: &MockHttpClient{
response: &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader([]byte(nil))),
},
err: nil,
},
apiEndpoint: "test.example.com",
accessToken: "token",
secretPath: "folder1/secret1",
want: nil,
wantErr: fmt.Errorf("API call failed: could not unmarshal response body: unexpected end of JSON input"),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result, err := dsvGetSecret(tc.client, tc.apiEndpoint, tc.accessToken, tc.secretPath)
if (tc.wantErr != nil && tc.wantErr.Error() != err.Error()) || (tc.wantErr == nil && err != nil) {
t.Errorf("want error:\n\t%v\ngot:\n\t%v", tc.wantErr, err)
}
if !reflect.DeepEqual(tc.want, result) {
t.Errorf("want:\n\t%v\ngot:\n\t%v", tc.want, result)
}
})
}
}
func TestOpenEnvFile(t *testing.T) {
cases := []struct {
name string
envs map[string]string
gitlabCI bool
githubCI bool
wantErr error
}{
{
name: "gitlabCI: no variable set",
envs: map[string]string{
"CI_JOB_NAME": "",
"CI_PROJECT_PATH": "",
"GITHUB_ENV": "",
},
gitlabCI: true,
wantErr: fmt.Errorf("CI_JOB_NAME environment is not defined"),
},
{
name: "githubCI: no variable set",
envs: map[string]string{
"CI_JOB_NAME": "",
"CI_PROJECT_PATH": "",
"GITHUB_ENV": "",
},
githubCI: true,
wantErr: fmt.Errorf("GITHUB_ENV environment is not defined"),
},
{
name: "githubCI: cannot open file",
envs: map[string]string{
"CI_JOB_NAME": "",
"CI_PROJECT_PATH": "",
"GITHUB_ENV": "./myfile",
},
githubCI: true,
wantErr: fmt.Errorf("cannot open file ./myfile: open ./myfile: no such file or directory"),
},
{
name: "gitlabCI: no CI_PROJECT_PATH",
envs: map[string]string{
"CI_JOB_NAME": "some_job",
"CI_PROJECT_PATH": "",
"GITHUB_ENV": "",
},
gitlabCI: true,
wantErr: fmt.Errorf("CI_PROJECT_PATH environment is not defined"),
},
{
name: "gitlabCI: cannot open file",
envs: map[string]string{
"CI_JOB_NAME": "some_job",
"CI_PROJECT_PATH": "some_project",
"GITHUB_ENV": "",
},
gitlabCI: true,
wantErr: fmt.Errorf("cannot open file /builds/some_project/some_job: open /builds/some_project/some_job: no such file or directory"),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
githubCI = tc.githubCI
gitlabCI = tc.gitlabCI
for key, val := range tc.envs {
os.Setenv(key, val)
}
_, err := openEnvFile(true)
if (tc.wantErr != nil && tc.wantErr.Error() != err.Error()) || (tc.wantErr == nil && err != nil) {
t.Errorf("want error:\n\t%v\ngot:\n\t%v", tc.wantErr, err)
}
})
}
}