rano/pkg/user/token_test.go

44 lines
864 B
Go

package user
import (
"errors"
"testing"
"time"
)
func FuzzNewVerifyEmailToken(f *testing.F) {
f.Add(int64(123))
f.Fuzz(func(t *testing.T, userID int64) {
_, err := newTokenToVerifyEmail(userID, time.Millisecond*100)
if err != nil {
t.Errorf("failed for input %d, %v", userID, err)
}
})
}
func TestEmailToken(t *testing.T) {
uID := int64(1234)
// create a token
t1, err := newTokenToVerifyEmail(uID, time.Millisecond*100)
if err != nil {
t.Error(err)
return
}
// let decode token
id, err := tokenToVerifyEmail(t1)
if err != nil {
t.Error(err)
return
} else if uID != id {
t.Error("uid and decoded id are not same, ", uID, "!=", id)
}
// lets wait and try decode again
time.Sleep(time.Millisecond * 100)
_, err = tokenToVerifyEmail(t1)
if !errors.Is(err, ErrExpiredToken) {
t.Error("expected expired token error")
}
}