fusenapi/utils/auth/user_test.go
2023-06-19 18:27:31 +08:00

57 lines
1.0 KiB
Go

package auth
import (
"testing"
"time"
)
// TestGenJwt 测试jwt序列化
func TestGenJwt(t *testing.T) {
now := time.Now().Unix()
secret := "fusen123"
a, err := GenerateJwtToken(&secret, 3600, now, 123, 1234)
if err != nil {
t.Error(err)
}
// log.Println(a)
claims, err := getJwtClaims(a, &secret)
if err != nil {
t.Error(err)
}
userinfo, err := GetUserInfoFormMapClaims(claims)
if err != nil {
t.Error(err)
}
if userinfo.UserId != 123 || userinfo.GuestId != 1234 {
t.Error(userinfo)
}
// log.Println(claims)
}
func TestCheckValueRange(t *testing.T) {
v := 1
rv1 := []int{1, 3, 4}
if !CheckValueRange[int](v, rv1...) {
t.Error("1 in [1 3 4], but is false")
}
v = 2
if CheckValueRange(v, rv1...) {
t.Error("2 not in [1 3 4], but is true")
}
vstr := "funsen123"
rvstr := []string{"funsun123", "funsen123", "funsen23"}
if !CheckValueRange(vstr, rvstr...) {
t.Error("vstr in rvstr, but is false")
}
vstr = "funsen1234"
if CheckValueRange(vstr, rvstr...) {
t.Error("vstr not in rvstr, but is true")
}
}