API版本化:迁移密码与剩余公共入口

This commit is contained in:
Christ committed 2026-08-14 18:40:13 +08:00
1 parent caecc09c79
commit 8d103b1cc0
13 files changed
+237 -35

No files matched your search

@@ -27,6 +27,7 @@ func (s *Server) mountVersionedAccounts(r chi.Router) {
r.Get("/api/v1/accounts/{cid}/pause-duration", s.getCookiePauseDuration)
r.Put("/api/v1/accounts/{cid}/pause-duration", s.setCookiePauseDuration)
r.Put("/api/v1/accounts/{cid}", s.updateCookie)
r.Delete("/api/v1/accounts/{cid}", s.deleteCookie)
r.Put("/api/v1/accounts/{cid}/login-info", s.updateCookieLoginInfo)
})
}
@@ -0,0 +1,22 @@
package server
import (
"github.com/go-chi/chi/v5"
"xianyu-go/internal/auth"
)
// mountVersionedAutomationRoutes 挂载自动化规则和异常处理的 `/api/v1` 兼容入口。
func (s *Server) mountVersionedAutomationRoutes(r chi.Router) {
r.Group(func(r chi.Router) {
r.Use(s.Auth.Middleware)
r.Use(auth.RequireAuth)
r.Get("/api/v1/automation-rules", s.listAutomationRules)
r.Post("/api/v1/automation-rules", s.createAutomationRule)
r.Put("/api/v1/automation-rules/{rule_id}", s.updateAutomationRule)
r.Delete("/api/v1/automation-rules/{rule_id}", s.deleteAutomationRule)
r.Get("/api/v1/automation-issues", s.listAutomationIssues)
r.Post("/api/v1/automation-runs/{run_id}/resolve", s.resolveAutomationRun)
r.Post("/api/v1/automation-pending-tasks/{task_id}/resolve", s.resolveDeferredAutomationTask)
})
}
+1
View File
@@ -13,6 +13,7 @@ func (s *Server) mountVersionedItems(r chi.Router) {
r.Use(auth.RequireAuth)
r.Get("/api/v1/items", s.listItems)
r.Get("/api/v1/items/{cookie_id}/{item_id}", s.getItem)
r.Post("/api/v1/items/{cookie_id}", s.createItem)
r.Post("/api/v1/items/publish", s.publishItem)
r.Put("/api/v1/items/{cookie_id}/{item_id}", s.updateItem)
r.Delete("/api/v1/items/{cookie_id}/{item_id}", s.deleteItem)
@@ -14,6 +14,7 @@ func (s *Server) mountVersionedOrders(r chi.Router) {
r.Get("/api/v1/orders", s.listOrders)
r.Get("/api/v1/orders/{order_id}", s.getOrder)
r.Put("/api/v1/orders/{order_id}", s.updateOrder)
r.Delete("/api/v1/orders/{order_id}", s.deleteOrder)
r.Post("/api/v1/orders/refresh", s.refreshOrders)
r.Post("/api/v1/orders/manual-ship", s.manualShipOrders)
r.Post("/api/v1/orders/import", s.importOrders)
@@ -0,0 +1,18 @@
package server
import (
"github.com/go-chi/chi/v5"
"xianyu-go/internal/auth"
)
// mountVersionedPasswordLoginRoutes 挂载已禁用密码登录的 `/api/v1` 兼容入口。
func (s *Server) mountVersionedPasswordLoginRoutes(r chi.Router) {
r.Group(func(r chi.Router) {
r.Use(s.Auth.Middleware)
r.Use(auth.RequireAuth)
r.Post("/api/v1/password-login", s.passwordLoginDisabled)
r.Get("/api/v1/password-login/check/{session_id}", s.passwordLoginDisabled)
r.Delete("/api/v1/password-login/cancel/{session_id}", s.passwordLoginDisabled)
})
}
@@ -0,0 +1,72 @@
package server
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// remainingVersionedRouteCase 描述一个剩余公共版本化入口测试样例。
type remainingVersionedRouteCase struct {
// name 是测试样例名称。
name string
// method 是请求使用的 HTTP 方法。
method string
// versionedPath 是版本化入口路径。
versionedPath string
// legacyPath 是旧兼容入口路径。
legacyPath string
// body 是两条入口共用的请求体。
body string
// wantStatus 是两条入口应返回的状态码。
wantStatus int
}
// requestRemainingVersionedRoute 发送带认证会话的公共请求并返回状态码。
func requestRemainingVersionedRoute(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
}
// TestVersionedRemainingRoutesPreserveLegacyContracts 验证剩余公共调用方版本化入口复用旧 handler 和权限边界。
func TestVersionedRemainingRoutesPreserveLegacyContracts(t *testing.T) {
// srv 是用于验证剩余公共版本化路由的 HTTP 测试服务。
srv, _, cleanup := newTestServer(t)
defer cleanup()
// handler 是当前测试使用的完整路由树。
handler := srv.Router()
// sessionCookie 是管理员登录后得到的认证会话。
sessionCookie := loginHelper(t, handler)
// cases 是覆盖会话、账号、密码登录、自动化、订单和商品入口的测试样例集合。
cases := []remainingVersionedRouteCase{
{name: "change-password", method: http.MethodPost, versionedPath: "/api/v1/session/password", legacyPath: "/change-password", body: `{"current_password":"wrong","new_password":"new-password"}`, wantStatus: http.StatusUnauthorized},
{name: "update-credentials", method: http.MethodPut, versionedPath: "/api/v1/session/credentials", legacyPath: "/account/credentials", body: `{"current_password":"wrong","new_username":"new-user"}`, wantStatus: http.StatusUnauthorized},
{name: "change-admin-password", method: http.MethodPost, versionedPath: "/api/v1/admin/password", legacyPath: "/change-admin-password", body: `{"current_password":"wrong","new_password":"new-password"}`, wantStatus: http.StatusUnauthorized},
{name: "delete-account", method: http.MethodDelete, versionedPath: "/api/v1/accounts/missing-account", legacyPath: "/cookies/missing-account", wantStatus: http.StatusNotFound},
{name: "password-login", method: http.MethodPost, versionedPath: "/api/v1/password-login", legacyPath: "/password-login", body: `{}`, wantStatus: http.StatusNotImplemented},
{name: "password-login-check", method: http.MethodGet, versionedPath: "/api/v1/password-login/check/missing-session", legacyPath: "/password-login/check/missing-session", wantStatus: http.StatusNotImplemented},
{name: "password-login-cancel", method: http.MethodDelete, versionedPath: "/api/v1/password-login/cancel/missing-session", legacyPath: "/password-login/cancel/missing-session", wantStatus: http.StatusNotImplemented},
{name: "automation-rules", method: http.MethodGet, versionedPath: "/api/v1/automation-rules", legacyPath: "/automation-rules", wantStatus: http.StatusOK},
{name: "automation-issues", method: http.MethodGet, versionedPath: "/api/v1/automation-issues", legacyPath: "/automation-issues", wantStatus: http.StatusOK},
{name: "automation-run-resolve", method: http.MethodPost, versionedPath: "/api/v1/automation-runs/invalid/resolve", legacyPath: "/automation-runs/invalid/resolve", body: `{}`, wantStatus: http.StatusBadRequest},
{name: "automation-task-resolve", method: http.MethodPost, versionedPath: "/api/v1/automation-pending-tasks/invalid/resolve", legacyPath: "/automation-pending-tasks/invalid/resolve", body: `{}`, wantStatus: http.StatusBadRequest},
{name: "delete-order", method: http.MethodDelete, versionedPath: "/api/v1/orders/missing-order", legacyPath: "/api/orders/missing-order", wantStatus: http.StatusNotFound},
{name: "create-item", method: http.MethodPost, versionedPath: "/api/v1/items/missing-account", legacyPath: "/items/missing-account", body: `{}`, wantStatus: http.StatusNotFound},
}
for _, routeCase := range cases { // routeCase 是当前正在执行的剩余公共路由样例。
// versionedStatus 是版本化入口实际返回的状态码。
versionedStatus := requestRemainingVersionedRoute(t, handler, sessionCookie, routeCase.method, routeCase.versionedPath, routeCase.body)
// legacyStatus 是旧兼容入口实际返回的状态码。
legacyStatus := requestRemainingVersionedRoute(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)
}
}
}
+19 -1
View File
@@ -1,6 +1,10 @@
package server
import "github.com/go-chi/chi/v5"
import (
"github.com/go-chi/chi/v5"
"xianyu-go/internal/auth"
)
// mountHealthAndVersionedRoutes 挂载健康检查及当前阶段的版本化 API 入口。
func (s *Server) mountHealthAndVersionedRoutes(r chi.Router) {
@@ -15,6 +19,8 @@ func (s *Server) mountHealthAndVersionedRoutes(r chi.Router) {
s.mountVersionedReplyRoutes(r)
s.mountVersionedAdminAnalyticsRoutes(r)
s.mountVersionedQRLoginRoutes(r)
s.mountVersionedPasswordLoginRoutes(r)
s.mountVersionedAutomationRoutes(r)
}
// mountVersionedSession 挂载会话 API 的 `/api/v1` 兼容入口,复用现有 handler。
@@ -26,4 +32,16 @@ func (s *Server) mountVersionedSession(r chi.Router) {
r.Get("/api/v1/session", s.verify)
r.Post("/api/v1/session/logout", s.logout)
})
r.Group(func(r chi.Router) {
r.Use(s.Auth.Middleware)
r.Use(auth.RequireAuth)
r.Post("/api/v1/session/password", s.changePassword)
r.Put("/api/v1/session/credentials", s.updateCredentials)
})
r.Group(func(r chi.Router) {
r.Use(s.Auth.Middleware)
r.Use(auth.RequireAuth)
r.Use(auth.RequireAdmin)
r.Post("/api/v1/admin/password", s.changeAdminPassword)
})
}