重构:完成架构一致性整改与生命周期收口
This commit is contained in:
1 parent
55904cc409
commit
9eb6be2875
344 files changed
+15961
-13926
No files matched your search
+108
-108
@@ -11,20 +11,20 @@ import (
|
||||
|
||||
// TestKeywordsCRUD 关键字增删查。
|
||||
func TestKeywordsCRUD(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// 添加(普通 + 商品ID)。
|
||||
post := func(body string) {
|
||||
// req 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodPost, "/keywords/acc1", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
@@ -37,10 +37,10 @@ func TestKeywordsCRUD(t *testing.T) {
|
||||
// 列表(带类型)。
|
||||
req := httptest.NewRequest(http.MethodGet, "/keywords-with-type/acc1", nil)
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
// arr 保存arr,供当前处理流程使用
|
||||
// arr 用于本次流程后续判断的arr
|
||||
var arr []map[string]any
|
||||
json.Unmarshal(rec.Body.Bytes(), &arr)
|
||||
if len(arr) != 2 {
|
||||
@@ -50,7 +50,7 @@ func TestKeywordsCRUD(t *testing.T) {
|
||||
// 按索引删除第一条。
|
||||
req2 := httptest.NewRequest(http.MethodDelete, "/keywords/acc1/0", nil)
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
// rec2 用于本次流程后续判断的rec2
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
@@ -60,20 +60,20 @@ func TestKeywordsCRUD(t *testing.T) {
|
||||
|
||||
// TestDefaultReplyCRUD 默认回复。
|
||||
func TestDefaultReplyCRUD(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// 设置。
|
||||
body := `{"enabled":true,"reply_content":"你好老板","reply_once":true}`
|
||||
// req 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodPut, "/default-replies/acc1", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
@@ -83,10 +83,10 @@ func TestDefaultReplyCRUD(t *testing.T) {
|
||||
// 读取。
|
||||
req2 := httptest.NewRequest(http.MethodGet, "/default-replies/acc1", nil)
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
// rec2 用于本次流程后续判断的rec2
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
// dr 保存dr,供当前处理流程使用
|
||||
// dr 用于本次流程后续判断的dr
|
||||
var dr map[string]any
|
||||
json.Unmarshal(rec2.Body.Bytes(), &dr)
|
||||
if dr["enabled"] != true || dr["reply_content"] != "你好老板" || dr["reply_once"] != true {
|
||||
@@ -96,28 +96,28 @@ func TestDefaultReplyCRUD(t *testing.T) {
|
||||
// 前端兼容路径:/api/default-reply/* 与 /api/default-replies。
|
||||
req3 := httptest.NewRequest(http.MethodPut, "/api/default-reply/acc1", strings.NewReader(body))
|
||||
req3.AddCookie(cookie)
|
||||
// rec3 保存rec3,供当前处理流程使用
|
||||
// rec3 用于本次流程后续判断的rec3
|
||||
rec3 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec3, req3)
|
||||
if rec3.Code != 200 {
|
||||
t.Fatalf("compat set status=%d", rec3.Code)
|
||||
}
|
||||
// req4 保存req4,供当前处理流程使用
|
||||
// req4 用于本次流程后续判断的req4
|
||||
req4 := httptest.NewRequest(http.MethodGet, "/api/default-replies", nil)
|
||||
req4.AddCookie(cookie)
|
||||
// rec4 保存rec4,供当前处理流程使用
|
||||
// rec4 用于本次流程后续判断的rec4
|
||||
rec4 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec4, req4)
|
||||
// all 保存all,供当前处理流程使用
|
||||
// all 用于本次流程后续判断的all
|
||||
var all map[string]map[string]any
|
||||
json.Unmarshal(rec4.Body.Bytes(), &all)
|
||||
if all["acc1"]["reply_content"] != "你好老板" {
|
||||
t.Fatalf("兼容默认回复列表异常: %+v", all)
|
||||
}
|
||||
// req5 保存req5,供当前处理流程使用
|
||||
// req5 用于本次流程后续判断的req5
|
||||
req5 := httptest.NewRequest(http.MethodPost, "/api/default-reply/acc1/clear-records", nil)
|
||||
req5.AddCookie(cookie)
|
||||
// rec5 保存rec5,供当前处理流程使用
|
||||
// rec5 用于本次流程后续判断的rec5
|
||||
rec5 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec5, req5)
|
||||
if rec5.Code != 200 {
|
||||
@@ -127,37 +127,37 @@ func TestDefaultReplyCRUD(t *testing.T) {
|
||||
|
||||
// TestNotificationChannelCRUD 通知渠道 + 绑定。
|
||||
func TestNotificationChannelCRUD(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// 创建渠道。
|
||||
body := `{"name":"我的钉钉","type":"dingtalk","config":"{\"webhook_url\":\"http://x\"}","enabled":true}`
|
||||
// req 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodPost, "/notification-channels", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("create channel status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
// cr 保存cr,供当前处理流程使用
|
||||
// cr 用于本次流程后续判断的cr
|
||||
var cr map[string]any
|
||||
json.Unmarshal(rec.Body.Bytes(), &cr)
|
||||
// id 保存标识,供当前处理流程使用
|
||||
// id 用于本次流程后续判断的标识
|
||||
id := int64(cr["id"].(float64))
|
||||
|
||||
// 绑定到账号。
|
||||
bindBody := `{"channel_ids":[` + itoa(id) + `]}`
|
||||
// req2 保存req2,供当前处理流程使用
|
||||
// req2 用于本次流程后续判断的req2
|
||||
req2 := httptest.NewRequest(http.MethodPost, "/message-notifications/acc1", strings.NewReader(bindBody))
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
// rec2 用于本次流程后续判断的rec2
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
@@ -167,13 +167,13 @@ func TestNotificationChannelCRUD(t *testing.T) {
|
||||
// 查询绑定。
|
||||
req3 := httptest.NewRequest(http.MethodGet, "/message-notifications/acc1", nil)
|
||||
req3.AddCookie(cookie)
|
||||
// rec3 保存rec3,供当前处理流程使用
|
||||
// rec3 用于本次流程后续判断的rec3
|
||||
rec3 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec3, req3)
|
||||
// b 保存b,供当前处理流程使用
|
||||
// b 用于本次流程后续判断的b
|
||||
var b map[string]any
|
||||
json.Unmarshal(rec3.Body.Bytes(), &b)
|
||||
// ids 保存ids,供当前处理流程使用
|
||||
// ids 用于本次流程后续判断的ids
|
||||
ids, _ := b["channel_ids"].([]any)
|
||||
if len(ids) != 1 {
|
||||
t.Fatalf("绑定数异常: %+v", b)
|
||||
@@ -182,49 +182,49 @@ func TestNotificationChannelCRUD(t *testing.T) {
|
||||
|
||||
// TestSystemSettings 系统设置。
|
||||
func TestSystemSettings(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// 公开设置无需登录。
|
||||
req := httptest.NewRequest(http.MethodGet, "/system-settings/public", nil)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("public settings status=%d", rec.Code)
|
||||
}
|
||||
// pub 保存pub,供当前处理流程使用
|
||||
// pub 用于本次流程后续判断的pub
|
||||
var pub map[string]any
|
||||
json.Unmarshal(rec.Body.Bytes(), &pub)
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
if // ok 用于本次流程后续判断的ok
|
||||
_, ok := pub["theme_color"]; !ok {
|
||||
t.Fatalf("公开设置应含 theme_color: %+v", pub)
|
||||
}
|
||||
|
||||
// 已认证:设置 + 读取全部。
|
||||
body := `{"value":"green"}`
|
||||
// req2 保存req2,供当前处理流程使用
|
||||
// req2 用于本次流程后续判断的req2
|
||||
req2 := httptest.NewRequest(http.MethodPut, "/system-settings/theme_color", strings.NewReader(body))
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
// rec2 用于本次流程后续判断的rec2
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
t.Fatalf("set status=%d", rec2.Code)
|
||||
}
|
||||
|
||||
// req3 保存req3,供当前处理流程使用
|
||||
// req3 用于本次流程后续判断的req3
|
||||
req3 := httptest.NewRequest(http.MethodGet, "/system-settings", nil)
|
||||
req3.AddCookie(cookie)
|
||||
// rec3 保存rec3,供当前处理流程使用
|
||||
// rec3 用于本次流程后续判断的rec3
|
||||
rec3 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec3, req3)
|
||||
// all 保存all,供当前处理流程使用
|
||||
// all 用于本次流程后续判断的all
|
||||
var all map[string]any
|
||||
json.Unmarshal(rec3.Body.Bytes(), &all)
|
||||
if all["theme_color"] != "green" {
|
||||
@@ -234,20 +234,20 @@ func TestSystemSettings(t *testing.T) {
|
||||
|
||||
// TestAIReplySettings AI 回复设置。
|
||||
func TestAIReplySettings(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// body 保存请求体,供当前处理流程使用
|
||||
// body 用于本次流程后续判断的请求体
|
||||
body := `{"ai_enabled":true,"max_discount_percent":12,"max_discount_amount":88,"max_bargain_rounds":4,"custom_prompts":"按商品信息回复"}`
|
||||
// req 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodPut, "/ai-reply-settings/acc1", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
@@ -257,10 +257,10 @@ func TestAIReplySettings(t *testing.T) {
|
||||
// 账号级设置只包含开关、议价策略和自定义提示词;模型/API Key/URL 走系统设置。
|
||||
req2 := httptest.NewRequest(http.MethodGet, "/ai-reply-settings/acc1", nil)
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
// rec2 用于本次流程后续判断的rec2
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
// cfg 保存cfg,供当前处理流程使用
|
||||
// cfg 用于本次流程后续判断的cfg
|
||||
var cfg map[string]any
|
||||
json.Unmarshal(rec2.Body.Bytes(), &cfg)
|
||||
if cfg["ai_enabled"] != true ||
|
||||
@@ -270,22 +270,22 @@ func TestAIReplySettings(t *testing.T) {
|
||||
cfg["custom_prompts"] != "按商品信息回复" {
|
||||
t.Fatalf("AI 设置异常: %+v", cfg)
|
||||
}
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
if // ok 用于本次流程后续判断的ok
|
||||
_, ok := cfg["model_name"]; ok {
|
||||
t.Fatalf("账号级 AI 设置不应返回模型: %+v", cfg)
|
||||
}
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
if // ok 用于本次流程后续判断的ok
|
||||
_, ok := cfg["api_key"]; ok {
|
||||
t.Fatalf("账号级 AI 设置不应返回 API Key: %+v", cfg)
|
||||
}
|
||||
|
||||
// req3 保存req3,供当前处理流程使用
|
||||
// req3 用于本次流程后续判断的req3
|
||||
req3 := httptest.NewRequest(http.MethodGet, "/ai-reply-settings", nil)
|
||||
req3.AddCookie(cookie)
|
||||
// rec3 保存rec3,供当前处理流程使用
|
||||
// rec3 用于本次流程后续判断的rec3
|
||||
rec3 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec3, req3)
|
||||
// all 保存all,供当前处理流程使用
|
||||
// all 用于本次流程后续判断的all
|
||||
var all map[string]map[string]any
|
||||
json.Unmarshal(rec3.Body.Bytes(), &all)
|
||||
if all["acc1"]["ai_enabled"] != true {
|
||||
@@ -293,14 +293,14 @@ func TestAIReplySettings(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestAIReplySettingsRejectInvalidBargainLimits 负责TestAI回复设置RejectInvalidBargainLimits相关处理。
|
||||
// TestAIReplySettingsRejectInvalidBargainLimits 封装TestAI回复设置RejectInvalidBargainLimits业务协调。
|
||||
func TestAIReplySettingsRejectInvalidBargainLimits(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// body 表示当前遍历过程中的请求体
|
||||
@@ -309,10 +309,10 @@ func TestAIReplySettingsRejectInvalidBargainLimits(t *testing.T) {
|
||||
`{"ai_enabled":true,"max_discount_percent":1,"max_discount_amount":-1,"max_bargain_rounds":1}`,
|
||||
`{"ai_enabled":true,"max_discount_percent":1,"max_discount_amount":1,"max_bargain_rounds":0}`,
|
||||
} {
|
||||
// req 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodPut, "/ai-reply-settings/acc1", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
@@ -321,14 +321,14 @@ func TestAIReplySettingsRejectInvalidBargainLimits(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseOpenAIModels 负责TestParseOpenAI模型列表相关处理。
|
||||
// TestParseOpenAIModels 封装TestParseOpenAI模型列表业务协调。
|
||||
func TestParseOpenAIModels(t *testing.T) {
|
||||
// models、err 保存models、err,供当前处理流程使用
|
||||
// models、err 用于本次流程后续判断的models、err
|
||||
models, err := parseOpenAIModels([]byte(`{"data":[{"id":"qwen-plus"},{"id":"qwen-max"},{"name":"fallback-name"}]}`))
|
||||
if err != nil {
|
||||
t.Fatalf("parse models: %v", err)
|
||||
}
|
||||
// want 保存want,供当前处理流程使用
|
||||
// want 用于本次流程后续判断的want
|
||||
want := []string{"qwen-plus", "qwen-max", "fallback-name"}
|
||||
if len(models) != len(want) {
|
||||
t.Fatalf("models length=%d want=%d: %+v", len(models), len(want), models)
|
||||
@@ -343,24 +343,24 @@ func TestParseOpenAIModels(t *testing.T) {
|
||||
|
||||
// TestItems 物品列表 + 多规格设置。
|
||||
func TestItems(t *testing.T) {
|
||||
// srv、store、cleanup 保存srv、store、cleanup,供当前处理流程使用
|
||||
// srv、store、cleanup 用于本次流程后续判断的srv、store、cleanup
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// ctx 保存ctx,供当前处理流程使用
|
||||
// ctx 用于本次流程后续判断的ctx
|
||||
ctx := context.Background()
|
||||
store.DB.ExecContext(ctx, `INSERT INTO item_info (cookie_id, item_id, item_title) VALUES ('acc1','it1','商品A')`)
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// 列表。
|
||||
req := httptest.NewRequest(http.MethodGet, "/items", nil)
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
// arr 保存arr,供当前处理流程使用
|
||||
// arr 用于本次流程后续判断的arr
|
||||
var arr []map[string]any
|
||||
json.Unmarshal(rec.Body.Bytes(), &arr)
|
||||
if len(arr) != 1 || arr[0]["item_title"] != "商品A" {
|
||||
@@ -369,10 +369,10 @@ func TestItems(t *testing.T) {
|
||||
|
||||
// 设置多规格。
|
||||
body := `{"is_multi_spec":true}`
|
||||
// req2 保存req2,供当前处理流程使用
|
||||
// req2 用于本次流程后续判断的req2
|
||||
req2 := httptest.NewRequest(http.MethodPut, "/items/acc1/it1/multi-spec", strings.NewReader(body))
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
// rec2 用于本次流程后续判断的rec2
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
@@ -389,45 +389,45 @@ func TestItems(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestOrderImportCompat 负责Test订单ImportCompat相关处理。
|
||||
// TestOrderImportCompat 封装Test订单ImportCompat业务协调。
|
||||
func TestOrderImportCompat(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// body 保存请求体,供当前处理流程使用
|
||||
// body 用于本次流程后续判断的请求体
|
||||
body := `[{"order_id":"order-import-1","item_id":"item-import-1","item_title":"导入商品","buyer_id":"buyer1","status":"pending_ship","quantity":2,"amount":"19.90"}]`
|
||||
// req 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/orders/import", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("import status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
// res 保存响应,供当前处理流程使用
|
||||
// res 用于本次流程后续判断的响应
|
||||
var res map[string]any
|
||||
json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
if res["success_count"] != float64(1) {
|
||||
t.Fatalf("导入结果异常: %+v", res)
|
||||
}
|
||||
|
||||
// req2 保存req2,供当前处理流程使用
|
||||
// req2 用于本次流程后续判断的req2
|
||||
req2 := httptest.NewRequest(http.MethodGet, "/api/orders/order-import-1", nil)
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
// rec2 用于本次流程后续判断的rec2
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
t.Fatalf("get imported order status=%d body=%s", rec2.Code, rec2.Body.String())
|
||||
}
|
||||
// order 保存订单,供当前处理流程使用
|
||||
// order 用于本次流程后续判断的订单
|
||||
var order map[string]any
|
||||
json.Unmarshal(rec2.Body.Bytes(), &order)
|
||||
if order["cookie_id"] != "acc1" || order["status"] != "pending_ship" {
|
||||
@@ -435,34 +435,34 @@ func TestOrderImportCompat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestOrderImportReportsPartialFailure 负责Test订单ImportReportsPartialFailure相关处理。
|
||||
// TestOrderImportReportsPartialFailure 封装Test订单ImportReportsPartialFailure业务协调。
|
||||
func TestOrderImportReportsPartialFailure(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
// body 保存请求体,供当前处理流程使用
|
||||
// body 用于本次流程后续判断的请求体
|
||||
body := `[{"order_id":"order-ok","status":"pending_ship"},{"item_id":"missing-order-id"}]`
|
||||
// req 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/orders/import", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
// result 保存结果,供当前处理流程使用
|
||||
// result 用于本次流程后续判断的结果
|
||||
var result struct {
|
||||
PartialFailure bool `json:"partial_failure"`
|
||||
SuccessCount int `json:"success_count"`
|
||||
FailedCount int `json:"failed_count"`
|
||||
}
|
||||
if // err 保存err,供当前处理流程使用
|
||||
if // err 用于本次流程后续判断的err
|
||||
err := json.Unmarshal(rec.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -473,25 +473,25 @@ func TestOrderImportReportsPartialFailure(t *testing.T) {
|
||||
|
||||
// TestAdminEndpoints 管理员统计 + 用户列表 + 非 admin 被拒。
|
||||
func TestAdminEndpoints(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// stats。
|
||||
// req 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodGet, "/admin/stats", nil)
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("stats status=%d", rec.Code)
|
||||
}
|
||||
// stats 保存stats,供当前处理流程使用
|
||||
// stats 用于本次流程后续判断的stats
|
||||
var stats map[string]any
|
||||
json.Unmarshal(rec.Body.Bytes(), &stats)
|
||||
if stats["total_users"] == nil || stats["total_cookies"] == nil {
|
||||
@@ -501,10 +501,10 @@ func TestAdminEndpoints(t *testing.T) {
|
||||
// 用户列表。
|
||||
req2 := httptest.NewRequest(http.MethodGet, "/admin/users", nil)
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
// rec2 用于本次流程后续判断的rec2
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
// users 保存用户列表,供当前处理流程使用
|
||||
// users 用于本次流程后续判断的用户列表
|
||||
var users []map[string]any
|
||||
json.Unmarshal(rec2.Body.Bytes(), &users)
|
||||
if len(users) != 1 || users[0]["username"] != "admin" {
|
||||
@@ -516,10 +516,10 @@ func TestAdminEndpoints(t *testing.T) {
|
||||
// 普通用户不应能访问 admin(需单独登录验证,此处仅验证 admin 能看到)。
|
||||
req3 := httptest.NewRequest(http.MethodGet, "/admin/users", nil)
|
||||
req3.AddCookie(cookie)
|
||||
// rec3 保存rec3,供当前处理流程使用
|
||||
// rec3 用于本次流程后续判断的rec3
|
||||
rec3 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec3, req3)
|
||||
// users2 保存users2,供当前处理流程使用
|
||||
// users2 用于本次流程后续判断的users2
|
||||
var users2 []map[string]any
|
||||
json.Unmarshal(rec3.Body.Bytes(), &users2)
|
||||
if len(users2) != 2 {
|
||||
@@ -529,15 +529,15 @@ func TestAdminEndpoints(t *testing.T) {
|
||||
|
||||
// TestPublicSettingsNoAuth 公开设置无需登录。
|
||||
func TestPublicSettingsNoAuth(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
|
||||
// req 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodGet, "/system-settings/public", nil)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
|
||||
Reference in new issue
Block a user