-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcredentials_test.go
301 lines (282 loc) · 8.12 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
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
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package config
import (
"os"
"testing"
"encoding/json"
"github.com/google/go-cmp/cmp"
)
func TestRetrieveCredentials(t *testing.T) {
var (
validSecret = "qaRZGEbnQNNvmaeTLqy8Bxs22wLZ6H7obIiNSveTLPdoQuylANnuy6WBOw16XoqH"
validClient = "CQ4iZ5sebOfhGRwUn3IV0r1YFMNrMTIx"
validOrganization = "dc6a6159-3cd5-41a2-b391-553b1351cd98"
validConfig = &Credentials{Client: validClient, Secret: validSecret}
validWithOptionalConfig = &Credentials{Client: validClient, Secret: validSecret, Organization: validOrganization}
invalidConfig = &Credentials{Client: "", Secret: validSecret}
clientEnv = EnvPrefix + "_CLIENT"
secretEnv = EnvPrefix + "_SECRET"
organizationEnv = EnvPrefix + "_ORGANIZATION"
)
tests := []struct {
name string
pre func()
post func()
wantedConfig *Credentials
wantedErr bool
}{
{
name: "valid credentials with only mandatory params written in env",
pre: func() {
os.Setenv(clientEnv, validConfig.Client)
os.Setenv(secretEnv, validConfig.Secret)
},
post: func() {
os.Unsetenv(clientEnv)
os.Unsetenv(secretEnv)
},
wantedConfig: validConfig,
wantedErr: false,
},
{
name: "valid credentials with optional params written in env",
pre: func() {
os.Setenv(clientEnv, validWithOptionalConfig.Client)
os.Setenv(secretEnv, validWithOptionalConfig.Secret)
os.Setenv(organizationEnv, validWithOptionalConfig.Organization)
},
post: func() {
os.Unsetenv(clientEnv)
os.Unsetenv(secretEnv)
os.Unsetenv(organizationEnv)
},
wantedConfig: validWithOptionalConfig,
wantedErr: false,
},
{
name: "invalid credentials written in env",
pre: func() {
os.Setenv(clientEnv, validConfig.Client)
os.Setenv(secretEnv, "")
},
post: func() {
os.Unsetenv(clientEnv)
os.Unsetenv(secretEnv)
},
wantedConfig: nil,
wantedErr: true,
},
{
name: "valid credentials written in parent of cwd",
pre: func() {
parent := "test-parent"
cwd := "test-parent/test-cwd"
os.MkdirAll(cwd, os.FileMode(0777))
// Write valid credentials in parent dir
os.Chdir(parent)
b, _ := json.Marshal(validConfig)
os.WriteFile(CredentialsFilename+".json", b, os.FileMode(0777))
// Cwd has no credentials file
os.Chdir("test-cwd")
},
post: func() {
os.Chdir("../..")
os.RemoveAll("test-parent")
},
wantedConfig: validConfig,
wantedErr: false,
},
{
name: "valid credentials with optional params written in parent of cwd",
pre: func() {
parent := "test-parent"
cwd := "test-parent/test-cwd"
os.MkdirAll(cwd, os.FileMode(0777))
// Write valid credentials in parent dir
os.Chdir(parent)
b, _ := json.Marshal(validWithOptionalConfig)
os.WriteFile(CredentialsFilename+".json", b, os.FileMode(0777))
// Cwd has no credentials file
os.Chdir("test-cwd")
},
post: func() {
os.Chdir("../..")
os.RemoveAll("test-parent")
},
wantedConfig: validWithOptionalConfig,
wantedErr: false,
},
{
name: "invalid credentials written in cwd, ignore credentials of parent dir",
pre: func() {
parent := "test-parent"
cwd := "test-parent/test-cwd"
os.MkdirAll(cwd, os.FileMode(0777))
// Write valid credentials in parent dir
os.Chdir(parent)
b, _ := json.Marshal(validConfig)
os.WriteFile(CredentialsFilename+".json", b, os.FileMode(0777))
// Write invalid credentials in cwd
os.Chdir("test-cwd")
b, _ = json.Marshal(invalidConfig)
os.WriteFile(CredentialsFilename+".json", b, os.FileMode(0777))
},
post: func() {
os.Chdir("../..")
os.RemoveAll("test-parent")
os.Unsetenv(clientEnv)
os.Unsetenv(secretEnv)
},
wantedConfig: nil,
wantedErr: true,
},
{
name: "invalid credentials written in env, ignore valid credentials of cwd",
pre: func() {
cwd := "test-cwd"
os.MkdirAll(cwd, os.FileMode(0777))
// Write valid credentials in cwd
os.Chdir(cwd)
b, _ := json.Marshal(validConfig)
os.WriteFile(CredentialsFilename+".json", b, os.FileMode(0777))
// Write invalid credentials in env
os.Setenv(clientEnv, validConfig.Client)
os.Setenv(secretEnv, "")
},
post: func() {
os.Chdir("..")
os.RemoveAll("test-cwd")
},
wantedConfig: nil,
wantedErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.pre()
got, err := RetrieveCredentials()
tt.post()
if tt.wantedErr && err == nil {
t.Errorf("Expected an error, but got nil")
}
if !tt.wantedErr && err != nil {
t.Errorf("Expected nil error, but got: %v", err)
}
if !cmp.Equal(got, tt.wantedConfig) {
t.Errorf("Wrong credentials received, diff:\n%s", cmp.Diff(tt.wantedConfig, got))
}
})
}
}
func TestValidate(t *testing.T) {
var (
validSecret = "qaRZGEbnQNNvmaeTLqy8Bxs22wLZ6H7obIiNSveTLPdoQuylANnuy6WBOw16XoqH"
validClient = "CQ4iZ5sebOfhGRwUn3IV0r1YFMNrMTIx"
validOrganization = "dc6a6159-3cd5-41a2-b391-553b1351cd98"
)
tests := []struct {
name string
config *Credentials
valid bool
}{
{
name: "valid credentials",
config: &Credentials{Client: validClient, Secret: validSecret, Organization: validOrganization},
valid: true,
},
{
name: "valid credentials, organization is optional",
config: &Credentials{Client: validClient, Secret: validSecret, Organization: ""},
valid: true,
},
{
name: "invalid client id",
config: &Credentials{Client: "", Secret: validSecret},
valid: false,
},
{
name: "invalid client secret",
config: &Credentials{Client: validClient, Secret: ""},
valid: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()
if tt.valid && err != nil {
t.Errorf(
"Wrong validation, the credentials were correct but an error was received: \ncredentials: %v\nerr: %v",
tt.config,
err,
)
}
if !tt.valid && err == nil {
t.Errorf(
"Wrong validation, the credentials were invalid but no error was received: \ncredentials: %v",
tt.config,
)
}
})
}
}
func TestIsEmpty(t *testing.T) {
var (
validSecret = "qaRZGEbnQNNvmaeTLqy8Bxs22wLZ6H7obIiNSveTLPdoQuylANnuy6WBOw16XoqH"
validClient = "CQ4iZ5sebOfhGRwUn3IV0r1YFMNrMTIx"
validOrganization = "dc6a6159-3cd5-41a2-b391-553b1351cd98"
)
tests := []struct {
name string
config *Credentials
want bool
}{
{
name: "empty credentials",
config: &Credentials{Client: "", Secret: "", Organization: ""},
want: true,
},
{
name: "empty mandatory credentials - optionals given",
config: &Credentials{Client: "", Secret: "", Organization: validOrganization},
want: true,
},
{
name: "credentials without id",
config: &Credentials{Client: "", Secret: validSecret},
want: false,
},
{
name: "credentials without secret",
config: &Credentials{Client: validClient, Secret: ""},
want: false,
},
{
name: "credentials with all mandatory params set",
config: &Credentials{Client: validClient, Secret: validSecret},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.config.IsEmpty()
if got != tt.want {
t.Errorf("Expected %v but got %v, with credentials: %v", tt.want, got, tt.config)
}
})
}
}