Files
go-sms-V2/internal/api/http/v1/account_warning_config_test.go
T

58 lines
2.5 KiB
Go

// account_warning_config_test.go(原 Day27):预警配置端点(本人/admin 读写、越权 403)。
package v1
import (
"net/http"
"testing"
"github.com/gin-gonic/gin"
)
// newD27Router 复用 D25 双账号结构(admin=1/customer=2,已含 Warning 服务)。
func newD27Router(t *testing.T) *gin.Engine {
t.Helper()
r, _ := newD25Router(t)
return r
}
// TestAPI_WarningConfigCRUD 设置/查询/停用闭环 + 越权拦截。
func TestAPI_WarningConfigCRUD(t *testing.T) {
r := newD27Router(t)
adminTok := d25Token(t, 1, "admin")
custTok := d25Token(t, 2, "customer")
body := []byte(`{"threshold":500,"cooldown_min":60,"notify_type":"wechat","notify_to":"http://hook/x"}`)
// customer 设置本人预警 → 200
if w := doReqBody(r, http.MethodPut, "/api/v1/accounts/2/warning", custTok, "application/json", body); w.Code != http.StatusOK {
t.Fatalf("本人设置预警 = %d body=%s", w.Code, w.Body.String())
}
// 查询回显
if w := doReq(r, http.MethodGet, "/api/v1/accounts/2/warning", custTok); w.Code != http.StatusOK || !contains(w.Body.String(), `"threshold":500`) {
t.Fatalf("本人查询预警 = %d body=%s", w.Code, w.Body.String())
}
// customer 越权设置 admin → 403
if w := doReqBody(r, http.MethodPut, "/api/v1/accounts/1/warning", custTok, "application/json", body); w.Code != http.StatusForbidden {
t.Fatalf("越权设置 = %d, 期望 403", w.Code)
}
// 非法阈值 → 400
if w := doReqBody(r, http.MethodPut, "/api/v1/accounts/2/warning", custTok, "application/json", []byte(`{"threshold":-1}`)); w.Code != http.StatusBadRequest {
t.Fatalf("非法阈值 = %d, 期望 400", w.Code)
}
// 非法渠道 → 400
if w := doReqBody(r, http.MethodPut, "/api/v1/accounts/2/warning", custTok, "application/json", []byte(`{"threshold":10,"notify_type":"pigeon"}`)); w.Code != http.StatusBadRequest {
t.Fatalf("非法渠道 = %d, 期望 400", w.Code)
}
// admin 代设置 customer → 200
if w := doReqBody(r, http.MethodPut, "/api/v1/accounts/2/warning", adminTok, "application/json", []byte(`{"threshold":900,"cooldown_min":15}`)); w.Code != http.StatusOK {
t.Fatalf("admin 代设置 = %d body=%s", w.Code, w.Body.String())
}
// 停用(admin)
if w := doReq(r, http.MethodDelete, "/api/v1/accounts/2/warning", adminTok); w.Code != http.StatusOK {
t.Fatalf("停用预警 = %d", w.Code)
}
// 未认证 → 401
if w := doReq(r, http.MethodGet, "/api/v1/accounts/2/warning", ""); w.Code != http.StatusUnauthorized {
t.Fatalf("未认证 = %d, 期望 401", w.Code)
}
}