Files
scrabble-game/backend/internal/session/token_test.go
T
Ilia Denisov eeaad62b10
Tests · Go / test (push) Successful in 11s
Tests · Integration / integration (push) Successful in 8s
Stage 1: backend foundation (Postgres, sessions, accounts, OTel)
- internal/postgres: pgx-over-database/sql pool (otelsql), embedded goose
  migrations into schema 'backend', committed go-jet code + cmd/jetgen tool.
- internal/account: durable accounts + unified telegram/email identities
  (UUIDv7 keys), find-or-create provisioning with unique-conflict handling.
- internal/session: opaque 256-bit tokens stored as a SHA-256 hash, revoke-only
  (no TTL); write-through cache gating /readyz; store + service.
- internal/telemetry: OTel tracer/meter providers (none/stdout) + request-timing
  middleware; internal/config gains Postgres + OTel env loading.
- internal/server: /api/v1 {public,user,internal,admin} skeleton + X-User-ID
  middleware; /readyz checks DB ping + cache; main wires
  telemetry -> db+migrate -> warm cache -> server.
- Tests: unit + integration (build tag 'integration', testcontainers
  postgres:17) for migrations, accounts, sessions, readyz; new integration.yaml.
- Docs: ARCHITECTURE, TESTING, PLAN refinements, root + backend READMEs.

Session/account REST handlers deferred to Stage 6 (gateway); OTLP + dashboards
to Stage 11.
2026-06-02 13:52:26 +02:00

45 lines
1.2 KiB
Go

package session
import "testing"
// TestGenerateTokenUniqueAndHashed checks that tokens are unique, the stored
// value is the hash (not the plaintext), and the hash is a 64-char SHA-256 hex.
func TestGenerateTokenUniqueAndHashed(t *testing.T) {
tok1, hash1, err := GenerateToken()
if err != nil {
t.Fatalf("GenerateToken: %v", err)
}
tok2, hash2, err := GenerateToken()
if err != nil {
t.Fatalf("GenerateToken: %v", err)
}
if tok1 == tok2 {
t.Error("tokens must be unique")
}
if hash1 == hash2 {
t.Error("hashes must differ for distinct tokens")
}
if hash1 != HashToken(tok1) {
t.Error("stored hash must equal HashToken(token)")
}
if tok1 == hash1 {
t.Error("stored hash must not equal the plaintext token")
}
if len(hash1) != 64 {
t.Errorf("hash length = %d, want 64 (sha256 hex)", len(hash1))
}
}
// TestHashTokenDeterministic checks that hashing is stable for a given token.
func TestHashTokenDeterministic(t *testing.T) {
first := HashToken("alpha")
second := HashToken("alpha")
if first != second {
t.Error("HashToken must be deterministic")
}
if first == HashToken("beta") {
t.Error("distinct tokens must hash differently")
}
}