重构:完成架构一致性整改与生命周期收口

This commit is contained in:
Christ committed 2026-08-18 14:05:08 +08:00
1 parent 55904cc409
commit 9eb6be2875
344 files changed
+15961 -13926

No files matched your search

+141 -141
View File
@@ -11,24 +11,24 @@ import (
// TestAdminListCookies 管理员列出所有账号。
func TestAdminListCookies(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, "/admin/cookies", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
}
// arr 保存arr,供当前处理流程使用
// arr 用于本次流程后续判断的arr
var arr []map[string]any
json.Unmarshal(rec.Body.Bytes(), &arr)
if len(arr) != 1 || arr[0]["id"] != "acc1" {
@@ -38,23 +38,23 @@ func TestAdminListCookies(t *testing.T) {
// TestAdminDeleteUser 删除其他用户。
func TestAdminDeleteUser(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.Users.Create(ctx, "user-del", "[email protected]", "pw")
// u 保存u,供当前处理流程使用
// u 用于本次流程后续判断的u
u, _ := store.Users.GetByUsername(ctx, "user-del")
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
// cookie 用于本次流程后续判断的登录凭证
cookie := loginHelper(t, h)
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodDelete, "/admin/users/"+itoa(u.ID), nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
@@ -62,57 +62,57 @@ func TestAdminDeleteUser(t *testing.T) {
}
}
// TestAdminDeleteUserRevokesSessions 负责TestAdminDelete用户RevokesSessions相关处理。
// TestAdminDeleteUserRevokesSessions 封装TestAdminDelete用户RevokesSessions业务协调。
func TestAdminDeleteUserRevokesSessions(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, "session-del", "[email protected]", "pw"); err != nil {
t.Fatalf("create user: %v", err)
}
// u、err 保存u、err,供当前处理流程使用
// u、err 用于本次流程后续判断的u、err
u, err := store.Users.GetByUsername(ctx, "session-del")
if err != nil {
t.Fatalf("get user: %v", err)
}
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// userLogin 保存用户登录,供当前处理流程使用
// userLogin 用于本次流程后续判断的用户登录
userLogin := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader(`{"username":"session-del","password":"pw"}`))
// userLoginRec 保存用户登录Rec,供当前处理流程使用
// userLoginRec 用于本次流程后续判断的用户登录Rec
userLoginRec := httptest.NewRecorder()
h.ServeHTTP(userLoginRec, userLogin)
if userLoginRec.Code != http.StatusOK || len(userLoginRec.Result().Cookies()) == 0 {
t.Fatalf("user login status=%d body=%s", userLoginRec.Code, userLoginRec.Body.String())
}
// userCookie 保存用户登录凭证,供当前处理流程使用
// userCookie 用于本次流程后续判断的用户登录凭证
userCookie := userLoginRec.Result().Cookies()[0]
// adminCookie 保存admin登录凭证,供当前处理流程使用
// adminCookie 用于本次流程后续判断的admin登录凭证
adminCookie := loginHelper(t, h)
// delReq 保存delReq,供当前处理流程使用
// delReq 用于本次流程后续判断的delReq
delReq := httptest.NewRequest(http.MethodDelete, "/admin/users/"+itoa(u.ID), nil)
delReq.AddCookie(adminCookie)
// delRec 保存delRec,供当前处理流程使用
// delRec 用于本次流程后续判断的delRec
delRec := httptest.NewRecorder()
h.ServeHTTP(delRec, delReq)
if delRec.Code != http.StatusOK {
t.Fatalf("delete status=%d body=%s", delRec.Code, delRec.Body.String())
}
// verifyReq 保存verifyReq,供当前处理流程使用
// verifyReq 用于本次流程后续判断的verifyReq
verifyReq := httptest.NewRequest(http.MethodGet, "/verify", nil)
verifyReq.AddCookie(userCookie)
// verifyRec 保存verifyRec,供当前处理流程使用
// verifyRec 用于本次流程后续判断的verifyRec
verifyRec := httptest.NewRecorder()
h.ServeHTTP(verifyRec, verifyReq)
// verify 保存verify,供当前处理流程使用
// verify 用于本次流程后续判断的verify
var verify map[string]any
if // err 保存err,供当前处理流程使用
if // err 用于本次流程后续判断的err
err := json.Unmarshal(verifyRec.Body.Bytes(), &verify); err != nil {
t.Fatalf("decode verify: %v", err)
}
@@ -123,20 +123,20 @@ func TestAdminDeleteUserRevokesSessions(t *testing.T) {
// TestAdminDeleteUserSelf 不能删除自己 400。
func TestAdminDeleteUserSelf(t *testing.T) {
// srv、store、cleanup 保存srv、store、cleanup,供当前处理流程使用
// srv、store、cleanup 用于本次流程后续判断的srv、store、cleanup
srv, store, cleanup := newTestServer(t)
defer cleanup()
// admin 保存admin,供当前处理流程使用
// admin 用于本次流程后续判断的admin
admin, _ := store.Users.GetByUsername(context.Background(), "admin")
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
// cookie 用于本次流程后续判断的登录凭证
cookie := loginHelper(t, h)
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodDelete, "/admin/users/"+itoa(admin.ID), nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -146,18 +146,18 @@ func TestAdminDeleteUserSelf(t *testing.T) {
// TestAdminDeleteUserBadID 无效 ID 400。
func TestAdminDeleteUserBadID(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.MethodDelete, "/admin/users/abc", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -167,28 +167,28 @@ func TestAdminDeleteUserBadID(t *testing.T) {
// TestAdminStatsWithOrders 带订单的统计。
func TestAdminStatsWithOrders(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 orders (order_id, item_id, cookie_id, order_status) VALUES ('s1','i1','acc1','2')`)
store.DB.ExecContext(ctx, `INSERT INTO cards (name, type, user_id) VALUES ('卡1','text',1)`)
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
// cookie 用于本次流程后续判断的登录凭证
cookie := loginHelper(t, h)
// 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("status=%d", rec.Code)
}
// stats 保存stats,供当前处理流程使用
// stats 用于本次流程后续判断的stats
var stats map[string]any
json.Unmarshal(rec.Body.Bytes(), &stats)
if stats["total_orders"] != float64(1) || stats["total_cards"] != float64(1) {
@@ -198,26 +198,26 @@ func TestAdminStatsWithOrders(t *testing.T) {
// TestChangeAdminPassword 修改管理员密码成功。
func TestChangeAdminPassword(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 := `{"current_password":"pw","new_password":"newpw123"}`
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPost, "/change-admin-password", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("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"] != true {
@@ -227,23 +227,23 @@ func TestChangeAdminPassword(t *testing.T) {
// TestChangeAdminPasswordWrongCurrent 当前密码错误。
func TestChangeAdminPasswordWrongCurrent(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 := `{"current_password":"wrong","new_password":"newpw123"}`
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPost, "/change-admin-password", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
// res 保存响应,供当前处理流程使用
// res 用于本次流程后续判断的响应
var res map[string]any
json.Unmarshal(rec.Body.Bytes(), &res)
if res["success"] == true {
@@ -253,18 +253,18 @@ func TestChangeAdminPasswordWrongCurrent(t *testing.T) {
// TestChangeAdminPasswordBadJSON 非法 JSON 400。
func TestChangeAdminPasswordBadJSON(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.MethodPost, "/change-admin-password", strings.NewReader("not-json"))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -272,20 +272,20 @@ func TestChangeAdminPasswordBadJSON(t *testing.T) {
}
}
// TestChangeAdminPasswordShortNewPassword 负责TestChangeAdmin密码ShortNew密码相关处理。
// TestChangeAdminPasswordShortNewPassword 封装TestChangeAdmin密码ShortNew密码业务协调。
func TestChangeAdminPasswordShortNewPassword(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.MethodPost, "/change-admin-password", strings.NewReader(`{"current_password":"pw","new_password":"123"}`))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -295,26 +295,26 @@ func TestChangeAdminPasswordShortNewPassword(t *testing.T) {
// TestChangeAdminPasswordNonAdmin 非 admin 403。
func TestChangeAdminPasswordNonAdmin(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
// srv、cleanup 用于本次流程后续判断的srv、cleanup
srv, store, cleanup := newTestServer(t)
defer cleanup()
store.Users.Create(context.Background(), "user2", "[email protected]", "pw")
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// body 保存请求体,供当前处理流程使用
// body 用于本次流程后续判断的请求体
body := `{"username":"user2","password":"pw"}`
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader(body))
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
// cookie 保存登录凭证,供当前处理流程使用
// cookie 用于本次流程后续判断的登录凭证
cookie := rec.Result().Cookies()[0]
// req2 保存req2,供当前处理流程使用
// req2 用于本次流程后续判断的req2
req2 := httptest.NewRequest(http.MethodPost, "/change-admin-password", strings.NewReader(`{"current_password":"pw","new_password":"newpw123"}`))
req2.AddCookie(cookie)
// rec2 保存rec2,供当前处理流程使用
// rec2 用于本次流程后续判断的rec2
rec2 := httptest.NewRecorder()
h.ServeHTTP(rec2, req2)
if rec2.Code != http.StatusForbidden {
@@ -324,26 +324,26 @@ func TestChangeAdminPasswordNonAdmin(t *testing.T) {
// TestChangePassword 修改当前用户密码。
func TestChangePassword(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 := `{"current_password":"pw","new_password":"newpw123"}`
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPost, "/change-password", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("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"] != true {
@@ -351,28 +351,28 @@ func TestChangePassword(t *testing.T) {
}
}
// TestChangePasswordRevokesCurrentSession 负责TestChange密码RevokesCurrent会话相关处理。
// TestChangePasswordRevokesCurrentSession 封装TestChange密码RevokesCurrent会话业务协调。
func TestChangePasswordRevokesCurrentSession(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.MethodPost, "/change-password", strings.NewReader(`{"current_password":"pw","new_password":"newpw123"}`))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("change status=%d body=%s", rec.Code, rec.Body.String())
}
// res 保存响应,供当前处理流程使用
// res 用于本次流程后续判断的响应
var res map[string]any
if // err 保存err,供当前处理流程使用
if // err 用于本次流程后续判断的err
err := json.Unmarshal(rec.Body.Bytes(), &res); err != nil {
t.Fatalf("decode change response: %v", err)
}
@@ -380,15 +380,15 @@ func TestChangePasswordRevokesCurrentSession(t *testing.T) {
t.Fatalf("change response should require relogin: %+v", res)
}
// verifyReq 保存verifyReq,供当前处理流程使用
// verifyReq 用于本次流程后续判断的verifyReq
verifyReq := httptest.NewRequest(http.MethodGet, "/verify", nil)
verifyReq.AddCookie(cookie)
// verifyRec 保存verifyRec,供当前处理流程使用
// verifyRec 用于本次流程后续判断的verifyRec
verifyRec := httptest.NewRecorder()
h.ServeHTTP(verifyRec, verifyReq)
// verify 保存verify,供当前处理流程使用
// verify 用于本次流程后续判断的verify
var verify map[string]any
if // err 保存err,供当前处理流程使用
if // err 用于本次流程后续判断的err
err := json.Unmarshal(verifyRec.Body.Bytes(), &verify); err != nil {
t.Fatalf("decode verify: %v", err)
}
@@ -399,23 +399,23 @@ func TestChangePasswordRevokesCurrentSession(t *testing.T) {
// TestChangePasswordWrongCurrent 当前密码错误。
func TestChangePasswordWrongCurrent(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 := `{"current_password":"wrong","new_password":"newpw123"}`
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPost, "/change-password", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
// res 保存响应,供当前处理流程使用
// res 用于本次流程后续判断的响应
var res map[string]any
json.Unmarshal(rec.Body.Bytes(), &res)
if res["success"] == true {
@@ -425,18 +425,18 @@ func TestChangePasswordWrongCurrent(t *testing.T) {
// TestChangePasswordBadJSON 非法 JSON 400。
func TestChangePasswordBadJSON(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.MethodPost, "/change-password", strings.NewReader("not-json"))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -444,20 +444,20 @@ func TestChangePasswordBadJSON(t *testing.T) {
}
}
// TestChangePasswordShortNewPassword 负责TestChange密码ShortNew密码相关处理。
// TestChangePasswordShortNewPassword 封装TestChange密码ShortNew密码业务协调。
func TestChangePasswordShortNewPassword(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.MethodPost, "/change-password", strings.NewReader(`{"current_password":"pw","new_password":"123"}`))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -467,20 +467,20 @@ func TestChangePasswordShortNewPassword(t *testing.T) {
// TestUpdateCredentialsBadUsername 用户名长度不足 400。
func TestUpdateCredentialsBadUsername(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 := `{"current_password":"pw","new_username":"ab"}`
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPut, "/account/credentials", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -490,20 +490,20 @@ func TestUpdateCredentialsBadUsername(t *testing.T) {
// TestUpdateCredentialsMissingCurrentPassword 缺当前密码 400。
func TestUpdateCredentialsMissingCurrentPassword(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 := `{"new_username":"newname"}`
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPut, "/account/credentials", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -513,20 +513,20 @@ func TestUpdateCredentialsMissingCurrentPassword(t *testing.T) {
// TestUpdateCredentialsShortNewPassword 新密码过短 400。
func TestUpdateCredentialsShortNewPassword(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 := `{"current_password":"pw","new_password":"123"}`
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPut, "/account/credentials", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -536,20 +536,20 @@ func TestUpdateCredentialsShortNewPassword(t *testing.T) {
// TestUpdateCredentialsNothingChanged 用户名密码均未修改 400。
func TestUpdateCredentialsNothingChanged(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 := `{"current_password":"pw","new_username":"admin"}`
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPut, "/account/credentials", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {