96 lines
2.8 KiB
Go
96 lines
2.8 KiB
Go
package router
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"chatapp3-golang/internal/config"
|
|
"chatapp3-golang/internal/integration"
|
|
"chatapp3-golang/internal/model"
|
|
"chatapp3-golang/internal/repo"
|
|
"chatapp3-golang/internal/service/propsstore"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestPropsStoreAdminRoutesSaveAndMap(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
db := newRouterPropsStoreDB(t)
|
|
service := propsstore.NewService(db, nil)
|
|
engine := NewRouter(config.Config{}, &repo.Repository{}, propsStoreAuthStub{}, Services{
|
|
PropsStore: service,
|
|
})
|
|
|
|
saveBody := []byte(`{
|
|
"sysOrigin": "LIKEI",
|
|
"sourceId": "100",
|
|
"propsType": "AVATAR_FRAME",
|
|
"currencyTypes": "GOLD",
|
|
"validDays": "7",
|
|
"discount": 0,
|
|
"shelfStatus": true,
|
|
"sort": 0,
|
|
"label": ""
|
|
}`)
|
|
saveReq := httptest.NewRequest(http.MethodPost, "/props/store/add-or-update", bytes.NewReader(saveBody))
|
|
saveReq.Header.Set("Authorization", "Bearer test-token")
|
|
saveReq.Header.Set("Content-Type", "application/json")
|
|
saveResp := httptest.NewRecorder()
|
|
engine.ServeHTTP(saveResp, saveReq)
|
|
if saveResp.Code != http.StatusOK {
|
|
t.Fatalf("save status = %d, body = %s", saveResp.Code, saveResp.Body.String())
|
|
}
|
|
|
|
mapReq := httptest.NewRequest(http.MethodPost, "/props/store/source-map", bytes.NewReader([]byte(`{
|
|
"sysOrigin": "LIKEI",
|
|
"sourceIds": ["100"]
|
|
}`)))
|
|
mapReq.Header.Set("Authorization", "Bearer test-token")
|
|
mapReq.Header.Set("Content-Type", "application/json")
|
|
mapResp := httptest.NewRecorder()
|
|
engine.ServeHTTP(mapResp, mapReq)
|
|
if mapResp.Code != http.StatusOK {
|
|
t.Fatalf("map status = %d, body = %s", mapResp.Code, mapResp.Body.String())
|
|
}
|
|
|
|
var decoded struct {
|
|
ErrorCode int `json:"errorCode"`
|
|
Body map[string]propsstore.CommodityView `json:"body"`
|
|
}
|
|
if err := json.Unmarshal(mapResp.Body.Bytes(), &decoded); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
item, ok := decoded.Body["100"]
|
|
if decoded.ErrorCode != 0 || !ok || item.ShelfStatus == nil || !*item.ShelfStatus {
|
|
t.Fatalf("unexpected source-map body: %#v", decoded)
|
|
}
|
|
}
|
|
|
|
type propsStoreAuthStub struct{}
|
|
|
|
func (propsStoreAuthStub) AuthenticateToken(context.Context, string) (integration.UserCredential, error) {
|
|
return integration.UserCredential{}, nil
|
|
}
|
|
|
|
func (propsStoreAuthStub) AuthenticateConsoleToken(context.Context, string) (integration.ConsoleAccount, error) {
|
|
return integration.ConsoleAccount{LoginName: "admin"}, nil
|
|
}
|
|
|
|
func newRouterPropsStoreDB(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.PropsCommodityStore{}); err != nil {
|
|
t.Fatalf("auto migrate: %v", err)
|
|
}
|
|
return db
|
|
}
|