From 071e7cc78d3f13fa782dbc6ca5fcec3a37263a4d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 16 Feb 2026 20:05:39 +0900 Subject: test(backend): add unit tests for auth, config, ratelimit, game, and api Cover previously untested logic: session ID generation/hashing, password authentication, IP rate limiting, game state helpers, handler endpoints, task enqueue/result processing, and config loading. Co-Authored-By: Claude Opus 4.6 --- backend/auth/session_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 backend/auth/session_test.go (limited to 'backend/auth/session_test.go') diff --git a/backend/auth/session_test.go b/backend/auth/session_test.go new file mode 100644 index 0000000..2a47739 --- /dev/null +++ b/backend/auth/session_test.go @@ -0,0 +1,47 @@ +package auth + +import ( + "testing" +) + +func TestGenerateSessionID(t *testing.T) { + id, err := GenerateSessionID() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + // 32 bytes → 64 hex characters + if len(id) != 64 { + t.Errorf("expected session ID length 64, got %d", len(id)) + } +} + +func TestGenerateSessionID_Unique(t *testing.T) { + id1, err := GenerateSessionID() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + id2, err := GenerateSessionID() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if id1 == id2 { + t.Error("expected unique session IDs, got identical values") + } +} + +func TestHashSessionID(t *testing.T) { + raw := "abc123" + hashed := HashSessionID(raw) + // SHA-256 produces 32 bytes → 64 hex characters + if len(hashed) != 64 { + t.Errorf("expected hash length 64, got %d", len(hashed)) + } + // Same input should produce same hash + if hashed != HashSessionID(raw) { + t.Error("expected deterministic hash") + } + // Different input should produce different hash + if hashed == HashSessionID("different") { + t.Error("expected different hashes for different inputs") + } +} -- cgit v1.3.1