diff options
Diffstat (limited to 'internal/config/config_test.go')
| -rw-r--r-- | internal/config/config_test.go | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/internal/config/config_test.go b/internal/config/config_test.go index e1df1aa..5fa159e 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go | |||
| @@ -2,6 +2,7 @@ package config | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "os" | 4 | "os" |
| 5 | "strings" | ||
| 5 | "testing" | 6 | "testing" |
| 6 | "time" | 7 | "time" |
| 7 | ) | 8 | ) |
| @@ -240,6 +241,102 @@ func TestSaveAndLoad(t *testing.T) { | |||
| 240 | } | 241 | } |
| 241 | } | 242 | } |
| 242 | 243 | ||
| 244 | func TestNpubNormalization(t *testing.T) { | ||
| 245 | // Create a test key to get a valid npub | ||
| 246 | tmpfile, err := os.CreateTemp("", "config-*.yaml") | ||
| 247 | if err != nil { | ||
| 248 | t.Fatal(err) | ||
| 249 | } | ||
| 250 | defer os.Remove(tmpfile.Name()) | ||
| 251 | |||
| 252 | // Use a real npub for testing | ||
| 253 | configData := ` | ||
| 254 | server: | ||
| 255 | grpc_addr: ":50051" | ||
| 256 | http_addr: ":8080" | ||
| 257 | |||
| 258 | database: | ||
| 259 | path: "test.db" | ||
| 260 | |||
| 261 | auth: | ||
| 262 | enabled: true | ||
| 263 | allowed_npubs: | ||
| 264 | - npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6 | ||
| 265 | - npub1l2vyh47mk2p0qlsku7hg0vn29faehy9hy34ygaclpn66ukqp3afqutajft | ||
| 266 | ` | ||
| 267 | |||
| 268 | if _, err := tmpfile.Write([]byte(configData)); err != nil { | ||
| 269 | t.Fatal(err) | ||
| 270 | } | ||
| 271 | tmpfile.Close() | ||
| 272 | |||
| 273 | cfg, err := Load(tmpfile.Name()) | ||
| 274 | if err != nil { | ||
| 275 | t.Fatalf("failed to load config: %v", err) | ||
| 276 | } | ||
| 277 | |||
| 278 | // Verify npubs were normalized to hex | ||
| 279 | if len(cfg.Auth.AllowedNpubs) != 2 { | ||
| 280 | t.Errorf("expected 2 allowed npubs, got %d", len(cfg.Auth.AllowedNpubs)) | ||
| 281 | } | ||
| 282 | |||
| 283 | // Check that they're hex format (64 chars, not npub1...) | ||
| 284 | for i, pubkey := range cfg.Auth.AllowedNpubs { | ||
| 285 | if len(pubkey) != 64 { | ||
| 286 | t.Errorf("npub %d: expected 64 hex chars, got %d", i, len(pubkey)) | ||
| 287 | } | ||
| 288 | if pubkey[:5] == "npub1" { | ||
| 289 | t.Errorf("npub %d: should be normalized to hex, still in npub format", i) | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | // Verify the actual hex values | ||
| 294 | expectedHex1 := "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d" | ||
| 295 | expectedHex2 := "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52" | ||
| 296 | |||
| 297 | if cfg.Auth.AllowedNpubs[0] != expectedHex1 { | ||
| 298 | t.Errorf("npub 0: expected %s, got %s", expectedHex1, cfg.Auth.AllowedNpubs[0]) | ||
| 299 | } | ||
| 300 | if cfg.Auth.AllowedNpubs[1] != expectedHex2 { | ||
| 301 | t.Errorf("npub 1: expected %s, got %s", expectedHex2, cfg.Auth.AllowedNpubs[1]) | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | func TestNpubValidation(t *testing.T) { | ||
| 306 | tmpfile, err := os.CreateTemp("", "config-*.yaml") | ||
| 307 | if err != nil { | ||
| 308 | t.Fatal(err) | ||
| 309 | } | ||
| 310 | defer os.Remove(tmpfile.Name()) | ||
| 311 | |||
| 312 | // Invalid: hex format instead of npub | ||
| 313 | configData := ` | ||
| 314 | server: | ||
| 315 | grpc_addr: ":50051" | ||
| 316 | http_addr: ":8080" | ||
| 317 | |||
| 318 | database: | ||
| 319 | path: "test.db" | ||
| 320 | |||
| 321 | auth: | ||
| 322 | allowed_npubs: | ||
| 323 | - 3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d | ||
| 324 | ` | ||
| 325 | |||
| 326 | if _, err := tmpfile.Write([]byte(configData)); err != nil { | ||
| 327 | t.Fatal(err) | ||
| 328 | } | ||
| 329 | tmpfile.Close() | ||
| 330 | |||
| 331 | _, err = Load(tmpfile.Name()) | ||
| 332 | if err == nil { | ||
| 333 | t.Error("expected error for hex format in allowed_npubs, got nil") | ||
| 334 | } | ||
| 335 | if err != nil && !strings.Contains(err.Error(), "must start with 'npub1'") { | ||
| 336 | t.Errorf("expected 'must start with npub1' error, got: %v", err) | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 243 | func TestDurationParsing(t *testing.T) { | 340 | func TestDurationParsing(t *testing.T) { |
| 244 | // Create config with durations | 341 | // Create config with durations |
| 245 | tmpfile, err := os.CreateTemp("", "config-*.yaml") | 342 | tmpfile, err := os.CreateTemp("", "config-*.yaml") |
