162 lines
5.0 KiB
Go
162 lines
5.0 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/service/rechargeagency"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type rechargeAgencyRouteAuth struct{}
|
|
|
|
func (rechargeAgencyRouteAuth) AuthenticateToken(context.Context, string) (integration.UserCredential, error) {
|
|
return integration.UserCredential{
|
|
UserID: integration.Int64Value(7),
|
|
SysOrigin: "LIKEI",
|
|
}, nil
|
|
}
|
|
|
|
func (rechargeAgencyRouteAuth) AuthenticateConsoleToken(context.Context, string) (integration.ConsoleAccount, error) {
|
|
return integration.ConsoleAccount{}, nil
|
|
}
|
|
|
|
type rechargeAgencyRouteJava struct {
|
|
authorization string
|
|
sysOrigin string
|
|
}
|
|
|
|
func (f *rechargeAgencyRouteJava) PageFreightSellers(_ context.Context, authorization string, sysOrigin string, cursor int, limit int) (integration.FreightSellerPage, error) {
|
|
f.authorization = authorization
|
|
f.sysOrigin = sysOrigin
|
|
return integration.FreightSellerPage{
|
|
Total: 1,
|
|
Current: cursor,
|
|
Size: limit,
|
|
Records: []integration.FreightSellerRecord{
|
|
{
|
|
ID: integration.Int64Value(1),
|
|
SysOrigin: sysOrigin,
|
|
UserID: integration.Int64Value(99),
|
|
Balance: integration.DecimalString("10"),
|
|
UserBaseInfo: integration.FreightSellerProfile{
|
|
Account: "u99",
|
|
UserNickname: "Nina",
|
|
},
|
|
},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func TestRechargeAgencySellersRoute(t *testing.T) {
|
|
java := &rechargeAgencyRouteJava{}
|
|
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.RechargeAgencySellerInfo{}); err != nil {
|
|
t.Fatalf("auto migrate: %v", err)
|
|
}
|
|
if err := db.Create(&model.RechargeAgencySellerInfo{
|
|
SysOrigin: "LIKEI",
|
|
UserID: 99,
|
|
Whatsapp: "+8613800138000",
|
|
}).Error; err != nil {
|
|
t.Fatalf("seed whatsapp: %v", err)
|
|
}
|
|
|
|
service := rechargeagency.NewService(config.Config{}, db, java)
|
|
engine := NewRouter(config.Config{}, nil, rechargeAgencyRouteAuth{}, Services{
|
|
RechargeAgency: service,
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/app/h5/recharge-agency-list/sellers?cursor=2&limit=1", 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())
|
|
}
|
|
if java.authorization != "Bearer token" || java.sysOrigin != "LIKEI" {
|
|
t.Fatalf("gateway authorization/sysOrigin = %q/%q", java.authorization, java.sysOrigin)
|
|
}
|
|
|
|
var payload struct {
|
|
Body struct {
|
|
Total int64 `json:"total"`
|
|
Records []struct {
|
|
UserID string `json:"userId"`
|
|
UserNickname string `json:"userNickname"`
|
|
Whatsapp string `json:"whatsapp"`
|
|
} `json:"records"`
|
|
} `json:"body"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
if payload.Body.Total != 1 || len(payload.Body.Records) != 1 {
|
|
t.Fatalf("payload body mismatch: %+v", payload.Body)
|
|
}
|
|
if payload.Body.Records[0].UserID != "99" || payload.Body.Records[0].UserNickname != "Nina" {
|
|
t.Fatalf("record mismatch: %+v", payload.Body.Records[0])
|
|
}
|
|
if payload.Body.Records[0].Whatsapp != "+8613800138000" {
|
|
t.Fatalf("whatsapp mismatch: %+v", payload.Body.Records[0])
|
|
}
|
|
}
|
|
|
|
func TestRechargeAgencyProfileRoutes(t *testing.T) {
|
|
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.RechargeAgencySellerInfo{}); err != nil {
|
|
t.Fatalf("auto migrate: %v", err)
|
|
}
|
|
service := rechargeagency.NewService(config.Config{}, db, nil)
|
|
engine := NewRouter(config.Config{}, nil, rechargeAgencyRouteAuth{}, Services{
|
|
RechargeAgency: service,
|
|
})
|
|
|
|
postReq := httptest.NewRequest(http.MethodPost, "/app/h5/recharge-agency/profile/whatsapp", bytes.NewBufferString(`{"whatsapp":"+8613800138000"}`))
|
|
postReq.Header.Set("Authorization", "Bearer token")
|
|
postReq.Header.Set("Content-Type", "application/json")
|
|
postRec := httptest.NewRecorder()
|
|
engine.ServeHTTP(postRec, postReq)
|
|
if postRec.Code != http.StatusOK {
|
|
t.Fatalf("post status = %d body = %s", postRec.Code, postRec.Body.String())
|
|
}
|
|
|
|
getReq := httptest.NewRequest(http.MethodGet, "/app/h5/recharge-agency/profile", nil)
|
|
getReq.Header.Set("Authorization", "Bearer token")
|
|
getRec := httptest.NewRecorder()
|
|
engine.ServeHTTP(getRec, getReq)
|
|
if getRec.Code != http.StatusOK {
|
|
t.Fatalf("get status = %d body = %s", getRec.Code, getRec.Body.String())
|
|
}
|
|
|
|
var payload struct {
|
|
Body struct {
|
|
UserID string `json:"userId"`
|
|
Whatsapp string `json:"whatsapp"`
|
|
} `json:"body"`
|
|
}
|
|
if err := json.Unmarshal(getRec.Body.Bytes(), &payload); err != nil {
|
|
t.Fatalf("decode get response: %v", err)
|
|
}
|
|
if payload.Body.UserID != "7" || payload.Body.Whatsapp != "+8613800138000" {
|
|
t.Fatalf("profile body mismatch: %+v", payload.Body)
|
|
}
|
|
}
|