This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
215 lines (176 loc) · 8.44 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
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
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"testing"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gopkg.in/yaml.v3"
)
// These are not complete tests of all functions in elixir. New tests should
// be added as the code is updated.
type ConfigTests struct {
suite.Suite
TempDir string
ConfigFile *os.File
ElixirConfig ElixirConfig
CegaConfig CegaConfig
ServerConfig ServerConfig
S3Inbox string
JwtIssuer string
JwtPrivateKey string
JwtPrivateKeyFile *os.File
JwtSignatureAlg string
ResignJwt bool
}
func TestConfigTestSuite(t *testing.T) {
suite.Run(t, new(ConfigTests))
}
func (suite *ConfigTests) SetupTest() {
var err error
// Create a temporary directory for our config file
suite.TempDir, err = os.MkdirTemp("", "sda-auth-test-")
if err != nil {
log.Fatal("Couldn't create temporary test directory", err)
}
suite.ConfigFile, err = os.Create(filepath.Join(suite.TempDir, "config.yaml"))
if err != nil {
log.Fatal("Cannot create temporary config file", err)
}
// Create temporary dummy keys
suite.JwtPrivateKeyFile, err = os.Create(filepath.Join(suite.TempDir, "jwt-dummy-sec.c4gh"))
if err != nil {
log.Fatal("Cannot create temporary private key file", err)
}
_, err = os.Create(filepath.Join(suite.TempDir, "jwt-dummy-sec.c4gh_env"))
if err != nil {
log.Fatal("Cannot create temporary private key file", err)
}
// config values to write to the config file
suite.ElixirConfig = ElixirConfig{
ID: "elixirTestID",
Provider: "elixirTestIssuer",
RedirectURL: "http://elixir/login",
RevocationURL: "http://elixir/revoke",
Secret: "elixirTestSecret",
}
suite.CegaConfig = CegaConfig{
AuthURL: "http://cega/auth",
ID: "cegaID",
Secret: "cegaSecret",
}
suite.ServerConfig = ServerConfig{
Cert: "serverCert.pem",
Key: "serverKey.pem",
}
suite.S3Inbox = "s3://testInbox"
suite.JwtIssuer = "JwtIssuer"
suite.JwtPrivateKey = suite.JwtPrivateKeyFile.Name()
suite.JwtSignatureAlg = "RS256"
suite.ResignJwt = true
// Write config to temp config file
configYaml, err := yaml.Marshal(Config{
Elixir: suite.ElixirConfig,
Cega: suite.CegaConfig,
Server: suite.ServerConfig,
S3Inbox: suite.S3Inbox,
JwtIssuer: suite.JwtIssuer,
JwtPrivateKey: suite.JwtPrivateKey,
JwtSignatureAlg: suite.JwtSignatureAlg,
ResignJwt: suite.ResignJwt,
})
if err != nil {
log.Errorf("Error marshalling config yaml: %v", err)
}
_, err = suite.ConfigFile.Write(configYaml)
if err != nil {
log.Errorf("Error writing config file: %v", err)
}
}
func (suite *ConfigTests) TearDownTest() {
os.RemoveAll(suite.TempDir)
}
// Both readConfig and parseConfig is called when using NewConfig, so they are
// both tested in this single test.
func (suite *ConfigTests) TestConfig() {
// change dir so that we read the right config
err := os.Chdir(suite.TempDir)
if err != nil {
log.Errorf("Couldn't access temp directory: %v", err)
}
config, err := NewConfig()
assert.NoError(suite.T(), err)
// Check elixir values
assert.Equal(suite.T(), suite.ElixirConfig.ID, config.Elixir.ID, "Elixir ID misread from config file")
assert.Equal(suite.T(), suite.ElixirConfig.Provider, config.Elixir.Provider, "Elixir Issuer misread from config file")
assert.Equal(suite.T(), suite.ElixirConfig.RedirectURL, config.Elixir.RedirectURL, "Elixir RedirectURL misread from config file")
assert.Equal(suite.T(), suite.ElixirConfig.Secret, config.Elixir.Secret, "Elixir Secret misread from config file")
// Check CEGA values
assert.Equal(suite.T(), suite.CegaConfig.ID, config.Cega.ID, "CEGA ID misread from config file")
assert.Equal(suite.T(), suite.CegaConfig.AuthURL, config.Cega.AuthURL, "CEGA AuthURL misread from config file")
assert.Equal(suite.T(), suite.CegaConfig.Secret, config.Cega.Secret, "CEGA Secret misread from config file")
// Check ServerConfig values
assert.Equal(suite.T(), suite.ServerConfig.Cert, config.Server.Cert, "ServerConfig Cert misread from config file")
assert.Equal(suite.T(), suite.ServerConfig.Key, config.Server.Key, "ServerConfig Key misread from config file")
// Check S3Inbox value
assert.Equal(suite.T(), suite.S3Inbox, config.S3Inbox, "S3Inbox misread from config file")
// Check JWT values
assert.Equal(suite.T(), suite.JwtIssuer, config.JwtIssuer, "JwtIssuer misread from config file")
assert.Equal(suite.T(), suite.JwtPrivateKey, config.JwtPrivateKey, "JwtPrivateKey misread from config file")
assert.Equal(suite.T(), suite.JwtSignatureAlg, config.JwtSignatureAlg, "JwtSignatureAlg misread from config file")
assert.Equal(suite.T(), suite.ResignJwt, config.ResignJwt, "ResignJwt misread from config file")
// sanitycheck without config file or ENVs
// this should fail
os.Remove(suite.ConfigFile.Name())
viper.Reset()
_, e := NewConfig()
assert.Error(suite.T(), e)
// Set all values as environment variables
os.Setenv("ELIXIR_ID", fmt.Sprintf("env_%v", suite.ElixirConfig.ID))
os.Setenv("ELIXIR_PROVIDER", fmt.Sprintf("env_%v", suite.ElixirConfig.Provider))
os.Setenv("ELIXIR_REDIRECTURL", fmt.Sprintf("env_%v", suite.ElixirConfig.RedirectURL))
os.Setenv("ELIXIR_SECRET", fmt.Sprintf("env_%v", suite.ElixirConfig.Secret))
os.Setenv("CEGA_ID", fmt.Sprintf("env_%v", suite.CegaConfig.ID))
os.Setenv("CEGA_AUTHURL", fmt.Sprintf("env_%v", suite.CegaConfig.AuthURL))
os.Setenv("CEGA_SECRET", fmt.Sprintf("env_%v", suite.CegaConfig.Secret))
os.Setenv("SERVER_CERT", fmt.Sprintf("env_%v", suite.ServerConfig.Cert))
os.Setenv("SERVER_KEY", fmt.Sprintf("env_%v", suite.ServerConfig.Key))
os.Setenv("S3INBOX", fmt.Sprintf("env_%v", suite.S3Inbox))
os.Setenv("JWTISSUER", fmt.Sprintf("env_%v", suite.JwtIssuer))
os.Setenv("JWTPRIVATEKEY", fmt.Sprintf("%v_env", suite.JwtPrivateKey))
os.Setenv("JWTSIGNATUREALG", fmt.Sprintf("env_%v", suite.JwtSignatureAlg))
os.Setenv("RESIGNJWT", fmt.Sprintf("%t", suite.ResignJwt))
// re-read the config
config, err = NewConfig()
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.ElixirConfig.ID), config.Elixir.ID, "Elixir ID misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.ElixirConfig.Provider), config.Elixir.Provider, "Elixir Issuer misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.ElixirConfig.RedirectURL), config.Elixir.RedirectURL, "Elixir RedirectURL misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.ElixirConfig.Secret), config.Elixir.Secret, "Elixir Secret misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.CegaConfig.ID), config.Cega.ID, "CEGA ID misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.CegaConfig.AuthURL), config.Cega.AuthURL, "CEGA AuthURL misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.CegaConfig.Secret), config.Cega.Secret, "CEGA Secret misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.ServerConfig.Cert), config.Server.Cert, "ServerConfig Cert misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.ServerConfig.Key), config.Server.Key, "ServerConfig Key misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.S3Inbox), config.S3Inbox, "S3Inbox misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.JwtIssuer), config.JwtIssuer, "JwtIssuer misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("%v_env", suite.JwtPrivateKey), config.JwtPrivateKey, "JwtPrivateKey misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("env_%v", suite.JwtSignatureAlg), config.JwtSignatureAlg, "JwtSignatureAlg misread from environment variable")
assert.Equal(suite.T(), fmt.Sprintf("%t", suite.ResignJwt), strconv.FormatBool(config.ResignJwt), "ResignJwt misread from environment variable")
// Check missing private key
os.Setenv("JWTPRIVATEKEY", "nonexistent-key-file")
// re-read the config
_, err = NewConfig()
assert.ErrorContains(suite.T(), err, "missing private key file")
// Repeat check with CEGA login and JWT resigning disabled
os.Setenv("CEGA_ID", "")
os.Setenv("CEGA_SECRET", "")
os.Setenv("RESIGNJWT", fmt.Sprintf("%t", false))
// re-read the config
_, err = NewConfig()
assert.NoError(suite.T(), err)
}