65 lines
2.9 KiB
Go
65 lines
2.9 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"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"
|
|
)
|
|
|
|
type templateRoleService struct{ service.ISysRole }
|
|
|
|
func (templateRoleService) ModelQuery(ctx context.Context, _ ...*handler.Option) *gdb.Model {
|
|
return g.DB().Model("sys_role").Ctx(ctx).Handler(handler.FilterTenant)
|
|
}
|
|
|
|
func TestConfiguredTemplateUsesInheritedDefaultAtEveryLevel(t *testing.T) {
|
|
setupAgentRoleSyncTestDB(t)
|
|
service.RegisterSysDept(paginationDeptService{})
|
|
service.RegisterSysRole(templateRoleService{})
|
|
t.Cleanup(func() {
|
|
service.RegisterSysDept(nil)
|
|
service.RegisterSysRole(nil)
|
|
})
|
|
ctx := contexts.Set(context.Background(), &model.Context{User: &model.Identity{BaseClaims: model.BaseClaims{ID: 99, TenantId: "000000", DeptId: 2}}})
|
|
for _, statement := range []string{
|
|
"ALTER TABLE sys_dept ADD COLUMN dept_name TEXT",
|
|
"INSERT INTO sys_dept(dept_id,tenant_id,parent_id,dept_type,dept_role) VALUES(1,'000000',0,1,0),(2,'000000',1,1,21),(3,'000000',2,1,31)",
|
|
"INSERT INTO sys_role(role_id,tenant_id,dept_id,status,is_pass,created_dept,data_scope) VALUES(10,'000000',1,'0',0,1,'4'),(21,'000000',2,'0',0,1,'4'),(31,'000000',3,'0',0,2,'4')",
|
|
"INSERT INTO sys_config(tenant_id,config_key,config_value) VALUES('000000','agent.role.template.role_id','10')",
|
|
"INSERT INTO sys_role_menu(tenant_id,role_id,menu_id,data_scope) VALUES('000000',10,100,'0'),('000000',10,200,'0'),('000000',21,100,'0'),('000000',21,300,'0'),('000000',31,100,'0')",
|
|
} {
|
|
mustExecAgentRoleSyncTest(t, ctx, statement)
|
|
}
|
|
for _, deptID := range []int64{1, 2, 3} {
|
|
source, menus, err := NewAgentDept().passRoleByDept(ctx, deptID)
|
|
if err != nil || source == nil {
|
|
t.Fatalf("source for department %d: %+v %v", deptID, source, err)
|
|
}
|
|
expected := map[int64][]int64{1: {100, 200}, 2: {100, 300}, 3: {100}}[deptID]
|
|
got := make([]int64, 0, len(menus))
|
|
for _, menu := range menus {
|
|
got = append(got, menu.MenuId)
|
|
}
|
|
slices.Sort(got)
|
|
if !slices.Equal(got, expected) {
|
|
t.Fatalf("department %d must copy its current default role including platform expansion: %v, want %v", deptID, got, expected)
|
|
}
|
|
}
|
|
mustExecAgentRoleSyncTest(t, ctx, "UPDATE sys_dept SET dept_role=0 WHERE dept_id=2")
|
|
if _, _, err := NewAgentDept().passRoleByDept(ctx, 2); err == nil {
|
|
t.Fatal("missing default role must not fall back to full platform permissions")
|
|
}
|
|
if err := NewAgentDept().EnsureCurrentDeptDefaultRole(ctx); err == nil {
|
|
t.Fatal("read request must report missing default role")
|
|
}
|
|
assertAgentRoleSyncCount(t, ctx, "SELECT COUNT(*) FROM sys_role", 3)
|
|
assertAgentRoleSyncCount(t, ctx, "SELECT COUNT(*) FROM sys_user_role", 0)
|
|
}
|