|
|
|
@@ -0,0 +1,106 @@ |
|
|
|
package controller |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"net/http" |
|
|
|
"net/http/httptest" |
|
|
|
"testing" |
|
|
|
|
|
|
|
"github.com/QuantumNous/new-api/common" |
|
|
|
"github.com/QuantumNous/new-api/i18n" |
|
|
|
"github.com/QuantumNous/new-api/model" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/glebarez/sqlite" |
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
"gorm.io/gorm" |
|
|
|
) |
|
|
|
|
|
|
|
func setupUserTopUpTestDB(t *testing.T) *gorm.DB { |
|
|
|
t.Helper() |
|
|
|
|
|
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) |
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
sqlDB, err := db.DB() |
|
|
|
require.NoError(t, err) |
|
|
|
sqlDB.SetMaxOpenConns(1) |
|
|
|
|
|
|
|
origDB := model.DB |
|
|
|
origLogDB := model.LOG_DB |
|
|
|
origUsingSQLite := common.UsingSQLite |
|
|
|
origRedisEnabled := common.RedisEnabled |
|
|
|
|
|
|
|
model.DB = db |
|
|
|
model.LOG_DB = db |
|
|
|
common.UsingSQLite = true |
|
|
|
common.RedisEnabled = false |
|
|
|
|
|
|
|
require.NoError(t, db.AutoMigrate(&model.User{}, &model.Redemption{}, &model.Log{})) |
|
|
|
|
|
|
|
t.Cleanup(func() { |
|
|
|
model.DB = origDB |
|
|
|
model.LOG_DB = origLogDB |
|
|
|
common.UsingSQLite = origUsingSQLite |
|
|
|
common.RedisEnabled = origRedisEnabled |
|
|
|
require.NoError(t, sqlDB.Close()) |
|
|
|
}) |
|
|
|
|
|
|
|
return db |
|
|
|
} |
|
|
|
|
|
|
|
func setupUserTopUpRouter(userID int) *gin.Engine { |
|
|
|
gin.SetMode(gin.TestMode) |
|
|
|
r := gin.New() |
|
|
|
r.POST("/api/user/topup", func(c *gin.Context) { |
|
|
|
c.Set("id", userID) |
|
|
|
c.Next() |
|
|
|
}, TopUp) |
|
|
|
return r |
|
|
|
} |
|
|
|
|
|
|
|
func TestTopUp_SyncedUserDenied(t *testing.T) { |
|
|
|
db := setupUserTopUpTestDB(t) |
|
|
|
i18n.Init() |
|
|
|
|
|
|
|
user := &model.User{ |
|
|
|
Id: 100, |
|
|
|
Username: "synced-topup-test", |
|
|
|
Password: "hashed_password", |
|
|
|
Quota: 100000, |
|
|
|
Source: common.UserSourceSynced, |
|
|
|
} |
|
|
|
require.NoError(t, db.Create(user).Error) |
|
|
|
|
|
|
|
redemption := &model.Redemption{ |
|
|
|
Id: 1, |
|
|
|
UserId: 1, |
|
|
|
Key: "synced-topup-key", |
|
|
|
Status: common.RedemptionCodeStatusEnabled, |
|
|
|
Quota: 50000, |
|
|
|
CreatedTime: common.GetTimestamp(), |
|
|
|
} |
|
|
|
require.NoError(t, db.Create(redemption).Error) |
|
|
|
|
|
|
|
router := setupUserTopUpRouter(100) |
|
|
|
w := httptest.NewRecorder() |
|
|
|
reqBody, err := common.Marshal(map[string]string{"key": "synced-topup-key"}) |
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, "/api/user/topup", bytes.NewReader(reqBody)) |
|
|
|
require.NoError(t, err) |
|
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
|
req.Header.Set("Accept-Language", "zh-CN") |
|
|
|
router.ServeHTTP(w, req) |
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code) |
|
|
|
|
|
|
|
var resp map[string]interface{} |
|
|
|
require.NoError(t, common.Unmarshal(w.Body.Bytes(), &resp)) |
|
|
|
assert.False(t, resp["success"].(bool)) |
|
|
|
assert.Equal(t, "同步用户不允许使用兑换码", resp["message"]) |
|
|
|
|
|
|
|
var updatedUser model.User |
|
|
|
require.NoError(t, db.First(&updatedUser, 100).Error) |
|
|
|
assert.Equal(t, 100000, updatedUser.Quota) |
|
|
|
} |