81 lines
3.7 KiB
Go
81 lines
3.7 KiB
Go
// template_lifecycle_test.go(原 Day41):模板 CRUD/审核状态机/发送预估端点(阶段 4 开篇)。
|
||
package v1
|
||
|
||
import (
|
||
"net/http"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// TestAPI_TemplateLifecycle 验收:创建待审→customer 可建(message:write)→审核通过→编辑回待审→拒审→删除。
|
||
func TestAPI_TemplateLifecycle(t *testing.T) {
|
||
r := newD35Router(t)
|
||
tok := d25Token(t, 2, "customer") // customer:message:write
|
||
adminTok := d25Token(t, 1, "admin")
|
||
|
||
// 创建(待审)
|
||
w := doReqBody(r, http.MethodPost, "/api/v1/templates", tok, "application/json",
|
||
[]byte(`{"account_id":2,"name":"验证码","content":"您的验证码{code},5分钟内有效"}`))
|
||
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `"status":0`) {
|
||
t.Fatalf("创建 = %d: %s", w.Code, w.Body.String())
|
||
}
|
||
// 审核(过)
|
||
if w := doReqBody(r, http.MethodPut, "/api/v1/templates/1/review", adminTok, "application/json",
|
||
[]byte(`{"pass":true}`)); w.Code != http.StatusOK {
|
||
t.Fatalf("审核 = %d: %s", w.Code, w.Body.String())
|
||
}
|
||
// 通过态删除 → 拒绝
|
||
if w := doReq(r, http.MethodDelete, "/api/v1/templates/1", tok); w.Code != http.StatusBadRequest {
|
||
t.Fatalf("通过态删除应 400: %d", w.Code)
|
||
}
|
||
// 编辑内容 → 回待审
|
||
if w := doReqBody(r, http.MethodPut, "/api/v1/templates/1", tok, "application/json",
|
||
[]byte(`{"content":"新内容{code}"}`)); w.Code != http.StatusOK {
|
||
t.Fatalf("编辑 = %d: %s", w.Code, w.Body.String())
|
||
}
|
||
if w := doReq(r, http.MethodGet, "/api/v1/templates/1", tok); !strings.Contains(w.Body.String(), `"status":0`) {
|
||
t.Fatalf("编辑后应待审: %s", w.Body.String())
|
||
}
|
||
// 列表状态过滤(指针语义:status=0 待审)
|
||
if w := doReq(r, http.MethodGet, "/api/v1/templates?status=0", tok); !strings.Contains(w.Body.String(), `"total":1`) {
|
||
t.Fatalf("待审过滤应 1: %s", w.Body.String())
|
||
}
|
||
// 拒审带理由 → 可删除
|
||
doReqBody(r, http.MethodPut, "/api/v1/templates/1/review", adminTok, "application/json", []byte(`{"pass":false,"reason":"不合规"}`))
|
||
if w := doReq(r, http.MethodDelete, "/api/v1/templates/1", tok); w.Code != http.StatusOK {
|
||
t.Fatalf("拒审后删除 = %d: %s", w.Code, w.Body.String())
|
||
}
|
||
}
|
||
|
||
// TestAPI_SendEstimate 验收:预估条数与后端 SmsLength 同源;模板未过审/缺参/非法号码拒绝路径。
|
||
func TestAPI_SendEstimate(t *testing.T) {
|
||
r := newD35Router(t)
|
||
tok := d25Token(t, 2, "customer")
|
||
|
||
// 直发中文 71 字 ×2 号码(1 重复) → 2×2=4 条
|
||
long := strings.Repeat("汉", 71)
|
||
body := `{"mobiles":["13800138000","13800138001","13800138000"],"content":"` + long + `","sign":"【测】"}`
|
||
w := doReqBody(r, http.MethodPost, "/api/v1/send/estimate", tok, "application/json", []byte(body))
|
||
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `"total_fee":4`) {
|
||
t.Fatalf("预估 = %d: %s", w.Code, w.Body.String())
|
||
}
|
||
// Day41 验收二变:签名可选(内容自带),缺 sign 不再拒——正向:内容自带签名直发
|
||
w = doReqBody(r, http.MethodPost, "/api/v1/send/estimate", tok, "application/json",
|
||
[]byte(`{"mobiles":["13800138000"],"content":"【测】hi","send_type":1}`))
|
||
if w.Code != http.StatusOK {
|
||
t.Fatalf("无 sign 自带签名应 200: %d: %s", w.Code, w.Body.String())
|
||
}
|
||
// 拒绝路径:内容为空
|
||
w = doReqBody(r, http.MethodPost, "/api/v1/send/estimate", tok, "application/json",
|
||
[]byte(`{"mobiles":["13800138000"],"content":"","send_type":1}`))
|
||
if w.Code != http.StatusBadRequest {
|
||
t.Fatalf("空内容应 400: %d", w.Code)
|
||
}
|
||
// 拒绝路径:模板不存在
|
||
w = doReqBody(r, http.MethodPost, "/api/v1/send/estimate", tok, "application/json",
|
||
[]byte(`{"mobiles":["13800138000"],"template_id":99,"variables":{"a":"b"},"sign":"【测】"}`))
|
||
if w.Code != http.StatusNotFound {
|
||
t.Fatalf("模板 99 应 404: %d", w.Code)
|
||
}
|
||
}
|