158 lines
5.2 KiB
Go
158 lines
5.2 KiB
Go
package v1
|
|
|
|
// settle_handler_test.go S2-D2:消耗日汇总两端点——customer scope 强制收窄
|
|
// (带参越权请求覆盖不报错,stats 同口径)+手动回补落账断言。
|
|
// gin TestMode + sqlite;claims 由中间件注入模拟(stats_handler_test 同款)。
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"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/settle"
|
|
)
|
|
|
|
// newSettleRouter 种子:账号 1/100 今日各 1 条成功明细(通道 1);返回注入器。
|
|
func newSettleRouter(t *testing.T) (*gin.Engine, *gorm.DB, func(*middleware.Claims)) {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s, e := db.DB(); e == nil {
|
|
s.SetMaxOpenConns(1)
|
|
}
|
|
if err := db.AutoMigrate(&entity.MessageDetail{}, &entity.BillingConsumeLog{},
|
|
&entity.StatsChannelDaily{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Now()
|
|
details := []entity.MessageDetail{
|
|
{SnowflakeModel: entity.SnowflakeModel{ID: 9100000001, CreatedAt: now},
|
|
TaskID: 800001, AccountID: 1, ChannelID: 1, Mobile: "13000000001",
|
|
Content: "t", Status: 2, Business: "sms", Fee: 1.5},
|
|
{SnowflakeModel: entity.SnowflakeModel{ID: 9100000002, CreatedAt: now},
|
|
TaskID: 800002, AccountID: 100, ChannelID: 1, Mobile: "13000000002",
|
|
Content: "t", Status: 2, Business: "sms", Fee: 2.5},
|
|
}
|
|
if err := db.Create(&details).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rg := gin.New()
|
|
api := rg.Group("/api/v1")
|
|
var claims *middleware.Claims
|
|
api.Use(func(c *gin.Context) {
|
|
if claims != nil {
|
|
c.Set(middleware.ContextKeyClaims, claims)
|
|
}
|
|
})
|
|
registerSettleRoutes(api, Deps{Settle: settle.NewService(db)})
|
|
return rg, db, func(cl *middleware.Claims) { claims = cl }
|
|
}
|
|
|
|
// TestSettleConsume_AdminSeesAll admin 全量;customer 强制收窄(带参越权覆盖)。
|
|
func TestSettleConsume_Scope(t *testing.T) {
|
|
rg, db, setClaims := newSettleRouter(t)
|
|
// 预落账(查询读事实源,不直查明细)。
|
|
svc := settle.NewService(db)
|
|
if _, _, err := svc.AggregateDay(t.Context(), time.Now()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
setClaims(&middleware.Claims{AccountID: 1, Username: "admin", Roles: []string{"admin"}})
|
|
w := httptest.NewRecorder()
|
|
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/billing/consume", nil))
|
|
if w.Code != 200 {
|
|
t.Fatalf("admin status=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
var res struct {
|
|
Data struct {
|
|
Total int64 `json:"total"`
|
|
List []entity.BillingConsumeLog `json:"list"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.Data.Total != 2 {
|
|
t.Fatalf("admin 应见全量 2 行,got %d", res.Data.Total)
|
|
}
|
|
|
|
// customer 带参越权(account_id=1):应被覆盖为自身 100。
|
|
setClaims(&middleware.Claims{AccountID: 100, Username: "cust", Roles: []string{"customer"}})
|
|
w = httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/billing/consume?account_id=1", nil)
|
|
rg.ServeHTTP(w, req)
|
|
if w.Code != 200 {
|
|
t.Fatalf("customer status=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.Data.Total != 1 || len(res.Data.List) != 1 || res.Data.List[0].AccountID != 100 {
|
|
t.Fatalf("customer 应强制收窄到自身 100: total=%d list=%v", res.Data.Total, res.Data.List)
|
|
}
|
|
}
|
|
|
|
// TestSettleAggregate_Manual 手动回补:返回行数+落库断言;坏日期 4xx。
|
|
func TestSettleAggregate_Manual(t *testing.T) {
|
|
rg, db, setClaims := newSettleRouter(t)
|
|
setClaims(&middleware.Claims{AccountID: 1, Username: "admin", Roles: []string{"admin"}})
|
|
|
|
w := httptest.NewRecorder()
|
|
rg.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/api/v1/ops/settle/aggregate", nil))
|
|
if w.Code != 200 {
|
|
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
var res struct {
|
|
Data struct {
|
|
Day string `json:"day"`
|
|
AccountRows int64 `json:"account_rows"`
|
|
ChannelRows int64 `json:"channel_rows"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// 缺省=昨日:种子明细是今天 → 0 行也合法;显式补今天断言 2/1。
|
|
w = httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/ops/settle/aggregate",
|
|
strings.NewReader(`{"date":"`+time.Now().Format("2006-01-02")+`"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rg.ServeHTTP(w, req)
|
|
if w.Code != 200 {
|
|
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.Data.AccountRows != 2 || res.Data.ChannelRows != 1 {
|
|
t.Fatalf("今日回补应 account_rows=2/channel_rows=1: %+v", res.Data)
|
|
}
|
|
var n int64
|
|
db.Model(&entity.BillingConsumeLog{}).Count(&n)
|
|
if n != 2 {
|
|
t.Fatalf("billing_consume_log 落账=%d 期望 2", n)
|
|
}
|
|
|
|
// 坏日期参数。
|
|
w = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodPost, "/api/v1/ops/settle/aggregate",
|
|
strings.NewReader(`{"date":"2026/11/14"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rg.ServeHTTP(w, req)
|
|
if w.Code == 200 {
|
|
t.Fatalf("坏日期应 4xx,got %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
}
|