sync: 对齐小程序 v1.0.2 生产代码并脱敏
This commit is contained in:
@@ -1,37 +1,28 @@
|
||||
// package system_test
|
||||
// @Link https://github.com/xiujiecn/xiu-admin
|
||||
// @Copyright Copyright (c) 2025 LiXiujie
|
||||
// @Author Lxj <li@xiujie.cn>
|
||||
// @Author Lxj <li@example.com>
|
||||
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
||||
package system_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/consts"
|
||||
"testing"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/library/contexts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/do"
|
||||
"testing"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/utility"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 数据库只读,不执行
|
||||
// g.DB().SetDryRun(true)
|
||||
g.DB().SetDebug(true)
|
||||
testing.Init()
|
||||
}
|
||||
|
||||
func TestSysUser_UpdateUserPassword(t *testing.T) {
|
||||
// 设置上下文用户身份为用户
|
||||
ctx := context.WithValue(gctx.New(), consts.ContextKey, &model.Context{
|
||||
ctx := contexts.Set(gctx.New(), &model.Context{
|
||||
// 为了测试只设置了hook中需要用到的数据
|
||||
User: &model.Identity{
|
||||
BaseClaims: model.BaseClaims{
|
||||
@@ -41,6 +32,33 @@ func TestSysUser_UpdateUserPassword(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
previous := gdb.GetConfig("default")
|
||||
if err := gdb.SetConfigGroup("default", gdb.ConfigGroup{{Type: "sqlite", Link: "sqlite::@file(:memory:)", MaxOpenConnCount: 1}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
database := g.DB()
|
||||
t.Cleanup(func() {
|
||||
if err := database.GetCore().ClearCacheAll(ctx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := database.Close(ctx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := gdb.SetConfigGroup("default", previous); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
if err := database.GetCore().ClearCacheAll(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, statement := range []string{
|
||||
"CREATE TABLE sys_user (user_id INTEGER PRIMARY KEY, password TEXT, salt TEXT, updated_by INTEGER, updated_at DATETIME, tenant_id TEXT)",
|
||||
"INSERT INTO sys_user VALUES (1,'old','old',0,NULL,'000000'),(2,'unchanged','unchanged',0,NULL,'000000')",
|
||||
} {
|
||||
if _, err := database.Exec(ctx, statement); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
newPassword := "123456"
|
||||
salt := "123456"
|
||||
password := utility.PasswordEncrypt(utility.Md5(newPassword), salt)
|
||||
@@ -48,11 +66,17 @@ func TestSysUser_UpdateUserPassword(t *testing.T) {
|
||||
Password: password,
|
||||
Salt: salt,
|
||||
UpdatedBy: 1,
|
||||
UpdatedAt: gtime.Now(),
|
||||
}
|
||||
_, err := dao.SysUser.Ctx(ctx).Where(dao.SysUser.Columns().UserId, 1).Data(data).OmitEmpty().Update()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Println("UpdateUserPassword success")
|
||||
actual, err := database.GetValue(ctx, "SELECT password FROM sys_user WHERE user_id = 1")
|
||||
if err != nil || actual.String() != password {
|
||||
t.Fatalf("password=%v, error=%v", actual, err)
|
||||
}
|
||||
unchanged, err := database.GetValue(ctx, "SELECT password FROM sys_user WHERE user_id = 2")
|
||||
if err != nil || unchanged.String() != "unchanged" {
|
||||
t.Fatalf("unrelated user changed: password=%v, error=%v", unchanged, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user