fix(listing): 分销站商品审核按所属租户可见
分销站商品按站点归属租户参与审核;商品管理、审核详情及首页待审数量统一租户范围。
This commit is contained in:
@@ -548,6 +548,140 @@ func TestPendingReviewAndApprovalAreScopedToSourceTenant(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPendingReviewIncludesDistributionSitesOwnedByTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
if err := database.MigrateListingLifecycleTestSchema(db); err != nil {
|
||||
t.Fatalf("migrate err: %v", err)
|
||||
}
|
||||
if err := database.AutoMigrateIfMissing(db, &model.ChatSiteTenantLink{}); err != nil {
|
||||
t.Fatalf("migrate site tenant link: %v", err)
|
||||
}
|
||||
|
||||
tenants := []model.Tenant{
|
||||
{Code: "tenant_scope_a", Name: "A站", Status: "active"},
|
||||
{Code: "tenant_scope_b", Name: "B站", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatalf("create tenants: %v", err)
|
||||
}
|
||||
sites := []model.DistributionSite{
|
||||
{SiteCode: "tenant_scope_site_a", SiteName: "A分销站", Domain: "scope-a.example.com", Status: "active", DisplayAccountEnabled: true},
|
||||
{SiteCode: "tenant_scope_site_b", SiteName: "B分销站", Domain: "scope-b.example.com", Status: "active", DisplayAccountEnabled: true},
|
||||
}
|
||||
if err := db.Create(&sites).Error; err != nil {
|
||||
t.Fatalf("create sites: %v", err)
|
||||
}
|
||||
if err := db.Create(&[]model.ChatSiteTenantLink{
|
||||
{SiteID: sites[0].ID, TenantID: tenants[0].ID, Enabled: true},
|
||||
{SiteID: sites[1].ID, TenantID: tenants[1].ID, Enabled: true},
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("create site tenant links: %v", err)
|
||||
}
|
||||
|
||||
users := []model.User{
|
||||
{TenantID: tenants[0].ID, Phone: "18800000201", RealnameStatus: "verified", Status: "active"},
|
||||
{TenantID: tenants[1].ID, Phone: "18800000202", RealnameStatus: "verified", Status: "active"},
|
||||
{TenantID: 0, DistributionSiteID: sites[0].ID, Phone: "18800000203", RealnameStatus: "verified", Status: "active"},
|
||||
{TenantID: 0, DistributionSiteID: sites[1].ID, Phone: "18800000204", RealnameStatus: "verified", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatalf("create users: %v", err)
|
||||
}
|
||||
|
||||
createListing := func(sourceTenantID, ownerID uint64, siteID *uint64, suffix string) model.RentalListing {
|
||||
account := model.GameAccount{
|
||||
SourceTenantID: sourceTenantID,
|
||||
OwnerID: ownerID,
|
||||
GameName: "delta_force",
|
||||
ServerRegion: "QQ",
|
||||
LoginPlatform: "扫码",
|
||||
Title: suffix,
|
||||
Status: "draft",
|
||||
}
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatalf("create account %s: %v", suffix, err)
|
||||
}
|
||||
listing := model.RentalListing{
|
||||
SourceTenantID: sourceTenantID,
|
||||
ListingNo: "SCOPE-" + suffix,
|
||||
AccountID: account.ID,
|
||||
OwnerID: ownerID,
|
||||
PriceCent: 10000,
|
||||
Status: "draft",
|
||||
ReviewStatus: "pending",
|
||||
DistributionSiteID: siteID,
|
||||
}
|
||||
if err := db.Create(&listing).Error; err != nil {
|
||||
t.Fatalf("create listing %s: %v", suffix, err)
|
||||
}
|
||||
return listing
|
||||
}
|
||||
|
||||
mainA := createListing(tenants[0].ID, users[0].ID, nil, "MAIN-A")
|
||||
siteA := createListing(0, users[2].ID, &sites[0].ID, "SITE-A")
|
||||
mainB := createListing(tenants[1].ID, users[1].ID, nil, "MAIN-B")
|
||||
siteB := createListing(0, users[3].ID, &sites[1].ID, "SITE-B")
|
||||
offlineA := createListing(tenants[0].ID, users[0].ID, nil, "OFFLINE-A")
|
||||
if err := db.Model(&offlineA).Updates(map[string]any{"status": "offline"}).Error; err != nil {
|
||||
t.Fatalf("mark offline: %v", err)
|
||||
}
|
||||
|
||||
repo := NewRepository(db, nil)
|
||||
ctxA := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenants[0].ID})
|
||||
items, err := repo.ListPendingReview(ctxA)
|
||||
if err != nil {
|
||||
t.Fatalf("ListPendingReview() error = %v", err)
|
||||
}
|
||||
got := make(map[uint64]bool, len(items))
|
||||
for _, item := range items {
|
||||
got[item.ID] = true
|
||||
}
|
||||
if len(items) != 2 || !got[mainA.ID] || !got[siteA.ID] {
|
||||
t.Fatalf("pending items = %+v, want main A and site A only", items)
|
||||
}
|
||||
if got[mainB.ID] || got[siteB.ID] || got[offlineA.ID] {
|
||||
t.Fatalf("pending items leaked cross-tenant/offline listings: %+v", items)
|
||||
}
|
||||
|
||||
ctxB := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenants[1].ID})
|
||||
itemsB, err := repo.ListPendingReview(ctxB)
|
||||
if err != nil {
|
||||
t.Fatalf("ListPendingReview(B) error = %v", err)
|
||||
}
|
||||
gotB := make(map[uint64]bool, len(itemsB))
|
||||
for _, item := range itemsB {
|
||||
gotB[item.ID] = true
|
||||
}
|
||||
if len(itemsB) != 2 || !gotB[mainB.ID] || !gotB[siteB.ID] {
|
||||
t.Fatalf("pending items(B) = %+v, want main B and site B only", itemsB)
|
||||
}
|
||||
if gotB[mainA.ID] || gotB[siteA.ID] {
|
||||
t.Fatalf("pending items(B) leaked tenant A listings: %+v", itemsB)
|
||||
}
|
||||
|
||||
adminResult, err := repo.ListAdmin(ctxA, AdminListQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatalf("ListAdmin() error = %v", err)
|
||||
}
|
||||
if adminResult.Total != 3 || len(adminResult.Items) != 3 {
|
||||
t.Fatalf("admin listings total=%d items=%d, want 3 (main A, site A, offline A)", adminResult.Total, len(adminResult.Items))
|
||||
}
|
||||
for _, item := range adminResult.Items {
|
||||
if item.ID == mainB.ID || item.ID == siteB.ID {
|
||||
t.Fatalf("ListAdmin leaked cross-tenant listing %d", item.ID)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := repo.FindAdmin(ctxA, siteB.ID); !IsNotFound(err) {
|
||||
t.Fatalf("cross-tenant site FindAdmin error = %v, want not found", err)
|
||||
}
|
||||
if _, err := repo.Approve(ctxA, siteB.ID); !IsNotFound(err) {
|
||||
t.Fatalf("cross-tenant site Approve error = %v, want not found", err)
|
||||
}
|
||||
if _, err := repo.Approve(ctxA, siteA.ID); err != nil {
|
||||
t.Fatalf("approve own distribution site listing: %v", err)
|
||||
}
|
||||
}
|
||||
func TestExternalAccountToCreateRequestMapsUploadFields(t *testing.T) {
|
||||
req := externalAccountToCreateRequest("客服1", 1772526103000, ExternalAccountData{
|
||||
LoginMethod: "QQ账号密码",
|
||||
|
||||
Reference in New Issue
Block a user