Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3038ba4

Browse files
committedDec 6, 2021
Add test
1 parent 7d0ae7a commit 3038ba4

File tree

1 file changed

+273
-0
lines changed

1 file changed

+273
-0
lines changed
 

‎internal/config/config_test.go

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package config
19+
20+
import (
21+
"os"
22+
"testing"
23+
24+
"encoding/json"
25+
26+
"github.com/google/go-cmp/cmp"
27+
)
28+
29+
func TestRetrieve(t *testing.T) {
30+
var (
31+
validSecret = "qaRZGEbnQNNvmaeTLqy8Bxs22wLZ6H7obIiNSveTLPdoQuylANnuy6WBOw16XoqH"
32+
validClient = "CQ4iZ5sebOfhGRwUn3IV0r1YFMNrMTIx"
33+
validConfig = &Config{validClient, validSecret}
34+
invalidConfig = &Config{"", validSecret}
35+
clientEnv = EnvPrefix + "_CLIENT"
36+
secretEnv = EnvPrefix + "_SECRET"
37+
clientEnvBackup *string
38+
secretEnvBackup *string
39+
)
40+
41+
// Preserve user environment variables when executing this test
42+
_ = func() {
43+
if c, ok := os.LookupEnv(clientEnv); ok {
44+
clientEnvBackup = &c
45+
}
46+
if s, ok := os.LookupEnv(secretEnv); ok {
47+
secretEnvBackup = &s
48+
}
49+
}
50+
_ = func() {
51+
if clientEnvBackup != nil {
52+
os.Setenv(clientEnv, *clientEnvBackup)
53+
clientEnvBackup = nil
54+
}
55+
if secretEnvBackup != nil {
56+
os.Setenv(secretEnv, *secretEnvBackup)
57+
secretEnvBackup = nil
58+
}
59+
}
60+
61+
tests := []struct {
62+
name string
63+
pre func()
64+
post func()
65+
wantedConfig *Config
66+
wantedErr bool
67+
}{
68+
{
69+
name: "valid config written in env",
70+
pre: func() {
71+
// pushEnv()
72+
os.Setenv(clientEnv, validConfig.Client)
73+
os.Setenv(secretEnv, validConfig.Secret)
74+
},
75+
post: func() {
76+
os.Unsetenv(clientEnv)
77+
os.Unsetenv(secretEnv)
78+
// popEnv()
79+
},
80+
wantedConfig: validConfig,
81+
wantedErr: false,
82+
},
83+
84+
{
85+
name: "invalid config written in env",
86+
pre: func() {
87+
// pushEnv()
88+
os.Setenv(clientEnv, validConfig.Client)
89+
os.Setenv(secretEnv, "")
90+
},
91+
post: func() {
92+
os.Unsetenv(clientEnv)
93+
os.Unsetenv(secretEnv)
94+
// popEnv()
95+
},
96+
wantedConfig: nil,
97+
wantedErr: true,
98+
},
99+
100+
{
101+
name: "valid config written in parent of cwd",
102+
pre: func() {
103+
parent := "test-parent"
104+
cwd := "test-parent/test-cwd"
105+
os.MkdirAll(cwd, os.FileMode(0777))
106+
// Write valid config in parent dir
107+
os.Chdir(parent)
108+
b, _ := json.Marshal(validConfig)
109+
os.WriteFile(Filename+".json", b, os.FileMode(0777))
110+
// Cwd has no config file
111+
os.Chdir("test-cwd")
112+
},
113+
post: func() {
114+
os.Chdir("../..")
115+
os.RemoveAll("test-parent")
116+
},
117+
wantedConfig: validConfig,
118+
wantedErr: false,
119+
},
120+
121+
{
122+
name: "invalid config written in cwd, ignore config of parent dir",
123+
pre: func() {
124+
parent := "test-parent"
125+
cwd := "test-parent/test-cwd"
126+
os.MkdirAll(cwd, os.FileMode(0777))
127+
// Write valid config in parent dir
128+
os.Chdir(parent)
129+
b, _ := json.Marshal(validConfig)
130+
os.WriteFile(Filename+".json", b, os.FileMode(0777))
131+
// Write invalid config in cwd
132+
os.Chdir("test-cwd")
133+
b, _ = json.Marshal(invalidConfig)
134+
os.WriteFile(Filename+".json", b, os.FileMode(0777))
135+
},
136+
post: func() {
137+
os.Chdir("../..")
138+
os.RemoveAll("test-parent")
139+
os.Unsetenv(clientEnv)
140+
os.Unsetenv(secretEnv)
141+
},
142+
wantedConfig: nil,
143+
wantedErr: true,
144+
},
145+
146+
{
147+
name: "invalid config written in env, ignore valid config of cwd",
148+
pre: func() {
149+
cwd := "test-cwd"
150+
os.MkdirAll(cwd, os.FileMode(0777))
151+
// Write valid config in cwd
152+
os.Chdir(cwd)
153+
b, _ := json.Marshal(validConfig)
154+
os.WriteFile(Filename+".json", b, os.FileMode(0777))
155+
// Write invalid config in env
156+
os.Setenv(clientEnv, validConfig.Client)
157+
os.Setenv(secretEnv, "")
158+
},
159+
post: func() {
160+
os.Chdir("..")
161+
os.RemoveAll("test-cwd")
162+
},
163+
wantedConfig: nil,
164+
wantedErr: true,
165+
},
166+
}
167+
168+
for _, tt := range tests {
169+
t.Run(tt.name, func(t *testing.T) {
170+
tt.pre()
171+
got, err := Retrieve()
172+
tt.post()
173+
174+
if tt.wantedErr && err == nil {
175+
t.Errorf("Expected an error, but got nil")
176+
}
177+
if !tt.wantedErr && err != nil {
178+
t.Errorf("Expected nil error, but got: %v", err)
179+
}
180+
181+
if !cmp.Equal(got, tt.wantedConfig) {
182+
t.Errorf("Wrong config received, diff:\n%s", cmp.Diff(tt.wantedConfig, got))
183+
}
184+
})
185+
}
186+
}
187+
188+
func TestValidate(t *testing.T) {
189+
var (
190+
validSecret = "qaRZGEbnQNNvmaeTLqy8Bxs22wLZ6H7obIiNSveTLPdoQuylANnuy6WBOw16XoqH"
191+
validClient = "CQ4iZ5sebOfhGRwUn3IV0r1YFMNrMTIx"
192+
)
193+
tests := []struct {
194+
name string
195+
config *Config
196+
valid bool
197+
}{
198+
{
199+
name: "valid config",
200+
config: &Config{
201+
Client: validClient,
202+
Secret: validSecret,
203+
},
204+
valid: true,
205+
},
206+
{
207+
name: "invalid client id",
208+
config: &Config{Client: "", Secret: validSecret},
209+
valid: false,
210+
},
211+
{
212+
name: "invalid client secret",
213+
config: &Config{Client: validClient, Secret: ""},
214+
valid: false,
215+
},
216+
}
217+
218+
for _, tt := range tests {
219+
t.Run(tt.name, func(t *testing.T) {
220+
err := tt.config.Validate()
221+
if tt.valid && err != nil {
222+
t.Errorf(
223+
"Wrong validation, the config was correct but an error was received: \nconfig: %v\nerr: %v",
224+
tt.config,
225+
err,
226+
)
227+
}
228+
if !tt.valid && err == nil {
229+
t.Errorf(
230+
"Wrong validation, the config was invalid but no error was received: \nconfig: %v",
231+
tt.config,
232+
)
233+
}
234+
})
235+
}
236+
}
237+
238+
func TestIsEmpty(t *testing.T) {
239+
var (
240+
validSecret = "qaRZGEbnQNNvmaeTLqy8Bxs22wLZ6H7obIiNSveTLPdoQuylANnuy6WBOw16XoqH"
241+
validClient = "CQ4iZ5sebOfhGRwUn3IV0r1YFMNrMTIx"
242+
)
243+
tests := []struct {
244+
name string
245+
config *Config
246+
want bool
247+
}{
248+
{
249+
name: "empty config",
250+
config: &Config{Client: "", Secret: ""},
251+
want: true,
252+
},
253+
{
254+
name: "config without id",
255+
config: &Config{Client: "", Secret: validSecret},
256+
want: false,
257+
},
258+
{
259+
name: "config without secret",
260+
config: &Config{Client: validClient, Secret: ""},
261+
want: false,
262+
},
263+
}
264+
265+
for _, tt := range tests {
266+
t.Run(tt.name, func(t *testing.T) {
267+
got := tt.config.IsEmpty()
268+
if got != tt.want {
269+
t.Errorf("Expected %v but got %v, with config: %v", tt.want, got, tt.config)
270+
}
271+
})
272+
}
273+
}

0 commit comments

Comments
 (0)
Please sign in to comment.