API契约:收尾兼容响应并审计版本入口
This commit is contained in:
1 parent
929b3fd8a9
commit
6ff3499ca9
8 files changed
+317
-37
No files matched your search
@@ -155,9 +155,9 @@ func (s *Server) completeQRVerification(w http.ResponseWriter, r *http.Request)
|
||||
"", map[string]any{"scanned_account_id": unb})
|
||||
return
|
||||
}
|
||||
resp := map[string]any{
|
||||
"success": true,
|
||||
"unb": unb,
|
||||
resp := qrLoginVerificationResponse{
|
||||
Success: true, UNB: unb,
|
||||
// 验证完成响应保留平台账号标识,兼容旧客户端。
|
||||
}
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
if sess != nil {
|
||||
@@ -181,8 +181,8 @@ func (s *Server) completeQRVerification(w http.ResponseWriter, r *http.Request)
|
||||
"保存扫码登录结果失败: "+persistErr.Error(), "")
|
||||
return
|
||||
}
|
||||
resp["account_id"] = persisted.AccountID
|
||||
resp["is_new_account"] = persisted.IsNew
|
||||
resp.AccountID = persisted.AccountID
|
||||
resp.IsNewAccount = persisted.IsNew
|
||||
}
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
@@ -352,8 +352,8 @@ func cloneQRStatus(src map[string]any) map[string]any {
|
||||
|
||||
// publicQRStatus 返回可暴露给浏览器的扫码状态。闲鱼 Cookie 只在服务端持久化,
|
||||
// 永远不进入前端、浏览器日志或代理响应。
|
||||
func publicQRStatus(src map[string]any) map[string]any {
|
||||
dst := cloneQRStatus(src)
|
||||
func publicQRStatus(src map[string]any) qrLoginStatusResponse {
|
||||
dst := qrLoginStatusResponse(cloneQRStatus(src))
|
||||
delete(dst, "cookies")
|
||||
delete(dst, "cookie_snapshot")
|
||||
return dst
|
||||
|
||||
@@ -73,7 +73,7 @@ func (s *Server) publicSettings(w http.ResponseWriter, r *http.Request) {
|
||||
writeErrRequest(w, r, http.StatusInternalServerError, "查询失败")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, m)
|
||||
writeJSON(w, http.StatusOK, settingsResponse(m))
|
||||
}
|
||||
|
||||
func (s *Server) allSettings(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -82,7 +82,7 @@ func (s *Server) allSettings(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusInternalServerError, "查询失败")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, m)
|
||||
writeJSON(w, http.StatusOK, settingsResponse(m))
|
||||
}
|
||||
|
||||
func (s *Server) setSetting(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -398,7 +398,7 @@ func (s *Server) listUserSettings(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusInternalServerError, "查询失败")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, m)
|
||||
writeJSON(w, http.StatusOK, settingsResponse(m))
|
||||
}
|
||||
|
||||
func (s *Server) getUserSetting(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -371,6 +371,130 @@ func TestSettingsCardsNotificationsBatchContracts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestDynamicCompatibilitySuccessContracts 验证动态设置与二维码兼容响应保留明确边界。
|
||||
func TestDynamicCompatibilitySuccessContracts(t *testing.T) {
|
||||
// srv 是用于验证动态成功响应的 HTTP 测试服务。
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// handler 是当前测试使用的完整路由树。
|
||||
handler := srv.Router()
|
||||
// sessionCookie 是管理员登录后得到的认证会话。
|
||||
sessionCookie := loginHelper(t, handler)
|
||||
|
||||
// publicSettingsReq 是读取公开系统设置的请求。
|
||||
publicSettingsReq := httptest.NewRequest(http.MethodGet, "/system-settings/public", nil)
|
||||
// publicSettingsRecorder 是捕获公开设置响应的记录器。
|
||||
publicSettingsRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(publicSettingsRecorder, publicSettingsReq)
|
||||
if publicSettingsRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("public settings status=%d body=%s", publicSettingsRecorder.Code, publicSettingsRecorder.Body.String())
|
||||
}
|
||||
// publicSettingsValue 是公开设置动态键值响应。
|
||||
var publicSettingsValue settingsResponse
|
||||
// publicSettingsDecodeErr 是公开设置响应反序列化失败的原因。
|
||||
if publicSettingsDecodeErr := json.Unmarshal(publicSettingsRecorder.Body.Bytes(), &publicSettingsValue); publicSettingsDecodeErr != nil {
|
||||
t.Fatalf("decode public settings response: %v", publicSettingsDecodeErr)
|
||||
}
|
||||
if publicSettingsValue == nil {
|
||||
t.Fatal("public settings response must be an object")
|
||||
}
|
||||
|
||||
// allSettingsReq 是读取管理员系统设置的请求。
|
||||
allSettingsReq := httptest.NewRequest(http.MethodGet, "/system-settings", nil)
|
||||
allSettingsReq.AddCookie(sessionCookie)
|
||||
// allSettingsRecorder 是捕获管理员设置响应的记录器。
|
||||
allSettingsRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(allSettingsRecorder, allSettingsReq)
|
||||
if allSettingsRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("all settings status=%d body=%s", allSettingsRecorder.Code, allSettingsRecorder.Body.String())
|
||||
}
|
||||
// allSettingsValue 是管理员设置动态键值响应。
|
||||
var allSettingsValue settingsResponse
|
||||
// allSettingsDecodeErr 是管理员设置响应反序列化失败的原因。
|
||||
if allSettingsDecodeErr := json.Unmarshal(allSettingsRecorder.Body.Bytes(), &allSettingsValue); allSettingsDecodeErr != nil {
|
||||
t.Fatalf("decode all settings response: %v", allSettingsDecodeErr)
|
||||
}
|
||||
if allSettingsValue == nil {
|
||||
t.Fatal("all settings response must be an object")
|
||||
}
|
||||
|
||||
// userSettingsReq 是读取当前用户设置的请求。
|
||||
userSettingsReq := httptest.NewRequest(http.MethodGet, "/user-settings", nil)
|
||||
userSettingsReq.AddCookie(sessionCookie)
|
||||
// userSettingsRecorder 是捕获用户设置响应的记录器。
|
||||
userSettingsRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(userSettingsRecorder, userSettingsReq)
|
||||
if userSettingsRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("user settings status=%d body=%s", userSettingsRecorder.Code, userSettingsRecorder.Body.String())
|
||||
}
|
||||
// userSettingsValue 是用户设置动态键值响应。
|
||||
var userSettingsValue settingsResponse
|
||||
// userSettingsDecodeErr 是用户设置响应反序列化失败的原因。
|
||||
if userSettingsDecodeErr := json.Unmarshal(userSettingsRecorder.Body.Bytes(), &userSettingsValue); userSettingsDecodeErr != nil {
|
||||
t.Fatalf("decode user settings response: %v", userSettingsDecodeErr)
|
||||
}
|
||||
if userSettingsValue == nil {
|
||||
t.Fatal("user settings response must be an object")
|
||||
}
|
||||
|
||||
// srv.QRLogin 是二维码状态与验证的测试替身。
|
||||
srv.QRLogin = &fakeQRLoginService{status: map[string]any{
|
||||
"status": "waiting",
|
||||
"session_id": "contract-qr",
|
||||
"custom_field": "kept",
|
||||
"cookies": "must-not-leak",
|
||||
}}
|
||||
// statusSessionID 是二维码状态测试会话标识。
|
||||
statusSessionID := "contract-qr"
|
||||
ownQRSession(t, srv, store, statusSessionID)
|
||||
// qrStatusReq 是读取二维码状态的请求。
|
||||
qrStatusReq := httptest.NewRequest(http.MethodGet, "/qr-login/check/"+statusSessionID, nil)
|
||||
qrStatusReq.AddCookie(sessionCookie)
|
||||
// qrStatusRecorder 是捕获二维码状态响应的记录器。
|
||||
qrStatusRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(qrStatusRecorder, qrStatusReq)
|
||||
if qrStatusRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("qr status=%d body=%s", qrStatusRecorder.Code, qrStatusRecorder.Body.String())
|
||||
}
|
||||
// qrStatusValue 是二维码状态兼容响应。
|
||||
var qrStatusValue qrLoginStatusResponse
|
||||
// qrStatusDecodeErr 是二维码状态响应反序列化失败的原因。
|
||||
if qrStatusDecodeErr := json.Unmarshal(qrStatusRecorder.Body.Bytes(), &qrStatusValue); qrStatusDecodeErr != nil {
|
||||
t.Fatalf("decode qr status response: %v", qrStatusDecodeErr)
|
||||
}
|
||||
if qrStatusValue["status"] != "waiting" || qrStatusValue["custom_field"] != "kept" {
|
||||
t.Fatalf("qr status response=%+v", qrStatusValue)
|
||||
}
|
||||
// leaked 表示二维码状态响应是否意外包含敏感 Cookie 字段。
|
||||
if _, leaked := qrStatusValue["cookies"]; leaked {
|
||||
t.Fatalf("qr status response leaked cookies: %+v", qrStatusValue)
|
||||
}
|
||||
|
||||
// srv.QRLogin 替换为完成验证场景的测试替身。
|
||||
srv.QRLogin = &fakeQRLoginService{completeCookies: "unb=contract-unb; _m_h5_tk=token;", completeUNB: "contract-unb"}
|
||||
// completeSessionID 是二维码验证完成测试会话标识。
|
||||
completeSessionID := "contract-complete"
|
||||
ownQRSession(t, srv, store, completeSessionID)
|
||||
// completeReq 是提交二维码风控验证完成的请求。
|
||||
completeReq := httptest.NewRequest(http.MethodPost, "/qr-login/complete-verification/"+completeSessionID, nil)
|
||||
completeReq.AddCookie(sessionCookie)
|
||||
// completeRecorder 是捕获验证完成响应的记录器。
|
||||
completeRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(completeRecorder, completeReq)
|
||||
if completeRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("complete verification status=%d body=%s", completeRecorder.Code, completeRecorder.Body.String())
|
||||
}
|
||||
// completeValue 是二维码验证完成具名响应。
|
||||
var completeValue qrLoginVerificationResponse
|
||||
// completeDecodeErr 是验证完成响应反序列化失败的原因。
|
||||
if completeDecodeErr := json.Unmarshal(completeRecorder.Body.Bytes(), &completeValue); completeDecodeErr != nil {
|
||||
t.Fatalf("decode complete verification response: %v", completeDecodeErr)
|
||||
}
|
||||
if !completeValue.Success || completeValue.UNB != "contract-unb" || completeValue.AccountID != "contract-unb" {
|
||||
t.Fatalf("complete verification response=%+v", completeValue)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReplyAndAccountTaskSuccessResponseContracts 验证回复规则、默认回复和账号任务成功响应的具名 DTO。
|
||||
func TestReplyAndAccountTaskSuccessResponseContracts(t *testing.T) {
|
||||
// srv 是用于验证回复和账号任务响应的 HTTP 测试服务。
|
||||
|
||||
@@ -1251,3 +1251,21 @@ type qrLoginGenerateResponse struct {
|
||||
// Message 是可选的提示文本。
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
// settingsResponse 是动态设置查询的具名边界类型,键和值仍由设置仓库定义。
|
||||
type settingsResponse map[string]string
|
||||
|
||||
// qrLoginStatusResponse 是二维码状态的兼容响应类型,允许上游扩展非敏感字段。
|
||||
type qrLoginStatusResponse map[string]any
|
||||
|
||||
// qrLoginVerificationResponse 是二维码风控验证完成的具名响应 DTO。
|
||||
type qrLoginVerificationResponse struct {
|
||||
// Success 表示验证结果是否成功。
|
||||
Success bool `json:"success"`
|
||||
// UNB 是平台账号标识。
|
||||
UNB string `json:"unb"`
|
||||
// AccountID 是持久化后的本地账号标识。
|
||||
AccountID string `json:"account_id,omitempty"`
|
||||
// IsNewAccount 表示是否新建了本地账号。
|
||||
IsNewAccount bool `json:"is_new_account,omitempty"`
|
||||
}
|
||||
Reference in new issue
Block a user