在 fast-forward 到 origin/main 后解决 music logic/repo 冲突,SQL 与模型统一 t_* 表名;Makefile 修正 golangci-lint 变量并令 test/deploy 依赖 lint;补齐 admin/transport 等 lint 修复与 music logic 单测以满足覆盖率门禁。 Co-authored-by: Cursor <cursoragent@cursor.com>
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
package httpx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
adminlogic "spark-server/internal/module/admin/logic"
|
|
adminhandler "spark-server/internal/transport/http/handler/admin"
|
|
)
|
|
|
|
func TestAdminImStatsRequiresJWT(t *testing.T) {
|
|
tokens := adminlogic.NewTokenService("test-admin-secret-at-least-32-bytes!!")
|
|
r := NewEngine(zap.NewNop(), &EngineDeps{
|
|
Admin: adminlogic.NewService(nil, nil, nil),
|
|
AdminTokens: tokens,
|
|
})
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/admin/spark/im/stats", nil))
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status %d body %s", w.Code, w.Body.String())
|
|
}
|
|
var env adminhandler.APIResponse
|
|
if err := json.Unmarshal(w.Body.Bytes(), &env); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if env.Success {
|
|
t.Fatalf("%+v", env)
|
|
}
|
|
}
|
|
|
|
func TestAdminImStatsWithToken(t *testing.T) {
|
|
tokens := adminlogic.NewTokenService("test-admin-secret-at-least-32-bytes!!")
|
|
tok, err := tokens.CreateToken(1, "admin", "SUPER_ADMIN", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := NewEngine(zap.NewNop(), &EngineDeps{
|
|
Admin: adminlogic.NewService(nil, nil, nil),
|
|
AdminTokens: tokens,
|
|
})
|
|
req := httptest.NewRequest(http.MethodGet, "/api/admin/spark/im/stats", nil)
|
|
req.Header.Set("Authorization", "Bearer "+tok)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusInternalServerError && w.Code != http.StatusOK {
|
|
// nil db → 500 with admin: mysql not ready message in Fail body
|
|
t.Fatalf("status %d body %s", w.Code, w.Body.String())
|
|
}
|
|
var env adminhandler.APIResponse
|
|
if err := json.Unmarshal(w.Body.Bytes(), &env); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if env.Success {
|
|
t.Fatalf("expected fail without db: %+v", env)
|
|
}
|
|
}
|
|
|
|
func TestAdminPopAlias(t *testing.T) {
|
|
tokens := adminlogic.NewTokenService("test-admin-secret-at-least-32-bytes!!")
|
|
tok, err := tokens.CreateToken(1, "admin", "SUPER_ADMIN", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := NewEngine(zap.NewNop(), &EngineDeps{
|
|
Admin: adminlogic.NewService(nil, nil, nil),
|
|
AdminTokens: tokens,
|
|
})
|
|
req := httptest.NewRequest(http.MethodGet, "/api/admin/pop/api-doc", nil)
|
|
req.Header.Set("Authorization", "Bearer "+tok)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status %d %s", w.Code, w.Body.String())
|
|
}
|
|
var env adminhandler.APIResponse
|
|
if err := json.Unmarshal(w.Body.Bytes(), &env); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !env.Success {
|
|
t.Fatalf("%+v", env)
|
|
}
|
|
}
|