API契约:迁移二维码与密码登录错误
This commit is contained in:
1 parent
82da4d8bb5
commit
1a14f18b07
6 files changed
+149
-30
No files matched your search
@@ -132,7 +132,8 @@ app shell / routes
|
||||
- 已完成阶段 3 第二个 PR 切片“剩余认证与公共 API 错误迁移”:初始化冲突、管理员/用户密码修改、登录凭据校验、用户名冲突、公开设置故障和 SPA API 404 均返回统一错误 DTO 与正确状态码;新增状态码/错误码契约测试,Go 全量门禁通过并合并为一个可回滚提交;
|
||||
- 已完成阶段 3 第三个 PR 切片“订单与账号业务 API 错误响应迁移”:订单刷新、手动发货和订单导入不再返回顶层 HTTP 200 + `success:false`,批量结果改用 `partial_failure`,刷新明细错误统一使用 `message`;账号任务的 502 错误改用统一错误 DTO;新增订单/账号契约测试并通过全量门禁,逐项结果中的 `success` 仅保留为批处理行状态;
|
||||
- 已完成阶段 3 第四个 PR 切片“聊天、商品与自动化业务 API 错误响应迁移”:聊天发送失败和商品发布失败均改用统一 `code`/`message`/`request_id` 错误 DTO,商品远端已成功但本地保存失败的恢复信息迁移到可选 `details`;React 错误类型同步支持 `details`,新增 HTTP DTO、聊天/自动化/商品契约测试并通过前端门禁;
|
||||
- 阶段 3 下一 PR 切片为“二维码登录、密码登录和剩余公共业务错误响应迁移”:继续收敛遗留 HTTP 200 + `success:false` 及动态错误别名,不拆分单个 handler 提交;
|
||||
- 已完成阶段 3 第五个 PR 切片“二维码登录、密码登录和剩余公共业务错误响应迁移”:二维码生成、扫码状态持久化、风控验证失败和账号不匹配均改用正确的非 2xx 状态码与统一错误 DTO;历史密码登录入口返回明确的 `password_login_disabled` 未实现错误;新增二维码/密码登录契约测试并通过全量门禁;
|
||||
- 阶段 3 下一 PR 切片为“统一业务成功响应具名 DTO 与版本化路径准备”:盘点剩余动态 map 成功响应,先为认证、账号、订单和聊天主链路补齐具名响应 DTO,并记录 `/api/v1` 兼容迁移边界,不拆分单个 handler 提交;
|
||||
- 禁止跳过当前入口直接开始 Engine、Automation 或 DB 的大规模拆分。
|
||||
|
||||
## 6. 阶段 0:治理文档与强约束
|
||||
@@ -513,3 +514,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-14 | 完成阶段 3 第二个 PR 切片“剩余认证与公共 API 错误迁移” | 初始化、密码修改、凭据校验、用户名冲突、公开设置故障和 SPA API 404 的状态码/错误码契约已统一;契约测试、全量测试、vet、lint、注释和 diff 门禁通过 | 阶段 3:订单与账号业务 API 错误响应迁移 |
|
||||
| 2026-08-14 | 完成阶段 3 第三个 PR 切片“订单与账号业务 API 错误响应迁移” | 订单批量接口移除顶层 HTTP 200 + `success:false`,逐项失败保留行级状态并统一为 `message`;账号任务 502 改用统一错误 DTO;订单/账号契约测试、全量测试、race、vet、lint、注释和 diff 门禁通过 | 阶段 3:聊天、商品与自动化业务 API 错误响应迁移 |
|
||||
| 2026-08-14 | 完成阶段 3 第四个 PR 切片“聊天、商品与自动化业务 API 错误响应迁移” | 聊天发送和商品发布失败统一为 `code`/`message`/`request_id`,远端发布后的商品核对信息迁移到 `details`;React 错误类型、HTTP DTO 测试和业务契约测试已更新;前端类型检查、测试、构建及 Go 注释门禁通过 | 阶段 3:二维码登录、密码登录和剩余公共业务错误响应迁移 |
|
||||
| 2026-08-14 | 完成阶段 3 第五个 PR 切片“二维码登录、密码登录和剩余公共业务错误响应迁移” | 二维码与密码登录遗留 HTTP 200 + `success:false` 已迁移为非 2xx 错误;账号不匹配保留 `scanned_account_id` 到 `details`;二维码/密码登录契约测试、全量测试、race、vet、lint、注释和 diff 门禁通过 | 阶段 3:统一业务成功响应具名 DTO 与版本化路径准备 |
|
||||
@@ -14,10 +14,11 @@ func (s *Server) mountPasswordLogin(r chi.Router) {
|
||||
r.Delete("/password-login/cancel/{session_id}", s.passwordLoginDisabled)
|
||||
}
|
||||
|
||||
// passwordLoginDisabled 返回密码登录已禁用的统一错误响应,保留旧路径兼容性。
|
||||
func (s *Server) passwordLoginDisabled(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"success": false,
|
||||
"status": "disabled",
|
||||
"message": "Go 客户端仅支持扫码登录,密码登录已禁用",
|
||||
})
|
||||
writeErrCode(
|
||||
w,
|
||||
http.StatusNotImplemented,
|
||||
"password_login_disabled",
|
||||
"Go 客户端仅支持扫码登录,密码登录已禁用", "")
|
||||
}
|
||||
@@ -23,14 +23,14 @@ func TestPasswordLoginAPIsArePermanentlyDisabled(t *testing.T) {
|
||||
req.AddCookie(authCookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
if rec.Code != http.StatusNotImplemented {
|
||||
t.Fatalf("%s %s status=%d body=%s", req.Method, req.URL.Path, rec.Code, rec.Body.String())
|
||||
}
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result["success"] != false || result["status"] != "disabled" {
|
||||
if result["code"] != "password_login_disabled" || result["message"] == "" {
|
||||
t.Fatalf("%s %s 应永久禁用: %+v", req.Method, req.URL.Path, result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"xianyu-go/internal/httpapi"
|
||||
)
|
||||
|
||||
// TestAPIContractQRLoginGenerationFailure 验证二维码生成上游失败返回非 2xx 统一错误 DTO。
|
||||
func TestAPIContractQRLoginGenerationFailure(t *testing.T) {
|
||||
// srv 是用于验证二维码生成错误契约的 HTTP 测试服务。
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
srv.QRLogin = &fakeQRLoginService{generateErr: errors.New("二维码服务不可用")}
|
||||
// handler 是当前测试使用的完整路由树。
|
||||
handler := srv.Router()
|
||||
// sessionCookie 是管理员登录后得到的认证会话。
|
||||
sessionCookie := loginHelper(t, handler)
|
||||
// req 是触发二维码上游失败的生成请求。
|
||||
req := httptest.NewRequest(http.MethodPost, "/qr-login/generate", nil)
|
||||
req.AddCookie(sessionCookie)
|
||||
// recorder 是捕获二维码错误响应的测试记录器。
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, req)
|
||||
if recorder.Code != http.StatusBadGateway {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
// response 是二维码生成失败的统一错误 DTO。
|
||||
var response httpapi.ErrorResponse
|
||||
// decodeErr 表示二维码错误响应 JSON 反序列化失败的原因。
|
||||
if decodeErr := json.Unmarshal(recorder.Body.Bytes(), &response); decodeErr != nil {
|
||||
t.Fatalf("decode response: %v", decodeErr)
|
||||
}
|
||||
if response.Code != "qr_login_generate_failed" || response.Message == "" {
|
||||
t.Fatalf("response=%+v", response)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAPIContractQRLoginPersistFailure 验证扫码成功但本地持久化失败返回统一内部错误。
|
||||
func TestAPIContractQRLoginPersistFailure(t *testing.T) {
|
||||
// srv 是用于验证扫码持久化错误契约的 HTTP 测试服务。
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
srv.Manager = nil
|
||||
srv.QRLogin = &fakeQRLoginService{status: map[string]any{"status": "success"}}
|
||||
ownQRSession(t, srv, store, "persist-failure")
|
||||
// handler 是当前测试使用的完整路由树。
|
||||
handler := srv.Router()
|
||||
// sessionCookie 是管理员登录后得到的认证会话。
|
||||
sessionCookie := loginHelper(t, handler)
|
||||
// req 是触发扫码结果缺少凭证的状态查询请求。
|
||||
req := httptest.NewRequest(http.MethodGet, "/qr-login/status/persist-failure", nil)
|
||||
req.AddCookie(sessionCookie)
|
||||
// recorder 是捕获扫码持久化错误响应的测试记录器。
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, req)
|
||||
if recorder.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
// response 是扫码持久化失败的统一错误 DTO。
|
||||
var response httpapi.ErrorResponse
|
||||
// decodeErr 表示扫码持久化错误响应 JSON 反序列化失败的原因。
|
||||
if decodeErr := json.Unmarshal(recorder.Body.Bytes(), &response); decodeErr != nil {
|
||||
t.Fatalf("decode response: %v", decodeErr)
|
||||
}
|
||||
if response.Code != "qr_login_persist_failed" || response.Message == "" {
|
||||
t.Fatalf("response=%+v", response)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAPIContractQRVerificationFailure 验证风控验证上游失败返回统一网关错误。
|
||||
func TestAPIContractQRVerificationFailure(t *testing.T) {
|
||||
// srv 是用于验证扫码风控错误契约的 HTTP 测试服务。
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
srv.QRLogin = &fakeQRLoginService{completeErr: errors.New("验证服务不可用")}
|
||||
ownQRSession(t, srv, store, "verification-failure")
|
||||
// handler 是当前测试使用的完整路由树。
|
||||
handler := srv.Router()
|
||||
// sessionCookie 是管理员登录后得到的认证会话。
|
||||
sessionCookie := loginHelper(t, handler)
|
||||
// req 是触发风控验证上游失败的请求。
|
||||
req := httptest.NewRequest(http.MethodPost, "/qr-login/complete-verification/verification-failure", nil)
|
||||
req.AddCookie(sessionCookie)
|
||||
// recorder 是捕获风控验证错误响应的测试记录器。
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, req)
|
||||
if recorder.Code != http.StatusBadGateway {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
// response 是风控验证失败的统一错误 DTO。
|
||||
var response httpapi.ErrorResponse
|
||||
// decodeErr 表示风控验证错误响应 JSON 反序列化失败的原因。
|
||||
if decodeErr := json.Unmarshal(recorder.Body.Bytes(), &response); decodeErr != nil {
|
||||
t.Fatalf("decode response: %v", decodeErr)
|
||||
}
|
||||
if response.Code != "qr_verification_failed" || response.Message == "" {
|
||||
t.Fatalf("response=%+v", response)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAPIContractPasswordLoginDisabled 验证历史密码登录兼容入口使用明确的未实现错误。
|
||||
func TestAPIContractPasswordLoginDisabled(t *testing.T) {
|
||||
// srv 是用于验证密码登录禁用契约的 HTTP 测试服务。
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// handler 是当前测试使用的完整路由树。
|
||||
handler := srv.Router()
|
||||
// sessionCookie 是管理员登录后得到的认证会话。
|
||||
sessionCookie := loginHelper(t, handler)
|
||||
assertUnifiedAPIError(t, handler, http.MethodPost, "/password-login", `{}`, sessionCookie, http.StatusNotImplemented, "password_login_disabled", "Go 客户端仅支持扫码登录,密码登录已禁用", false)
|
||||
}
|
||||
@@ -51,10 +51,10 @@ func (s *Server) generateQRLogin(w http.ResponseWriter, r *http.Request) {
|
||||
default:
|
||||
s.Logger.Error("生成二维码失败", "err", err)
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"success": false,
|
||||
"message": message,
|
||||
})
|
||||
writeErrCode(
|
||||
w,
|
||||
http.StatusBadGateway, "qr_login_generate_failed",
|
||||
message, "")
|
||||
return
|
||||
}
|
||||
s.qrMu.Lock()
|
||||
@@ -106,10 +106,10 @@ func (s *Server) checkQRLoginStatusAndPersist(w http.ResponseWriter, r *http.Req
|
||||
if s.Logger != nil {
|
||||
s.Logger.Warn("保存扫码登录结果失败", "session_id", sessionID, "err", err)
|
||||
}
|
||||
result["success"] = false
|
||||
result["status"] = "error"
|
||||
result["message"] = "保存扫码登录结果失败: " + err.Error()
|
||||
writeJSON(w, http.StatusOK, publicQRStatus(result))
|
||||
writeErrCode(
|
||||
w,
|
||||
http.StatusInternalServerError, "qr_login_persist_failed",
|
||||
"保存扫码登录结果失败: "+err.Error(), "")
|
||||
return
|
||||
}
|
||||
result["success"] = true
|
||||
@@ -131,10 +131,10 @@ func (s *Server) completeQRVerification(w http.ResponseWriter, r *http.Request)
|
||||
cookies, unb, err := s.QRLogin.CompleteVerification(r.Context(), sessionID)
|
||||
if err != nil {
|
||||
s.Logger.Error("验证完成处理失败", "err", err)
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"success": false,
|
||||
"message": err.Error(),
|
||||
})
|
||||
writeErrCode(
|
||||
w,
|
||||
http.StatusBadGateway, "qr_verification_failed",
|
||||
err.Error(), "")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -148,11 +148,11 @@ func (s *Server) completeQRVerification(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
req.TargetAccountID = strings.TrimSpace(req.TargetAccountID)
|
||||
if req.TargetAccountID != "" && req.TargetAccountID != unb {
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"success": false,
|
||||
"scanned_account_id": unb,
|
||||
"message": "扫码账号与待重新授权账号不一致,已拒绝覆盖;请使用正确账号重新扫码",
|
||||
})
|
||||
writeErrDetails(
|
||||
w, http.StatusConflict,
|
||||
"qr_account_mismatch",
|
||||
"扫码账号与待重新授权账号不一致,已拒绝覆盖;请使用正确账号重新扫码",
|
||||
"", map[string]any{"scanned_account_id": unb})
|
||||
return
|
||||
}
|
||||
resp := map[string]any{
|
||||
@@ -176,9 +176,9 @@ func (s *Server) completeQRVerification(w http.ResponseWriter, r *http.Request)
|
||||
if s.Logger != nil {
|
||||
s.Logger.Warn("保存扫码验证结果失败", "session_id", sessionID, "err", persistErr)
|
||||
}
|
||||
resp["success"] = false
|
||||
resp["message"] = "保存扫码登录结果失败: " + persistErr.Error()
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
writeErrCode(
|
||||
w, http.StatusInternalServerError, "qr_login_persist_failed",
|
||||
"保存扫码登录结果失败: "+persistErr.Error(), "")
|
||||
return
|
||||
}
|
||||
resp["account_id"] = persisted.AccountID
|
||||
|
||||
@@ -329,7 +329,7 @@ func TestCompleteQRVerificationRejectsDifferentTarget(t *testing.T) {
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
if rec.Code != http.StatusConflict {
|
||||
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var result map[string]any
|
||||
@@ -340,7 +340,7 @@ func TestCompleteQRVerificationRejectsDifferentTarget(t *testing.T) {
|
||||
}
|
||||
|
||||
result := request()
|
||||
if result["success"] != false || result["scanned_account_id"] != "scanned-other" {
|
||||
if result["code"] != "qr_account_mismatch" || result["details"].(map[string]any)["scanned_account_id"] != "scanned-other" {
|
||||
t.Fatalf("response=%+v", result)
|
||||
}
|
||||
original, _ := store.Cookies.GetValue(context.Background(), "acc1")
|
||||
|
||||
Reference in new issue
Block a user