优化结构
This commit is contained in:
@@ -3,9 +3,9 @@ package system
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"kra/internal/biz/system"
|
||||
"testing"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -44,8 +44,8 @@ func TestCopyAuthorityStrictPolicyValidationMatchesAdministrationContract(t *tes
|
||||
}
|
||||
|
||||
repo := &authorityAccessRepo{data: data}
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{AuthorityID: rootID})
|
||||
created := &biz.Authority{AuthorityID: 910, AuthorityName: "copy", ParentID: &rootID}
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{AuthorityID: rootID})
|
||||
created := &system.Authority{AuthorityID: 910, AuthorityName: "copy", ParentID: &rootID}
|
||||
if err := repo.CopyAuthority(ctx, 900, created); err != nil {
|
||||
t.Fatalf("root copy of a registered API failed: %v", err)
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func TestCopyAuthorityStrictPolicyValidationMatchesAdministrationContract(t *tes
|
||||
t.Fatalf("copied policy exists = %v, err = %v", exists, err)
|
||||
}
|
||||
|
||||
staleCopy := &biz.Authority{AuthorityID: 911, AuthorityName: "stale-copy", ParentID: &rootID}
|
||||
staleCopy := &system.Authority{AuthorityID: 911, AuthorityName: "stale-copy", ParentID: &rootID}
|
||||
if err := repo.CopyAuthority(ctx, 901, staleCopy); err == nil || err.Error() != "存在api不在权限列表中" {
|
||||
t.Fatalf("stale API copy error = %v", err)
|
||||
}
|
||||
@@ -78,8 +78,8 @@ func TestCopyAuthorityDuplicateIDWinsOverStrictParentValidation(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
invalidParent := uint(999999)
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{AuthorityID: rootID})
|
||||
err := (&authorityAccessRepo{data: data}).CopyAuthority(ctx, 0, &biz.Authority{AuthorityID: 920, AuthorityName: "duplicate", ParentID: &invalidParent})
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{AuthorityID: rootID})
|
||||
err := (&authorityAccessRepo{data: data}).CopyAuthority(ctx, 0, &system.Authority{AuthorityID: 920, AuthorityName: "duplicate", ParentID: &invalidParent})
|
||||
if err == nil || err.Error() != "存在相同角色id" {
|
||||
t.Fatalf("duplicate copy error = %v", err)
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func TestCopyAuthorityDuplicateIDWinsOverStrictParentValidation(t *testing.T) {
|
||||
|
||||
func TestListAuthoritiesRequiresCurrentAuthorityOutsideStrictMode(t *testing.T) {
|
||||
data := newTransactionTestData(t)
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{AuthorityID: 999999})
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{AuthorityID: 999999})
|
||||
if _, err := (&authorityAccessRepo{data: data}).ListAuthorities(ctx); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("missing current authority error = %v", err)
|
||||
}
|
||||
@@ -198,7 +198,7 @@ func TestSetAuthorityUsersStrictRejectsUsersOutsideManagedRoles(t *testing.T) {
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{AuthorityID: actorID})
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{AuthorityID: actorID})
|
||||
repo := &authorityAccessRepo{data: data}
|
||||
if err := repo.SetAuthorityUsers(ctx, childID, []uint{users[1].ID}); err == nil {
|
||||
t.Fatal("SetAuthorityUsers() accepted a user outside the managed role tree")
|
||||
@@ -231,7 +231,7 @@ func TestSetAuthorityUsersStrictRejectsMixedRoleUserAlreadyLinked(t *testing.T)
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{AuthorityID: actorID})
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{AuthorityID: actorID})
|
||||
if err := (&authorityAccessRepo{data: data}).SetAuthorityUsers(ctx, childID, nil); err == nil {
|
||||
t.Fatal("SetAuthorityUsers() modified a linked user that also has an out-of-scope role")
|
||||
}
|
||||
@@ -257,8 +257,8 @@ func TestUpdateAuthorityStrictRejectsHierarchyCycle(t *testing.T) {
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{AuthorityID: actorID})
|
||||
err := (&authorityAccessRepo{data: data}).UpdateAuthority(ctx, &biz.Authority{AuthorityID: targetID, AuthorityName: "target", ParentID: &childID})
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{AuthorityID: actorID})
|
||||
err := (&authorityAccessRepo{data: data}).UpdateAuthority(ctx, &system.Authority{AuthorityID: targetID, AuthorityName: "target", ParentID: &childID})
|
||||
if err == nil {
|
||||
t.Fatal("UpdateAuthority() accepted a parent that forms a cycle")
|
||||
}
|
||||
@@ -283,7 +283,7 @@ func TestSetDataScopeValidatesScopeAndStrictDepartments(t *testing.T) {
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{AuthorityID: actorID})
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{AuthorityID: actorID})
|
||||
repo := &authorityAccessRepo{data: data}
|
||||
if err := repo.SetDataScope(ctx, childID, 0, nil); !errors.Is(err, errInvalidDataScope) {
|
||||
t.Fatalf("SetDataScope() invalid scope error = %v", err)
|
||||
@@ -323,7 +323,7 @@ func TestSetDataScopeStrictRejectsDepartmentsOutsideActorScope(t *testing.T) {
|
||||
if err := db.Create(&authorityDepartmentPO{AuthorityID: actorID, DepartmentID: departments[0].ID}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{UserID: actorUser.ID, AuthorityID: actorID})
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{UserID: actorUser.ID, AuthorityID: actorID})
|
||||
repo := &authorityAccessRepo{data: data}
|
||||
if err := repo.SetDataScope(ctx, childID, 5, []uint{departments[1].ID}); err == nil {
|
||||
t.Fatal("SetDataScope() accepted a department outside the actor's data scope")
|
||||
@@ -350,31 +350,31 @@ func TestStrictDataScopeGrantRejectsBroaderChildScopes(t *testing.T) {
|
||||
if err := db.Create(&actorUser).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{UserID: actorUser.ID, AuthorityID: actorID})
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{UserID: actorUser.ID, AuthorityID: actorID})
|
||||
repo := &authorityAccessRepo{data: data}
|
||||
|
||||
created := &biz.Authority{AuthorityID: 1373, AuthorityName: "created", ParentID: &actorID}
|
||||
created := &system.Authority{AuthorityID: 1373, AuthorityName: "created", ParentID: &actorID}
|
||||
if err := repo.CreateAuthority(ctx, created); !errors.Is(err, errInvalidDataScope) {
|
||||
t.Fatalf("CreateAuthority() broader scope error = %v", err)
|
||||
}
|
||||
if created.DataScope != 1 {
|
||||
t.Fatalf("CreateAuthority() did not normalize zero scope before validation: %d", created.DataScope)
|
||||
}
|
||||
copied := &biz.Authority{AuthorityID: 1374, AuthorityName: "copied", ParentID: &actorID}
|
||||
copied := &system.Authority{AuthorityID: 1374, AuthorityName: "copied", ParentID: &actorID}
|
||||
if err := repo.CopyAuthority(ctx, sourceID, copied); !errors.Is(err, errInvalidDataScope) {
|
||||
t.Fatalf("CopyAuthority() broader scope error = %v", err)
|
||||
}
|
||||
if copied.DataScope != 1 {
|
||||
t.Fatalf("CopyAuthority() did not normalize zero scope before validation: %d", copied.DataScope)
|
||||
}
|
||||
if err := repo.UpdateAuthority(ctx, &biz.Authority{AuthorityID: targetID, AuthorityName: "target", ParentID: &actorID, DataScope: 2}); !errors.Is(err, errInvalidDataScope) {
|
||||
if err := repo.UpdateAuthority(ctx, &system.Authority{AuthorityID: targetID, AuthorityName: "target", ParentID: &actorID, DataScope: 2}); !errors.Is(err, errInvalidDataScope) {
|
||||
t.Fatalf("UpdateAuthority() broader scope error = %v", err)
|
||||
}
|
||||
if err := repo.SetDataScope(ctx, targetID, 1, nil); !errors.Is(err, errInvalidDataScope) {
|
||||
t.Fatalf("SetDataScope() broader scope error = %v", err)
|
||||
}
|
||||
|
||||
allowed := &biz.Authority{AuthorityID: 1375, AuthorityName: "self-only", ParentID: &actorID, DataScope: 4}
|
||||
allowed := &system.Authority{AuthorityID: 1375, AuthorityName: "self-only", ParentID: &actorID, DataScope: 4}
|
||||
if err := repo.CreateAuthority(ctx, allowed); err != nil {
|
||||
t.Fatalf("CreateAuthority() rejected a narrower scope: %v", err)
|
||||
}
|
||||
@@ -405,9 +405,9 @@ func TestCreateAuthorityStrictDefaultsStayWithinActorPermissions(t *testing.T) {
|
||||
if err := db.Create(&apiPO{Path: "/menu/getMenu", Method: "POST"}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{AuthorityID: actorID})
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{AuthorityID: actorID})
|
||||
repo := &authorityAccessRepo{data: data}
|
||||
first := &biz.Authority{AuthorityID: 1401, AuthorityName: "no-defaults", ParentID: &actorID}
|
||||
first := &system.Authority{AuthorityID: 1401, AuthorityName: "no-defaults", ParentID: &actorID}
|
||||
if err := repo.CreateAuthority(ctx, first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -429,7 +429,7 @@ func TestCreateAuthorityStrictDefaultsStayWithinActorPermissions(t *testing.T) {
|
||||
if err := db.Create(&policy).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second := &biz.Authority{AuthorityID: 1402, AuthorityName: "owned-defaults", ParentID: &actorID}
|
||||
second := &system.Authority{AuthorityID: 1402, AuthorityName: "owned-defaults", ParentID: &actorID}
|
||||
if err := repo.CreateAuthority(ctx, second); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -476,21 +476,21 @@ func TestCopyAuthorityStrictValidatesCopiedMenusAndButtons(t *testing.T) {
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := biz.NewActorContext(context.Background(), biz.Actor{AuthorityID: actorID})
|
||||
ctx := system.NewActorContext(context.Background(), system.Actor{AuthorityID: actorID})
|
||||
repo := &authorityAccessRepo{data: data}
|
||||
if err := repo.CopyAuthority(ctx, sourceID, &biz.Authority{AuthorityID: 1510, AuthorityName: "menu-fail", ParentID: &actorID}); err == nil {
|
||||
if err := repo.CopyAuthority(ctx, sourceID, &system.Authority{AuthorityID: 1510, AuthorityName: "menu-fail", ParentID: &actorID}); err == nil {
|
||||
t.Fatal("CopyAuthority() copied a menu not assigned to the actor")
|
||||
}
|
||||
if err := db.Where("sys_authority_authority_id = ? AND sys_base_menu_id = ?", sourceID, 11).Delete(&authorityMenuPO{}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repo.CopyAuthority(ctx, sourceID, &biz.Authority{AuthorityID: 1511, AuthorityName: "button-fail", ParentID: &actorID}); err == nil {
|
||||
if err := repo.CopyAuthority(ctx, sourceID, &system.Authority{AuthorityID: 1511, AuthorityName: "button-fail", ParentID: &actorID}); err == nil {
|
||||
t.Fatal("CopyAuthority() copied a button not assigned to the actor")
|
||||
}
|
||||
if err := db.Where("authority_id = ? AND sys_base_menu_btn_id = ?", sourceID, 32).Delete(&authorityButtonPO{}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repo.CopyAuthority(ctx, sourceID, &biz.Authority{AuthorityID: 1512, AuthorityName: "valid", ParentID: &actorID}); err != nil {
|
||||
if err := repo.CopyAuthority(ctx, sourceID, &system.Authority{AuthorityID: 1512, AuthorityName: "valid", ParentID: &actorID}); err != nil {
|
||||
t.Fatalf("CopyAuthority() rejected owned permissions: %v", err)
|
||||
}
|
||||
for _, failedID := range []uint{1510, 1511} {
|
||||
@@ -509,7 +509,7 @@ func TestAuthorityCreateAndCopyNormalizeZeroDataScope(t *testing.T) {
|
||||
db := data.gormDB.WithContext(context.Background())
|
||||
repo := &authorityAccessRepo{data: data}
|
||||
|
||||
created := &biz.Authority{AuthorityID: 1600, AuthorityName: "created"}
|
||||
created := &system.Authority{AuthorityID: 1600, AuthorityName: "created"}
|
||||
if err := repo.CreateAuthority(context.Background(), created); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -520,7 +520,7 @@ func TestAuthorityCreateAndCopyNormalizeZeroDataScope(t *testing.T) {
|
||||
if err := db.Create(&authorityPO{AuthorityID: 1601, AuthorityName: "source", DataScope: 3}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
copied := &biz.Authority{AuthorityID: 1602, AuthorityName: "copied"}
|
||||
copied := &system.Authority{AuthorityID: 1602, AuthorityName: "copied"}
|
||||
if err := repo.CopyAuthority(context.Background(), 1601, copied); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -549,13 +549,13 @@ func TestAuthorityMutationsRejectInvalidDataScope(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, scope := range []int{-1, 6} {
|
||||
if err := repo.CreateAuthority(context.Background(), &biz.Authority{AuthorityID: uint(1800 + scope + 1), AuthorityName: "invalid-create", DataScope: scope}); !errors.Is(err, errInvalidDataScope) {
|
||||
if err := repo.CreateAuthority(context.Background(), &system.Authority{AuthorityID: uint(1800 + scope + 1), AuthorityName: "invalid-create", DataScope: scope}); !errors.Is(err, errInvalidDataScope) {
|
||||
t.Fatalf("CreateAuthority() scope %d error = %v", scope, err)
|
||||
}
|
||||
if err := repo.CopyAuthority(context.Background(), 1700, &biz.Authority{AuthorityID: uint(1900 + scope + 1), AuthorityName: "invalid-copy", DataScope: scope}); !errors.Is(err, errInvalidDataScope) {
|
||||
if err := repo.CopyAuthority(context.Background(), 1700, &system.Authority{AuthorityID: uint(1900 + scope + 1), AuthorityName: "invalid-copy", DataScope: scope}); !errors.Is(err, errInvalidDataScope) {
|
||||
t.Fatalf("CopyAuthority() scope %d error = %v", scope, err)
|
||||
}
|
||||
if err := repo.UpdateAuthority(context.Background(), &biz.Authority{AuthorityID: 1701, AuthorityName: "invalid-update", DataScope: scope}); !errors.Is(err, errInvalidDataScope) {
|
||||
if err := repo.UpdateAuthority(context.Background(), &system.Authority{AuthorityID: 1701, AuthorityName: "invalid-update", DataScope: scope}); !errors.Is(err, errInvalidDataScope) {
|
||||
t.Fatalf("UpdateAuthority() scope %d error = %v", scope, err)
|
||||
}
|
||||
}
|
||||
@@ -568,7 +568,7 @@ func TestUpdateAuthorityZeroDataScopeKeepsStoredValue(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := (&authorityAccessRepo{data: data}).UpdateAuthority(context.Background(), &biz.Authority{AuthorityID: 2000, AuthorityName: "after", DataScope: 0}); err != nil {
|
||||
if err := (&authorityAccessRepo{data: data}).UpdateAuthority(context.Background(), &system.Authority{AuthorityID: 2000, AuthorityName: "after", DataScope: 0}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var stored authorityPO
|
||||
|
||||
Reference in New Issue
Block a user