Files
hfb_sys/backend/internal/modules/invitation/tenant_scope_test.go
T

155 lines
5.6 KiB
Go

package invitation
import (
"context"
"strings"
"testing"
"time"
"hfb_sys/backend/internal/database"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/tenancy"
)
func TestRebateConfigIsScopedByTenant(t *testing.T) {
db := database.NewTestDB()
if err := database.AutoMigrateIfMissing(db, &model.InvitationRebateConfig{}); err != nil {
t.Fatalf("migrate rebate config: %v", err)
}
repo := NewRepository(db)
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101, TenantCode: "tenant_a"})
ctxB := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202, TenantCode: "tenant_b"})
enabled := true
if _, err := repo.UpdateRebateConfig(ctxA, 0, UpdateConfigRequest{
FirstMonthRate: 0.50,
SecondMonthRate: 0.25,
SubsequentRate: 0.10,
Enabled: &enabled,
}); err != nil {
t.Fatalf("update tenant A config: %v", err)
}
if _, err := repo.UpdateRebateConfig(ctxB, 0, UpdateConfigRequest{
FirstMonthRate: 0.70,
SecondMonthRate: 0.35,
SubsequentRate: 0.20,
Enabled: &enabled,
}); err != nil {
t.Fatalf("update tenant B config: %v", err)
}
cfgA, err := repo.GetRebateConfig(ctxA, 0)
if err != nil {
t.Fatalf("get tenant A config: %v", err)
}
cfgB, err := repo.GetRebateConfig(ctxB, 0)
if err != nil {
t.Fatalf("get tenant B config: %v", err)
}
if cfgA.TenantID != 101 || cfgA.FirstMonthRate != 0.50 {
t.Fatalf("tenant A config = %+v", cfgA)
}
if cfgB.TenantID != 202 || cfgB.FirstMonthRate != 0.70 {
t.Fatalf("tenant B config = %+v", cfgB)
}
}
func TestAdminInvitationAndRebateQueriesAreScopedByTenant(t *testing.T) {
db := setupTestDB(t)
repo := NewRepository(db)
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101, TenantCode: "dachui"})
ctxB := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202, TenantCode: "jieyou"})
now := time.Now()
codeA1 := "tenant-a-inviter"
codeB1 := "tenant-b-inviter"
users := []model.User{
{TenantID: 101, Phone: "13800000101", Nickname: "大锤邀请人", Status: "active", RealnameStatus: "verified", InviteCode: &codeA1},
{TenantID: 101, Phone: "13900000101", Nickname: "大锤被邀请人", Status: "active", RealnameStatus: "verified"},
{TenantID: 202, Phone: "13800000202", Nickname: "解忧邀请人", Status: "active", RealnameStatus: "verified", InviteCode: &codeB1},
{TenantID: 202, Phone: "13900000202", Nickname: "解忧被邀请人", Status: "active", RealnameStatus: "verified"},
}
if err := db.Create(&users).Error; err != nil {
t.Fatalf("create tenant users: %v", err)
}
invitations := []model.UserInvitation{
{InviterID: users[0].ID, InviteeID: users[1].ID, InviteCode: codeA1, Status: StatusActive, VerifiedAt: &now},
{InviterID: users[2].ID, InviteeID: users[3].ID, InviteCode: codeB1, Status: StatusActive, VerifiedAt: &now},
}
if err := db.Create(&invitations).Error; err != nil {
t.Fatalf("create invitations: %v", err)
}
ledgers := []model.InvitationRebateLedger{
{
OrderID: 9001, OrderNo: "ORDER-TENANT-A", Source: "rental_order",
InviterID: users[0].ID, InviteeID: users[1].ID, TradeRole: "buyer",
RateApplied: 0.6, CycleStage: CycleFirstMonth, RebateAmountCent: 100,
Status: "settled", SettledAt: now,
},
{
OrderID: 9002, OrderNo: "ORDER-TENANT-B", Source: "rental_order",
InviterID: users[2].ID, InviteeID: users[3].ID, TradeRole: "buyer",
RateApplied: 0.6, CycleStage: CycleFirstMonth, RebateAmountCent: 200,
Status: "settled", SettledAt: now,
},
}
if err := db.Create(&ledgers).Error; err != nil {
t.Fatalf("create rebate ledgers: %v", err)
}
itemsA, totalA, err := repo.ListAdminInvitations(ctxA, AdminInvitationQuery{Page: 1, PageSize: 20})
if err != nil {
t.Fatalf("list tenant A invitations: %v", err)
}
if totalA != 1 || len(itemsA) != 1 {
t.Fatalf("tenant A invitations total/items = %d/%d, want 1/1", totalA, len(itemsA))
}
if got := itemsA[0]["inviter_phone"]; got != "13800000101" {
t.Fatalf("tenant A inviter phone = %v", got)
}
if got := itemsA[0]["inviter_total_invited"]; got != int64(1) {
t.Fatalf("tenant A inviter total invited = %v, want 1", got)
}
itemsB, totalB, err := repo.ListAdminInvitations(ctxB, AdminInvitationQuery{Page: 1, PageSize: 20})
if err != nil {
t.Fatalf("list tenant B invitations: %v", err)
}
if totalB != 1 || len(itemsB) != 1 || itemsB[0]["inviter_phone"] != "13800000202" {
t.Fatalf("tenant B invitations leaked: total=%d items=%+v", totalB, itemsB)
}
ledgersA, totalLedgersA, err := repo.ListAdminRebateLedgers(ctxA, AdminRebateLedgerQuery{Page: 1, PageSize: 20})
if err != nil {
t.Fatalf("list tenant A rebate ledgers: %v", err)
}
if totalLedgersA != 1 || len(ledgersA) != 1 || ledgersA[0]["order_no"] != "ORDER-TENANT-A" {
t.Fatalf("tenant A rebate ledgers leaked: total=%d items=%+v", totalLedgersA, ledgersA)
}
summaryA, err := repo.AdminRebateSummary(ctxA, AdminRebateLedgerQuery{})
if err != nil {
t.Fatalf("tenant A rebate summary: %v", err)
}
if summaryA.LedgerCount != 1 || summaryA.TotalRebateCent != 100 {
t.Fatalf("tenant A rebate summary leaked: %+v", summaryA)
}
}
func TestInvitationTenantScopeSQL(t *testing.T) {
db := database.NewDryRunDB()
ctx := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202, TenantCode: "jieyou"})
stmt := applyInvitationUserTenantScope(ctx, db.Table("user_invitations AS ui"), "inviter", "invitee").
Find(&[]struct{}{}).Statement
sql := stmt.SQL.String()
if !strings.Contains(sql, "inviter.tenant_id = ? AND invitee.tenant_id = ?") {
t.Fatalf("tenant scope SQL missing: %s", sql)
}
if len(stmt.Vars) < 2 || stmt.Vars[0] != uint64(202) || stmt.Vars[1] != uint64(202) {
t.Fatalf("tenant scope args = %#v", stmt.Vars)
}
}