| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package user
- import (
- "errors"
- "testing"
- "time"
- productModel "perms-system-server/internal/model/product"
- "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-0734: M-14 修复:产品被禁用时,setUserPerms 应拒绝
- func TestSetUserPerms_ProductDisabled(t *testing.T) {
- ctx := ctxhelper.SuperAdminCtx()
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- conn := testutil.GetTestSqlConn()
- now := time.Now().Unix()
- code := testutil.UniqueId()
- pRes, err := svcCtx.SysProductModel.Insert(ctx, &productModel.SysProduct{
- Code: code, Name: "p_" + code, AppKey: code + "_k", AppSecret: "s",
- Status: 2, CreateTime: now, UpdateTime: now, // 禁用
- })
- require.NoError(t, err)
- pId, _ := pRes.LastInsertId()
- username := testutil.UniqueId()
- userId := insertTestUser(t, ctx, username, testutil.HashPassword("pw"))
- mId := insertTestMember(t, svcCtx, code, userId)
- t.Cleanup(func() {
- testutil.CleanTable(ctx, conn, "`sys_product_member`", mId)
- testutil.CleanTable(ctx, conn, "`sys_user`", userId)
- testutil.CleanTable(ctx, conn, "`sys_product`", pId)
- })
- adminCtx := ctxhelper.AdminCtx(code)
- err = NewSetUserPermsLogic(adminCtx, svcCtx).SetUserPerms(&types.SetPermsReq{
- UserId: userId, Perms: nil,
- })
- require.Error(t, err)
- var ce *response.CodeError
- require.True(t, errors.As(err, &ce))
- assert.Equal(t, 400, ce.Code())
- assert.Contains(t, ce.Error(), "禁用")
- }
- // TC-0735: M-14 修复:产品不存在时拒绝
- func TestSetUserPerms_ProductNotFound(t *testing.T) {
- ctx := ctxhelper.SuperAdminCtx()
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- username := testutil.UniqueId()
- userId := insertTestUser(t, ctx, username, testutil.HashPassword("pw"))
- conn := testutil.GetTestSqlConn()
- t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_user`", userId) })
- // 用一个不存在的 productCode
- adminCtx := ctxhelper.AdminCtx("non_existent_" + testutil.UniqueId())
- err := NewSetUserPermsLogic(adminCtx, svcCtx).SetUserPerms(&types.SetPermsReq{
- UserId: userId, Perms: nil,
- })
- require.Error(t, err)
- var ce *response.CodeError
- require.True(t, errors.As(err, &ce))
- assert.Equal(t, 404, ce.Code())
- assert.Contains(t, ce.Error(), "产品不存在")
- }
|