payment_method_guard_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package model
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func insertUserForPaymentGuardTest(t *testing.T, id int, quota int) {
  10. t.Helper()
  11. user := &User{
  12. Id: id,
  13. Username: "payment_guard_user",
  14. Status: common.UserStatusEnabled,
  15. Quota: quota,
  16. }
  17. require.NoError(t, DB.Create(user).Error)
  18. }
  19. func insertSubscriptionPlanForPaymentGuardTest(t *testing.T, id int) *SubscriptionPlan {
  20. t.Helper()
  21. plan := &SubscriptionPlan{
  22. Id: id,
  23. Title: "Guard Plan",
  24. PriceAmount: 9.99,
  25. Currency: "USD",
  26. DurationUnit: SubscriptionDurationMonth,
  27. DurationValue: 1,
  28. Enabled: true,
  29. TotalAmount: 1000,
  30. }
  31. require.NoError(t, DB.Create(plan).Error)
  32. return plan
  33. }
  34. func insertSubscriptionOrderForPaymentGuardTest(t *testing.T, tradeNo string, userID int, planID int, paymentMethod string) {
  35. t.Helper()
  36. order := &SubscriptionOrder{
  37. UserId: userID,
  38. PlanId: planID,
  39. Money: 9.99,
  40. TradeNo: tradeNo,
  41. PaymentMethod: paymentMethod,
  42. Status: common.TopUpStatusPending,
  43. CreateTime: time.Now().Unix(),
  44. }
  45. require.NoError(t, order.Insert())
  46. }
  47. func insertTopUpForPaymentGuardTest(t *testing.T, tradeNo string, userID int, paymentMethod string) {
  48. t.Helper()
  49. topUp := &TopUp{
  50. UserId: userID,
  51. Amount: 2,
  52. Money: 9.99,
  53. TradeNo: tradeNo,
  54. PaymentMethod: paymentMethod,
  55. Status: common.TopUpStatusPending,
  56. CreateTime: time.Now().Unix(),
  57. }
  58. require.NoError(t, topUp.Insert())
  59. }
  60. func getTopUpStatusForPaymentGuardTest(t *testing.T, tradeNo string) string {
  61. t.Helper()
  62. topUp := GetTopUpByTradeNo(tradeNo)
  63. require.NotNil(t, topUp)
  64. return topUp.Status
  65. }
  66. func countUserSubscriptionsForPaymentGuardTest(t *testing.T, userID int) int64 {
  67. t.Helper()
  68. var count int64
  69. require.NoError(t, DB.Model(&UserSubscription{}).Where("user_id = ?", userID).Count(&count).Error)
  70. return count
  71. }
  72. func getUserQuotaForPaymentGuardTest(t *testing.T, userID int) int {
  73. t.Helper()
  74. var user User
  75. require.NoError(t, DB.Select("quota").Where("id = ?", userID).First(&user).Error)
  76. return user.Quota
  77. }
  78. func TestRechargeWaffoPancake_RejectsMismatchedPaymentMethod(t *testing.T) {
  79. truncateTables(t)
  80. insertUserForPaymentGuardTest(t, 101, 0)
  81. insertTopUpForPaymentGuardTest(t, "waffo-pancake-guard", 101, PaymentMethodStripe)
  82. err := RechargeWaffoPancake("waffo-pancake-guard")
  83. require.Error(t, err)
  84. topUp := GetTopUpByTradeNo("waffo-pancake-guard")
  85. require.NotNil(t, topUp)
  86. assert.Equal(t, common.TopUpStatusPending, topUp.Status)
  87. assert.Equal(t, 0, getUserQuotaForPaymentGuardTest(t, 101))
  88. }
  89. func TestUpdatePendingTopUpStatus_RejectsMismatchedPaymentMethod(t *testing.T) {
  90. testCases := []struct {
  91. name string
  92. tradeNo string
  93. storedPaymentMethod string
  94. expectedPaymentMethod string
  95. targetStatus string
  96. }{
  97. {
  98. name: "stripe expire",
  99. tradeNo: "stripe-expire-guard",
  100. storedPaymentMethod: PaymentMethodCreem,
  101. expectedPaymentMethod: PaymentMethodStripe,
  102. targetStatus: common.TopUpStatusExpired,
  103. },
  104. {
  105. name: "waffo failed",
  106. tradeNo: "waffo-failed-guard",
  107. storedPaymentMethod: PaymentMethodStripe,
  108. expectedPaymentMethod: PaymentMethodWaffo,
  109. targetStatus: common.TopUpStatusFailed,
  110. },
  111. }
  112. for _, tc := range testCases {
  113. t.Run(tc.name, func(t *testing.T) {
  114. truncateTables(t)
  115. insertUserForPaymentGuardTest(t, 150, 0)
  116. insertTopUpForPaymentGuardTest(t, tc.tradeNo, 150, tc.storedPaymentMethod)
  117. err := UpdatePendingTopUpStatus(tc.tradeNo, tc.expectedPaymentMethod, tc.targetStatus)
  118. require.ErrorIs(t, err, ErrPaymentMethodMismatch)
  119. assert.Equal(t, common.TopUpStatusPending, getTopUpStatusForPaymentGuardTest(t, tc.tradeNo))
  120. })
  121. }
  122. }
  123. func TestCompleteSubscriptionOrder_RejectsMismatchedPaymentMethod(t *testing.T) {
  124. truncateTables(t)
  125. insertUserForPaymentGuardTest(t, 202, 0)
  126. plan := insertSubscriptionPlanForPaymentGuardTest(t, 301)
  127. insertSubscriptionOrderForPaymentGuardTest(t, "sub-guard-order", 202, plan.Id, PaymentMethodStripe)
  128. err := CompleteSubscriptionOrder("sub-guard-order", `{"provider":"epay"}`, "alipay")
  129. require.ErrorIs(t, err, ErrPaymentMethodMismatch)
  130. order := GetSubscriptionOrderByTradeNo("sub-guard-order")
  131. require.NotNil(t, order)
  132. assert.Equal(t, common.TopUpStatusPending, order.Status)
  133. assert.Zero(t, countUserSubscriptionsForPaymentGuardTest(t, 202))
  134. topUp := GetTopUpByTradeNo("sub-guard-order")
  135. assert.Nil(t, topUp)
  136. }
  137. func TestExpireSubscriptionOrder_RejectsMismatchedPaymentMethod(t *testing.T) {
  138. truncateTables(t)
  139. insertUserForPaymentGuardTest(t, 303, 0)
  140. plan := insertSubscriptionPlanForPaymentGuardTest(t, 401)
  141. insertSubscriptionOrderForPaymentGuardTest(t, "sub-expire-guard", 303, plan.Id, PaymentMethodStripe)
  142. err := ExpireSubscriptionOrder("sub-expire-guard", PaymentMethodCreem)
  143. require.ErrorIs(t, err, ErrPaymentMethodMismatch)
  144. order := GetSubscriptionOrderByTradeNo("sub-expire-guard")
  145. require.NotNil(t, order)
  146. assert.Equal(t, common.TopUpStatusPending, order.Status)
  147. }