-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
226 lines (199 loc) · 6.41 KB
/
errors_test.go
File metadata and controls
226 lines (199 loc) · 6.41 KB
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
216
217
218
219
220
221
222
223
224
225
226
package singpass
import (
"context"
"errors"
"testing"
"time"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vector233/go-singpass/internal/auth"
)
// TestConfigValidationErrors tests that config validation returns proper error types
func TestConfigValidationErrors(t *testing.T) {
tests := []struct {
name string
config *Config
expected string
}{
{
name: "missing ClientID",
config: &Config{},
expected: "ClientID",
},
{
name: "missing RedirectURI",
config: &Config{
ClientID: "test-client",
},
expected: "RedirectURI",
},
{
name: "missing AuthURL",
config: &Config{
ClientID: "test-client",
RedirectURI: "https://example.com/callback",
},
expected: "AuthURL",
},
{
name: "missing TokenURL",
config: &Config{
ClientID: "test-client",
RedirectURI: "https://example.com/callback",
AuthURL: "https://example.com/auth",
},
expected: "TokenURL",
},
{
name: "missing UserInfoURL",
config: &Config{
ClientID: "test-client",
RedirectURI: "https://example.com/callback",
AuthURL: "https://example.com/auth",
TokenURL: "https://example.com/token",
},
expected: "UserInfoURL",
},
{
name: "missing JWKSURL",
config: &Config{
ClientID: "test-client",
RedirectURI: "https://example.com/callback",
AuthURL: "https://example.com/auth",
TokenURL: "https://example.com/token",
UserInfoURL: "https://example.com/userinfo",
},
expected: "JWKSURL",
},
{
name: "missing RedisAddr when UseRedis is true",
config: &Config{
ClientID: "test-client",
RedirectURI: "https://example.com/callback",
AuthURL: "https://example.com/auth",
TokenURL: "https://example.com/token",
UserInfoURL: "https://example.com/userinfo",
JWKSURL: "https://example.com/jwks",
UseRedis: true,
},
expected: "RedisAddr (required when UseRedis is true)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()
require.Error(t, err)
// Check that it's the correct error type
var configErr ErrInvalidConfig
require.True(t, errors.As(err, &configErr))
assert.Equal(t, tt.expected, configErr.Field)
})
}
}
// TestStateManagerErrors tests state management error types
func TestStateManagerErrors(t *testing.T) {
t.Run("memory store - invalid state", func(t *testing.T) {
store := auth.NewMemoryStateStore(time.Minute)
ctx := context.Background()
// Try to get non-existent state
_, err := store.Get(ctx, "non-existent-state")
require.Error(t, err)
// Check that it's the correct error type
var stateErr ErrInvalidState
require.True(t, errors.As(err, &stateErr))
assert.Equal(t, "state not found or expired", stateErr.Message)
})
t.Run("memory store - expired state", func(t *testing.T) {
store := auth.NewMemoryStateStore(time.Millisecond)
ctx := context.Background()
// Store state data
stateData := &auth.StateData{
CodeVerifier: "test-verifier",
Nonce: "test-nonce",
}
err := store.Store(ctx, "test-state", stateData)
require.NoError(t, err)
// Wait for expiration
time.Sleep(10 * time.Millisecond)
// Try to get expired state
_, err = store.Get(ctx, "test-state")
require.Error(t, err)
// Check that it's the correct error type
var stateErr ErrInvalidState
require.True(t, errors.As(err, &stateErr))
assert.Equal(t, "state not found or expired", stateErr.Message)
})
}
// TestRedisStateStoreErrors tests Redis state store error types
func TestRedisStateStoreErrors(t *testing.T) {
// Skip if Redis is not available
if testing.Short() {
t.Skip("Skipping Redis tests in short mode")
}
t.Run("redis store - connection error", func(t *testing.T) {
// Create Redis client with invalid address
redisClient := redis.NewClient(&redis.Options{
Addr: "invalid:6379",
DB: 0,
})
defer redisClient.Close()
store := auth.NewRedisStateStore(redisClient, time.Minute)
ctx := context.Background()
// Try to store state data (should fail due to connection error)
stateData := &auth.StateData{
CodeVerifier: "test-verifier",
Nonce: "test-nonce",
}
err := store.Store(ctx, "test-state", stateData)
if err != nil {
// Check that it's the correct error type
var redisErr ErrRedisOperation
if errors.As(err, &redisErr) {
assert.Equal(t, "set", redisErr.Operation)
// Check that error message contains network-related keywords
assert.True(t, len(redisErr.Message) > 0, "Error message should not be empty")
}
}
})
t.Run("redis store - get non-existent state", func(t *testing.T) {
// Create Redis client with invalid address
redisClient := redis.NewClient(&redis.Options{
Addr: "invalid:6379",
DB: 0,
})
defer redisClient.Close()
store := auth.NewRedisStateStore(redisClient, time.Minute)
ctx := context.Background()
// Try to get non-existent state
_, err := store.Get(ctx, "non-existent-state")
if err != nil {
// Could be either ErrInvalidState (if Redis returns Nil) or ErrRedisOperation (if connection fails)
var stateErr ErrInvalidState
var redisErr ErrRedisOperation
if errors.As(err, &stateErr) {
assert.Equal(t, "state not found or expired", stateErr.Message)
} else if errors.As(err, &redisErr) {
assert.Equal(t, "get", redisErr.Operation)
}
}
})
}
// TestErrorTypeExports tests that error types are properly exported
func TestErrorTypeExports(t *testing.T) {
t.Run("error types are accessible", func(t *testing.T) {
// Test that we can create instances of exported error types
configErr := ErrInvalidConfig{Field: "test"}
assert.Equal(t, "invalid config: test is required", configErr.Error())
stateErr := ErrInvalidState{Message: "test message"}
assert.Equal(t, "invalid state: test message", stateErr.Error())
tokenErr := ErrTokenValidation{Message: "test validation"}
assert.Equal(t, "token validation failed: test validation", tokenErr.Error())
httpErr := ErrHTTPRequest{StatusCode: 400, Message: "bad request"}
assert.Equal(t, "HTTP request failed (status 400): bad request", httpErr.Error())
redisErr := ErrRedisOperation{Operation: "set", Message: "connection failed"}
assert.Equal(t, "Redis set failed: connection failed", redisErr.Error())
jwksErr := ErrJWKSFetch{Message: "fetch failed"}
assert.Equal(t, "JWKS fetch failed: fetch failed", jwksErr.Error())
})
}