-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
203 lines (190 loc) · 5.42 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
package corbado
import (
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewConfig_Success(t *testing.T) {
projectID := "pro-12345678"
secret := "mysupersecret"
backendAPI := "http://localhost:8080"
frontendAPI := "http://localhost:8081"
cfg, err := NewConfig(projectID, secret, frontendAPI, backendAPI)
require.NoError(t, err)
assert.Equal(t, projectID, cfg.ProjectID)
assert.Equal(t, secret, cfg.APISecret)
assert.Equal(t, frontendAPI, cfg.FrontendAPI)
assert.Equal(t, backendAPI, cfg.BackendAPI)
assert.Equal(t, configDefaultCacheMaxAge, cfg.CacheMaxAge)
assert.Equal(t, configDefaultJWKSRefreshInterval, cfg.JWKSRefreshInterval)
assert.Equal(t, configDefaultJWKSRefreshRateLimit, cfg.JWKSRefreshRateLimit)
assert.Equal(t, configDefaultJWKSRefreshTimeout, cfg.JWKSRefreshTimeout)
}
func TestNewConfig_Failure(t *testing.T) {
tests := []struct {
name string
projectID string
secret string
frontendAPI string
backendAPI string
}{
{
name: "empty projectID and secret",
},
{
name: "empty projectID",
secret: "secret",
},
{
name: "empty secret",
projectID: "pro-12345678",
},
{
name: "empty frontendAPI",
projectID: "pro-12345678",
secret: "secret",
},
{
name: "empty backendAPI",
projectID: "pro-12345678",
secret: "secret",
frontendAPI: "http://localhost:8080",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfg, err := NewConfig(test.projectID, test.secret, test.frontendAPI, test.backendAPI)
assert.Nil(t, cfg)
assert.ErrorContains(t, err, "given value '' is too short")
})
}
}
// nolint:funlen
func TestConfig_Validate(t *testing.T) {
tests := []struct {
name string
config Config
expectedErrorContains string
}{
{
name: "invalid ProjectID",
config: Config{
ProjectID: "",
APISecret: "corbado1_secret",
FrontendAPI: "http://localhost:8080",
BackendAPI: "http://localhost:9090",
},
expectedErrorContains: "Invalid ProjectID given",
},
{
name: "invalid APISecret",
config: Config{
ProjectID: "pro-12345678",
APISecret: "",
FrontendAPI: "http://localhost:8080",
BackendAPI: "http://localhost:9090",
},
expectedErrorContains: "Invalid APISecret given",
},
{
name: "invalid FrontendAPI",
config: Config{
ProjectID: "pro-12345678",
APISecret: "corbado1_secret",
FrontendAPI: "",
BackendAPI: "http://localhost:9090",
},
expectedErrorContains: "Invalid FrontendAPI given",
},
{
name: "invalid BackendAPI",
config: Config{
ProjectID: "pro-12345678",
APISecret: "corbado1_secret",
FrontendAPI: "http://localhost:8080",
BackendAPI: "",
},
expectedErrorContains: "Invalid BackendAPI given",
},
{
name: "invalid CacheMaxAge",
config: Config{
ProjectID: "pro-12345678",
APISecret: "corbado1_secret",
FrontendAPI: "http://localhost:8080",
BackendAPI: "http://localhost:9090",
CacheMaxAge: 0,
},
expectedErrorContains: "Invalid CacheMaxAge given",
},
{
name: "invalid JWKSRefreshInterval",
config: Config{
ProjectID: "pro-12345678",
APISecret: "corbado1_secret",
FrontendAPI: "http://localhost:8080",
BackendAPI: "http://localhost:9090",
CacheMaxAge: 10 * time.Second,
JWKSRefreshInterval: 0,
},
expectedErrorContains: "Invalid JWKSRefreshInterval given",
},
{
name: "invalid JWKSRefreshRateLimit",
config: Config{
ProjectID: "pro-12345678",
APISecret: "corbado1_secret",
FrontendAPI: "http://localhost:8080",
BackendAPI: "http://localhost:9090",
CacheMaxAge: 10 * time.Second,
JWKSRefreshInterval: 10 * time.Second,
JWKSRefreshRateLimit: 0,
},
expectedErrorContains: "Invalid JWKSRefreshRateLimit given",
},
{
name: "invalid JWKSRefreshTimeout",
config: Config{
ProjectID: "pro-12345678",
APISecret: "corbado1_secret",
FrontendAPI: "http://localhost:8080",
BackendAPI: "http://localhost:9090",
CacheMaxAge: 10 * time.Second,
JWKSRefreshInterval: 10 * time.Second,
JWKSRefreshRateLimit: 10 * time.Second,
JWKSRefreshTimeout: 0,
},
expectedErrorContains: "Invalid JWKSRefreshTimeout given",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.config.validate()
// Assert that an error exists and contains the expected substring
if err == nil {
t.Errorf("Expected an error, but got none")
} else if !strings.Contains(err.Error(), test.expectedErrorContains) {
t.Errorf("Expected error containing %q, but got %q", test.expectedErrorContains, err.Error())
}
})
}
}
func TestConfig_Validate_Success(t *testing.T) {
validConfig := Config{
ProjectID: "pro-12345678",
APISecret: "corbado1_secret",
FrontendAPI: "http://localhost:8080",
BackendAPI: "http://localhost:9090",
CacheMaxAge: 10 * time.Second,
JWKSRefreshInterval: 10 * time.Second,
JWKSRefreshRateLimit: 10 * time.Second,
JWKSRefreshTimeout: 10 * time.Second,
}
err := validConfig.validate()
// Assert no error is returned for valid config
if err != nil {
t.Errorf("Expected no error, but got %q", err.Error())
}
}