forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_test.go
151 lines (145 loc) · 3.47 KB
/
cli_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
package cli
import (
"os"
"testing"
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExecutionContext_readAdminSecret(t *testing.T) {
adminSecretEnv := "HASURA_GRAPHQL_ADMIN_SECRET"
oldAdminSecret := os.Getenv(adminSecretEnv)
os.Setenv(adminSecretEnv, "")
defer func() {
os.Setenv(adminSecretEnv, oldAdminSecret)
}()
fs := afero.NewMemMapFs()
require.NoError(t, fs.MkdirAll("/project", 0755))
tests := []struct {
name string
before func() func()
wantAdminSecret string
wantAdminSecrets []string
wantErr bool
assertErr require.ErrorAssertionFunc
}{
{
"can set admin secret from config file",
func() func() {
configFile := `admin_secret: test`
require.NoError(t, afero.WriteFile(fs, "/project/config.yml", []byte(configFile), 0644))
_, err := fs.Stat("/project/config.yml")
require.NoError(t, err)
return func() {}
},
"test",
[]string{},
false,
require.NoError,
},
{
"can set admin secrets from config file",
func() func() {
configFile := `admin_secrets: '["s1","s2","s3"]'`
require.NoError(t, afero.WriteFile(fs, "/project/config.yml", []byte(configFile), 0644))
_, err := fs.Stat("/project/config.yml")
require.NoError(t, err)
return func() {}
},
"",
[]string{"s1", "s2", "s3"},
false,
require.NoError,
},
{
"can set admin secret from env variable",
func() func() {
require.NoError(t, afero.WriteFile(fs, "/project/config.yml", []byte(""), 0644))
oldEnvValue := os.Getenv(adminSecretEnv)
teardown := func() {
os.Setenv(adminSecretEnv, oldEnvValue)
}
os.Setenv(adminSecretEnv, "test")
return teardown
},
"test",
[]string{},
false,
require.NoError,
},
{
"can set admin secrets from env variable",
func() func() {
require.NoError(t, afero.WriteFile(fs, "/project/config.yml", []byte(""), 0644))
adminSecretEnv := "HASURA_GRAPHQL_ADMIN_SECRETS"
oldEnvValue := os.Getenv(adminSecretEnv)
teardown := func() {
os.Setenv(adminSecretEnv, oldEnvValue)
}
os.Setenv(adminSecretEnv, "[\"s1\",\"s2\",\"s3\"]")
return teardown
},
"",
[]string{"s1", "s2", "s3"},
false,
require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
teardown := tt.before()
defer teardown()
v := viper.New()
v.SetFs(fs)
v.AddConfigPath("/project")
ec := &ExecutionContext{
Viper: v,
}
err := ec.readConfig()
tt.assertErr(t, err)
if tt.wantErr {
return
}
assert.Equal(t, tt.wantAdminSecret, ec.Config.AdminSecret)
assert.Equal(t, tt.wantAdminSecrets, ec.Config.AdminSecrets)
})
}
}
func TestServerConfig_GetAdminSecret(t *testing.T) {
type fields struct {
adminSecret string
adminSecrets []string
}
tests := []struct {
name string
fields fields
want string
}{
{
"admin secrets takes precedence when adminSecret & adminSecrets are set",
fields{"test", []string{"foo", "bar"}},
"foo",
},
{
"admin secret is returned when set",
fields{"test", []string{}},
"test",
},
{
"empty string is returned when none is set",
fields{"", []string{}},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &ServerConfig{
AdminSecret: tt.fields.adminSecret,
AdminSecrets: tt.fields.adminSecrets,
}
got := c.GetAdminSecret()
assert.Equal(t, tt.want, got)
})
}
}