feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -105,6 +106,30 @@ func TestRecordPrivateFileUpload(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivateFileAuthorizerRejectsCrossTenantOwner(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
if err := database.AutoMigrateIfMissing(db, &model.FileUploadOwner{}); err != nil {
|
||||
t.Fatalf("AutoMigrate() error = %v", err)
|
||||
}
|
||||
if err := db.Create(&model.FileUploadOwner{TenantID: 101, UserID: 11, ObjectKey: "listing/tenant-a.jpg"}).Error; err != nil {
|
||||
t.Fatalf("create upload owner: %v", err)
|
||||
}
|
||||
authorize := privateFileAuthorizer(db)
|
||||
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101})
|
||||
ctxB := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202})
|
||||
allowed, err := authorize(ctxA, 11, "listing/tenant-a.jpg")
|
||||
if err != nil || !allowed {
|
||||
t.Fatalf("same tenant allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
allowed, err = authorize(ctxB, 11, "listing/tenant-a.jpg")
|
||||
if err != nil {
|
||||
t.Fatalf("cross tenant authorize error: %v", err)
|
||||
}
|
||||
if allowed {
|
||||
t.Fatal("cross tenant upload owner must not be readable")
|
||||
}
|
||||
}
|
||||
|
||||
func openAuthorizerTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db := database.NewTestDB()
|
||||
|
||||
Reference in New Issue
Block a user