-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
108 lines (77 loc) · 2.45 KB
/
config_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
package main
import (
"os"
"testing"
)
func TestNewConfigNotEmptyData(t *testing.T) {
cfg := NewConfig()
if cfg.user == "" || cfg.password == "" || cfg.secretKey == "" || cfg.httpPort == "" || cfg.httpsPort == "" || cfg.tlsCert == "" || cfg.tlsKey == "" || string(cfg.passwordHash) == "" {
t.Errorf("Config struct should not have an empty values: got %v", cfg)
}
}
func TestNewConfigEmptyData(t *testing.T) {
envs := []string{"LOGIN", "PASSWORD", "SECRET_KEY", "HTTP_PORT", "HTTPS_PORT", "TLS_CERT_PATH", "TLS_KEY_PATH", "FORCED_TLS"}
envsBuffer := make(map[string]string)
// clear environments variables
for _, env := range envs {
// save to buffer for later restore envs
envsBuffer[env] = os.Getenv(env)
// clear env
os.Setenv(env, "")
}
cfg := NewConfig()
if cfg.user != "" || cfg.password != "" || cfg.secretKey != "" || cfg.httpPort != "" || cfg.httpsPort != "" || cfg.tlsCert != "" || cfg.tlsKey != "" || cfg.forcedTLS {
t.Errorf("Config struct should be an empty values: got %v", cfg)
}
// restore envs for another tests
// loads values from .env into the system
for k, v := range envsBuffer {
os.Setenv(k, v)
}
}
func TestNewConfigEnvsNotExist(t *testing.T) {
envs := []string{"LOGIN", "PASSWORD", "SECRET_KEY", "HTTP_PORT", "HTTPS_PORT", "TLS_CERT_PATH", "TLS_KEY_PATH", "FORCED_TLS"}
envsBuffer := make(map[string]string)
// clear environments variables
for _, env := range envs {
// save to buffer for later restore envs
envsBuffer[env] = os.Getenv(env)
// clear env
os.Setenv(env, "")
}
// flush all environments
// os.Clearenv()
for _, env := range envs {
// clear env
os.Setenv(env, "")
value := getEnv(env)
if value != "" {
t.Errorf("Env variable %s should not exist!", env)
}
}
// restore envs for another tests
// loads values from .env into the system
for k, v := range envsBuffer {
os.Setenv(k, v)
}
}
func TestGetEnvExist(t *testing.T) {
envs := []string{"LOGIN", "PASSWORD", "SECRET_KEY", "HTTP_PORT", "HTTPS_PORT", "TLS_CERT_PATH", "TLS_KEY_PATH", "FORCED_TLS"}
for _, env := range envs {
value := getEnv(env)
if value == "" {
t.Errorf("Env variable %s does not exist!", env)
}
}
}
func TestGetEnvNotExist(t *testing.T) {
envs := []string{"FAKE_LOGIN", "FAKE_PASSWORD", "FAKE_SECRET_KEY", "FAKE_PORT"}
for _, env := range envs {
// clear env
os.Setenv(env, "")
value := getEnv(env)
if value != "" {
t.Errorf("Env variable %s should not exist!", env)
}
}
}