API版本化:迁移设置卡券与通知入口
This commit is contained in:
1 parent
78a256c51e
commit
42e37f7898
9 files changed
+269
-37
No files matched your search
@@ -10,6 +10,7 @@ func (s *Server) mountHealthAndVersionedRoutes(r chi.Router) {
|
||||
s.mountVersionedOrders(r)
|
||||
s.mountVersionedItems(r)
|
||||
s.mountVersionedItemBatchRoutes(r)
|
||||
s.mountVersionedSettingsCardNotificationRoutes(r)
|
||||
}
|
||||
|
||||
// mountVersionedSession 挂载会话 API 的 `/api/v1` 兼容入口,复用现有 handler。
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// settingsCardNotificationRouteCase 描述一个设置、卡券或通知兼容路由测试样例。
|
||||
type settingsCardNotificationRouteCase struct {
|
||||
// name 是测试样例名称。
|
||||
name string
|
||||
// method 是请求使用的 HTTP 方法。
|
||||
method string
|
||||
// versionedPath 是版本化入口路径。
|
||||
versionedPath string
|
||||
// legacyPath 是旧兼容入口路径。
|
||||
legacyPath string
|
||||
// body 是两条入口共用的请求体。
|
||||
body string
|
||||
// wantStatus 是两条入口应返回的状态码。
|
||||
wantStatus int
|
||||
}
|
||||
|
||||
// requestSettingsCardNotificationRoute 发送带认证会话的兼容请求并返回状态码。
|
||||
func requestSettingsCardNotificationRoute(t *testing.T, handler http.Handler, sessionCookie *http.Cookie, method, path, body string) int {
|
||||
t.Helper()
|
||||
// request 是当前设置、卡券或通知兼容入口请求。
|
||||
request := httptest.NewRequest(method, path, strings.NewReader(body))
|
||||
request.AddCookie(sessionCookie)
|
||||
// recorder 是捕获兼容入口响应的记录器。
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
return recorder.Code
|
||||
}
|
||||
|
||||
// TestVersionedSettingsCardNotificationRoutesPreserveLegacyContracts 验证三类领域入口复用旧 handler 和权限边界。
|
||||
func TestVersionedSettingsCardNotificationRoutesPreserveLegacyContracts(t *testing.T) {
|
||||
// srv 是用于验证设置、卡券和通知路由的 HTTP 测试服务。
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// handler 是当前测试使用的完整路由树。
|
||||
handler := srv.Router()
|
||||
// sessionCookie 是管理员登录后得到的认证会话。
|
||||
sessionCookie := loginHelper(t, handler)
|
||||
// cases 是覆盖版本化与旧路径状态码兼容性的测试样例集合。
|
||||
cases := []settingsCardNotificationRouteCase{
|
||||
{name: "public-system-settings", method: http.MethodGet, versionedPath: "/api/v1/settings/system/public", legacyPath: "/system-settings/public", wantStatus: http.StatusOK},
|
||||
{name: "system-settings-list", method: http.MethodGet, versionedPath: "/api/v1/settings/system", legacyPath: "/system-settings", wantStatus: http.StatusOK},
|
||||
{name: "system-settings-bulk", method: http.MethodPut, versionedPath: "/api/v1/settings/system", legacyPath: "/system-settings", body: `{}`, wantStatus: http.StatusBadRequest},
|
||||
{name: "system-settings-key", method: http.MethodPut, versionedPath: "/api/v1/settings/system/theme_color", legacyPath: "/system-settings/theme_color", body: `{"value":"blue"}`, wantStatus: http.StatusOK},
|
||||
{name: "ai-models-invalid", method: http.MethodPost, versionedPath: "/api/v1/settings/ai-models", legacyPath: "/ai-models", body: `not-json`, wantStatus: http.StatusBadRequest},
|
||||
{name: "ai-reply-list", method: http.MethodGet, versionedPath: "/api/v1/settings/ai-reply", legacyPath: "/ai-reply-settings", wantStatus: http.StatusOK},
|
||||
{name: "ai-reply-missing", method: http.MethodGet, versionedPath: "/api/v1/settings/ai-reply/missing", legacyPath: "/ai-reply-settings/missing", wantStatus: http.StatusNotFound},
|
||||
{name: "ai-reply-update-missing", method: http.MethodPut, versionedPath: "/api/v1/settings/ai-reply/missing", legacyPath: "/ai-reply-settings/missing", body: `{}`, wantStatus: http.StatusNotFound},
|
||||
{name: "user-settings-list", method: http.MethodGet, versionedPath: "/api/v1/settings/user", legacyPath: "/user-settings", wantStatus: http.StatusOK},
|
||||
{name: "user-setting-missing", method: http.MethodGet, versionedPath: "/api/v1/settings/user/theme", legacyPath: "/user-settings/theme", wantStatus: http.StatusOK},
|
||||
{name: "user-setting-update", method: http.MethodPut, versionedPath: "/api/v1/settings/user/theme", legacyPath: "/user-settings/theme", body: `{"value":"blue"}`, wantStatus: http.StatusOK},
|
||||
{name: "cards-list", method: http.MethodGet, versionedPath: "/api/v1/cards", legacyPath: "/cards", wantStatus: http.StatusOK},
|
||||
{name: "cards-create-invalid", method: http.MethodPost, versionedPath: "/api/v1/cards", legacyPath: "/cards", body: `{}`, wantStatus: http.StatusBadRequest},
|
||||
{name: "cards-batch-invalid", method: http.MethodPost, versionedPath: "/api/v1/cards/batch", legacyPath: "/cards/batch", wantStatus: http.StatusBadRequest},
|
||||
{name: "card-details-invalid", method: http.MethodGet, versionedPath: "/api/v1/cards/invalid/details", legacyPath: "/cards/invalid/details", wantStatus: http.StatusBadRequest},
|
||||
{name: "card-get-invalid", method: http.MethodGet, versionedPath: "/api/v1/cards/invalid", legacyPath: "/cards/invalid", wantStatus: http.StatusBadRequest},
|
||||
{name: "card-update-invalid", method: http.MethodPut, versionedPath: "/api/v1/cards/invalid", legacyPath: "/cards/invalid", body: `{}`, wantStatus: http.StatusBadRequest},
|
||||
{name: "card-delete-invalid", method: http.MethodDelete, versionedPath: "/api/v1/cards/invalid", legacyPath: "/cards/invalid", wantStatus: http.StatusBadRequest},
|
||||
{name: "card-append-invalid", method: http.MethodPost, versionedPath: "/api/v1/cards/invalid/append-data", legacyPath: "/cards/invalid/append-data", body: `{}`, wantStatus: http.StatusBadRequest},
|
||||
{name: "notification-channels-list", method: http.MethodGet, versionedPath: "/api/v1/notifications/channels", legacyPath: "/notification-channels", wantStatus: http.StatusOK},
|
||||
{name: "notification-channel-create-invalid", method: http.MethodPost, versionedPath: "/api/v1/notifications/channels", legacyPath: "/notification-channels", body: `{}`, wantStatus: http.StatusBadRequest},
|
||||
{name: "notification-channel-update-invalid", method: http.MethodPut, versionedPath: "/api/v1/notifications/channels/invalid", legacyPath: "/notification-channels/invalid", body: `{}`, wantStatus: http.StatusBadRequest},
|
||||
{name: "notification-channel-delete-invalid", method: http.MethodDelete, versionedPath: "/api/v1/notifications/channels/invalid", legacyPath: "/notification-channels/invalid", wantStatus: http.StatusBadRequest},
|
||||
{name: "notification-channel-test-invalid", method: http.MethodPost, versionedPath: "/api/v1/notifications/channels/invalid/test", legacyPath: "/notification-channels/invalid/test", wantStatus: http.StatusBadRequest},
|
||||
{name: "message-notifications-list", method: http.MethodGet, versionedPath: "/api/v1/notifications/messages", legacyPath: "/message-notifications", wantStatus: http.StatusOK},
|
||||
{name: "message-notification-delete-invalid", method: http.MethodDelete, versionedPath: "/api/v1/notifications/messages/invalid", legacyPath: "/message-notifications/invalid", wantStatus: http.StatusBadRequest},
|
||||
{name: "account-notifications-delete", method: http.MethodDelete, versionedPath: "/api/v1/notifications/messages/account/acc1", legacyPath: "/message-notifications/account/acc1", wantStatus: http.StatusOK},
|
||||
{name: "account-bindings-missing", method: http.MethodGet, versionedPath: "/api/v1/notifications/accounts/missing/bindings", legacyPath: "/message-notifications/missing", wantStatus: http.StatusNotFound},
|
||||
{name: "account-bindings-update-missing", method: http.MethodPost, versionedPath: "/api/v1/notifications/accounts/missing/bindings", legacyPath: "/message-notifications/missing", body: `{}`, wantStatus: http.StatusNotFound},
|
||||
}
|
||||
for _, routeCase := range cases { // routeCase 是当前正在执行的设置、卡券或通知路由样例。
|
||||
// versionedStatus 是版本化入口实际返回的状态码。
|
||||
versionedStatus := requestSettingsCardNotificationRoute(t, handler, sessionCookie, routeCase.method, routeCase.versionedPath, routeCase.body)
|
||||
// legacyStatus 是旧兼容入口实际返回的状态码。
|
||||
legacyStatus := requestSettingsCardNotificationRoute(t, handler, sessionCookie, routeCase.method, routeCase.legacyPath, routeCase.body)
|
||||
if versionedStatus != routeCase.wantStatus || legacyStatus != routeCase.wantStatus {
|
||||
t.Errorf("%s status versioned=%d legacy=%d want=%d", routeCase.name, versionedStatus, legacyStatus, routeCase.wantStatus)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"xianyu-go/internal/auth"
|
||||
)
|
||||
|
||||
// mountVersionedSettingsCardNotificationRoutes 挂载设置、卡券和通知的 `/api/v1` 兼容入口。
|
||||
func (s *Server) mountVersionedSettingsCardNotificationRoutes(r chi.Router) {
|
||||
// 公开系统设置只读取允许匿名访问的主题等配置。
|
||||
r.Get("/api/v1/settings/system/public", s.publicSettings)
|
||||
|
||||
// 管理员设置入口保持旧路径的管理员权限边界。
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(s.Auth.Middleware)
|
||||
r.Use(auth.RequireAuth)
|
||||
r.Use(auth.RequireAdmin)
|
||||
r.Get("/api/v1/settings/system", s.allSettings)
|
||||
r.Put("/api/v1/settings/system", s.setSettings)
|
||||
r.Put("/api/v1/settings/system/{key}", s.setSetting)
|
||||
r.Post("/api/v1/settings/ai-models", s.listAIModels)
|
||||
})
|
||||
|
||||
// 普通登录用户可访问账号 AI 设置、用户设置、卡券和通知资源。
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(s.Auth.Middleware)
|
||||
r.Use(auth.RequireAuth)
|
||||
|
||||
r.Get("/api/v1/settings/ai-reply", s.listAIReply)
|
||||
r.Get("/api/v1/settings/ai-reply/{cookie_id}", s.getAIReply)
|
||||
r.Put("/api/v1/settings/ai-reply/{cookie_id}", s.setAIReply)
|
||||
r.Get("/api/v1/settings/user", s.listUserSettings)
|
||||
r.Put("/api/v1/settings/user/{key}", s.setUserSetting)
|
||||
r.Get("/api/v1/settings/user/{key}", s.getUserSetting)
|
||||
|
||||
r.Get("/api/v1/cards", s.listCards)
|
||||
r.Post("/api/v1/cards", s.createCard)
|
||||
r.Post("/api/v1/cards/batch", s.batchCreateCards)
|
||||
r.Post("/api/v1/cards/{card_id}/append-data", s.appendCardData)
|
||||
r.Get("/api/v1/cards/{card_id}/details", s.getCard)
|
||||
r.Get("/api/v1/cards/{card_id}", s.getCard)
|
||||
r.Put("/api/v1/cards/{card_id}", s.updateCard)
|
||||
r.Delete("/api/v1/cards/{card_id}", s.deleteCard)
|
||||
|
||||
r.Get("/api/v1/notifications/channels", s.listChannels)
|
||||
r.Post("/api/v1/notifications/channels", s.createChannel)
|
||||
r.Put("/api/v1/notifications/channels/{channel_id}", s.updateChannel)
|
||||
r.Delete("/api/v1/notifications/channels/{channel_id}", s.deleteChannel)
|
||||
r.Post("/api/v1/notifications/channels/{channel_id}/test", s.testChannel)
|
||||
r.Get("/api/v1/notifications/messages", s.listMessageNotifications)
|
||||
r.Delete("/api/v1/notifications/messages/account/{cid}", s.deleteAccountNotifications)
|
||||
r.Delete("/api/v1/notifications/messages/{notification_id}", s.deleteMessageNotification)
|
||||
r.Get("/api/v1/notifications/accounts/{cid}/bindings", s.getAccountBindings)
|
||||
r.Post("/api/v1/notifications/accounts/{cid}/bindings", s.setAccountBindings)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user