| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package perm_test
- import (
- "context"
- "testing"
- "time"
- "perms-system-server/internal/model/perm"
- "perms-system-server/internal/testutil"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "github.com/zeromicro/go-zero/core/stores/sqlx"
- )
- // ---------------------------------------------------------------------------
- // 覆盖目标:审计 M-6 修复的基础设施 —— FindMapByProductCodeWithTx。L-5 审计把非事务版
- // FindMapByProductCode 作为僵尸接口移除,这里改为只钉死 WithTx 版本的三条契约:
- // 1) 必须在 TransactCtx 内跑通,并返回当前产品下"全量"权限(含 status!=1 的行),
- // 因为 SyncPermsService 依赖读出现状对比(禁用重启分支);
- // 2) 返回 map 的 key 必须严格等于 SysPerm.Code,便于上层直接按 code 去重;
- // 3) 当 productCode 下没有任何行时,返回 empty map 而不是 nil(避免上层 `m[code] = x`
- // 时的 nil map 赋值 panic)。
- // ---------------------------------------------------------------------------
- // TC-0807: FindMapByProductCodeWithTx 返回覆盖全状态行 + key 等于 Code
- func TestSysPermModel_FindMapByProductCodeWithTx_EqualsNonTx(t *testing.T) {
- ctx := context.Background()
- m := newTestSysPermModel(t)
- conn := testutil.GetTestSqlConn()
- productCode := "pc_fmwtx_" + testutil.UniqueId()
- now := time.Now().Unix()
- codeA := "a_" + testutil.UniqueId()
- codeB := "b_" + testutil.UniqueId()
- res1, err := m.Insert(ctx, &perm.SysPerm{
- ProductCode: productCode, Name: "a", Code: codeA,
- Status: 1, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- id1, _ := res1.LastInsertId()
- res2, err := m.Insert(ctx, &perm.SysPerm{
- ProductCode: productCode, Name: "b", Code: codeB,
- Status: 2, CreateTime: now, UpdateTime: now, // 禁用行也必须被 Map 出来
- })
- require.NoError(t, err)
- id2, _ := res2.LastInsertId()
- t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", id1, id2) })
- var withTx map[string]*perm.SysPerm
- require.NoError(t, m.TransactCtx(ctx, func(c context.Context, session sqlx.Session) error {
- var err error
- withTx, err = m.FindMapByProductCodeWithTx(c, session, productCode)
- return err
- }))
- require.Len(t, withTx, 2)
- pA, ok := withTx[codeA]
- require.True(t, ok, "map key 必须等于 SysPerm.Code")
- assert.Equal(t, id1, pA.Id)
- assert.Equal(t, int64(1), pA.Status)
- assert.Equal(t, "a", pA.Name)
- pB, ok := withTx[codeB]
- require.True(t, ok, "禁用行同样必须出现在 map 中(M-6 依赖它识别 disabled 重启)")
- assert.Equal(t, id2, pB.Id)
- assert.Equal(t, int64(2), pB.Status, "status 必须真实透传,不得被过滤")
- }
- // TC-0808: 空 productCode 下 FindMapByProductCodeWithTx 返回非 nil 的空 map。
- // 上层同步逻辑里会对 map 直接做 `_, ok := existingMap[item.Code]`;如果是 nil 依然安全,
- // 但若不慎写成 `existingMap[item.Code] = ...` 就会炸,因此约定为"空 map"更稳。
- func TestSysPermModel_FindMapByProductCodeWithTx_EmptyIsNonNil(t *testing.T) {
- ctx := context.Background()
- m := newTestSysPermModel(t)
- productCode := "pc_empty_" + testutil.UniqueId()
- var withTx map[string]*perm.SysPerm
- require.NoError(t, m.TransactCtx(ctx, func(c context.Context, session sqlx.Session) error {
- var err error
- withTx, err = m.FindMapByProductCodeWithTx(c, session, productCode)
- return err
- }))
- require.NotNil(t, withTx, "空集必须是 empty map,而不是 nil(避免上层误用 map 赋值时 panic)")
- assert.Empty(t, withTx)
- }
|