79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package paymentaccount
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
)
|
|
|
|
func paymentTenantContext(tenantID uint64) context.Context {
|
|
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
|
}
|
|
|
|
func TestPaymentAccountsAreScopedByTenant(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
dachuiUser := model.User{TenantID: 101, Phone: "13500000001", Status: "active"}
|
|
jieyouUser := model.User{TenantID: 202, Phone: "13500000001", Status: "active"}
|
|
if err := db.Create(&dachuiUser).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&jieyouUser).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
dachuiAccount := model.UserPaymentAccount{
|
|
TenantID: 101, UserID: dachuiUser.ID, AccountType: "bank",
|
|
AccountName: "张三", AccountNo: "encrypted-dachui", Status: "active",
|
|
}
|
|
jieyouAccount := model.UserPaymentAccount{
|
|
TenantID: 202, UserID: jieyouUser.ID, AccountType: "bank",
|
|
AccountName: "张三", AccountNo: "encrypted-jieyou", Status: "active",
|
|
}
|
|
if err := db.Create(&dachuiAccount).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&jieyouAccount).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
repo := NewRepository(db, nil)
|
|
page, err := repo.List(paymentTenantContext(101), dachuiUser.ID, 1, 10)
|
|
if err != nil {
|
|
t.Fatalf("list dachui accounts: %v", err)
|
|
}
|
|
if page.Total != 1 || len(page.Items) != 1 || page.Items[0].ID != dachuiAccount.ID {
|
|
t.Fatalf("dachui payment accounts = %+v", page)
|
|
}
|
|
if _, err := repo.FindByID(paymentTenantContext(202), jieyouUser.ID, dachuiAccount.ID); !errors.Is(err, ErrAccountNotFound) {
|
|
t.Fatalf("cross-tenant account lookup error = %v, want ErrAccountNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestPaymentAccountCreateWritesTenant(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
user := model.User{TenantID: 101, Phone: "13500000002", Status: "active"}
|
|
if err := db.Create(&user).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := NewRepository(db, nil)
|
|
account, err := repo.Create(paymentTenantContext(101), user.ID, CreatePaymentAccountRequest{
|
|
AccountType: "bank",
|
|
AccountName: "李四",
|
|
AccountNo: "6222020000000000",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create payment account: %v", err)
|
|
}
|
|
var stored model.UserPaymentAccount
|
|
if err := db.First(&stored, account.ID).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stored.TenantID != 101 {
|
|
t.Fatalf("stored tenant_id = %d, want 101", stored.TenantID)
|
|
}
|
|
}
|