add auth lib
This commit is contained in:
38
token/token_test.go
Normal file
38
token/token_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package token
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenerateURLSafe(t *testing.T) {
|
||||
t.Run("invalid size", func(t *testing.T) {
|
||||
if _, err := GenerateURLSafe(0); err == nil {
|
||||
t.Fatal("expected error for non-positive size")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("valid token decodes from raw url base64", func(t *testing.T) {
|
||||
got, err := GenerateURLSafe(32)
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateURLSafe: %v", err)
|
||||
}
|
||||
if got == "" {
|
||||
t.Fatal("expected non-empty token")
|
||||
}
|
||||
raw, err := base64.RawURLEncoding.DecodeString(got)
|
||||
if err != nil {
|
||||
t.Fatalf("token was not raw-url-base64: %v", err)
|
||||
}
|
||||
if len(raw) != 32 {
|
||||
t.Fatalf("expected 32 random bytes, got %d", len(raw))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestHashSHA256(t *testing.T) {
|
||||
const expected = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
|
||||
if got := HashSHA256("hello"); got != expected {
|
||||
t.Fatalf("expected %q, got %q", expected, got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user