119 lines
3.5 KiB
Go
119 lines
3.5 KiB
Go
package router
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"chatapp3-golang/internal/config"
|
|
"chatapp3-golang/internal/model"
|
|
"chatapp3-golang/internal/repo"
|
|
wheelservice "chatapp3-golang/internal/service/wheel"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestWheelLegacyRewardsExposeDynamicResourceURL(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
db := newRouterWheelDB(t)
|
|
now := time.Now()
|
|
resourceID := int64(2045029471274201089)
|
|
resourceURL := "https://cdn.example.com/reward.svga"
|
|
coverURL := "https://cdn.example.com/reward.png"
|
|
|
|
if err := db.Create(&model.WheelConfig{
|
|
ID: 1,
|
|
SysOrigin: "LIKEI",
|
|
Enabled: true,
|
|
Timezone: "Asia/Shanghai",
|
|
CreateTime: now,
|
|
UpdateTime: now,
|
|
}).Error; err != nil {
|
|
t.Fatalf("create wheel config: %v", err)
|
|
}
|
|
if err := db.Create(&model.WheelPoolConfig{
|
|
ID: 10,
|
|
ConfigID: 1,
|
|
SysOrigin: "LIKEI",
|
|
Category: "CLASSIC",
|
|
Enabled: true,
|
|
PriceOneGold: 500,
|
|
Sort: 1,
|
|
CreateTime: now,
|
|
UpdateTime: now,
|
|
}).Error; err != nil {
|
|
t.Fatalf("create wheel pool: %v", err)
|
|
}
|
|
if err := db.Create(&model.WheelRewardConfig{
|
|
ID: 100,
|
|
ConfigID: 1,
|
|
SysOrigin: "LIKEI",
|
|
Category: "CLASSIC",
|
|
RewardType: "RESOURCE",
|
|
ResourceID: &resourceID,
|
|
ResourceType: "GIFT",
|
|
ResourceName: "Dynamic Gift",
|
|
ResourceURL: resourceURL,
|
|
CoverURL: coverURL,
|
|
DurationDays: 7,
|
|
DisplayGoldAmount: 1000,
|
|
Probability: 1000000,
|
|
Sort: 1,
|
|
Enabled: true,
|
|
CreateTime: now,
|
|
UpdateTime: now,
|
|
}).Error; err != nil {
|
|
t.Fatalf("create wheel reward: %v", err)
|
|
}
|
|
|
|
service := wheelservice.NewService(config.Config{}, db, nil)
|
|
engine := NewRouter(config.Config{}, &repo.Repository{}, appPopupAuthStub{}, Services{Wheel: service})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/client/api/v1/probability/lucky-box/rewards", nil)
|
|
req.Header.Set("Authorization", "Bearer token")
|
|
rec := httptest.NewRecorder()
|
|
engine.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body = %s", rec.Code, rec.Body.String())
|
|
}
|
|
var payload map[string]any
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
if payload["code"] != float64(0) {
|
|
t.Fatalf("code = %#v body = %s", payload["code"], rec.Body.String())
|
|
}
|
|
data, ok := payload["data"].([]any)
|
|
if !ok || len(data) == 0 {
|
|
t.Fatalf("data = %#v, want rewards", payload["data"])
|
|
}
|
|
item := data[0].(map[string]any)
|
|
if item["resourceUrl"] != resourceURL || item["animationUrl"] != resourceURL {
|
|
t.Fatalf("dynamic urls = resource:%#v animation:%#v", item["resourceUrl"], item["animationUrl"])
|
|
}
|
|
merchandise := item["merchandise"].(map[string]any)
|
|
if merchandise["resourceUrl"] != resourceURL || merchandise["animationUrl"] != resourceURL {
|
|
t.Fatalf("merchandise dynamic urls = resource:%#v animation:%#v", merchandise["resourceUrl"], merchandise["animationUrl"])
|
|
}
|
|
if merchandise["coverUrl"] != coverURL {
|
|
t.Fatalf("coverUrl = %#v, want %q", merchandise["coverUrl"], coverURL)
|
|
}
|
|
}
|
|
|
|
func newRouterWheelDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.WheelConfig{}, &model.WheelPoolConfig{}, &model.WheelRewardConfig{}); err != nil {
|
|
t.Fatalf("migrate wheel tables: %v", err)
|
|
}
|
|
return db
|
|
}
|