f882ef8224
Replace generic "METHOD path" summaries with curated Chinese summaries and longer descriptions, surfaced in Swagger UI. Adds a parity test that fails when a new tenant-api route is wired up without a docs entry. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
78 lines
2.7 KiB
Go
78 lines
2.7 KiB
Go
package swagger
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBuildOpenAPI_GeneratesUsableSpecFromGinRoutes(t *testing.T) {
|
|
spec := BuildOpenAPI(gin.RoutesInfo{
|
|
{Method: http.MethodPost, Path: "/api/auth/login"},
|
|
{Method: http.MethodGet, Path: "/api/tenant/articles/:id"},
|
|
{Method: http.MethodPost, Path: "/api/tenant/articles/:id/images"},
|
|
{Method: http.MethodGet, Path: "/api/internal/metrics/monitoring"},
|
|
{Method: http.MethodGet, Path: "/swagger/openapi.json"},
|
|
}, Config{Title: "Test API", Version: "v1"})
|
|
|
|
_, err := json.Marshal(spec)
|
|
require.NoError(t, err)
|
|
|
|
paths := spec["paths"].(map[string]any)
|
|
require.Contains(t, paths, "/api/auth/login")
|
|
require.Contains(t, paths, "/api/tenant/articles/{id}")
|
|
require.Contains(t, paths, "/api/tenant/articles/{id}/images")
|
|
require.NotContains(t, paths, "/api/internal/metrics/monitoring")
|
|
require.NotContains(t, paths, "/swagger/openapi.json")
|
|
|
|
articlePath := paths["/api/tenant/articles/{id}"].(map[string]any)
|
|
getArticle := articlePath["get"].(map[string]any)
|
|
require.Equal(t, []map[string][]string{{"bearerAuth": []string{}}}, getArticle["security"])
|
|
require.Equal(t, "文章详情", getArticle["summary"])
|
|
require.NotEmpty(t, getArticle["description"])
|
|
|
|
loginPath := paths["/api/auth/login"].(map[string]any)
|
|
loginPost := loginPath["post"].(map[string]any)
|
|
require.Equal(t, "账号密码登录", loginPost["summary"])
|
|
|
|
params := getArticle["parameters"].([]map[string]any)
|
|
require.Contains(t, parameterNames(params), "id")
|
|
require.Contains(t, parameterNames(params), "X-Workspace-ID")
|
|
|
|
imagePath := paths["/api/tenant/articles/{id}/images"].(map[string]any)
|
|
uploadImage := imagePath["post"].(map[string]any)
|
|
body := uploadImage["requestBody"].(map[string]any)
|
|
content := body["content"].(map[string]any)
|
|
require.Contains(t, content, "multipart/form-data")
|
|
}
|
|
|
|
func TestEnabledForMode_DisablesReleaseOnly(t *testing.T) {
|
|
require.False(t, EnabledForMode("release"))
|
|
require.False(t, EnabledForMode(" Release "))
|
|
require.True(t, EnabledForMode(""))
|
|
require.True(t, EnabledForMode("debug"))
|
|
}
|
|
|
|
func TestDocFor_FallsBackWhenMissing(t *testing.T) {
|
|
doc := docFor("GET", "/api/some/unregistered/path")
|
|
require.Equal(t, "GET /api/some/unregistered/path", doc.Summary)
|
|
require.Empty(t, doc.Description)
|
|
}
|
|
|
|
func TestDocFor_KnownRoute(t *testing.T) {
|
|
doc := docFor("POST", "/api/auth/login")
|
|
require.Equal(t, "账号密码登录", doc.Summary)
|
|
require.NotEmpty(t, doc.Description)
|
|
}
|
|
|
|
func parameterNames(params []map[string]any) []string {
|
|
names := make([]string, 0, len(params))
|
|
for _, param := range params {
|
|
names = append(names, param["name"].(string))
|
|
}
|
|
return names
|
|
}
|