chore(init): 初始化解忧商行项目并完成 Phase 0 基座
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
func TestOrderDTOForUserHidesDepositHoldFields(t *testing.T) {
|
||||
now := time.Now()
|
||||
row := orderRow{
|
||||
RentalOrder: model.RentalOrder{
|
||||
ID: 1,
|
||||
OwnerID: 10,
|
||||
RenterID: 20,
|
||||
DepositHoldStatus: model.DepositHoldStatusHeld,
|
||||
DepositHoldAmountCent: 1000,
|
||||
DepositHoldReason: "风控复核",
|
||||
DepositHeldAt: &now,
|
||||
DepositHoldReleasedAt: &now,
|
||||
},
|
||||
}
|
||||
|
||||
dto := row.toDTOForUser(20)
|
||||
if dto.DepositHoldStatus != "" {
|
||||
t.Fatalf("deposit hold status = %q, want empty", dto.DepositHoldStatus)
|
||||
}
|
||||
if dto.DepositHoldAmountCent != 0 {
|
||||
t.Fatalf("deposit hold amount = %d, want 0", dto.DepositHoldAmountCent)
|
||||
}
|
||||
if dto.DepositHoldReason != "" {
|
||||
t.Fatalf("deposit hold reason = %q, want empty", dto.DepositHoldReason)
|
||||
}
|
||||
if dto.DepositHeldAt != nil {
|
||||
t.Fatalf("deposit held at = %v, want nil", dto.DepositHeldAt)
|
||||
}
|
||||
if dto.DepositHoldReleasedAt != nil {
|
||||
t.Fatalf("deposit hold released at = %v, want nil", dto.DepositHoldReleasedAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserOrderListItemDoesNotSerializeDetailFields(t *testing.T) {
|
||||
row := orderRow{
|
||||
RentalOrder: model.RentalOrder{
|
||||
ID: 1,
|
||||
OrderNo: "ORD-001",
|
||||
ListingID: 2,
|
||||
OwnerID: 10,
|
||||
RenterID: 20,
|
||||
RentAmountCent: 1000,
|
||||
DepositAmountCent: 2000,
|
||||
DepositHoldReason: "风控复核",
|
||||
DepositFreeManualQuotaCent: 5000,
|
||||
Status: orderStatusPendingPayment,
|
||||
},
|
||||
ListingNo: "SP000002",
|
||||
Title: "测试账号",
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(row.toUserListItem(20, nil))
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal() error = %v", err)
|
||||
}
|
||||
text := string(raw)
|
||||
for _, field := range []string{"account_snapshot", "deposit_hold_reason", "deposit_free_manual_quota_cent", "owner_id", "renter_id"} {
|
||||
if strings.Contains(text, field) {
|
||||
t.Fatalf("list response contains sensitive field %q: %s", field, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserOrderListItemIncludesRentalTerms(t *testing.T) {
|
||||
row := orderRow{
|
||||
RentalOrder: model.RentalOrder{
|
||||
AccountSnapshot: datatypes.JSON([]byte(`{"haf_coin_amount":100000000,"asset_summary":{"daily_loss_m":10}}`)),
|
||||
},
|
||||
Title: "微信 三角洲巅峰 100M哈夫币",
|
||||
}
|
||||
|
||||
item := row.toUserListItem(20, nil)
|
||||
if item.RentalDays != 10 {
|
||||
t.Fatalf("rental days = %d, want 10", item.RentalDays)
|
||||
}
|
||||
if item.DailyLossM != 10 {
|
||||
t.Fatalf("daily loss = %v, want 10", item.DailyLossM)
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(item)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal() error = %v", err)
|
||||
}
|
||||
text := string(raw)
|
||||
if !strings.Contains(text, `"rental_days":10`) || !strings.Contains(text, `"daily_loss_m":10`) {
|
||||
t.Fatalf("list response missing rental terms: %s", text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserOrderListItemOmitsRentalTermsWithoutSnapshot(t *testing.T) {
|
||||
item := orderRow{Title: "测试账号"}.toUserListItem(20, nil)
|
||||
raw, err := json.Marshal(item)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal() error = %v", err)
|
||||
}
|
||||
text := string(raw)
|
||||
if strings.Contains(text, "rental_days") || strings.Contains(text, "daily_loss_m") {
|
||||
t.Fatalf("empty snapshot should omit rental terms: %s", text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminOrderListItemShowsPhonesAndOmitsSnapshot(t *testing.T) {
|
||||
row := orderRow{
|
||||
RentalOrder: model.RentalOrder{ID: 1, OrderNo: "ORD-001", Status: orderStatusPendingPayment},
|
||||
OwnerPhone: "13800001234",
|
||||
RenterPhone: "13900005678",
|
||||
}
|
||||
raw, err := json.Marshal(row.toAdminListItem())
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal() error = %v", err)
|
||||
}
|
||||
text := string(raw)
|
||||
// 后台订单列表需要展示完整手机号,方便客服定位用户。
|
||||
for _, phone := range []string{"13800001234", "13900005678"} {
|
||||
if !strings.Contains(text, phone) {
|
||||
t.Fatalf("list response should contain full phone %q: %s", phone, text)
|
||||
}
|
||||
}
|
||||
if strings.Contains(text, "account_snapshot") {
|
||||
t.Fatalf("list response contains account snapshot: %s", text)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user