fix(tenant): 收紧双站运营提号与录单边界
This commit is contained in:
@@ -3,6 +3,7 @@ package router
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -91,10 +92,10 @@ func TestRecordPrivateFileUpload(t *testing.T) {
|
||||
}
|
||||
record := recordPrivateFileUpload(db)
|
||||
keys := []string{"listing/original.jpg", "listing/original.thumb.jpg", "listing/original.medium.jpg"}
|
||||
if err := record(context.Background(), 11, keys); err != nil {
|
||||
if err := record(context.Background(), 11, 0, keys); err != nil {
|
||||
t.Fatalf("record() error = %v", err)
|
||||
}
|
||||
if err := record(context.Background(), 11, keys); err != nil {
|
||||
if err := record(context.Background(), 11, 0, keys); err != nil {
|
||||
t.Fatalf("duplicate record() error = %v", err)
|
||||
}
|
||||
var count int64
|
||||
@@ -104,6 +105,16 @@ func TestRecordPrivateFileUpload(t *testing.T) {
|
||||
if count != int64(len(keys)) {
|
||||
t.Fatalf("record count = %d, want %d", count, len(keys))
|
||||
}
|
||||
adminKeys := []string{"qrcode/admin.jpg", "qrcode/admin.thumb.jpg"}
|
||||
if err := record(context.Background(), 0, 21, adminKeys); err != nil {
|
||||
t.Fatalf("record admin uploads: %v", err)
|
||||
}
|
||||
if err := db.Model(&model.FileUploadOwner{}).Where("admin_id = ?", 21).Count(&count).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != int64(len(adminKeys)) {
|
||||
t.Fatalf("admin record count = %d, want %d", count, len(adminKeys))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivateFileAuthorizerRejectsCrossTenantOwner(t *testing.T) {
|
||||
@@ -136,8 +147,8 @@ func TestAdminPrivateFileAuthorizerScopesOperatingTenant(t *testing.T) {
|
||||
t.Fatalf("AutoMigrate() error = %v", err)
|
||||
}
|
||||
rows := []model.FileUploadOwner{
|
||||
{TenantID: 101, UserID: 11, ObjectKey: "listing/admin-tenant-a.jpg"},
|
||||
{TenantID: 202, UserID: 12, ObjectKey: "listing/admin-tenant-b.jpg"},
|
||||
{TenantID: 101, UserID: 11, AdminID: 1, ObjectKey: "listing/admin-tenant-a.jpg"},
|
||||
{TenantID: 202, UserID: 12, AdminID: 2, ObjectKey: "listing/admin-tenant-b.jpg"},
|
||||
}
|
||||
if err := db.Create(&rows).Error; err != nil {
|
||||
t.Fatalf("create upload owners: %v", err)
|
||||
@@ -157,11 +168,118 @@ func TestAdminPrivateFileAuthorizerScopesOperatingTenant(t *testing.T) {
|
||||
if allowed, err := authorize(ctxB, 2, "listing/admin-tenant-b.jpg"); err != nil || !allowed {
|
||||
t.Fatalf("tenant B own file allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := authorize(context.Background(), 3, "listing/admin-tenant-a.jpg"); err != nil || !allowed {
|
||||
if allowed, err := authorize(context.Background(), 1, "listing/admin-tenant-a.jpg"); err != nil || !allowed {
|
||||
t.Fatalf("legacy admin file allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivateFileAuthorizerAllowsDeliveredQrCodeParticipants(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
tenants := []model.Tenant{
|
||||
{Code: "file_tenant_a", Name: "A站", Status: "active"},
|
||||
{Code: "file_tenant_b", Name: "B站", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tenantA := tenants[0]
|
||||
tenantB := tenants[1]
|
||||
conversations := []model.ChatConversation{
|
||||
{TenantID: tenantA.ID, Type: "listing_group", Title: "A会话", Status: "active"},
|
||||
{TenantID: tenantB.ID, Type: "listing_group", Title: "B会话", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&conversations).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversationA := conversations[0]
|
||||
conversationB := conversations[1]
|
||||
participants := []model.ChatParticipant{
|
||||
{ConversationID: conversationA.ID, ParticipantType: "user", ParticipantID: 31, Role: "renter", JoinedAt: time.Now()},
|
||||
{ConversationID: conversationB.ID, ParticipantType: "user", ParticipantID: 32, Role: "renter", JoinedAt: time.Now()},
|
||||
}
|
||||
if err := db.Create(&participants).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
qrcodes := []model.ChatQrCode{
|
||||
{TenantID: tenantA.ID, ImageURL: "/api/files/object?key=qrcode%2Fa.png", ConversationID: &conversationA.ID, Status: "used"},
|
||||
{TenantID: tenantB.ID, ImageURL: "/api/files/object?key=qrcode%2Fb.png", ConversationID: &conversationB.ID, Status: "used"},
|
||||
}
|
||||
if err := db.Create(&qrcodes).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
userAuthorize := privateFileAuthorizer(db)
|
||||
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantA.ID})
|
||||
ctxB := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantB.ID})
|
||||
if allowed, err := userAuthorize(ctxA, 31, "qrcode/a.png"); err != nil || !allowed {
|
||||
t.Fatalf("A participant qrcode allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := userAuthorize(ctxA, 32, "qrcode/b.png"); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if allowed {
|
||||
t.Fatal("cross-tenant qrcode must be denied")
|
||||
}
|
||||
|
||||
adminAuthorize := adminPrivateFileAuthorizer(db)
|
||||
if allowed, err := adminAuthorize(ctxA, 1, "qrcode/a.png"); err != nil || !allowed {
|
||||
t.Fatalf("A admin qrcode allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := adminAuthorize(ctxB, 1, "qrcode/a.png"); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if allowed {
|
||||
t.Fatal("cross-tenant admin qrcode must be denied")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminPrivateFileAuthorizerAllowsOnlySuperAdminFinanceVouchers(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
roles := []model.Role{
|
||||
{Code: "super_admin", Name: "超级管理员"},
|
||||
{Code: "finance", Name: "财务"},
|
||||
}
|
||||
if err := db.Create(&roles).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
admins := []model.AdminUser{
|
||||
{Username: "file-root", PasswordHash: "x", Status: "active"},
|
||||
{Username: "file-finance", PasswordHash: "x", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&admins).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&[]model.AdminUserRole{
|
||||
{AdminID: admins[0].ID, RoleID: roles[0].ID, TenantID: 0},
|
||||
{AdminID: admins[1].ID, RoleID: roles[1].ID, TenantID: 101},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Exec(`INSERT INTO operating_expenses
|
||||
(expense_no, category, payee_name, amount_cent, occurred_at, remark, voucher_url, status, created_by)
|
||||
VALUES ('OE-FILE', '推广', '测试', 100, NOW(), '测试', ?, 'paid', ?)`,
|
||||
"/api/files/object?key=operating-expense%2Fvoucher.webp", admins[0].ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Exec(`INSERT INTO manual_disbursements
|
||||
(disbursement_no, category, payee_name, amount_cent, paid_at, remark, voucher_url, status, created_by)
|
||||
VALUES ('OD-FILE', 'other', '测试', 100, NOW(), '测试', ?, 'paid', ?)`,
|
||||
"/api/files/object?key=manual-disbursement%2Fvoucher.webp", admins[0].ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
authorize := adminPrivateFileAuthorizer(db)
|
||||
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101})
|
||||
for _, key := range []string{"operating-expense/voucher.webp", "manual-disbursement/voucher.webp"} {
|
||||
if allowed, err := authorize(ctxA, admins[0].ID, key); err != nil || !allowed {
|
||||
t.Fatalf("super admin %s allowed=%v err=%v", key, allowed, err)
|
||||
}
|
||||
if allowed, err := authorize(ctxA, admins[1].ID, key); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if allowed {
|
||||
t.Fatalf("tenant finance must not read %s", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func openAuthorizerTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db := database.NewTestDB()
|
||||
|
||||
Reference in New Issue
Block a user