forked from prest/config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
142 lines (115 loc) · 2.9 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
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
package config
import (
"testing"
"os"
)
func TestLoad(t *testing.T) {
os.Setenv("PREST_CONF", "testdata/prest.toml")
Load()
if len(PrestConf.AccessConf.Tables) < 2 {
t.Errorf("expected > 2, got: %d", len(PrestConf.AccessConf.Tables))
}
Load()
if !PrestConf.AccessConf.Restrict {
t.Error("expected true, but got false")
}
}
func TestParse(t *testing.T) {
os.Setenv("PREST_CONF", "testdata/prest.toml")
viperCfg()
cfg := &Prest{}
err := Parse(cfg)
if err != nil {
t.Errorf("expected no errors, but got %v", err)
}
if cfg.HTTPPort != 6000 {
t.Errorf("expected port: 6000, got: %d", cfg.HTTPPort)
}
if cfg.PGDatabase != "prest" {
t.Errorf("expected database: prest, got: %s", cfg.PGDatabase)
}
os.Setenv("PREST_CONF", "../prest.toml")
os.Setenv("PREST_HTTP_PORT", "4000")
viperCfg()
cfg = &Prest{}
err = Parse(cfg)
if err != nil {
t.Errorf("expected no errors, but got %v", err)
}
if cfg.HTTPPort != 4000 {
t.Errorf("expected port: 4000, got: %d", cfg.HTTPPort)
}
if !cfg.EnableDefaultJWT {
t.Error("expected true but got false")
}
os.Setenv("PREST_CONF", "")
os.Setenv("PREST_HTTP_PORT", "4000")
os.Setenv("PREST_JWT_DEFAULT", "false")
viperCfg()
cfg = &Prest{}
err = Parse(cfg)
if err != nil {
t.Errorf("expected no errors, but got %v", err)
}
if cfg.HTTPPort != 4000 {
t.Errorf("expected port: 4000, got: %d", cfg.HTTPPort)
}
if cfg.EnableDefaultJWT {
t.Error("expected false but got true")
}
os.Setenv("PREST_HTTP_PORT", "4000")
os.Setenv("PREST_CONF", "testdata/prest.toml")
viperCfg()
cfg = &Prest{}
err = Parse(cfg)
if err != nil {
t.Errorf("expected no errors, but got %v", err)
}
if cfg.HTTPPort != 4000 {
t.Errorf("expected port: 4000, got: %d", cfg.HTTPPort)
}
os.Setenv("PREST_JWT_KEY", "s3cr3t")
viperCfg()
cfg = &Prest{}
err = Parse(cfg)
if err != nil {
t.Errorf("expected no errors, but got %v", err)
}
if cfg.JWTKey != "s3cr3t" {
t.Errorf("expected jwt key: s3cr3t, got: %s", cfg.JWTKey)
}
if cfg.JWTAlgo != "HS256" {
t.Errorf("expected (default) jwt algo: HS256, got: %s", cfg.JWTAlgo)
}
os.Setenv("PREST_JWT_ALGO", "HS512")
viperCfg()
cfg = &Prest{}
err = Parse(cfg)
if err != nil {
t.Errorf("expected no errors, but got %v", err)
}
if cfg.JWTAlgo != "HS512" {
t.Errorf("expected jwt algo: HS512, got: %s", cfg.JWTAlgo)
}
}
func TestGetDefaultPrestConf(t *testing.T) {
testCases := []struct {
name string
defaultFile string
prestConf string
result string
}{
{"empty config", "./prest.toml", "", ""},
{"custom config", "./prest.toml", "../prest.toml", "../prest.toml"},
{"default config", "./testdata/prest.toml", "", "./testdata/prest.toml"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
defaultFile = tc.defaultFile
cfg := getDefaultPrestConf(tc.prestConf)
if cfg != tc.result {
t.Errorf("expected %v, but got %v", tc.result, cfg)
}
})
}
}