Files
ez-api/internal/api/master_tokens_handler_test.go
zenfun 33838b1e2c feat(api): wrap JSON responses in envelope
Add response envelope middleware to standardize JSON responses as
`{code,data,message}` with consistent business codes across endpoints.
Update Swagger annotations and tests to reflect the new response shape.

BREAKING CHANGE: API responses are now wrapped in a response envelope; clients must read payloads from `data` and handle `code`/`message` fields.
2026-01-10 00:15:08 +08:00

89 lines
2.8 KiB
Go

package api
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/alicebob/miniredis/v2"
"github.com/ez-api/ez-api/internal/middleware"
"github.com/ez-api/ez-api/internal/model"
"github.com/ez-api/ez-api/internal/service"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func TestMaster_ListTokens_AndUpdateToken(t *testing.T) {
gin.SetMode(gin.TestMode)
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := db.AutoMigrate(&model.Master{}, &model.Key{}, &model.LogRecord{}); err != nil {
t.Fatalf("migrate: %v", err)
}
m := &model.Master{Name: "m1", Group: "g", Status: "active", Epoch: 1, DefaultNamespace: "ns", Namespaces: "ns", MaxChildKeys: 5, GlobalQPS: 3}
if err := db.Create(m).Error; err != nil {
t.Fatalf("create master: %v", err)
}
k := &model.Key{MasterID: m.ID, TokenHash: "h", Group: "g", Status: "active", Scopes: "chat:write", IssuedAtEpoch: 1, DefaultNamespace: "ns", Namespaces: "ns"}
if err := db.Create(k).Error; err != nil {
t.Fatalf("create key: %v", err)
}
mr := miniredis.RunT(t)
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
syncSvc := service.NewSyncService(rdb)
masterSvc := service.NewMasterService(db)
statsSvc := service.NewStatsService(rdb)
h := NewMasterHandler(db, db, masterSvc, syncSvc, statsSvc, nil)
withMaster := func(next gin.HandlerFunc) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("master", m)
next(c)
}
}
r := gin.New()
r.Use(middleware.ResponseEnvelope())
r.GET("/v1/tokens", withMaster(h.ListTokens))
r.PUT("/v1/tokens/:id", withMaster(h.UpdateToken))
rr := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/v1/tokens", nil)
r.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
}
var list []TokenView
decodeEnvelope(t, rr, &list)
if len(list) != 1 || list[0].ID != k.ID {
t.Fatalf("unexpected list: %+v", list)
}
body := []byte(`{"scopes":"chat:write,chat:read","status":"suspended"}`)
rr = httptest.NewRecorder()
req = httptest.NewRequest(http.MethodPut, fmt.Sprintf("/v1/tokens/%d", k.ID), bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
}
var updated TokenView
decodeEnvelope(t, rr, &updated)
if updated.Status != "suspended" {
t.Fatalf("expected status suspended, got %+v", updated)
}
if mr.HGet("auth:token:h", "status") != "suspended" {
t.Fatalf("expected redis token status suspended, got %q", mr.HGet("auth:token:h", "status"))
}
}