-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
330 lines (314 loc) · 8.39 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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"testing"
"github.com/SkynetLabs/skynet-accounts/database"
"github.com/SkynetLabs/skynet-accounts/email"
"github.com/sirupsen/logrus"
"gitlab.com/NebulousLabs/errors"
)
// TestParseConfiguration ensures that we properly parse and validate all
// required environment variables.
func TestParseConfiguration(t *testing.T) {
// Fetch current state of env and make sure we restore it on exit.
{
keys := []string{
envDBUser,
envDBPass,
envDBHost,
envDBPort,
envPortal,
envServerDomain,
envStripeAPIKey,
envAccountsJWKSFile,
envJWTTTL,
envEmailURI,
envEmailFrom,
envMaxNumAPIKeysPerUser,
}
values := make(map[string]string)
for _, k := range keys {
v, ok := os.LookupEnv(k)
if ok {
values[k] = v
}
}
defer func() {
errorList := make([]error, 0)
for _, k := range keys {
v, ok := values[k]
if ok {
errorList = append(errorList, os.Setenv(k, v))
} else {
errorList = append(errorList, os.Unsetenv(k))
}
}
if e := errors.Compose(errorList...); e != nil {
t.Error(e)
}
}()
}
logger := logrus.New()
// Set required env vars to test values.
err := os.Setenv(envDBUser, "mongoUser")
if err != nil {
t.Fatal(err)
}
err = os.Setenv(envDBPass, "mongoPass")
if err != nil {
t.Fatal(err)
}
err = os.Setenv(envDBHost, "mongoHost")
if err != nil {
t.Fatal(err)
}
err = os.Setenv(envDBPort, "1234")
if err != nil {
t.Fatal(err)
}
emailFrom := "disabled@example.net"
emailURIValue := fmt.Sprintf("smtps://%s:not-a-password@smtp.gmail.com:465/?skip_ssl_verify=false", emailFrom)
err = os.Setenv(envEmailURI, emailURIValue)
if err != nil {
t.Fatal(err)
}
// Missing PORTAL_DOMAIN
err = os.Setenv(envPortal, "")
if err != nil {
t.Fatal(err)
}
_, err = parseConfiguration(logger)
if err == nil || !strings.Contains(err.Error(), "missing env var "+envPortal) {
t.Fatal("Failed to error out on invalid", envPortal)
}
portal := "siasky.net"
err = os.Setenv(envPortal, portal)
if err != nil {
t.Fatal(err)
}
// Missing SERVER_DOMAIN
err = os.Setenv(envServerDomain, "")
if err != nil {
t.Fatal(err)
}
conf, err := parseConfiguration(logger)
if err != nil {
t.Fatal(err)
}
if conf.ServerLockID != conf.PortalName {
t.Fatalf("Expected ServerLockID to be %s, got %s", conf.PortalName, conf.ServerLockID)
}
serverDomain := "test.siasky.net"
err = os.Setenv(envServerDomain, serverDomain)
if err != nil {
t.Fatal(err)
}
// Invalid ACCOUNTS_JWT_TTL
err = os.Setenv(envJWTTTL, "invalid TTL value")
if err != nil {
t.Fatal(err)
}
_, err = parseConfiguration(logger)
if err == nil || !strings.Contains(err.Error(), "failed to parse env var "+envJWTTTL) {
t.Fatal("Failed to error out on invalid", envJWTTTL)
}
// Zero ACCOUNTS_JWT_TTL
err = os.Setenv(envJWTTTL, "0")
if err != nil {
t.Fatal(err)
}
_, err = parseConfiguration(logger)
if err == nil || !strings.Contains(err.Error(), "env var is set to zero, which is an invalid value (must be positive or unset)") {
t.Fatal("Failed to error out on zero", envJWTTTL)
}
ttl := 123
err = os.Setenv(envJWTTTL, strconv.Itoa(ttl))
if err != nil {
t.Fatal(err)
}
// Missing ACCOUNTS_EMAIL_URI
err = os.Setenv(envEmailURI, "")
if err != nil {
t.Fatal(err)
}
_, err = parseConfiguration(logger)
if err == nil || !errors.Contains(err, email.ErrInvalidEmailConfiguration) {
t.Fatal("Failed to error out on empty", envEmailURI)
}
// Invalid ACCOUNTS_EMAIL_URI
err = os.Setenv(envEmailURI, "this is not empty but it's also not valid")
if err != nil {
t.Fatal(err)
}
_, err = parseConfiguration(logger)
if err == nil || !errors.Contains(err, email.ErrInvalidEmailConfiguration) {
t.Log(err)
t.Fatal("Failed to error out on invalid", envEmailURI)
}
// Set all values
err = os.Setenv(envEmailURI, emailURIValue)
if err != nil {
t.Fatal(err)
}
sk := "sk_live_THIS_IS_A_LIVE_KEY"
err = os.Setenv(envStripeAPIKey, sk)
if err != nil {
t.Fatal(err)
}
// All values should be correct now. Make sure we have the correct
// corresponding values in the returned configuration struct.
config, err := parseConfiguration(logger)
if err != nil {
t.Fatal(err)
}
if config.EmailURI != emailURIValue {
t.Fatalf("Expected %s, got %s", emailURIValue, config.EmailURI)
}
if config.EmailFrom != emailFrom {
t.Fatalf("Expected %s, got %s", emailFrom, config.EmailFrom)
}
if config.PortalName != "https://"+portal {
t.Fatalf("Expected %s, got %s", "https://"+portal, config.PortalName)
}
if config.PortalAddressAccounts != "https://account."+portal {
t.Fatalf("Expected %s, got %s", "https://accounts."+portal, config.PortalAddressAccounts)
}
if config.StripeKey != sk {
t.Fatalf("Expected %s, got %s", sk, config.StripeKey)
}
if config.ServerLockID != serverDomain {
t.Fatalf("Expected %s, got %s", serverDomain, config.ServerLockID)
}
if config.ServerLockID != serverDomain {
t.Fatalf("Expected %s, got %s", serverDomain, config.ServerLockID)
}
if config.JWTTTL != ttl {
t.Fatalf("Expected %d, got %d", ttl, config.JWTTTL)
}
if config.MaxAPIKeys != database.MaxNumAPIKeysPerUser {
t.Fatalf("Expected %d, got %d", database.MaxNumAPIKeysPerUser, config.MaxAPIKeys)
}
// Set alternative config values and test their outcomes.
// Set a test stripe key. Expect the key to change and tp enter test mode.
sk = "sk_test_THIS_IS_A_TEST_KEY"
err = os.Setenv(envStripeAPIKey, sk)
if err != nil {
t.Fatal(err)
}
maxKeys := 321
err = os.Setenv(envMaxNumAPIKeysPerUser, strconv.Itoa(maxKeys))
if err != nil {
t.Fatal(err)
}
config, err = parseConfiguration(logger)
if err != nil {
t.Fatal(err)
}
if config.StripeKey != sk {
t.Fatalf("Expected %s, got %s", sk, config.StripeKey)
}
if config.MaxAPIKeys != maxKeys {
t.Fatalf("Expected %d, got %d", maxKeys, config.MaxAPIKeys)
}
}
// TestLoadDBCredentials ensures that we validate that all required environment
// variables are present.
func TestLoadDBCredentials(t *testing.T) {
{
u := os.Getenv(envDBUser)
p := os.Getenv(envDBPass)
h := os.Getenv(envDBHost)
po := os.Getenv(envDBPort)
defer func() {
e1 := os.Setenv(envDBUser, u)
e2 := os.Setenv(envDBPass, p)
e3 := os.Setenv(envDBHost, h)
e4 := os.Setenv(envDBPort, po)
e := errors.Compose(e1, e2, e3, e4)
if e != nil {
t.Error(e)
}
}()
}
// Set valid test values.
e1 := os.Setenv(envDBUser, "mongoUser")
e2 := os.Setenv(envDBPass, "mongoPass")
e3 := os.Setenv(envDBHost, "mongoHost")
e4 := os.Setenv(envDBPort, "123")
e := errors.Compose(e1, e2, e3, e4)
if e != nil {
t.Error(e)
}
originals, err := loadDBCredentials()
if err != nil {
t.Fatal(err)
}
// Missing user
err = os.Unsetenv(envDBUser)
if err != nil {
t.Fatal(err)
}
_, err = loadDBCredentials()
if err == nil || !strings.Contains(err.Error(), "missing env var "+envDBUser) {
t.Log(err)
t.Fatal("Failed to error out on missing", envDBUser)
}
err = os.Setenv(envDBUser, originals.User)
if err != nil {
t.Fatal(err)
}
// Missing password
err = os.Unsetenv(envDBPass)
if err != nil {
t.Fatal(err)
}
_, err = loadDBCredentials()
if err == nil || !strings.Contains(err.Error(), "missing env var "+envDBPass) {
t.Fatal("Failed to error out on missing", envDBPass)
}
err = os.Setenv(envDBPass, originals.Password)
if err != nil {
t.Fatal(err)
}
// Missing host
err = os.Unsetenv(envDBHost)
if err != nil {
t.Fatal(err)
}
_, err = loadDBCredentials()
if err == nil || !strings.Contains(err.Error(), "missing env var "+envDBHost) {
t.Fatal("Failed to error out on missing", envDBHost)
}
err = os.Setenv(envDBHost, originals.Host)
if err != nil {
t.Fatal(err)
}
// Missing port
err = os.Unsetenv(envDBPort)
if err != nil {
t.Fatal(err)
}
_, err = loadDBCredentials()
if err == nil || !strings.Contains(err.Error(), "missing env var "+envDBPort) {
t.Fatal("Failed to error out on missing", envDBPort)
}
err = os.Setenv(envDBPort, originals.Port)
if err != nil {
t.Fatal(err)
}
// Ensure the returned values are what we expect.
creds, err := loadDBCredentials()
if err != nil {
t.Fatal(err)
}
if creds.User != os.Getenv(envDBUser) || creds.Password != os.Getenv(envDBPass) ||
creds.Host != os.Getenv(envDBHost) || creds.Port != os.Getenv(envDBPort) {
t.Fatalf("Expected %s, %s, %s, %s, got %s, %s, %s, %s.",
os.Getenv(envDBUser), os.Getenv(envDBPass), os.Getenv(envDBHost), os.Getenv(envDBPort),
creds.User, creds.Password, creds.Host, creds.Port,
)
}
}