fusenapi/utils/auth/user_test.go
2023-06-28 19:32:41 +08:00

87 lines
1.7 KiB
Go

package auth
import (
"crypto/sha256"
"encoding/base64"
"log"
"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 TestGenBackendJwt(t *testing.T) {
now := time.Now().Unix()
secret := "fusen_backend_2023"
a, err := GenerateBackendJwtToken(&secret, 3600*24*7, now, 1, 1)
if err != nil {
t.Error(err)
}
log.Println(a)
claims, err := getJwtClaims(a, &secret)
if err != nil {
t.Error(err)
}
userinfo, err := GetBackendUserInfoFormMapClaims(claims)
if err != nil {
t.Error(err)
}
if userinfo.UserId != 1 || userinfo.DepartmentId != 1 {
t.Error(userinfo)
}
// log.Println(claims)
}
func TestCase1(t *testing.T) {
log.Println(base64.URLEncoding.EncodeToString(sha256.New().Sum([]byte("fusen_backend_2023"))))
}
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")
}
}