212 lines
8.9 KiB
Go
212 lines
8.9 KiB
Go
// channel_day35_test.go Day 35:通道路由——CRUD/测试工具/拦截(channel:write 门禁)。
|
|
package v1
|
|
|
|
import (
|
|
"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/pkg/casbin"
|
|
"go-sms/internal/service/account"
|
|
"go-sms/internal/service/audit"
|
|
"go-sms/internal/service/channel"
|
|
"go-sms/internal/service/template"
|
|
)
|
|
|
|
// newD35Router 双账号(admin 通配/customer)+ 通道服务。
|
|
func newD35Router(t *testing.T) *gin.Engine {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sqlDB, e := db.DB(); e == nil {
|
|
sqlDB.SetMaxOpenConns(1)
|
|
}
|
|
if err := db.AutoMigrate(&entity.AccountUser{}, &entity.AccountConfig{}, &entity.AccountBalanceLog{},
|
|
&entity.SystemAlertRule{}, &entity.SystemOperateLog{}, &entity.SystemLoginLog{},
|
|
&entity.ChannelChannel{}, &entity.ChannelGroup{}, &entity.ChannelGroupItem{}, &entity.ChannelLog{},
|
|
&entity.TemplateSMS{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e, err := casbin.New(db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, p := range [][]string{{"admin", "*", "*"}, {"customer", "message", "write"}} {
|
|
if _, err := e.AddPolicy(p[0], p[1], p[2]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
rows := []entity.AccountUser{
|
|
{Username: "d35-admin", Role: "admin", Status: 1},
|
|
{Username: "d35-cust", Role: "customer", Status: 1},
|
|
}
|
|
for i := range rows {
|
|
if err := db.Create(&rows[i]).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := e.BindRole(rows[i].ID, rows[i].Role); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
acctSvc := account.NewService(db, account.BcryptMinCost).WithEnforcer(e)
|
|
r := gin.New()
|
|
Register(r.Group("/api/v1"), Deps{
|
|
Account: acctSvc,
|
|
Warning: account.NewWarningService(db, nil),
|
|
Audit: audit.NewService(db),
|
|
Channel: channel.NewService(db),
|
|
Template: template.NewService(db), // Day 41:模板/预估(customer 角色有 message:write 策略)
|
|
Auth: middleware.AuthJWTWithChecker(d25Secret, acctSvc.CheckEnabled),
|
|
Perm: middleware.CasbinChecker(e),
|
|
})
|
|
return r
|
|
}
|
|
|
|
// TestAPI_ChannelCRUDAndTest admin 全通 / customer 403 / 测试工具返回 MsgID / 拦截配置。
|
|
func TestAPI_ChannelCRUDAndTest(t *testing.T) {
|
|
r := newD35Router(t)
|
|
adminTok := d25Token(t, 1, "admin")
|
|
custTok := d25Token(t, 2, "customer")
|
|
|
|
body := []byte(`{"name":"测试通道","code":"api-ch-1","protocol":"http","host":"http://nonexistent-upstream"}`)
|
|
// customer 创建 → 403
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channels", custTok, "application/json", body); w.Code != http.StatusForbidden {
|
|
t.Fatalf("customer 创建通道 = %d, 期望 403", w.Code)
|
|
}
|
|
// admin 创建 → 200
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channels", adminTok, "application/json", body); w.Code != http.StatusOK {
|
|
t.Fatalf("admin 创建 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
// 重复 code → 400
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channels", adminTok, "application/json", body); w.Code != http.StatusBadRequest {
|
|
t.Fatalf("重复 code = %d", w.Code)
|
|
}
|
|
// 列表
|
|
if w := doReq(r, http.MethodGet, "/api/v1/channels?protocol=http&status=1", adminTok); w.Code != http.StatusOK || !contains(w.Body.String(), `"total":1`) {
|
|
t.Fatalf("列表 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
// 详情
|
|
if w := doReq(r, http.MethodGet, "/api/v1/channels/1", adminTok); w.Code != http.StatusOK || !contains(w.Body.String(), "api-ch-1") {
|
|
t.Fatalf("详情 = %d", w.Code)
|
|
}
|
|
// 拦截配置:set + get
|
|
ic := []byte(`{"night_limit":1,"day_limit":100,"block_regions":"新疆"}`)
|
|
if w := doReqBody(r, http.MethodPut, "/api/v1/channels/1/intercept", adminTok, "application/json", ic); w.Code != http.StatusOK {
|
|
t.Fatalf("拦截配置 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
if w := doReq(r, http.MethodGet, "/api/v1/channels/1/intercept", adminTok); w.Code != http.StatusOK || !contains(w.Body.String(), "新疆") {
|
|
t.Fatalf("拦截查询 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
// 测试工具(上游不可达 → 错误但非 panic;停用后 400)
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channels/1/test", adminTok, "application/json",
|
|
[]byte(`{"mobile":"13800001111","content":"t"}`)); w.Code != http.StatusOK {
|
|
// 不可达上游返回 500/错误——此处验证链路与参数校验;不视为失败
|
|
t.Logf("测试工具(不可达上游)= %d(预期错误)", w.Code)
|
|
}
|
|
// 缺参 → 400
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channels/1/test", adminTok, "application/json", []byte(`{}`)); w.Code != http.StatusBadRequest {
|
|
t.Fatalf("缺参 = %d", w.Code)
|
|
}
|
|
// 删除守卫:启用中拒删
|
|
if w := doReq(r, http.MethodDelete, "/api/v1/channels/1", adminTok); w.Code != http.StatusBadRequest {
|
|
t.Fatalf("启用删除 = %d", w.Code)
|
|
}
|
|
}
|
|
|
|
// TestAPI_ChannelGroupAndItems 通道组 CRUD/成员管理/权限/守卫。
|
|
func TestAPI_ChannelGroupAndItems(t *testing.T) {
|
|
r := newD35Router(t)
|
|
adminTok := d25Token(t, 1, "admin")
|
|
custTok := d25Token(t, 2, "customer")
|
|
|
|
// customer 建组 → 403
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channel-groups", custTok, "application/json", []byte(`{"name":"g"}`)); w.Code != http.StatusForbidden {
|
|
t.Fatalf("customer 建组 = %d", w.Code)
|
|
}
|
|
// admin 建组 → 200
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channel-groups", adminTok, "application/json", []byte(`{"name":"主路由组"}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("建组 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
// 建两通道 + 成员(权重 7/3)
|
|
for _, body := range []string{
|
|
`{"name":"甲","code":"ga","protocol":"http","host":"http://a"}`,
|
|
`{"name":"乙","code":"gb","protocol":"http","host":"http://b"}`,
|
|
} {
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channels", adminTok, "application/json", []byte(body)); w.Code != http.StatusOK {
|
|
t.Fatalf("建通道 = %d", w.Code)
|
|
}
|
|
}
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channel-groups/1/items", adminTok, "application/json",
|
|
[]byte(`{"channel_id":1,"step":3,"weight":7}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("加成员 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
// 非法 step/权重 → 400
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channel-groups/1/items", adminTok, "application/json",
|
|
[]byte(`{"channel_id":2,"step":3,"weight":0}`)); w.Code != http.StatusBadRequest {
|
|
t.Fatalf("weight=0 = %d", w.Code)
|
|
}
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channel-groups/1/items", adminTok, "application/json",
|
|
[]byte(`{"channel_id":2,"step":9}`)); w.Code != http.StatusBadRequest {
|
|
t.Fatalf("step=9 = %d", w.Code)
|
|
}
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channel-groups/1/items", adminTok, "application/json",
|
|
[]byte(`{"channel_id":2,"step":3,"weight":3}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("乙成员 = %d", w.Code)
|
|
}
|
|
// 组详情含成员(权重 7/3 可见)
|
|
if w := doReq(r, http.MethodGet, "/api/v1/channel-groups/1", adminTok); w.Code != http.StatusOK ||
|
|
!contains(w.Body.String(), `"weight":7`) || !contains(w.Body.String(), `"weight":3`) {
|
|
t.Fatalf("组详情 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
// 通道删除守卫:被组引用拒删
|
|
_ = doReqBody(r, http.MethodPut, "/api/v1/channels/1/status", adminTok, "application/json", []byte(`{"status":0}`))
|
|
if w := doReq(r, http.MethodDelete, "/api/v1/channels/1", adminTok); w.Code != http.StatusBadRequest || !contains(w.Body.String(), "引用") {
|
|
t.Fatalf("引用守卫 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
// 移除成员后组可删
|
|
if w := doReq(r, http.MethodDelete, "/api/v1/channel-groups/1/items/1", adminTok); w.Code != http.StatusOK {
|
|
t.Fatalf("移除成员 = %d", w.Code)
|
|
}
|
|
if w := doReq(r, http.MethodDelete, "/api/v1/channel-groups/1/items/2", adminTok); w.Code != http.StatusOK {
|
|
t.Fatalf("移除成员2 = %d", w.Code)
|
|
}
|
|
if w := doReq(r, http.MethodDelete, "/api/v1/channel-groups/1", adminTok); w.Code != http.StatusOK {
|
|
t.Fatalf("删组 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
// newUpstream35 可达上游(httpadapter 契约)。
|
|
func newUpstream35(t *testing.T) *httptest.Server {
|
|
t.Helper()
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"code":0,"msg":"ok","data":{"message_id":"up-35"}}`))
|
|
}))
|
|
}
|
|
|
|
// TestAPI_ChannelTestWithUpstream http 通道 + 可达上游:测试工具返回 MsgID(闭环)。
|
|
func TestAPI_ChannelTestWithUpstream(t *testing.T) {
|
|
r := newD35Router(t)
|
|
adminTok := d25Token(t, 1, "admin")
|
|
up := newUpstream35(t)
|
|
defer up.Close()
|
|
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/channels", adminTok, "application/json",
|
|
[]byte(`{"name":"ok","code":"api-ch-2","protocol":"http","host":"`+up.URL+`"}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("创建 = %d", w.Code)
|
|
}
|
|
w := doReqBody(r, http.MethodPost, "/api/v1/channels/1/test", adminTok, "application/json",
|
|
[]byte(`{"mobile":"13800001111","content":"验证"}`))
|
|
if w.Code != http.StatusOK || !contains(w.Body.String(), "up-35") {
|
|
t.Fatalf("测试工具 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|