chore: 并入 iotlcm-app 历史到 app/(原提交 f40bda9)
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
// package system_test
|
||||
// @Link https://github.com/xiujiecn/xiu-admin
|
||||
// @Copyright Copyright (c) 2025 LiXiujie
|
||||
// @Author Lxj <li@example.com>
|
||||
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
||||
package system_test
|
||||
|
||||
import (
|
||||
"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"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/utility"
|
||||
|
||||
_ "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"
|
||||
)
|
||||
|
||||
func TestSysUser_UpdateUserPassword(t *testing.T) {
|
||||
// 设置上下文用户身份为用户
|
||||
ctx := contexts.Set(gctx.New(), &model.Context{
|
||||
// 为了测试只设置了hook中需要用到的数据
|
||||
User: &model.Identity{
|
||||
BaseClaims: model.BaseClaims{
|
||||
ID: 1,
|
||||
TenantId: "000000",
|
||||
DeptId: 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
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)
|
||||
data := do.SysUser{
|
||||
Password: password,
|
||||
Salt: salt,
|
||||
UpdatedBy: 1,
|
||||
}
|
||||
_, err := dao.SysUser.Ctx(ctx).Where(dao.SysUser.Columns().UserId, 1).Data(data).OmitEmpty().Update()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
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