287 lines
9.7 KiB
Go
287 lines
9.7 KiB
Go
package order
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
|
|
"gorm.io/datatypes"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func orderTenantContext(tenantID uint64) context.Context {
|
|
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
|
}
|
|
|
|
func createOrderTenantListing(t *testing.T, db *gorm.DB, tenantIDs ...uint64) (*model.RentalListing, []model.User) {
|
|
t.Helper()
|
|
var tenants []model.Tenant
|
|
for index, tenantID := range tenantIDs {
|
|
tenant := model.Tenant{
|
|
Code: "tenant_" + string(rune('a'+index)),
|
|
Name: "租户",
|
|
Status: "active",
|
|
}
|
|
tenant.ID = tenantID
|
|
if err := db.Create(&tenant).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tenants = append(tenants, tenant)
|
|
}
|
|
owner := model.User{TenantID: tenants[0].ID, Phone: "13000000001", Status: "active"}
|
|
if err := db.Create(&owner).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
account := model.GameAccount{
|
|
SourceTenantID: tenants[0].ID, OwnerID: owner.ID, ServerRegion: "国服",
|
|
LoginPlatform: "steam", Title: "跨租户商品", Status: "published",
|
|
AssetSummary: datatypes.JSON([]byte(`{
|
|
"price_breakdown": {
|
|
"seller_total_price": 80,
|
|
"platform_markup_amount": 20
|
|
}
|
|
}`)),
|
|
}
|
|
if err := db.Create(&account).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
listing := model.RentalListing{
|
|
SourceTenantID: tenants[0].ID, ListingNo: "L-ORDER-TENANT-1",
|
|
AccountID: account.ID, OwnerID: owner.ID, PriceCent: 10000,
|
|
Status: listingStatusPublished, ReviewStatus: listingReviewStatusApproved,
|
|
}
|
|
if err := db.Create(&listing).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for index, tenantID := range tenantIDs {
|
|
var price *int64
|
|
if index > 0 {
|
|
value := int64(10000 + index*2000)
|
|
price = &value
|
|
}
|
|
if err := db.Create(&model.TenantListingOffer{
|
|
TenantID: tenantID, ListingID: listing.ID, DisplayStatus: "enabled", PriceCent: price,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
renters := make([]model.User, 0, len(tenantIDs)-1)
|
|
for index, tenantID := range tenantIDs[1:] {
|
|
renter := model.User{
|
|
TenantID: tenantID,
|
|
Phone: "1300000001" + string(rune('0'+index+1)),
|
|
Status: "active",
|
|
}
|
|
if err := db.Create(&renter).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
renters = append(renters, renter)
|
|
}
|
|
return &listing, renters
|
|
}
|
|
|
|
func TestCreateOrderUsesBuyerTenantOfferAndSnapshotsTenantRoles(t *testing.T) {
|
|
db := setupOrderTestDB(t)
|
|
listing, renters := createOrderTenantListing(t, db, 101, 202, 303)
|
|
repo := NewRepository(db)
|
|
|
|
noOfferTenant := model.Tenant{ID: 404, Code: "tenant_no_offer", Name: "无货架租户", Status: "active"}
|
|
if err := db.Create(&noOfferTenant).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
noOfferRenter := model.User{TenantID: noOfferTenant.ID, Phone: "13000000019", Status: "active"}
|
|
if err := db.Create(&noOfferRenter).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := repo.Create(orderTenantContext(noOfferTenant.ID), noOfferRenter.ID, CreateRequest{ListingID: listing.ID}); !errors.Is(err, ErrListingUnavailable) {
|
|
t.Fatalf("tenant without offer error = %v, want ErrListingUnavailable", err)
|
|
}
|
|
dto, err := repo.Create(orderTenantContext(202), renters[0].ID, CreateRequest{ListingID: listing.ID})
|
|
if err != nil {
|
|
t.Fatalf("create cross-tenant order: %v", err)
|
|
}
|
|
if dto.BuyerTenantID != 202 || dto.SourceTenantID != 101 || dto.PaymentTenantID != 202 {
|
|
t.Fatalf("order tenant snapshots = buyer:%d source:%d payment:%d",
|
|
dto.BuyerTenantID, dto.SourceTenantID, dto.PaymentTenantID)
|
|
}
|
|
if dto.BasePriceCent != 12000 || dto.ShelfMarkupCent != 0 {
|
|
t.Fatalf("offer price snapshot = base:%d markup:%d", dto.BasePriceCent, dto.ShelfMarkupCent)
|
|
}
|
|
if dto.SourceAmountCent != 8000 {
|
|
t.Fatalf("source amount snapshot = %d, want 8000", dto.SourceAmountCent)
|
|
}
|
|
var saved model.RentalOrder
|
|
if err := db.First(&saved, dto.ID).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if saved.BuyerTenantID != 202 || saved.SourceTenantID != 101 || saved.PaymentTenantID != 202 {
|
|
t.Fatalf("saved tenant roles = %+v", saved)
|
|
}
|
|
if saved.OwnerRentAmountCent != saved.SourceAmountCent {
|
|
t.Fatalf("source amount = %d, owner rent = %d", saved.SourceAmountCent, saved.OwnerRentAmountCent)
|
|
}
|
|
}
|
|
|
|
func TestConcurrentTenantsCanOnlyRentListingOnce(t *testing.T) {
|
|
db := setupOrderTestDB(t)
|
|
listing, renters := createOrderTenantListing(t, db, 101, 202, 303)
|
|
repo := NewRepository(db)
|
|
|
|
start := make(chan struct{})
|
|
results := make(chan error, 2)
|
|
var wg sync.WaitGroup
|
|
for index, tenantID := range []uint64{202, 303} {
|
|
wg.Add(1)
|
|
go func(renterID uint64, tenantID uint64) {
|
|
defer wg.Done()
|
|
<-start
|
|
_, err := repo.Create(orderTenantContext(tenantID), renterID, CreateRequest{ListingID: listing.ID})
|
|
results <- err
|
|
}(renters[index].ID, tenantID)
|
|
}
|
|
close(start)
|
|
wg.Wait()
|
|
close(results)
|
|
|
|
var success, unavailable int
|
|
for err := range results {
|
|
switch {
|
|
case err == nil:
|
|
success++
|
|
case errors.Is(err, ErrListingUnavailable):
|
|
unavailable++
|
|
default:
|
|
t.Fatalf("unexpected create order error: %v", err)
|
|
}
|
|
}
|
|
if success != 1 || unavailable != 1 {
|
|
t.Fatalf("concurrent order result = success:%d unavailable:%d", success, unavailable)
|
|
}
|
|
var count int64
|
|
if err := db.Model(&model.RentalOrder{}).Where("listing_id = ?", listing.ID).Count(&count).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 1 {
|
|
t.Fatalf("order count = %d, want 1", count)
|
|
}
|
|
}
|
|
|
|
func TestAdminOrdersAreScopedByCurrentTenant(t *testing.T) {
|
|
db := setupOrderTestDB(t)
|
|
tenants := []model.Tenant{
|
|
{ID: 101, Code: "tenant_a", Name: "A站", Status: "active"},
|
|
{ID: 202, Code: "tenant_b", Name: "B站", Status: "active"},
|
|
{ID: 303, Code: "tenant_c", Name: "C站", Status: "active"},
|
|
}
|
|
if err := db.Create(&tenants).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
owner := model.User{TenantID: 101, Phone: "13000000101", Status: "active"}
|
|
renterB := model.User{TenantID: 202, Phone: "13000000202", Status: "active"}
|
|
renterA := model.User{TenantID: 101, Phone: "13000000102", Status: "active"}
|
|
if err := db.Create(&owner).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&renterB).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&renterA).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
accountA := model.GameAccount{
|
|
SourceTenantID: 101, OwnerID: owner.ID, ServerRegion: "国服",
|
|
LoginPlatform: "steam", Title: "A站商品", Status: "published",
|
|
}
|
|
if err := db.Create(&accountA).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
listingA := model.RentalListing{
|
|
SourceTenantID: 101, ListingNo: "L-ADMIN-TENANT-A",
|
|
AccountID: accountA.ID, OwnerID: owner.ID, PriceCent: 1000,
|
|
Status: listingStatusPublished, ReviewStatus: listingReviewStatusApproved,
|
|
}
|
|
accountC := model.GameAccount{
|
|
SourceTenantID: 303, OwnerID: owner.ID, ServerRegion: "国服",
|
|
LoginPlatform: "steam", Title: "C站商品", Status: "published",
|
|
}
|
|
if err := db.Create(&accountC).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
listingC := model.RentalListing{
|
|
SourceTenantID: 303, ListingNo: "L-ADMIN-TENANT-C",
|
|
AccountID: accountC.ID, OwnerID: owner.ID, PriceCent: 1000,
|
|
Status: listingStatusPublished, ReviewStatus: listingReviewStatusApproved,
|
|
}
|
|
if err := db.Create(&listingA).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&listingC).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
orders := []model.RentalOrder{
|
|
{
|
|
OrderNo: "RO-TENANT-VISIBLE-B", ListingID: listingA.ID, AccountID: accountA.ID,
|
|
OwnerID: owner.ID, RenterID: renterB.ID, BuyerTenantID: 202,
|
|
SourceTenantID: 101, PaymentTenantID: 202, Status: orderStatusRenting,
|
|
},
|
|
{
|
|
OrderNo: "RO-TENANT-HIDDEN", ListingID: listingC.ID, AccountID: accountC.ID,
|
|
OwnerID: owner.ID, RenterID: renterA.ID, BuyerTenantID: 101,
|
|
SourceTenantID: 303, PaymentTenantID: 101, Status: orderStatusRenting,
|
|
},
|
|
}
|
|
if err := db.Create(&orders).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := NewRepository(db)
|
|
|
|
result, err := repo.ListAdmin(orderTenantContext(202), AdminOrderQuery{Page: 1, PageSize: 20})
|
|
if err != nil {
|
|
t.Fatalf("list B tenant orders: %v", err)
|
|
}
|
|
if result.Total != 1 || len(result.Items) != 1 || result.Items[0].ID != orders[0].ID {
|
|
t.Fatalf("B tenant admin orders = %+v", result)
|
|
}
|
|
if result.Items[0].SiteRole != "buyer" || result.Items[0].SourceTenantID != 101 {
|
|
t.Fatalf("B tenant order role = %+v", result.Items[0])
|
|
}
|
|
if _, err := repo.FindAdmin(orderTenantContext(202), orders[1].ID); !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
t.Fatalf("cross-tenant admin detail error = %v", err)
|
|
}
|
|
service := NewService(repo)
|
|
if _, err := service.AdminRefundStatus(orderTenantContext(202), orders[1].ID); !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
t.Fatalf("cross-tenant admin action error = %v", err)
|
|
}
|
|
if err := repo.EnsureAdminOrderCapability(orderTenantContext(101), orders[0].ID, tenantOrderCapabilityRefund); !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
t.Fatalf("source tenant refund capability error = %v", err)
|
|
}
|
|
if err := repo.EnsureAdminOrderCapability(orderTenantContext(202), orders[0].ID, tenantOrderCapabilityRefund); err != nil {
|
|
t.Fatalf("buyer tenant refund capability: %v", err)
|
|
}
|
|
if err := repo.EnsureAdminOrderCapability(orderTenantContext(101), orders[0].ID, tenantOrderCapabilityHandoff); err != nil {
|
|
t.Fatalf("source tenant handoff capability: %v", err)
|
|
}
|
|
if err := repo.EnsureAdminOrderCapability(orderTenantContext(202), orders[0].ID, tenantOrderCapabilityHandoff); !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
t.Fatalf("buyer tenant handoff capability error = %v", err)
|
|
}
|
|
|
|
sourceResult, err := repo.ListAdmin(orderTenantContext(101), AdminOrderQuery{Page: 1, PageSize: 20})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sourceResult.Total != 2 {
|
|
t.Fatalf("A tenant relevant orders = %+v", sourceResult)
|
|
}
|
|
roles := map[uint64]string{}
|
|
for _, item := range sourceResult.Items {
|
|
roles[item.ID] = item.SiteRole
|
|
}
|
|
if roles[orders[0].ID] != "source" || roles[orders[1].ID] != "buyer" {
|
|
t.Fatalf("A tenant order roles = %+v", roles)
|
|
}
|
|
}
|