mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-08-31 02:41:34 +00:00
Merge d286642385 into 2b6f1dfefb
This commit is contained in:
@@ -770,8 +770,8 @@ func PurchaseSubscriptionWithBalance(userId int, planId int) error {
|
||||
if !plan.Enabled {
|
||||
return errors.New("套餐未启用")
|
||||
}
|
||||
if plan.PriceAmount < 0 {
|
||||
return errors.New("套餐价格不能为负数")
|
||||
if plan.PriceAmount <= 0 {
|
||||
return errors.New("余额兑换套餐价格必须大于 0")
|
||||
}
|
||||
if plan.AllowBalancePay != nil && !*plan.AllowBalancePay {
|
||||
return errors.New("该套餐不允许使用余额兑换")
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPurchaseSubscriptionWithBalanceRejectsZeroPricePlan(t *testing.T) {
|
||||
truncateTables(t)
|
||||
|
||||
user := &User{
|
||||
Username: "zero-price-user",
|
||||
Password: "password",
|
||||
Role: common.RoleCommonUser,
|
||||
Status: common.UserStatusEnabled,
|
||||
Group: "default",
|
||||
Quota: 1000,
|
||||
}
|
||||
require.NoError(t, DB.Create(user).Error)
|
||||
|
||||
plan := &SubscriptionPlan{
|
||||
Title: "Zero price plan",
|
||||
PriceAmount: 0,
|
||||
Currency: "USD",
|
||||
DurationUnit: "month",
|
||||
DurationValue: 1,
|
||||
Enabled: true,
|
||||
UpgradeGroup: "claude",
|
||||
TotalAmount: 1000,
|
||||
}
|
||||
require.NoError(t, DB.Create(plan).Error)
|
||||
|
||||
err := PurchaseSubscriptionWithBalance(user.Id, plan.Id)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "必须大于 0")
|
||||
|
||||
var subCount int64
|
||||
require.NoError(t, DB.Model(&UserSubscription{}).Where("user_id = ?", user.Id).Count(&subCount).Error)
|
||||
assert.EqualValues(t, 0, subCount)
|
||||
|
||||
var orderCount int64
|
||||
require.NoError(t, DB.Model(&SubscriptionOrder{}).Where("user_id = ?", user.Id).Count(&orderCount).Error)
|
||||
assert.EqualValues(t, 0, orderCount)
|
||||
|
||||
var reloaded User
|
||||
require.NoError(t, DB.First(&reloaded, user.Id).Error)
|
||||
assert.Equal(t, "default", reloaded.Group)
|
||||
assert.Equal(t, 1000, reloaded.Quota)
|
||||
}
|
||||
Reference in New Issue
Block a user