test: report failures as got/want and use subtests for tables

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 00:18:02 +01:00
1 parent d20225982a
commit b6843330a8
8 files changed
+184 -175

No files matched your search

+16 -19
View File
@@ -2,42 +2,39 @@ package auth
import (
"context"
"os"
"testing"
"time"
"github.com/google/uuid"
"github.com/pquerna/otp/totp"
"github.com/redis/go-redis/v9"
)
func TestTOTPRejectsReplay(t *testing.T) {
url := os.Getenv("UPTIME_TEST_REDIS_URL")
if url == "" {
t.Skip("UPTIME_TEST_REDIS_URL not set")
}
opts, _ := redis.ParseURL(url)
rdb := redis.NewClient(opts)
defer rdb.Close()
func TestTOTPVerifierRejectsReplay(t *testing.T) {
key, err := NewTOTPKey("a@example.test")
if err != nil {
t.Fatal(err)
t.Fatalf("NewTOTPKey() failed: %v", err)
}
now := time.Now()
code, _ := totp.GenerateCode(key.Secret(), now)
v := NewTOTPVerifier(rdb)
code, err := totp.GenerateCode(key.Secret(), now)
if err != nil {
t.Fatalf("GenerateCode() failed: %v", err)
}
v := NewTOTPVerifier(newTestRedis(t))
v.now = func() time.Time { return now }
user := uuid.New()
ctx := context.Background()
if ok, err := v.Verify(ctx, user, key.Secret(), code); !ok || err != nil {
t.Fatalf("first use rejected: %v", err)
t.Errorf("Verify(fresh code) = %t, %v; want true, nil", ok, err)
}
if ok, _ := v.Verify(ctx, user, key.Secret(), code); ok {
t.Fatal("replayed code accepted")
if ok, err := v.Verify(ctx, user, key.Secret(), code); ok || err != nil {
t.Errorf("Verify(replayed code) = %t, %v; want false, nil", ok, err)
}
if ok, _ := v.Verify(ctx, user, key.Secret(), "000000"); ok {
t.Fatal("wrong code accepted")
wrong := "000000"
if wrong == code {
wrong = "000001"
}
if ok, err := v.Verify(ctx, user, key.Secret(), wrong); ok || err != nil {
t.Errorf("Verify(wrong code) = %t, %v; want false, nil", ok, err)
}
}