Codes are stored as SHA-256 hashes and consumed with one conditional update, so concurrent use of the same code succeeds at most once. Refs #40 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package auth
|
|
|
|
import (
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewRecoveryCodes(t *testing.T) {
|
|
codes := NewRecoveryCodes()
|
|
if len(codes) != RecoveryCodeCount {
|
|
t.Fatalf("len(NewRecoveryCodes()) = %d, want %d", len(codes), RecoveryCodeCount)
|
|
}
|
|
format := regexp.MustCompile(`^[a-z2-9]{5}-[a-z2-9]{5}$`)
|
|
seen := map[string]bool{}
|
|
for _, c := range codes {
|
|
if !format.MatchString(c) {
|
|
t.Errorf("code %q does not match %s", c, format)
|
|
}
|
|
if NormalizeRecoveryCode(c) == "" {
|
|
t.Errorf("NormalizeRecoveryCode(%q) = \"\", want the code", c)
|
|
}
|
|
if seen[c] {
|
|
t.Errorf("duplicate code %q", c)
|
|
}
|
|
seen[c] = true
|
|
}
|
|
}
|
|
|
|
func TestNormalizeRecoveryCode(t *testing.T) {
|
|
tests := []struct{ in, want string }{
|
|
{"abcde-fghjk", "abcdefghjk"},
|
|
{" ABCDE FGHJK ", "abcdefghjk"},
|
|
{"abcdefghjk", "abcdefghjk"},
|
|
{"123456", ""},
|
|
{"abcde-fghj0", ""},
|
|
{"abcde-fghjkm", ""},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := NormalizeRecoveryCode(tt.in); got != tt.want {
|
|
t.Errorf("NormalizeRecoveryCode(%q) = %q, want %q", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
}
|