package perm import ( "testing" "time" permModel "perms-system-server/internal/model/perm" "perms-system-server/internal/svc" "perms-system-server/internal/testutil" "perms-system-server/internal/testutil/ctxhelper" "perms-system-server/internal/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // TC-0113: 正常查询 func TestPermList_Normal(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc := testutil.UniqueId() var permIds []int64 for i := 0; i < 3; i++ { res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{ ProductCode: pc, Name: testutil.UniqueId(), Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) id, _ := res.LastInsertId() permIds = append(permIds, id) } t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", permIds...) }) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ ProductCode: pc, Page: 1, PageSize: 10, }) require.NoError(t, err) assert.Equal(t, int64(3), resp.Total) items := resp.List.([]types.PermItem) assert.Len(t, items, 3) assert.Equal(t, pc, items[0].ProductCode) } // TC-0114: 默认分页 func TestPermList_DefaultPagination(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc := testutil.UniqueId() res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{ ProductCode: pc, Name: testutil.UniqueId(), Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) id, _ := res.LastInsertId() t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", id) }) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ ProductCode: pc, }) require.NoError(t, err) assert.Equal(t, int64(1), resp.Total) } // TC-0115: pageSize超过上限 func TestPermList_PageSizeExceedsLimit(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc := testutil.UniqueId() res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{ ProductCode: pc, Name: testutil.UniqueId(), Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) id, _ := res.LastInsertId() t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", id) }) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ ProductCode: pc, Page: 1, PageSize: 200, }) require.NoError(t, err) assert.Equal(t, int64(1), resp.Total) } // TC-0116: 不存在的productCode func TestPermList_NonExistentProductCode(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ ProductCode: "nonexistent_" + testutil.UniqueId(), Page: 1, PageSize: 10, }) require.NoError(t, err) assert.Equal(t, int64(0), resp.Total) } // TC-1311: 非超管不传productCode时从JWT获取 func TestPermList_NonSuperAdminUsesJWTProductCode(t *testing.T) { svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc := testutil.UniqueId() res, err := svcCtx.SysPermModel.Insert(ctxhelper.SuperAdminCtx(), &permModel.SysPerm{ ProductCode: pc, Name: testutil.UniqueId(), Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) id, _ := res.LastInsertId() t.Cleanup(func() { testutil.CleanTable(ctxhelper.SuperAdminCtx(), conn, "`sys_perm`", id) }) ctx := ctxhelper.AdminCtx(pc) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ Page: 1, PageSize: 10, }) require.NoError(t, err) assert.Equal(t, int64(1), resp.Total) } // TC-1312: 非超管传了其他产品code时被忽略 func TestPermList_NonSuperAdminIgnoresReqProductCode(t *testing.T) { svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc := testutil.UniqueId() res, err := svcCtx.SysPermModel.Insert(ctxhelper.SuperAdminCtx(), &permModel.SysPerm{ ProductCode: pc, Name: testutil.UniqueId(), Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) id, _ := res.LastInsertId() t.Cleanup(func() { testutil.CleanTable(ctxhelper.SuperAdminCtx(), conn, "`sys_perm`", id) }) ctx := ctxhelper.AdminCtx(pc) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ ProductCode: "other_product", Page: 1, PageSize: 10, }) require.NoError(t, err) assert.Equal(t, int64(1), resp.Total) } // TC-1313: 超管不传productCode时返回全量数据 func TestPermList_SuperAdminNoProductCodeReturnsAll(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc1 := testutil.UniqueId() pc2 := testutil.UniqueId() r1, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{ ProductCode: pc1, Name: testutil.UniqueId(), Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) id1, _ := r1.LastInsertId() r2, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{ ProductCode: pc2, Name: testutil.UniqueId(), Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) id2, _ := r2.LastInsertId() t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", id1, id2) }) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ Page: 1, PageSize: 10000, }) require.NoError(t, err) assert.GreaterOrEqual(t, resp.Total, int64(2)) }