package role import ( "errors" "testing" "time" roleModel "perms-system-server/internal/model/role" "perms-system-server/internal/response" "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-0087: 正常更新 func TestUpdateRole_Normal(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc := testutil.UniqueId() res, err := svcCtx.SysRoleModel.Insert(ctx, &roleModel.SysRole{ ProductCode: pc, Name: testutil.UniqueId(), Status: 1, PermsLevel: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) roleId, _ := res.LastInsertId() t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_role`", roleId) }) newName := testutil.UniqueId() logic := NewUpdateRoleLogic(ctx, svcCtx) err = logic.UpdateRole(&types.UpdateRoleReq{ Id: roleId, Name: newName, Remark: "updated remark", PermsLevel: 2, Status: 2, }) require.NoError(t, err) updated, err := svcCtx.SysRoleModel.FindOne(ctx, roleId) require.NoError(t, err) assert.Equal(t, newName, updated.Name) assert.Equal(t, "updated remark", updated.Remark) assert.Equal(t, int64(2), updated.PermsLevel) assert.Equal(t, int64(2), updated.Status) } // TC-0088: 不存在 func TestUpdateRole_NotFound(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) logic := NewUpdateRoleLogic(ctx, svcCtx) err := logic.UpdateRole(&types.UpdateRoleReq{ Id: 999999999, Name: "whatever", PermsLevel: 1, }) require.Error(t, err) var ce *response.CodeError require.True(t, errors.As(err, &ce)) assert.Equal(t, 404, ce.Code()) assert.Equal(t, "角色不存在", ce.Error()) } // TC-0488: updateRole非管理员拒绝 func TestUpdateRole_MemberRejected(t *testing.T) { ctx := ctxhelper.MemberCtx("test_product") svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) logic := NewUpdateRoleLogic(ctx, svcCtx) err := logic.UpdateRole(&types.UpdateRoleReq{Id: 1, Name: "test", PermsLevel: 1}) require.Error(t, err) var ce *response.CodeError require.True(t, errors.As(err, &ce)) assert.Equal(t, 403, ce.Code()) }