fix(tenant): 收紧双站后台边界并补齐货架管理
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package adminuser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -8,10 +9,15 @@ import (
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/rentergrowth"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func adminUserTenantContext(tenantID uint64) context.Context {
|
||||
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
func TestDepositFreeQuotaAmountCent(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -578,6 +584,45 @@ func TestFreezeAndUnfreezeBumpUserTokenVersion(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserAdminIsScopedToCurrentTenantAndOperatingUsers(t *testing.T) {
|
||||
db := setupAdminUserTestDB(t)
|
||||
tenants := []model.Tenant{
|
||||
{ID: 101, Code: "tenant_a", Name: "A站", Status: "active"},
|
||||
{ID: 202, Code: "tenant_b", Name: "B站", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users := []model.User{
|
||||
{TenantID: 101, Phone: "13700000101", Status: "active"},
|
||||
{TenantID: 101, Phone: "13700000102", Status: "active", DistributionSiteID: 999},
|
||||
{TenantID: 202, Phone: "13700000201", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
ctx := adminUserTenantContext(101)
|
||||
|
||||
result, err := repo.List(ctx, 1, 20, ListQuery{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := result.Items.([]UserDTO)
|
||||
if result.Total != 1 || len(items) != 1 || items[0].ID != users[0].ID {
|
||||
t.Fatalf("tenant user list = %+v", result)
|
||||
}
|
||||
if _, err := repo.Find(ctx, users[2].ID); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("cross-tenant user detail error = %v", err)
|
||||
}
|
||||
if _, err := repo.Freeze(ctx, 9, users[2].ID, FreezeRequest{Reason: "越权测试"}, AuditMeta{}); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("cross-tenant user freeze error = %v", err)
|
||||
}
|
||||
if _, err := repo.Find(ctx, users[1].ID); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("legacy distribution user detail error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func setupAdminUserTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db := database.NewTestDB()
|
||||
|
||||
Reference in New Issue
Block a user