重构:完成架构一致性整改与生命周期收口
This commit is contained in:
1 parent
55904cc409
commit
9eb6be2875
344 files changed
+15961
-13926
No files matched your search
@@ -11,20 +11,20 @@ import (
|
||||
|
||||
// TestListDefaultReplies 列表(数组形式)。
|
||||
func TestListDefaultReplies(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":false}`
|
||||
// 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 {
|
||||
@@ -34,13 +34,13 @@ func TestListDefaultReplies(t *testing.T) {
|
||||
// 列表(数组)。
|
||||
req2 := httptest.NewRequest(http.MethodGet, "/default-replies", nil)
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
// rec2 用于本次流程后续判断的rec2
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
t.Fatalf("list status=%d", rec2.Code)
|
||||
}
|
||||
// arr 保存arr,供当前处理流程使用
|
||||
// arr 用于本次流程后续判断的arr
|
||||
var arr []map[string]any
|
||||
json.Unmarshal(rec2.Body.Bytes(), &arr)
|
||||
if len(arr) != 1 || arr[0]["reply_content"] != "你好" {
|
||||
@@ -50,13 +50,13 @@ func TestListDefaultReplies(t *testing.T) {
|
||||
// 列表(map 形式,兼容路径)。
|
||||
req3 := httptest.NewRequest(http.MethodGet, "/api/default-replies", nil)
|
||||
req3.AddCookie(cookie)
|
||||
// rec3 保存rec3,供当前处理流程使用
|
||||
// rec3 用于本次流程后续判断的rec3
|
||||
rec3 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec3, req3)
|
||||
if rec3.Code != 200 {
|
||||
t.Fatalf("map status=%d", rec3.Code)
|
||||
}
|
||||
// m 保存m,供当前处理流程使用
|
||||
// m 用于本次流程后续判断的m
|
||||
var m map[string]map[string]any
|
||||
json.Unmarshal(rec3.Body.Bytes(), &m)
|
||||
if m["acc1"]["reply_content"] != "你好" {
|
||||
@@ -66,18 +66,18 @@ func TestListDefaultReplies(t *testing.T) {
|
||||
|
||||
// TestSetDefaultReplyBadJSON 非法 JSON 400。
|
||||
func TestSetDefaultReplyBadJSON(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 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodPut, "/default-replies/acc1", strings.NewReader("not-json"))
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
@@ -85,20 +85,20 @@ func TestSetDefaultReplyBadJSON(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetDefaultReplyMissingAccountIsNotFound 负责TestGetDefault回复Missing账号IsNotFound相关处理。
|
||||
// TestGetDefaultReplyMissingAccountIsNotFound 封装TestGetDefault回复Missing账号IsNotFound业务协调。
|
||||
func TestGetDefaultReplyMissingAccountIsNotFound(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 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodGet, "/default-replies/no-such", nil)
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusNotFound {
|
||||
@@ -106,26 +106,26 @@ func TestGetDefaultReplyMissingAccountIsNotFound(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetDefaultReplyExistingAccountWithoutConfigReturnsDefault 负责TestGetDefault回复Existing账号Without配置ReturnsDefault相关处理。
|
||||
// TestGetDefaultReplyExistingAccountWithoutConfigReturnsDefault 封装TestGetDefault回复Existing账号Without配置ReturnsDefault业务协调。
|
||||
func TestGetDefaultReplyExistingAccountWithoutConfigReturnsDefault(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 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodGet, "/default-replies/acc1", nil)
|
||||
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())
|
||||
}
|
||||
// got 保存got,供当前处理流程使用
|
||||
// got 用于本次流程后续判断的got
|
||||
var got map[string]any
|
||||
json.Unmarshal(rec.Body.Bytes(), &got)
|
||||
if got["enabled"] != false || got["reply_content"] != "" {
|
||||
@@ -133,35 +133,35 @@ func TestGetDefaultReplyExistingAccountWithoutConfigReturnsDefault(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetDefaultReplyRejectsCrossUserAccount 负责TestGetDefault回复RejectsCross用户账号相关处理。
|
||||
// TestGetDefaultReplyRejectsCrossUserAccount 封装TestGetDefault回复RejectsCross用户账号业务协调。
|
||||
func TestGetDefaultReplyRejectsCrossUserAccount(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()
|
||||
if // err 保存err,供当前处理流程使用
|
||||
if // err 用于本次流程后续判断的err
|
||||
_, err := store.Users.Create(ctx, "default-other", "[email protected]", "pw"); err != nil {
|
||||
t.Fatalf("create other user: %v", err)
|
||||
}
|
||||
// other、err 保存other、err,供当前处理流程使用
|
||||
// other、err 用于本次流程后续判断的other、err
|
||||
other, err := store.Users.GetByUsername(ctx, "default-other")
|
||||
if err != nil {
|
||||
t.Fatalf("get other user: %v", err)
|
||||
}
|
||||
if // err 保存err,供当前处理流程使用
|
||||
if // err 用于本次流程后续判断的err
|
||||
err := store.Cookies.Save(ctx, "other-acc", "unb=456; _m_h5_tk=tk2_1;", other.ID); err != nil {
|
||||
t.Fatalf("save other cookie: %v", err)
|
||||
}
|
||||
// h 保存h,供当前处理流程使用
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
// cookie 用于本次流程后续判断的登录凭证
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// req 保存req,供当前处理流程使用
|
||||
// req 用于本次流程后续判断的req
|
||||
req := httptest.NewRequest(http.MethodGet, "/default-replies/other-acc", nil)
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
// rec 用于本次流程后续判断的rec
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusForbidden {
|
||||
@@ -171,27 +171,27 @@ func TestGetDefaultReplyRejectsCrossUserAccount(t *testing.T) {
|
||||
|
||||
// TestDeleteDefaultReply 删除。
|
||||
func TestDeleteDefaultReply(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":"你好"}`
|
||||
// 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)
|
||||
|
||||
// 删除。
|
||||
req2 := httptest.NewRequest(http.MethodDelete, "/default-replies/acc1", nil)
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
// rec2 用于本次流程后续判断的rec2
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
@@ -211,11 +211,11 @@ func TestBtoi(t *testing.T) {
|
||||
|
||||
// TestNullIfEmpty nullIfEmpty 表驱动。
|
||||
func TestNullIfEmpty(t *testing.T) {
|
||||
if // v 保存v,供当前处理流程使用
|
||||
if // v 用于本次流程后续判断的v
|
||||
v := nullIfEmpty(""); v != nil {
|
||||
t.Fatalf("空串应为 nil,got %v", v)
|
||||
}
|
||||
if // v 保存v,供当前处理流程使用
|
||||
if // v 用于本次流程后续判断的v
|
||||
v := nullIfEmpty("x"); v != "x" {
|
||||
t.Fatalf("非空应原样返回,got %v", v)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user