73 lines
3.6 KiB
Go
73 lines
3.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/contexts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/handler"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
|
|
|
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
type paginationDeptService struct{ service.ISysDept }
|
|
|
|
func (paginationDeptService) Model(ctx context.Context, _ ...*handler.Option) *gdb.Model {
|
|
return g.DB().Model("sys_dept").Ctx(ctx).Handler(handler.FilterTenant)
|
|
}
|
|
|
|
type paginationUserService struct{ service.ISysUser }
|
|
|
|
func (paginationUserService) Model(ctx context.Context, _ ...*handler.Option) *gdb.Model {
|
|
return g.DB().Model("sys_user").Ctx(ctx).Handler(handler.FilterTenant)
|
|
}
|
|
|
|
func TestEnterpriseCustomerDatabasePaginationAndDefaultUsers(t *testing.T) {
|
|
setupAgentRoleSyncTestDB(t)
|
|
ctx := contexts.Set(context.Background(), &model.Context{User: &model.Identity{BaseClaims: model.BaseClaims{TenantId: "000000", DeptId: 1}}})
|
|
service.RegisterSysDept(paginationDeptService{})
|
|
service.RegisterSysUser(paginationUserService{})
|
|
t.Cleanup(func() {
|
|
service.RegisterSysDept(nil)
|
|
service.RegisterSysUser(nil)
|
|
})
|
|
statements := []string{
|
|
"ALTER TABLE sys_dept ADD COLUMN dept_name TEXT",
|
|
"CREATE TABLE sys_user (user_id INTEGER PRIMARY KEY, tenant_id TEXT, dept_id INTEGER, user_name TEXT, nick_name TEXT, deleted_at DATETIME)",
|
|
"INSERT INTO sys_dept (dept_id,tenant_id,parent_id,dept_type,dept_role,dept_name) VALUES (2,'000000',1,1,12,'企业A'),(3,'000000',1,1,13,'企业B'),(4,'000000',1,1,14,'企业C'),(5,'000000',2,1,15,'孙级企业'),(6,'other',1,1,16,'其他租户'),(7,'000000',1,0,17,'内部部门'),(8,'000000',1,1,18,'已删除企业'),(9,'000000',1,1,0,'无账号企业')",
|
|
"UPDATE sys_dept SET deleted_at=CURRENT_TIMESTAMP WHERE dept_id=8",
|
|
"INSERT INTO sys_user VALUES (20,'000000',2,'fallback-a','回退A',NULL),(21,'000000',2,'admin-a','管理员A',NULL),(30,'000000',3,'deleted-admin','已删除',CURRENT_TIMESTAMP),(31,'000000',3,'fallback-b','回退B',NULL),(40,'000000',4,'fallback-c','回退C',NULL),(50,'other',4,'other-user','跨租户',NULL)",
|
|
"INSERT INTO sys_user_role (tenant_id,user_id,role_id) VALUES ('000000',21,12),('000000',30,13),('other',50,14)",
|
|
}
|
|
for _, statement := range statements {
|
|
mustExecAgentRoleSyncTest(t, ctx, statement)
|
|
}
|
|
subject := NewAgentDept()
|
|
params := &model.AgentDeptListParam{Page: 1, PageSize: 2}
|
|
list, total, err := subject.DeptList(ctx, params)
|
|
if err != nil || total != 4 || len(list) != 2 {
|
|
t.Fatalf("page 1: total=%d list=%+v err=%v", total, list, err)
|
|
}
|
|
if list[0].DeptId != 2 || list[0].UserName != "admin-a" || list[1].DeptId != 3 || list[1].UserName != "fallback-b" {
|
|
t.Fatalf("unexpected account selection: %+v %+v", list[0], list[1])
|
|
}
|
|
params.Page = 2
|
|
list, total, err = subject.DeptList(ctx, params)
|
|
if err != nil || total != 4 || len(list) != 2 || list[0].DeptId != 4 || list[0].UserName != "fallback-c" || list[1].DeptId != 9 || list[1].UserName != "" {
|
|
t.Fatalf("page 2: total=%d list=%+v err=%v", total, list, err)
|
|
}
|
|
params.Page = 3
|
|
list, total, err = subject.DeptList(ctx, params)
|
|
if err != nil || total != 4 || len(list) != 0 || list == nil {
|
|
t.Fatalf("out of range: total=%d list=%+v err=%v", total, list, err)
|
|
}
|
|
list, total, err = subject.DeptList(ctx, &model.AgentDeptListParam{DeptName: "企业A", Status: "0"})
|
|
if err != nil || total != 1 || len(list) != 1 || list[0].DeptId != 2 {
|
|
t.Fatalf("filtered list: total=%d list=%+v err=%v", total, list, err)
|
|
}
|
|
}
|