|
|
@@ -76,7 +76,8 @@ func TestSetUserPerms_Allow(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err := logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: p1, Effect: "ALLOW"},
|
|
|
{PermId: p2, Effect: "ALLOW"},
|
|
|
@@ -112,7 +113,8 @@ func TestSetUserPerms_Deny(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err := logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: p1, Effect: "DENY"},
|
|
|
},
|
|
|
@@ -132,7 +134,8 @@ func TestSetUserPerms_UserNotFound(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err := logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: 999999999,
|
|
|
+ UserId: 999999999,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: 1, Effect: "ALLOW"},
|
|
|
},
|
|
|
@@ -166,7 +169,8 @@ func TestSetUserPerms_EmptyPerms_ClearsAll(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err := logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: p1, Effect: "ALLOW"},
|
|
|
},
|
|
|
@@ -174,8 +178,9 @@ func TestSetUserPerms_EmptyPerms_ClearsAll(t *testing.T) {
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
err = logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
- Perms: []types.UserPermItem{},
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
+ Perms: []types.UserPermItem{},
|
|
|
})
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
@@ -199,7 +204,8 @@ func TestSetUserPerms_InvalidEffect(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err := logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: 1, Effect: "INVALID"},
|
|
|
},
|
|
|
@@ -228,7 +234,8 @@ func TestSetUserPerms_PermNotExists(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err := logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: 999999999, Effect: "ALLOW"},
|
|
|
},
|
|
|
@@ -261,7 +268,8 @@ func TestSetUserPerms_PermBelongsToOtherProduct(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err := logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: otherPerm, Effect: "ALLOW"},
|
|
|
},
|
|
|
@@ -295,7 +303,8 @@ func TestSetUserPerms_ConflictingEffects(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err := logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: p1, Effect: "ALLOW"},
|
|
|
{PermId: p1, Effect: "DENY"},
|
|
|
@@ -330,7 +339,8 @@ func TestSetUserPerms_DuplicatePermDedup(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err := logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: p1, Effect: "ALLOW"},
|
|
|
{PermId: p1, Effect: "ALLOW"},
|
|
|
@@ -374,7 +384,8 @@ func TestSetUserPerms_DisabledPermRejected(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err = logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: disabledPermId, Effect: "ALLOW"},
|
|
|
},
|
|
|
@@ -399,8 +410,9 @@ func TestSetUserPerms_NonMemberRejected(t *testing.T) {
|
|
|
|
|
|
logic := NewSetUserPermsLogic(ctx, svcCtx)
|
|
|
err := logic.SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
- Perms: []types.UserPermItem{},
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
+ Perms: []types.UserPermItem{},
|
|
|
})
|
|
|
require.Error(t, err)
|
|
|
|
|
|
@@ -464,8 +476,9 @@ func TestSetUserPerms_L4_TOCTOU_CountMismatch_RollsBackWith409(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
err = NewSetUserPermsLogic(ctx, svcCtx).SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
- Perms: []types.UserPermItem{{PermId: disabledPermId, Effect: "ALLOW"}},
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
+ Perms: []types.UserPermItem{{PermId: disabledPermId, Effect: "ALLOW"}},
|
|
|
})
|
|
|
|
|
|
require.Error(t, err, "前置通过但 DB 实际 Disabled 时,事务末 COUNT 必须触发 409")
|
|
|
@@ -506,7 +519,8 @@ func TestSetUserPerms_L4_AllEnabled_CountPasses(t *testing.T) {
|
|
|
})
|
|
|
|
|
|
err := NewSetUserPermsLogic(ctx, svcCtx).SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
Perms: []types.UserPermItem{
|
|
|
{PermId: p1, Effect: "ALLOW"},
|
|
|
{PermId: p2, Effect: "DENY"},
|
|
|
@@ -770,8 +784,9 @@ func TestSetUserPerms_L_R13_2_DenyTypeFlipRollsBack(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
err := NewSetUserPermsLogic(ctx, svcCtx).SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
- Perms: []types.UserPermItem{{PermId: permId, Effect: consts.PermEffectDeny}},
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
+ Perms: []types.UserPermItem{{PermId: permId, Effect: consts.PermEffectDeny}},
|
|
|
})
|
|
|
require.Error(t, err, "事务内读到 ADMIN 必须拒绝写 DENY")
|
|
|
var ce *response.CodeError
|
|
|
@@ -816,8 +831,9 @@ func TestSetUserPerms_L_R13_2_AllowOnlySkipsShareLock(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
err := NewSetUserPermsLogic(ctx, svcCtx).SetUserPerms(&types.SetPermsReq{
|
|
|
- UserId: userId,
|
|
|
- Perms: []types.UserPermItem{{PermId: permId, Effect: consts.PermEffectAllow}},
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
+ Perms: []types.UserPermItem{{PermId: permId, Effect: consts.PermEffectAllow}},
|
|
|
})
|
|
|
require.NoError(t, err,
|
|
|
"纯 ALLOW 请求 hasDeny==false,必须短路、不调 FindOneForShareTx;"+
|
|
|
@@ -827,3 +843,49 @@ func TestSetUserPerms_L_R13_2_AllowOnlySkipsShareLock(t *testing.T) {
|
|
|
require.Len(t, rows, 1)
|
|
|
assert.Equal(t, "ALLOW", rows[0].Effect)
|
|
|
}
|
|
|
+
|
|
|
+// TC-1302: 超级管理员不传 ProductCode 时必须返回 400。
|
|
|
+func TestSetUserPerms_SuperAdmin_MissingProductCode(t *testing.T) {
|
|
|
+ ctx := ctxhelper.SuperAdminCtx()
|
|
|
+ svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
|
|
|
+
|
|
|
+ err := NewSetUserPermsLogic(ctx, svcCtx).SetUserPerms(&types.SetPermsReq{
|
|
|
+ UserId: 999999999,
|
|
|
+ Perms: []types.UserPermItem{},
|
|
|
+ })
|
|
|
+ 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-1303: 超级管理员传入 ProductCode 时正常工作。
|
|
|
+func TestSetUserPerms_SuperAdmin_WithProductCode(t *testing.T) {
|
|
|
+ ctx := ctxhelper.SuperAdminCtx()
|
|
|
+ svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
|
|
|
+ conn := testutil.GetTestSqlConn()
|
|
|
+
|
|
|
+ username := testutil.UniqueId()
|
|
|
+ userId := insertTestUser(t, ctx, username, testutil.HashPassword("pass"))
|
|
|
+ mId := insertTestMember(t, svcCtx, "test_product", userId)
|
|
|
+ permId := insertTestPerm(t, svcCtx, "test_product")
|
|
|
+
|
|
|
+ t.Cleanup(func() {
|
|
|
+ testutil.CleanTableByField(ctx, conn, "`sys_user_perm`", "userId", userId)
|
|
|
+ testutil.CleanTable(ctx, conn, "`sys_product_member`", mId)
|
|
|
+ testutil.CleanTable(ctx, conn, "`sys_user`", userId)
|
|
|
+ testutil.CleanTable(ctx, conn, "`sys_perm`", permId)
|
|
|
+ })
|
|
|
+
|
|
|
+ err := NewSetUserPermsLogic(ctx, svcCtx).SetUserPerms(&types.SetPermsReq{
|
|
|
+ UserId: userId,
|
|
|
+ ProductCode: "test_product",
|
|
|
+ Perms: []types.UserPermItem{{PermId: permId, Effect: consts.PermEffectAllow}},
|
|
|
+ })
|
|
|
+ require.NoError(t, err, "超级管理员传入 ProductCode 时应正常设置权限")
|
|
|
+
|
|
|
+ rows := findUserPerms(t, ctx, userId)
|
|
|
+ require.Len(t, rows, 1)
|
|
|
+ assert.Equal(t, consts.PermEffectAllow, rows[0].Effect)
|
|
|
+}
|