fusenapi/utils/auth/user_test.go

103 lines
2.1 KiB
Go
Raw Normal View History

2023-06-14 10:17:45 +00:00
package auth
import (
2023-07-27 02:18:49 +00:00
"crypto/sha256"
"encoding/base64"
"fmt"
2023-06-28 11:32:41 +00:00
"log"
2023-06-14 10:17:45 +00:00
"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)
}
2023-06-19 10:27:31 +00:00
2023-06-28 11:32:41 +00:00
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) {
2023-07-27 02:18:49 +00:00
a := sha256.New()
a.Write([]byte("fusen_backend_3021"))
base64.URLEncoding.EncodeToString(a.Sum(nil))
as := fmt.Sprintf("%x", a.Sum(nil))
log.Println(as, len(as), base64.URLEncoding.EncodeToString(a.Sum(nil)))
// b := sha256.New().Sum([]byte("fusen_backend_2022"))
// bs := fmt.Sprintf("%x", b)
// log.Println(bs)
// log.Println(as == bs)
2023-07-03 06:34:16 +00:00
// log.Println(basic.CBCEncrypt("sdfsdfsadsasdasadas", "1232132131232132"))
// log.Println(basic.CBCDecrypt("8pq8ud33FfFZN2jUmBaNFfYbOeyrK1C/xVEtnbV2HfeG9KCGZfEcF6qrRwOHzWvT", "1232132131232132"))
2023-06-28 11:32:41 +00:00
}
2023-06-19 10:27:31 +00:00
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")
}
}