53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGenerateToken(t *testing.T) {
|
|
const n = 1000
|
|
tokens := make([]string, 0, n)
|
|
for i := 0; i < n; i++ {
|
|
tok, err := GenerateToken()
|
|
if err != nil {
|
|
t.Fatalf("GenerateToken() returned error: %v", err)
|
|
}
|
|
tokens = append(tokens, tok)
|
|
}
|
|
|
|
t.Run("all tokens are non-empty", func(t *testing.T) {
|
|
for i, tok := range tokens {
|
|
if tok == "" {
|
|
t.Fatalf("token at index %d is empty", i)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("all tokens are distinct", func(t *testing.T) {
|
|
seen := make(map[string]struct{}, len(tokens))
|
|
for i, tok := range tokens {
|
|
if _, ok := seen[tok]; ok {
|
|
t.Fatalf("duplicate token found at index %d: %s", i, tok)
|
|
}
|
|
seen[tok] = struct{}{}
|
|
}
|
|
})
|
|
|
|
t.Run("no token contains whitespace", func(t *testing.T) {
|
|
for i, tok := range tokens {
|
|
if strings.ContainsAny(tok, " \t\n\r\v\f") {
|
|
t.Fatalf("token at index %d contains whitespace: %q", i, tok)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("every token has length at least 32 characters", func(t *testing.T) {
|
|
for i, tok := range tokens {
|
|
if len(tok) < 32 {
|
|
t.Fatalf("token at index %d has length %d, want >= 32", i, len(tok))
|
|
}
|
|
}
|
|
})
|
|
}
|