| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- package pub
- import (
- "context"
- "errors"
- "fmt"
- "testing"
- "time"
- permModel "perms-system-server/internal/model/perm"
- productModel "perms-system-server/internal/model/product"
- "perms-system-server/internal/response"
- "perms-system-server/internal/testutil"
- "perms-system-server/internal/types"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "golang.org/x/crypto/bcrypt"
- )
- func insertSyncTestProduct(t *testing.T, ctx context.Context, code, appKey, appSecret string, status int64) (int64, func()) {
- t.Helper()
- svcCtx := newTestSvcCtx()
- conn := testutil.GetTestSqlConn()
- now := time.Now().Unix()
- hashedSecret, err := bcrypt.GenerateFromPassword([]byte(appSecret), bcrypt.MinCost)
- require.NoError(t, err)
- res, err := svcCtx.SysProductModel.Insert(ctx, &productModel.SysProduct{
- Code: code,
- Name: code,
- AppKey: appKey,
- AppSecret: string(hashedSecret),
- Status: status,
- CreateTime: now,
- UpdateTime: now,
- })
- require.NoError(t, err)
- id, _ := res.LastInsertId()
- cleanup := func() {
- testutil.CleanTable(ctx, conn, "`sys_product`", id)
- }
- return id, cleanup
- }
- // TC-0032: 全部新增
- func TestSyncPerms_AllNew(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- conn := testutil.GetTestSqlConn()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 1)
- t.Cleanup(cleanProduct)
- t.Cleanup(func() { testutil.CleanTableByField(ctx, conn, "`sys_perm`", "productCode", pc) })
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: appSecret,
- Perms: []types.SyncPermItem{
- {Code: "perm_a", Name: "Perm A", Remark: "remark a"},
- {Code: "perm_b", Name: "Perm B"},
- {Code: "perm_c", Name: "Perm C"},
- },
- })
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.Equal(t, int64(3), resp.Added)
- assert.Equal(t, int64(0), resp.Updated)
- assert.Equal(t, int64(0), resp.Disabled)
- }
- // TC-0033: 更新已有(名称变更)
- func TestSyncPerms_UpdateExisting(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- conn := testutil.GetTestSqlConn()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- now := time.Now().Unix()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 1)
- t.Cleanup(cleanProduct)
- permRes, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
- ProductCode: pc, Name: "Old Name", Code: "upd_code", Remark: "old remark", Status: 1, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- permId, _ := permRes.LastInsertId()
- t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", permId) })
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: appSecret,
- Perms: []types.SyncPermItem{
- {Code: "upd_code", Name: "New Name", Remark: "new remark"},
- },
- })
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.Equal(t, int64(0), resp.Added)
- assert.Equal(t, int64(1), resp.Updated)
- assert.Equal(t, int64(0), resp.Disabled)
- updated, err := svcCtx.SysPermModel.FindOne(ctx, permId)
- require.NoError(t, err)
- assert.Equal(t, "New Name", updated.Name)
- assert.Equal(t, "new remark", updated.Remark)
- assert.Equal(t, int64(1), updated.Status)
- }
- // TC-0034: 无变化
- func TestSyncPerms_NoChanges(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- conn := testutil.GetTestSqlConn()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- now := time.Now().Unix()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 1)
- t.Cleanup(cleanProduct)
- permRes, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
- ProductCode: pc, Name: "Same Name", Code: "same_code", Remark: "same remark", Status: 1, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- permId, _ := permRes.LastInsertId()
- t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", permId) })
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: appSecret,
- Perms: []types.SyncPermItem{
- {Code: "same_code", Name: "Same Name", Remark: "same remark"},
- },
- })
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.Equal(t, int64(0), resp.Added)
- assert.Equal(t, int64(0), resp.Updated)
- assert.Equal(t, int64(0), resp.Disabled)
- }
- // TC-0035: 禁用权限重启
- func TestSyncPerms_ReEnableDisabled(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- conn := testutil.GetTestSqlConn()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- now := time.Now().Unix()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 1)
- t.Cleanup(cleanProduct)
- permRes, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
- ProductCode: pc, Name: "Disabled Perm", Code: "dis_code", Remark: "", Status: 2, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- permId, _ := permRes.LastInsertId()
- t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", permId) })
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: appSecret,
- Perms: []types.SyncPermItem{
- {Code: "dis_code", Name: "Disabled Perm"},
- },
- })
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.Equal(t, int64(0), resp.Added)
- assert.Equal(t, int64(1), resp.Updated)
- reEnabled, err := svcCtx.SysPermModel.FindOne(ctx, permId)
- require.NoError(t, err)
- assert.Equal(t, int64(1), reEnabled.Status)
- }
- // TC-0036: 移除不在列表的权限
- func TestSyncPerms_DisableNotInList(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- conn := testutil.GetTestSqlConn()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- now := time.Now().Unix()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 1)
- t.Cleanup(cleanProduct)
- keepCode := testutil.UniqueId()
- removeCode := testutil.UniqueId()
- keepRes, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
- ProductCode: pc, Name: "Keep", Code: keepCode, Status: 1, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- keepId, _ := keepRes.LastInsertId()
- removeRes, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
- ProductCode: pc, Name: "Remove", Code: removeCode, Status: 1, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- removeId, _ := removeRes.LastInsertId()
- t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", keepId, removeId) })
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: appSecret,
- Perms: []types.SyncPermItem{
- {Code: keepCode, Name: "Keep"},
- },
- })
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.Equal(t, int64(0), resp.Added)
- assert.Equal(t, int64(1), resp.Disabled)
- disabled, err := svcCtx.SysPermModel.FindOne(ctx, removeId)
- require.NoError(t, err)
- assert.Equal(t, int64(2), disabled.Status)
- kept, err := svcCtx.SysPermModel.FindOne(ctx, keepId)
- require.NoError(t, err)
- assert.Equal(t, int64(1), kept.Status)
- }
- // TC-0037: 空perms数组
- func TestSyncPerms_EmptyPermsDisablesAll(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- conn := testutil.GetTestSqlConn()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- now := time.Now().Unix()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 1)
- t.Cleanup(cleanProduct)
- p1Res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
- ProductCode: pc, Name: "P1", Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- p1Id, _ := p1Res.LastInsertId()
- p2Res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
- ProductCode: pc, Name: "P2", Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- p2Id, _ := p2Res.LastInsertId()
- t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", p1Id, p2Id) })
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: appSecret,
- Perms: []types.SyncPermItem{},
- })
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.Equal(t, int64(0), resp.Added)
- assert.Equal(t, int64(0), resp.Updated)
- assert.Equal(t, int64(2), resp.Disabled)
- d1, err := svcCtx.SysPermModel.FindOne(ctx, p1Id)
- require.NoError(t, err)
- assert.Equal(t, int64(2), d1.Status)
- d2, err := svcCtx.SysPermModel.FindOne(ctx, p2Id)
- require.NoError(t, err)
- assert.Equal(t, int64(2), d2.Status)
- }
- // TC-0039: appKey无效
- func TestSyncPerms_InvalidAppKey(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: "nonexistent_key_" + testutil.UniqueId(),
- AppSecret: "whatever",
- Perms: []types.SyncPermItem{{Code: "x", Name: "x"}},
- })
- require.Nil(t, resp)
- require.Error(t, err)
- var codeErr *response.CodeError
- require.True(t, errors.As(err, &codeErr))
- assert.Equal(t, 401, codeErr.Code())
- assert.Equal(t, "无效的appKey", codeErr.Error())
- }
- // TC-0040: appSecret错误
- func TestSyncPerms_WrongAppSecret(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 1)
- t.Cleanup(cleanProduct)
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: "wrong_secret",
- Perms: []types.SyncPermItem{{Code: "x", Name: "x"}},
- })
- require.Nil(t, resp)
- require.Error(t, err)
- var codeErr *response.CodeError
- require.True(t, errors.As(err, &codeErr))
- assert.Equal(t, 401, codeErr.Code())
- assert.Equal(t, "appSecret验证失败", codeErr.Error())
- }
- // TC-0041: 产品已禁用
- func TestSyncPerms_ProductDisabled(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 2)
- t.Cleanup(cleanProduct)
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: appSecret,
- Perms: []types.SyncPermItem{{Code: "x", Name: "x"}},
- })
- require.Nil(t, resp)
- require.Error(t, err)
- var codeErr *response.CodeError
- require.True(t, errors.As(err, &codeErr))
- assert.Equal(t, 403, codeErr.Code())
- assert.Equal(t, "产品已被禁用", codeErr.Error())
- }
- // TC-0042: 大批量(1000条)
- func TestSyncPerms_LargeBatch1000(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- conn := testutil.GetTestSqlConn()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 1)
- t.Cleanup(cleanProduct)
- t.Cleanup(func() { testutil.CleanTableByField(ctx, conn, "`sys_perm`", "productCode", pc) })
- perms := make([]types.SyncPermItem, 1000)
- for i := 0; i < 1000; i++ {
- perms[i] = types.SyncPermItem{
- Code: fmt.Sprintf("batch_%s_%d", pc, i),
- Name: fmt.Sprintf("Perm_%d", i),
- }
- }
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: appSecret,
- Perms: perms,
- })
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.Equal(t, int64(1000), resp.Added)
- assert.Equal(t, int64(0), resp.Updated)
- assert.Equal(t, int64(0), resp.Disabled)
- }
- // TC-0043: 重复code去重
- func TestSyncPerms_DeduplicateCodes(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- conn := testutil.GetTestSqlConn()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 1)
- t.Cleanup(cleanProduct)
- t.Cleanup(func() { testutil.CleanTableByField(ctx, conn, "`sys_perm`", "productCode", pc) })
- logic := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: appSecret,
- Perms: []types.SyncPermItem{
- {Code: "dup_code", Name: "Perm First"},
- {Code: "dup_code", Name: "Perm Duplicate"},
- {Code: "unique_code", Name: "Unique"},
- },
- })
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.Equal(t, int64(2), resp.Added, "重复code应被去重,只添加2条")
- }
- // TC-0038: 验证disabled返回值
- func TestSyncPerms_VerifyDisabledCount(t *testing.T) {
- ctx := context.Background()
- svcCtx := newTestSvcCtx()
- conn := testutil.GetTestSqlConn()
- pc := testutil.UniqueId()
- appKey := testutil.UniqueId()
- appSecret := testutil.UniqueId()
- now := time.Now().Unix()
- _, cleanProduct := insertSyncTestProduct(t, ctx, pc, appKey, appSecret, 1)
- t.Cleanup(cleanProduct)
- var permIds []int64
- for i := 0; i < 5; i++ {
- res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
- ProductCode: pc, Name: fmt.Sprintf("p%d", i), Code: fmt.Sprintf("code_%s_%d", pc, i),
- 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 := NewSyncPermsLogic(ctx, svcCtx)
- resp, err := logic.SyncPerms(&types.SyncPermsReq{
- AppKey: appKey,
- AppSecret: appSecret,
- Perms: []types.SyncPermItem{
- {Code: fmt.Sprintf("code_%s_0", pc), Name: "p0"},
- {Code: fmt.Sprintf("code_%s_1", pc), Name: "p1"},
- },
- })
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.Equal(t, int64(0), resp.Added)
- assert.Equal(t, int64(3), resp.Disabled)
- }
|