simpleauth/pkg/token/token_test.go

37 lines
721 B
Go
Raw Normal View History

2023-02-18 16:39:18 -07:00
package token
import (
"testing"
"time"
)
func TestToken(t *testing.T) {
secret := []byte("bloop")
username := "rodney"
token := New(secret, username, time.Now().Add(10*time.Second))
if token.Username != username {
t.Error("Wrong username")
}
if !token.Valid(secret) {
t.Error("Not valid")
}
tokenStr := token.String()
if nt, err := ParseString(tokenStr); err != nil {
t.Error("ParseString", err)
} else if nt.Username != token.Username {
t.Error("Decoded username wrong")
}
}
2023-02-18 21:01:36 -07:00
func TestExpired(t *testing.T) {
secret := []byte("bloop")
username := "rodney"
token := New(secret, username, time.Now().Add(-10*time.Second))
2023-02-20 17:38:51 -07:00
if token.Valid(secret) {
t.Error("Expired token still valid")
}
2023-02-18 21:01:36 -07:00
}