Files
Ydisks-Xianyu-Helper/internal/server/account_summary_handlers_test.go
Christ 375e84d663 重构:统一应用服务与基础设施适配边界
将账号、认证、管理员、分析、自动化、卡券、聊天、商品、关键词、设置和订单能力迁移到 application 与 adapter,删除 Server 内对应的旧仓储和业务服务实现,并收紧架构检查。

完善批量发布与订单刷新生命周期、凭证写回、账号任务补偿、订单 reconciliation、通知和敏感数据边界;补充正常、失败、取消、并发、越权及多方言回归测试,并修正 MySQL 延迟任务表的 InnoDB 配置。

修复空卡密响应为 null 时前端加载失败的问题,重建嵌入式前端资产;重写重构总计划,清除历史流水并明确阶段 1 至 10 各对应一次完整迭代和一个 PR。

验证:make check;make test-server-race;npm test/typecheck/comments:check/build;Docker SQLite/MySQL 8.4/PostgreSQL 17 TestMultiDB_*。
2026-08-16 23:51:06 +08:00

88 lines
3.7 KiB
Go

package server
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"xianyu-go/internal/auth"
"xianyu-go/internal/db"
)
// TestRequireCookieOwnershipPreservesHTTPCompatibility 验证账号所有权辅助函数保留未登录、越权、缺失和成功状态码。
func TestRequireCookieOwnershipPreservesHTTPCompatibility(t *testing.T) {
// srv、store、cleanup 保存当前测试使用的 HTTP 服务和数据库。
srv, store, cleanup := newTestServer(t)
defer cleanup()
// ctx 是本测试共用的数据库上下文。
ctx := context.Background()
// _, createErr 保存第二个用户创建错误;用户主键随后从查询得到。
_, createErr := store.Users.Create(ctx, "summary-other", "summary-other@example.com", "pw")
if createErr != nil {
t.Fatalf("创建第二用户失败: %v", createErr)
}
// other、otherLookupErr 保存第二个用户及读取错误。
other, otherLookupErr := store.Users.GetByUsername(ctx, "summary-other")
if otherLookupErr != nil {
t.Fatalf("读取第二用户失败: %v", otherLookupErr)
}
// saveErr 保存第二个用户账号的创建错误。
if saveErr := store.Cookies.Save(ctx, "other-account", "unb=other", other.ID); saveErr != nil {
t.Fatalf("创建第二用户账号失败: %v", saveErr)
}
// cases 覆盖所有权辅助函数的 HTTP 兼容分支。
cases := []struct {
// name 是当前 HTTP 所有权场景名称。
name string
// session 是请求上下文中的认证会话;nil 表示未登录。
session *db.Session
// accountID 是待校验账号标识。
accountID string
// wantStatus 是预期错误响应状态;成功场景为 0。
wantStatus int
// wantOwned 表示预期的所有权结论。
wantOwned bool
}{
{name: "unauthenticated", accountID: "acc1", wantStatus: http.StatusUnauthorized},
{name: "owned", session: &db.Session{UserID: 1}, accountID: "acc1", wantOwned: true},
{name: "forbidden", session: &db.Session{UserID: 1}, accountID: "other-account", wantStatus: http.StatusForbidden},
{name: "missing", session: &db.Session{UserID: 1}, accountID: "missing-account", wantStatus: http.StatusNotFound},
}
// testCase 表示当前正在验证的 HTTP 所有权场景。
for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
// request 是注入测试会话后的所有权请求。
request := httptest.NewRequest(http.MethodGet, "/items", nil)
if testCase.session != nil {
request = request.WithContext(auth.WithSession(request.Context(), testCase.session))
}
// recorder 捕获所有权辅助函数的 HTTP 错误响应。
recorder := httptest.NewRecorder()
// owned 保存应用 Port 返回的所有权结论。
owned := srv.requireCookieOwnership(recorder, request, testCase.accountID)
if owned != testCase.wantOwned {
t.Fatalf("owned=%v,期望=%v", owned, testCase.wantOwned)
}
if testCase.wantStatus != 0 && recorder.Code != testCase.wantStatus {
t.Fatalf("status=%d,期望=%d body=%s", recorder.Code, testCase.wantStatus, recorder.Body.String())
}
})
}
}
// TestLoadCookieSummaryDetailUsesNonSensitiveApplicationModel 验证 Server 摘要兼容模型不携带 Cookie 或密码。
func TestLoadCookieSummaryDetailUsesNonSensitiveApplicationModel(t *testing.T) {
// srv、cleanup 保存当前测试使用的 HTTP 服务。
srv, _, cleanup := newTestServer(t)
defer cleanup()
// detail、loadErr 保存应用 Port 返回的非敏感摘要及读取错误。
detail, loadErr := srv.loadCookieSummaryDetail(context.Background(), 1, "acc1")
if loadErr != nil {
t.Fatalf("读取账号摘要失败: %v", loadErr)
}
if detail.ID != "acc1" || detail.UserID != 1 {
t.Fatalf("摘要模型不完整: %+v", detail)
}
}