summaryrefslogtreecommitdiffstats
path: root/internal/config/config_test.go
blob: 65a742a62e7eb50f13141250bd3b44debc9ccc8e (plain)
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
package config

import (
	"os"
	"strings"
	"testing"
	"time"
)

func TestDefault(t *testing.T) {
	cfg := Default()

	if cfg.Server.GrpcAddr != ":50051" {
		t.Errorf("expected default grpc_addr :50051, got %s", cfg.Server.GrpcAddr)
	}

	if cfg.Database.Path != "relay.db" {
		t.Errorf("expected default db path relay.db, got %s", cfg.Database.Path)
	}

	if cfg.Metrics.Enabled != true {
		t.Error("expected metrics enabled by default")
	}
}

func TestLoadYAML(t *testing.T) {
	// Create temporary config file
	tmpfile, err := os.CreateTemp("", "config-*.yaml")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(tmpfile.Name())

	configData := `
server:
  grpc_addr: ":9999"
  http_addr: ":8888"

database:
  path: "test.db"

auth:
  read:
    enabled: true
  write:
    enabled: true
  timestamp_window: 120

rate_limit:
  enabled: true
  default_rps: 50
  default_burst: 100

metrics:
  enabled: true
  addr: ":9191"
  namespace: "test"
`

	if _, err := tmpfile.Write([]byte(configData)); err != nil {
		t.Fatal(err)
	}
	tmpfile.Close()

	// Load config
	cfg, err := Load(tmpfile.Name())
	if err != nil {
		t.Fatalf("failed to load config: %v", err)
	}

	// Verify values
	if cfg.Server.GrpcAddr != ":9999" {
		t.Errorf("expected grpc_addr :9999, got %s", cfg.Server.GrpcAddr)
	}

	if cfg.Database.Path != "test.db" {
		t.Errorf("expected db path test.db, got %s", cfg.Database.Path)
	}

	if !cfg.Auth.Read.Enabled {
		t.Error("expected auth read enabled")
	}

	if !cfg.Auth.Write.Enabled {
		t.Error("expected auth write enabled")
	}

	if cfg.Auth.TimestampWindow != 120 {
		t.Errorf("expected timestamp window 120, got %d", cfg.Auth.TimestampWindow)
	}

	if cfg.RateLimit.DefaultRPS != 50 {
		t.Errorf("expected rate limit 50, got %.1f", cfg.RateLimit.DefaultRPS)
	}

	if cfg.Metrics.Namespace != "test" {
		t.Errorf("expected metrics namespace test, got %s", cfg.Metrics.Namespace)
	}
}

func TestEnvOverrides(t *testing.T) {
	// Set environment variables
	os.Setenv("MUXSTR_SERVER_GRPC_ADDR", ":7777")
	os.Setenv("MUXSTR_AUTH_READ_ENABLED", "true")
	os.Setenv("MUXSTR_AUTH_WRITE_ENABLED", "true")
	os.Setenv("MUXSTR_RATE_LIMIT_DEFAULT_RPS", "200")
	defer func() {
		os.Unsetenv("MUXSTR_SERVER_GRPC_ADDR")
		os.Unsetenv("MUXSTR_AUTH_READ_ENABLED")
		os.Unsetenv("MUXSTR_AUTH_WRITE_ENABLED")
		os.Unsetenv("MUXSTR_RATE_LIMIT_DEFAULT_RPS")
	}()

	// Load with empty file (just defaults + env)
	cfg, err := Load("")
	if err != nil {
		t.Fatalf("failed to load config: %v", err)
	}

	// Verify env overrides
	if cfg.Server.GrpcAddr != ":7777" {
		t.Errorf("expected env override :7777, got %s", cfg.Server.GrpcAddr)
	}

	if !cfg.Auth.Read.Enabled {
		t.Error("expected auth read enabled from env")
	}

	if !cfg.Auth.Write.Enabled {
		t.Error("expected auth write enabled from env")
	}

	if cfg.RateLimit.DefaultRPS != 200 {
		t.Errorf("expected rate limit 200 from env, got %.1f", cfg.RateLimit.DefaultRPS)
	}
}

func TestValidation(t *testing.T) {
	tests := []struct {
		name    string
		cfg     *Config
		wantErr bool
	}{
		{
			name:    "valid default config",
			cfg:     Default(),
			wantErr: false,
		},
		{
			name: "missing grpc_addr",
			cfg: &Config{
				Server: ServerConfig{
					HttpAddr: ":8080",
				},
				Database: DatabaseConfig{
					Path: "test.db",
				},
			},
			wantErr: true,
		},
		{
			name: "missing http_addr",
			cfg: &Config{
				Server: ServerConfig{
					GrpcAddr: ":50051",
				},
				Database: DatabaseConfig{
					Path: "test.db",
				},
			},
			wantErr: true,
		},
		{
			name: "missing database path",
			cfg: &Config{
				Server: ServerConfig{
					GrpcAddr: ":50051",
					HttpAddr: ":8080",
				},
				Database: DatabaseConfig{},
			},
			wantErr: true,
		},
		{
			name: "invalid log level",
			cfg: &Config{
				Server: ServerConfig{
					GrpcAddr: ":50051",
					HttpAddr: ":8080",
				},
				Database: DatabaseConfig{
					Path: "test.db",
				},
				Logging: LoggingConfig{
					Level:  "invalid",
					Format: "json",
				},
			},
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := tt.cfg.Validate()
			if (err != nil) != tt.wantErr {
				t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func TestSaveAndLoad(t *testing.T) {
	// Create config
	cfg := Default()
	cfg.Server.GrpcAddr = ":9999"
	cfg.Auth.Read.Enabled = true
	cfg.Auth.Write.Enabled = true
	cfg.RateLimit.DefaultRPS = 100

	// Save to temp file
	tmpfile, err := os.CreateTemp("", "config-*.yaml")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(tmpfile.Name())
	tmpfile.Close()

	if err := cfg.Save(tmpfile.Name()); err != nil {
		t.Fatalf("failed to save config: %v", err)
	}

	// Load it back
	loaded, err := Load(tmpfile.Name())
	if err != nil {
		t.Fatalf("failed to load config: %v", err)
	}

	// Verify
	if loaded.Server.GrpcAddr != ":9999" {
		t.Errorf("expected grpc_addr :9999, got %s", loaded.Server.GrpcAddr)
	}

	if !loaded.Auth.Read.Enabled {
		t.Error("expected auth read enabled")
	}

	if !loaded.Auth.Write.Enabled {
		t.Error("expected auth write enabled")
	}

	if loaded.RateLimit.DefaultRPS != 100 {
		t.Errorf("expected rate limit 100, got %.1f", loaded.RateLimit.DefaultRPS)
	}
}

func TestNpubNormalization(t *testing.T) {
	// Create a test key to get a valid npub
	tmpfile, err := os.CreateTemp("", "config-*.yaml")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(tmpfile.Name())

	// Use a real npub for testing
	configData := `
server:
  grpc_addr: ":50051"
  http_addr: ":8080"

database:
  path: "test.db"

auth:
  read:
    enabled: true
    allowed_npubs:
      - npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6
      - npub1l2vyh47mk2p0qlsku7hg0vn29faehy9hy34ygaclpn66ukqp3afqutajft
  write:
    enabled: true
    allowed_npubs:
      - npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6
`

	if _, err := tmpfile.Write([]byte(configData)); err != nil {
		t.Fatal(err)
	}
	tmpfile.Close()

	cfg, err := Load(tmpfile.Name())
	if err != nil {
		t.Fatalf("failed to load config: %v", err)
	}

	// Verify read npubs were normalized to hex
	if len(cfg.Auth.Read.AllowedNpubs) != 2 {
		t.Errorf("expected 2 allowed npubs for read, got %d", len(cfg.Auth.Read.AllowedNpubs))
	}

	// Verify write npubs were normalized to hex
	if len(cfg.Auth.Write.AllowedNpubs) != 1 {
		t.Errorf("expected 1 allowed npub for write, got %d", len(cfg.Auth.Write.AllowedNpubs))
	}

	// Check that they're hex format (64 chars, not npub1...)
	for i, pubkey := range cfg.Auth.Read.AllowedNpubs {
		if len(pubkey) != 64 {
			t.Errorf("read npub %d: expected 64 hex chars, got %d", i, len(pubkey))
		}
		if len(pubkey) >= 5 && pubkey[:5] == "npub1" {
			t.Errorf("read npub %d: should be normalized to hex, still in npub format", i)
		}
	}

	for i, pubkey := range cfg.Auth.Write.AllowedNpubs {
		if len(pubkey) != 64 {
			t.Errorf("write npub %d: expected 64 hex chars, got %d", i, len(pubkey))
		}
		if len(pubkey) >= 5 && pubkey[:5] == "npub1" {
			t.Errorf("write npub %d: should be normalized to hex, still in npub format", i)
		}
	}

	// Verify the actual hex values
	expectedHex1 := "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"
	expectedHex2 := "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52"

	if cfg.Auth.Read.AllowedNpubs[0] != expectedHex1 {
		t.Errorf("read npub 0: expected %s, got %s", expectedHex1, cfg.Auth.Read.AllowedNpubs[0])
	}
	if cfg.Auth.Read.AllowedNpubs[1] != expectedHex2 {
		t.Errorf("read npub 1: expected %s, got %s", expectedHex2, cfg.Auth.Read.AllowedNpubs[1])
	}
	if cfg.Auth.Write.AllowedNpubs[0] != expectedHex1 {
		t.Errorf("write npub 0: expected %s, got %s", expectedHex1, cfg.Auth.Write.AllowedNpubs[0])
	}
}

func TestNpubValidation(t *testing.T) {
	tests := []struct {
		name        string
		config      string
		expectError bool
		errorMsg    string
	}{
		{
			name: "invalid hex in read list",
			config: `
server:
  grpc_addr: ":50051"
  http_addr: ":8080"
database:
  path: "test.db"
auth:
  read:
    enabled: true
    allowed_npubs:
      - 3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d
`,
			expectError: true,
			errorMsg:    "must start with 'npub1'",
		},
		{
			name: "invalid hex in write list",
			config: `
server:
  grpc_addr: ":50051"
  http_addr: ":8080"
database:
  path: "test.db"
auth:
  write:
    enabled: true
    allowed_npubs:
      - 3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d
`,
			expectError: true,
			errorMsg:    "must start with 'npub1'",
		},
		{
			name: "valid npub lists",
			config: `
server:
  grpc_addr: ":50051"
  http_addr: ":8080"
database:
  path: "test.db"
auth:
  read:
    enabled: true
    allowed_npubs:
      - npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6
  write:
    enabled: true
    allowed_npubs:
      - npub1l2vyh47mk2p0qlsku7hg0vn29faehy9hy34ygaclpn66ukqp3afqutajft
`,
			expectError: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tmpfile, err := os.CreateTemp("", "config-*.yaml")
			if err != nil {
				t.Fatal(err)
			}
			defer os.Remove(tmpfile.Name())

			if _, err := tmpfile.Write([]byte(tt.config)); err != nil {
				t.Fatal(err)
			}
			tmpfile.Close()

			_, err = Load(tmpfile.Name())
			if tt.expectError {
				if err == nil {
					t.Error("expected error, got nil")
				} else if !strings.Contains(err.Error(), tt.errorMsg) {
					t.Errorf("expected error containing %q, got: %v", tt.errorMsg, err)
				}
			} else {
				if err != nil {
					t.Errorf("unexpected error: %v", err)
				}
			}
		})
	}
}

func TestDurationParsing(t *testing.T) {
	// Create config with durations
	tmpfile, err := os.CreateTemp("", "config-*.yaml")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(tmpfile.Name())

	configData := `
server:
  grpc_addr: ":50051"
  http_addr: ":8080"
  read_timeout: "1m"
  write_timeout: "2m"

database:
  path: "test.db"

rate_limit:
  cleanup_interval: "10m"
  max_idle_time: "20m"
`

	if _, err := tmpfile.Write([]byte(configData)); err != nil {
		t.Fatal(err)
	}
	tmpfile.Close()

	cfg, err := Load(tmpfile.Name())
	if err != nil {
		t.Fatalf("failed to load config: %v", err)
	}

	if cfg.Server.ReadTimeout != 1*time.Minute {
		t.Errorf("expected read timeout 1m, got %v", cfg.Server.ReadTimeout)
	}

	if cfg.Server.WriteTimeout != 2*time.Minute {
		t.Errorf("expected write timeout 2m, got %v", cfg.Server.WriteTimeout)
	}

	if cfg.RateLimit.CleanupInterval != 10*time.Minute {
		t.Errorf("expected cleanup interval 10m, got %v", cfg.RateLimit.CleanupInterval)
	}
}