1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package token
- import (
- "fmt"
- "testing"
- "time"
- "github.com/google/uuid"
- )
- func TestToken(t *testing.T) {
- secret := uuid.New().String()
- // create maker
- issuer := "daisy"
- user := "user1"
- maker, err := NewJWTMaker(secret)
- if err != nil {
- t.Errorf("creating token maker error %s", err.Error())
- }
- token, err := maker.CreateToken(issuer, user, time.Second*10)
- if err != nil {
- t.Errorf("creating token error %s", err.Error())
- }
- // create verifier
- verifier, err := NewJWTMaker(secret)
- if err != nil {
- t.Errorf("creating token maker verifier error %s", err.Error())
- }
- customClaim, err := verifier.VerifyToken(token)
- if err != nil {
- t.Errorf("verify token error %s", err.Error())
- }
- if customClaim.Issuer != issuer {
- t.Errorf("issuer mismatch ")
- }
- if customClaim.UserName != user {
- t.Errorf("user mismatch ")
- }
- }
- func TestTokenExpiration(t *testing.T) {
- secret := uuid.New().String()
- // create maker
- issuer := "daisy"
- user := "user1"
- maker, err := NewJWTMaker(secret)
- if err != nil {
- t.Errorf("creating token maker error %s", err.Error())
- }
- token, err := maker.CreateToken(issuer, user, time.Second*2)
- if err != nil {
- t.Errorf("creating token error %s", err.Error())
- }
- time.Sleep(time.Second * 3)
- // create verifier
- verifier, err := NewJWTMaker(secret)
- if err != nil {
- t.Errorf("creating token maker verifier error %s", err.Error())
- }
- _, err = verifier.VerifyToken(token)
- if err == nil {
- t.Errorf("verify token should return error")
- }
- }
- func TestSecretKeyGenerator(t *testing.T) {
- key, err := GenerateSecretKey()
- fmt.Println("key:", key)
- // t.Logf("key:%s\n", key)
- if err != nil {
- t.Errorf("generate secret key error %s", err.Error())
- }
- if len(key) != 120 {
- t.Errorf("secret key length, expected %d, got %d", 120, len(key))
- }
- }
|