-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain_test.go
More file actions
537 lines (463 loc) · 14.3 KB
/
main_test.go
File metadata and controls
537 lines (463 loc) · 14.3 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"sort"
"strings"
"testing"
"github.com/fiware/VCVerifier/config"
"github.com/fiware/VCVerifier/database"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "modernc.org/sqlite"
)
// newTestDB creates an in-memory SQLite database with initialized schema
// suitable for use in tests. The caller must call db.Close() when done.
func newTestDB(t *testing.T) (*database.SqlServiceRepository, *http.Server, func()) {
t.Helper()
cfg := config.Database{
Type: database.DriverTypeSQLite,
Name: "",
}
db, err := database.NewConnection(cfg)
require.NoError(t, err)
err = database.InitSchema(db, cfg.Type)
require.NoError(t, err)
repo := database.NewServiceRepository(db, cfg.Type)
router := getConfigRouter(db, repo)
srv := httptest.NewServer(router)
return repo, nil, func() {
srv.Close()
database.Close(db)
}
}
func TestInitConfigServer_WithSQLite(t *testing.T) {
configuration := &config.Configuration{
Database: config.Database{
Type: database.DriverTypeSQLite,
Name: "",
},
ConfigServer: config.ConfigServer{
Enabled: true,
Port: 0, // Will be overridden by httptest
ReadTimeout: 5,
WriteTimeout: 10,
IdleTimeout: 120,
ShutdownTimeout: 5,
},
}
db, srv, repo, err := initConfigServer(configuration)
require.NoError(t, err)
assert.NotNil(t, db)
assert.NotNil(t, srv)
assert.NotNil(t, repo)
// Verify server is configured with correct address
assert.Contains(t, srv.Addr, fmt.Sprintf(":%v", configuration.ConfigServer.Port))
// Cleanup
database.Close(db)
}
func TestInitConfigServer_InvalidDBType(t *testing.T) {
configuration := &config.Configuration{
Database: config.Database{
Type: "invalid",
Name: "test",
},
ConfigServer: config.ConfigServer{
Enabled: true,
Port: 9999,
},
}
db, srv, repo, err := initConfigServer(configuration)
assert.Error(t, err)
assert.Nil(t, db)
assert.Nil(t, srv)
assert.Nil(t, repo)
}
func TestGetConfigRouter_HasHealthEndpoint(t *testing.T) {
cfg := config.Database{
Type: database.DriverTypeSQLite,
Name: "",
}
db, err := database.NewConnection(cfg)
require.NoError(t, err)
defer database.Close(db)
err = database.InitSchema(db, cfg.Type)
require.NoError(t, err)
repo := database.NewServiceRepository(db, cfg.Type)
router := getConfigRouter(db, repo)
// Test health endpoint
req := httptest.NewRequest(http.MethodGet, "/health", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
}
func TestGetConfigRouter_HasCCSAPIEndpoints(t *testing.T) {
cfg := config.Database{
Type: database.DriverTypeSQLite,
Name: "",
}
db, err := database.NewConnection(cfg)
require.NoError(t, err)
defer database.Close(db)
err = database.InitSchema(db, cfg.Type)
require.NoError(t, err)
repo := database.NewServiceRepository(db, cfg.Type)
router := getConfigRouter(db, repo)
tests := []struct {
name string
method string
path string
body string
expectedStatus int
}{
{
name: "GET /service returns empty list",
method: http.MethodGet,
path: "/service",
expectedStatus: http.StatusOK,
},
{
name: "GET /service/:id returns 404 for missing service",
method: http.MethodGet,
path: "/service/nonexistent",
expectedStatus: http.StatusNotFound,
},
{
name: "DELETE /service/:id returns 404 for missing service",
method: http.MethodDelete,
path: "/service/nonexistent",
expectedStatus: http.StatusNotFound,
},
{
name: "POST /service creates a service",
method: http.MethodPost,
path: "/service",
body: `{
"id": "test-service",
"defaultOidcScope": "defaultScope",
"oidcScopes": {
"defaultScope": {
"credentials": [{"type": "TestCredential", "trustedIssuersLists": ["https://tir.example.com"]}]
}
}
}`,
expectedStatus: http.StatusCreated,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var req *http.Request
if tc.body != "" {
req = httptest.NewRequest(tc.method, tc.path, strings.NewReader(tc.body))
req.Header.Set("Content-Type", "application/json")
} else {
req = httptest.NewRequest(tc.method, tc.path, nil)
}
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, tc.expectedStatus, w.Code,
"endpoint %s %s returned unexpected status", tc.method, tc.path)
})
}
}
func TestGetConfigRouter_CORSHeaders(t *testing.T) {
cfg := config.Database{
Type: database.DriverTypeSQLite,
Name: "",
}
db, err := database.NewConnection(cfg)
require.NoError(t, err)
defer database.Close(db)
err = database.InitSchema(db, cfg.Type)
require.NoError(t, err)
repo := database.NewServiceRepository(db, cfg.Type)
router := getConfigRouter(db, repo)
// Test CORS preflight request. The Origin must differ from the request Host
// so the gin-contrib/cors middleware treats it as a cross-origin request.
req := httptest.NewRequest(http.MethodOptions, "/service", nil)
req.Header.Set("Origin", "http://other-domain.com")
req.Header.Set("Access-Control-Request-Method", "POST")
req.Header.Set("Access-Control-Request-Headers", "Content-Type")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, "*", w.Header().Get("Access-Control-Allow-Origin"))
}
func TestGetConfigRouter_FullCRUDFlow(t *testing.T) {
cfg := config.Database{
Type: database.DriverTypeSQLite,
Name: "",
}
db, err := database.NewConnection(cfg)
require.NoError(t, err)
defer database.Close(db)
err = database.InitSchema(db, cfg.Type)
require.NoError(t, err)
repo := database.NewServiceRepository(db, cfg.Type)
router := getConfigRouter(db, repo)
serviceID := "crud-test-service"
createBody := fmt.Sprintf(`{
"id": "%s",
"defaultOidcScope": "myScope",
"oidcScopes": {
"myScope": {
"credentials": [{"type": "VerifiableCredential", "trustedIssuersLists": ["https://tir.example.com"]}]
}
}
}`, serviceID)
// CREATE
req := httptest.NewRequest(http.MethodPost, "/service", strings.NewReader(createBody))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
// READ
req = httptest.NewRequest(http.MethodGet, "/service/"+serviceID, nil)
w = httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var getResp map[string]interface{}
err = json.Unmarshal(w.Body.Bytes(), &getResp)
require.NoError(t, err)
assert.Equal(t, serviceID, getResp["id"])
assert.Equal(t, "myScope", getResp["defaultOidcScope"])
// UPDATE
updateBody := fmt.Sprintf(`{
"id": "%s",
"defaultOidcScope": "updatedScope",
"oidcScopes": {
"updatedScope": {
"credentials": [{"type": "UpdatedCredential", "trustedIssuersLists": ["https://tir.example.com"]}]
}
}
}`, serviceID)
req = httptest.NewRequest(http.MethodPut, "/service/"+serviceID, strings.NewReader(updateBody))
req.Header.Set("Content-Type", "application/json")
w = httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
// LIST
req = httptest.NewRequest(http.MethodGet, "/service", nil)
w = httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var listResp map[string]interface{}
err = json.Unmarshal(w.Body.Bytes(), &listResp)
require.NoError(t, err)
assert.Equal(t, float64(1), listResp["total"])
// DELETE
req = httptest.NewRequest(http.MethodDelete, "/service/"+serviceID, nil)
w = httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusNoContent, w.Code)
// Verify deleted
req = httptest.NewRequest(http.MethodGet, "/service/"+serviceID, nil)
w = httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
}
func TestConfigServerDisabled_NoDBRequired(t *testing.T) {
// When ConfigServer.Enabled is false, the main function should not attempt
// to open a database. This test verifies the condition is correct.
configuration := config.Configuration{
ConfigServer: config.ConfigServer{
Enabled: false,
},
}
// The condition in main: configuration.ConfigServer.Enabled
// When false, initConfigServer should not be called.
assert.False(t, configuration.ConfigServer.Enabled)
}
func TestGetConfigRouter_RegistersAllRoutes(t *testing.T) {
cfg := config.Database{
Type: database.DriverTypeSQLite,
Name: "",
}
db, err := database.NewConnection(cfg)
require.NoError(t, err)
defer database.Close(db)
err = database.InitSchema(db, cfg.Type)
require.NoError(t, err)
repo := database.NewServiceRepository(db, cfg.Type)
router := getConfigRouter(db, repo)
// Verify all expected routes are registered
routes := router.Routes()
routeMap := make(map[string]bool)
for _, r := range routes {
key := r.Method + " " + r.Path
routeMap[key] = true
}
expectedRoutes := []string{
"GET /health",
"POST /service",
"GET /service",
"GET /service/:id",
"PUT /service/:id",
"DELETE /service/:id",
"GET /service/:id/scope",
}
for _, expected := range expectedRoutes {
assert.True(t, routeMap[expected], "expected route %q to be registered", expected)
}
}
func TestInitConfigServer_SetsCorrectTimeouts(t *testing.T) {
configuration := &config.Configuration{
Database: config.Database{
Type: database.DriverTypeSQLite,
Name: "",
},
ConfigServer: config.ConfigServer{
Enabled: true,
Port: 9876,
ReadTimeout: 15,
WriteTimeout: 30,
IdleTimeout: 240,
ShutdownTimeout: 10,
},
}
db, srv, _, err := initConfigServer(configuration)
require.NoError(t, err)
defer database.Close(db)
assert.Contains(t, srv.Addr, "9876")
// Verify timeouts are set correctly (in seconds, converted to time.Duration)
assert.Equal(t, 15*1e9, float64(srv.ReadTimeout))
assert.Equal(t, 30*1e9, float64(srv.WriteTimeout))
assert.Equal(t, 240*1e9, float64(srv.IdleTimeout))
}
func TestGetConfigRouter_HealthEndpointIncludesDBCheck(t *testing.T) {
cfg := config.Database{
Type: database.DriverTypeSQLite,
Name: "",
}
db, err := database.NewConnection(cfg)
require.NoError(t, err)
defer database.Close(db)
err = database.InitSchema(db, cfg.Type)
require.NoError(t, err)
repo := database.NewServiceRepository(db, cfg.Type)
router := getConfigRouter(db, repo)
// Test health endpoint returns JSON with system info
req := httptest.NewRequest(http.MethodGet, "/health", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var result map[string]interface{}
err = json.Unmarshal(w.Body.Bytes(), &result)
require.NoError(t, err)
assert.Equal(t, "OK", result["status"])
}
func TestResolveAllowedOrigins(t *testing.T) {
tests := []struct {
name string
services []config.ConfiguredService
want []string
}{
{
name: "no services returns wildcard",
services: nil,
want: []string{"*"},
},
{
name: "empty services slice returns wildcard",
services: []config.ConfiguredService{},
want: []string{"*"},
},
{
name: "services with no allowedOrigins returns wildcard",
services: []config.ConfiguredService{
{Id: "svc1"},
{Id: "svc2"},
},
want: []string{"*"},
},
{
name: "services with empty allowedOrigins returns wildcard",
services: []config.ConfiguredService{
{Id: "svc1", AllowedOrigins: []string{}},
},
want: []string{"*"},
},
{
name: "single service with specific origins",
services: []config.ConfiguredService{
{Id: "svc1", AllowedOrigins: []string{"https://example.com", "https://app.example.com"}},
},
want: []string{"https://example.com", "https://app.example.com"},
},
{
name: "multiple services with different origins returns deduplicated union",
services: []config.ConfiguredService{
{Id: "svc1", AllowedOrigins: []string{"https://alpha.com"}},
{Id: "svc2", AllowedOrigins: []string{"https://beta.com"}},
},
want: []string{"https://alpha.com", "https://beta.com"},
},
{
name: "duplicate origins across services are deduplicated",
services: []config.ConfiguredService{
{Id: "svc1", AllowedOrigins: []string{"https://shared.com", "https://alpha.com"}},
{Id: "svc2", AllowedOrigins: []string{"https://shared.com", "https://beta.com"}},
},
want: []string{"https://shared.com", "https://alpha.com", "https://beta.com"},
},
{
name: "any service with wildcard returns wildcard only",
services: []config.ConfiguredService{
{Id: "svc1", AllowedOrigins: []string{"https://example.com"}},
{Id: "svc2", AllowedOrigins: []string{"*"}},
},
want: []string{"*"},
},
{
name: "first service with wildcard short-circuits",
services: []config.ConfiguredService{
{Id: "svc1", AllowedOrigins: []string{"*"}},
{Id: "svc2", AllowedOrigins: []string{"https://example.com"}},
},
want: []string{"*"},
},
{
name: "wildcard mixed within origins of a single service",
services: []config.ConfiguredService{
{Id: "svc1", AllowedOrigins: []string{"https://example.com", "*", "https://other.com"}},
},
want: []string{"*"},
},
{
name: "mix of configured and unconfigured services",
services: []config.ConfiguredService{
{Id: "svc1"},
{Id: "svc2", AllowedOrigins: []string{"https://example.com"}},
{Id: "svc3", AllowedOrigins: []string{}},
},
want: []string{"https://example.com"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ResolveAllowedOrigins(tt.services)
// Sort both slices for order-independent comparison when not testing
// wildcard (wildcard is always a single element so order is irrelevant).
if len(got) > 1 || len(tt.want) > 1 {
sortedGot := make([]string, len(got))
copy(sortedGot, got)
sort.Strings(sortedGot)
sortedWant := make([]string, len(tt.want))
copy(sortedWant, tt.want)
sort.Strings(sortedWant)
if !reflect.DeepEqual(sortedGot, sortedWant) {
t.Errorf("ResolveAllowedOrigins() = %v, want %v", got, tt.want)
}
} else if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ResolveAllowedOrigins() = %v, want %v", got, tt.want)
}
})
}
}
// Ensure init() sets gin to test mode without interfering with other tests
func init() {
gin.SetMode(gin.TestMode)
}