chore: 并入 iotlcm-admin-svr 历史到 admin-svr/(原提交 49818c6)
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package system_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/do"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility"
|
||||
)
|
||||
|
||||
func TestSysUser_UpdateUserPassword(t *testing.T) {
|
||||
database, err := gdb.New(gdb.ConfigNode{Type: "sqlite", Link: "sqlite::@file(:memory:)"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
t.Cleanup(func() { _ = database.Close(ctx) })
|
||||
if _, err := database.Exec(ctx, "CREATE TABLE sys_user (user_id INTEGER PRIMARY KEY, tenant_id TEXT, password TEXT, salt TEXT, updated_by INTEGER, created_at DATETIME, updated_at DATETIME, deleted_at DATETIME)"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, userID := range []int64{900001, 900002} {
|
||||
if _, err := database.Model(dao.SysUser.Table()).Ctx(ctx).Data(do.SysUser{
|
||||
UserId: userID, TenantId: "test-only", Password: "original", Salt: "original",
|
||||
}).Insert(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
password := utility.PasswordEncrypt(utility.Md5("isolated-test-password"), "isolated-test-salt")
|
||||
if _, err := database.Model(dao.SysUser.Table()).Ctx(ctx).
|
||||
Where("tenant_id", "test-only").Where("user_id", 900001).
|
||||
Data(do.SysUser{Password: password, Salt: "isolated-test-salt", UpdatedBy: 900001}).Update(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var updated struct {
|
||||
Password string
|
||||
Salt string
|
||||
}
|
||||
if err := database.Model(dao.SysUser.Table()).Ctx(ctx).Where("user_id", 900001).Scan(&updated); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if updated.Password != password || updated.Salt != "isolated-test-salt" {
|
||||
t.Fatal("password and salt were not updated in the isolated database")
|
||||
}
|
||||
untouched, err := database.Model(dao.SysUser.Table()).Ctx(ctx).Where("user_id", 900002).Value("password")
|
||||
if err != nil || untouched.String() != "original" {
|
||||
t.Fatalf("another test user was modified: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user