|
|
@@ -2,6 +2,7 @@ package auth
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "database/sql"
|
|
|
"fmt"
|
|
|
"math/rand"
|
|
|
"testing"
|
|
|
@@ -12,6 +13,7 @@ import (
|
|
|
"perms-system-server/internal/model/productmember"
|
|
|
"perms-system-server/internal/model/role"
|
|
|
"perms-system-server/internal/model/roleperm"
|
|
|
+ userModel "perms-system-server/internal/model/user"
|
|
|
"perms-system-server/internal/model/userperm"
|
|
|
"perms-system-server/internal/model/userrole"
|
|
|
"perms-system-server/internal/svc"
|
|
|
@@ -27,7 +29,29 @@ func newTestSvcCtx() *svc.ServiceContext {
|
|
|
return svc.NewServiceContext(c)
|
|
|
}
|
|
|
|
|
|
-// TC-0231: isSuperAdmin=true, deptId=0, FindAllCodesByProductCode返回["a","b"]
|
|
|
+func createPermsTestUser(t *testing.T, ctx context.Context, svcCtx *svc.ServiceContext, isSuperAdmin int64, deptId int64) (int64, func()) {
|
|
|
+ t.Helper()
|
|
|
+ conn := testutil.GetTestSqlConn()
|
|
|
+ now := time.Now().Unix()
|
|
|
+ username := fmt.Sprintf("perms_u_%d", rand.Intn(1000000))
|
|
|
+ res, err := svcCtx.SysUserModel.Insert(ctx, &userModel.SysUser{
|
|
|
+ Username: username,
|
|
|
+ Password: testutil.HashPassword("pass123"),
|
|
|
+ Nickname: username,
|
|
|
+ Avatar: sql.NullString{},
|
|
|
+ DeptId: deptId,
|
|
|
+ IsSuperAdmin: isSuperAdmin,
|
|
|
+ MustChangePassword: 2,
|
|
|
+ Status: 1,
|
|
|
+ CreateTime: now,
|
|
|
+ UpdateTime: now,
|
|
|
+ })
|
|
|
+ require.NoError(t, err)
|
|
|
+ id, _ := res.LastInsertId()
|
|
|
+ return id, func() { testutil.CleanTable(ctx, conn, "`sys_user`", id) }
|
|
|
+}
|
|
|
+
|
|
|
+// TC-0231: superAdmin gets all enabled perms
|
|
|
func TestGetUserPerms_SuperAdmin(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
@@ -35,6 +59,9 @@ func TestGetUserPerms_SuperAdmin(t *testing.T) {
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_%d", rand.Intn(100000))
|
|
|
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 1, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
+
|
|
|
p1, err := svcCtx.SysPermModel.Insert(ctx, &perm.SysPerm{
|
|
|
ProductCode: pc, Name: "sa_perm1", Code: "sa_code1", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
})
|
|
|
@@ -57,42 +84,50 @@ func TestGetUserPerms_SuperAdmin(t *testing.T) {
|
|
|
testutil.CleanTable(ctx, conn, "`sys_perm`", p1Id, p2Id, p3Id)
|
|
|
})
|
|
|
|
|
|
- perms, memberType, err := GetUserPerms(ctx, svcCtx, 1, 0, pc, true)
|
|
|
+ perms, memberType, err := GetUserPerms(ctx, svcCtx, userId, 0, pc, true)
|
|
|
require.NoError(t, err)
|
|
|
assert.Equal(t, "SUPER_ADMIN", memberType)
|
|
|
assert.ElementsMatch(t, []string{"sa_code1", "sa_code2"}, perms)
|
|
|
}
|
|
|
|
|
|
-// TC-0232: isSuperAdmin=true, deptId=0, FindAllCodesByProductCode返回err
|
|
|
+// TC-0232: superAdmin with empty product
|
|
|
func TestGetUserPerms_SuperAdmin_EmptyProduct(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
|
|
|
- perms, memberType, err := GetUserPerms(ctx, svcCtx, 1, 0, "nonexist_product_xyz", true)
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 1, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
+
|
|
|
+ perms, memberType, err := GetUserPerms(ctx, svcCtx, userId, 0, "nonexist_product_xyz", true)
|
|
|
require.NoError(t, err)
|
|
|
assert.Equal(t, "SUPER_ADMIN", memberType)
|
|
|
assert.Empty(t, perms)
|
|
|
}
|
|
|
|
|
|
-// TC-0233: deptId=0, FindOneByProductCodeUserId返回ErrNotFound
|
|
|
+// TC-0233: non product member
|
|
|
func TestGetUserPerms_NotProductMember(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
|
|
|
- perms, memberType, err := GetUserPerms(ctx, svcCtx, 999999, 0, "some_product", false)
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
+
|
|
|
+ perms, memberType, err := GetUserPerms(ctx, svcCtx, userId, 0, "some_product", false)
|
|
|
require.NoError(t, err)
|
|
|
assert.Empty(t, memberType)
|
|
|
assert.Nil(t, perms)
|
|
|
}
|
|
|
|
|
|
-// TC-0235: deptId=0, member.MemberType="DEVELOPER"
|
|
|
+// TC-0235: DEVELOPER member
|
|
|
func TestGetUserPerms_Developer(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_dev_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "DEVELOPER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -117,14 +152,16 @@ func TestGetUserPerms_Developer(t *testing.T) {
|
|
|
assert.Contains(t, perms, "dev_c1")
|
|
|
}
|
|
|
|
|
|
-// TC-0236: deptId=0, member.MemberType="ADMIN"
|
|
|
+// TC-0236: ADMIN member
|
|
|
func TestGetUserPerms_Admin(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_adm_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "ADMIN", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -149,14 +186,16 @@ func TestGetUserPerms_Admin(t *testing.T) {
|
|
|
assert.Contains(t, perms, "adm_c1")
|
|
|
}
|
|
|
|
|
|
-// TC-0243: deptId=0, MemberType="MEMBER", roleIds=[], allowPermIds=[], denyPermIds=[]
|
|
|
+// TC-0243: MEMBER no roles no user perms
|
|
|
func TestGetUserPerms_Member_NoRolesNoUserPerms(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_mbr0_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -174,14 +213,16 @@ func TestGetUserPerms_Member_NoRolesNoUserPerms(t *testing.T) {
|
|
|
assert.Empty(t, perms)
|
|
|
}
|
|
|
|
|
|
-// TC-0244: deptId=0, roleIds=[1], role.ProductCode=productCode+Status=1, rolePermIds=[10,20]
|
|
|
+// TC-0244: MEMBER with roles
|
|
|
func TestGetUserPerms_Member_WithRoles(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_mbrr_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -243,14 +284,16 @@ func TestGetUserPerms_Member_WithRoles(t *testing.T) {
|
|
|
assert.ElementsMatch(t, []string{p1.Code, p2.Code}, perms)
|
|
|
}
|
|
|
|
|
|
-// TC-0248: deptId=0, rolePermIds=[10], denyPermIds=[10]
|
|
|
+// TC-0248: DENY overrides role perm
|
|
|
func TestGetUserPerms_Member_DENYOverridesRolePerm(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_deny_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -317,14 +360,16 @@ func TestGetUserPerms_Member_DENYOverridesRolePerm(t *testing.T) {
|
|
|
assert.Equal(t, []string{permB.Code}, permsResult)
|
|
|
}
|
|
|
|
|
|
-// TC-0247: deptId=0, rolePermIds=[], allowPermIds=[30]
|
|
|
+// TC-0247: ALLOW adds extra perm
|
|
|
func TestGetUserPerms_Member_ALLOWAddsExtra(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_allow_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -359,7 +404,7 @@ func TestGetUserPerms_Member_ALLOWAddsExtra(t *testing.T) {
|
|
|
assert.Contains(t, permsResult, permObj.Code)
|
|
|
}
|
|
|
|
|
|
-// TC-0245: deptId=0, roleIds=[1,2], role1.ProductCode=target, role2.ProductCode=other
|
|
|
+// TC-0245: cross-product role filter
|
|
|
func TestGetUserPerms_Member_CrossProductRoleFilter(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
@@ -367,7 +412,9 @@ func TestGetUserPerms_Member_CrossProductRoleFilter(t *testing.T) {
|
|
|
now := time.Now().Unix()
|
|
|
pcTarget := fmt.Sprintf("tp_cross_t_%d", rand.Intn(100000))
|
|
|
pcOther := fmt.Sprintf("tp_cross_o_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pcTarget, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -431,14 +478,16 @@ func TestGetUserPerms_Member_CrossProductRoleFilter(t *testing.T) {
|
|
|
assert.Equal(t, []string{targetPerm.Code}, permsResult)
|
|
|
}
|
|
|
|
|
|
-// TC-0246: deptId=0, role.Status=2
|
|
|
+// TC-0246: disabled role filtered
|
|
|
func TestGetUserPerms_Member_DisabledRoleFiltered(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_disrole_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -480,14 +529,16 @@ func TestGetUserPerms_Member_DisabledRoleFiltered(t *testing.T) {
|
|
|
assert.Empty(t, permsResult)
|
|
|
}
|
|
|
|
|
|
-// TC-0251: deptId=0, finalIds含已禁用权限
|
|
|
+// TC-0251: disabled perm filtered
|
|
|
func TestGetUserPerms_Member_DisabledPermFiltered(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_disperm_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -519,14 +570,16 @@ func TestGetUserPerms_Member_DisabledPermFiltered(t *testing.T) {
|
|
|
assert.Empty(t, permsResult)
|
|
|
}
|
|
|
|
|
|
-// TC-0249: deptId=0, allowPermIds=[10], denyPermIds=[10]
|
|
|
+// TC-0249: DENY only excludes target perm
|
|
|
func TestGetUserPerms_Member_DENYOnlyExcludesTargetPerm(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_denyonly_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -573,14 +626,16 @@ func TestGetUserPerms_Member_DENYOnlyExcludesTargetPerm(t *testing.T) {
|
|
|
assert.NotContains(t, permsResult, permB.Code, "DENY perm should be excluded even if it exists")
|
|
|
}
|
|
|
|
|
|
-// TC-0250: deptId=0, rolePermIds=[10], allowPermIds=[10]
|
|
|
+// TC-0250: ALLOW + role dedup
|
|
|
func TestGetUserPerms_Member_ALLOWAndRoleDedup(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_dedup_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
+
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, 0)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
@@ -636,14 +691,13 @@ func TestGetUserPerms_Member_ALLOWAndRoleDedup(t *testing.T) {
|
|
|
assert.Equal(t, permObj.Code, permsResult[0])
|
|
|
}
|
|
|
|
|
|
-// TC-0238: deptId>0, MemberType="MEMBER", SysDeptModel.FindOne返回DeptType="DEV", FindAllCodesByProductCode返回["a","b","c"]
|
|
|
+// TC-0238: DEV dept member gets all perms
|
|
|
func TestGetUserPerms_Member_DevDept_AllPerms(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_devdept_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
|
|
|
deptRes, err := svcCtx.SysDeptModel.Insert(ctx, &deptModel.SysDept{
|
|
|
ParentId: 0, Name: "dev_dept_" + fmt.Sprintf("%d", rand.Intn(100000)),
|
|
|
@@ -652,6 +706,9 @@ func TestGetUserPerms_Member_DevDept_AllPerms(t *testing.T) {
|
|
|
require.NoError(t, err)
|
|
|
deptId, _ := deptRes.LastInsertId()
|
|
|
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, deptId)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
+
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
})
|
|
|
@@ -685,14 +742,13 @@ func TestGetUserPerms_Member_DevDept_AllPerms(t *testing.T) {
|
|
|
assert.ElementsMatch(t, []string{p1.Code, p2.Code}, permsResult)
|
|
|
}
|
|
|
|
|
|
-// TC-0240: deptId>0, DeptType="NORMAL"
|
|
|
+// TC-0240: NORMAL dept member no auto perms
|
|
|
func TestGetUserPerms_Member_NormalDept_NoAutoPerms(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
svcCtx := newTestSvcCtx()
|
|
|
conn := testutil.GetTestSqlConn()
|
|
|
now := time.Now().Unix()
|
|
|
pc := fmt.Sprintf("tp_normdept_%d", rand.Intn(100000))
|
|
|
- userId := int64(900000 + rand.Intn(10000))
|
|
|
|
|
|
deptRes, err := svcCtx.SysDeptModel.Insert(ctx, &deptModel.SysDept{
|
|
|
ParentId: 0, Name: "normal_dept_" + fmt.Sprintf("%d", rand.Intn(100000)),
|
|
|
@@ -701,6 +757,9 @@ func TestGetUserPerms_Member_NormalDept_NoAutoPerms(t *testing.T) {
|
|
|
require.NoError(t, err)
|
|
|
deptId, _ := deptRes.LastInsertId()
|
|
|
|
|
|
+ userId, cleanUser := createPermsTestUser(t, ctx, svcCtx, 2, deptId)
|
|
|
+ t.Cleanup(cleanUser)
|
|
|
+
|
|
|
pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
|
|
|
ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
|
|
|
})
|