163 lines
5.6 KiB
Go
163 lines
5.6 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"go-sms/internal/middleware"
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/service/router"
|
|
)
|
|
|
|
// getJSON/putJSON 通用 HTTP 辅助(与 postJSON 同构)。
|
|
func getJSON(r *gin.Engine, path string) *httptest.ResponseRecorder {
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
|
|
return w
|
|
}
|
|
|
|
func putJSON(r *gin.Engine, path string, body any) *httptest.ResponseRecorder {
|
|
raw, _ := json.Marshal(body)
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPut, path, bytes.NewReader(raw))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
return w
|
|
}
|
|
|
|
// newSteerTestRouter 内存库+导流路由(/accounts/:id/channel-steer)。
|
|
// 前置注入 claims 模拟已认证(PUT 门禁 RequirePermission 依赖 ContextKeyClaims)。
|
|
func newSteerTestRouter(t *testing.T) (*gin.Engine, *gorm.DB) {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.AutoMigrate(&entity.RouteRule{}, &entity.ChannelChannel{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := gin.New()
|
|
r.Use(func(c *gin.Context) {
|
|
c.Set(middleware.ContextKeyClaims, &middleware.Claims{AccountID: 1, Username: "tester"})
|
|
c.Next()
|
|
})
|
|
Register(r.Group("/api/v1"), Deps{Router: router.NewRouter(db)})
|
|
return r, db
|
|
}
|
|
|
|
// seedSteerChannel 建一条通道(status 可控;零值会被列 default 吞,UPDATE 强制写)。
|
|
func seedSteerChannel(t *testing.T, db *gorm.DB, id int64, name string, status int8) {
|
|
t.Helper()
|
|
ch := &entity.ChannelChannel{
|
|
Model: entity.Model{ID: id}, Name: name, Code: "1069" + name,
|
|
Protocol: "cmpp", Host: "127.0.0.1", Port: 7890, Status: 1,
|
|
}
|
|
if err := db.Create(ch).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if status != 1 {
|
|
if err := db.Model(ch).Update("status", status).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// steerData 解析成功响应 data 段(非 ISC:000 即 Fatal,可传 wantFail 反转)。
|
|
func steerData(t *testing.T, w *httptest.ResponseRecorder) map[string]any {
|
|
t.Helper()
|
|
var resp struct {
|
|
Code string `json:"code"`
|
|
Data map[string]any `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("响应解析失败: %v body=%s", err, w.Body.String())
|
|
}
|
|
if resp.Code != "ISC:000" {
|
|
t.Fatalf("业务码=%s body=%s", resp.Code, w.Body.String())
|
|
}
|
|
return resp.Data
|
|
}
|
|
|
|
// steerCode 仅取业务码。
|
|
func steerCode(t *testing.T, w *httptest.ResponseRecorder) string {
|
|
t.Helper()
|
|
var resp struct {
|
|
Code string `json:"code"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("响应解析失败: %v body=%s", err, w.Body.String())
|
|
}
|
|
return resp.Code
|
|
}
|
|
|
|
// TestAPI_AccountChannelSteer 绑定→查询→原子切换(旧规则软删)→解绑全链。
|
|
func TestAPI_AccountChannelSteer(t *testing.T) {
|
|
r, db := newSteerTestRouter(t)
|
|
seedSteerChannel(t, db, 1, "ch-a", 1)
|
|
seedSteerChannel(t, db, 2, "ch-b", 1)
|
|
|
|
// 未绑定初态:rule_id=0(走四步智能路由)。
|
|
if d := steerData(t, getJSON(r, "/api/v1/accounts/3/channel-steer")); d["rule_id"].(float64) != 0 {
|
|
t.Fatalf("初态应未绑定,实际 %v", d["rule_id"])
|
|
}
|
|
|
|
// 绑定 ch-a:快照含通道名/状态。
|
|
steerData(t, putJSON(r, "/api/v1/accounts/3/channel-steer", map[string]any{"channel_id": 1}))
|
|
d := steerData(t, getJSON(r, "/api/v1/accounts/3/channel-steer"))
|
|
if d["channel_name"] != "ch-a" || d["channel_status"].(float64) != 1 {
|
|
t.Fatalf("绑定快照错误: %v", d)
|
|
}
|
|
|
|
// 切换 ch-b:新规则 ID 变化+库内启用规则恰 1 条(旧规则软删,原子)。
|
|
d = steerData(t, putJSON(r, "/api/v1/accounts/3/channel-steer", map[string]any{"channel_id": 2}))
|
|
rule2 := d["rule_id"].(float64)
|
|
var active int64
|
|
db.Model(&entity.RouteRule{}).Where("account_id=3 AND rule_type='account'").Count(&active)
|
|
if active != 1 || rule2 == 0 {
|
|
t.Fatalf("切换后启用规则应恰 1 条且新 ID 有效,实际 active=%d rule_id=%v", active, rule2)
|
|
}
|
|
if d = steerData(t, getJSON(r, "/api/v1/accounts/3/channel-steer")); d["channel_name"] != "ch-b" {
|
|
t.Fatalf("切换后快照应指向 ch-b: %v", d)
|
|
}
|
|
|
|
// 解绑:rule_id=0,启用规则清零。
|
|
steerData(t, putJSON(r, "/api/v1/accounts/3/channel-steer", map[string]any{"channel_id": 0}))
|
|
if d := steerData(t, getJSON(r, "/api/v1/accounts/3/channel-steer")); d["rule_id"].(float64) != 0 {
|
|
t.Fatal("解绑后应 rule_id=0")
|
|
}
|
|
db.Model(&entity.RouteRule{}).Where("account_id=3 AND rule_type='account'").Count(&active)
|
|
if active != 0 {
|
|
t.Fatalf("解绑后启用规则应为 0,实际 %d", active)
|
|
}
|
|
}
|
|
|
|
// TestAPI_AccountChannelSteer_Invalid 通道不存在/停用拒绝且不残留规则。
|
|
func TestAPI_AccountChannelSteer_Invalid(t *testing.T) {
|
|
r, db := newSteerTestRouter(t)
|
|
seedSteerChannel(t, db, 1, "ch-a", 0) // 停用
|
|
seedSteerChannel(t, db, 2, "ch-b", 1)
|
|
|
|
if c := steerCode(t, putJSON(r, "/api/v1/accounts/3/channel-steer", map[string]any{"channel_id": 999})); c == "ISC:000" {
|
|
t.Fatal("不存在通道应拒绝")
|
|
}
|
|
if c := steerCode(t, putJSON(r, "/api/v1/accounts/3/channel-steer", map[string]any{"channel_id": 1})); c == "ISC:000" {
|
|
t.Fatal("停用通道应拒绝")
|
|
}
|
|
if c := steerCode(t, putJSON(r, "/api/v1/accounts/3/channel-steer", map[string]any{"channel_id": -1})); c == "ISC:000" {
|
|
t.Fatal("负 channel_id 应拒绝")
|
|
}
|
|
var active int64
|
|
db.Model(&entity.RouteRule{}).Where("account_id=3 AND rule_type='account'").Count(&active)
|
|
if active != 0 {
|
|
t.Fatalf("失败请求不应建规则,实际 %d 条", active)
|
|
}
|
|
}
|