阶段十:完成 Go Server 注释基线清零
This commit is contained in:
1 parent
6257fad453
commit
c47271ae9c
64 files changed
+4217
-30552
No files matched your search
@@ -10,13 +10,18 @@ import (
|
||||
|
||||
// TestOrderDetailNotFound 查询不存在的订单应 404。
|
||||
func TestOrderDetailNotFound(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// req 保存req,供当前处理流程使用
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/orders/no-such-order", nil)
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusNotFound {
|
||||
@@ -26,15 +31,20 @@ func TestOrderDetailNotFound(t *testing.T) {
|
||||
|
||||
// TestCardEndpointsErrorBranches 卡券相关错误分支:
|
||||
// 无效 ID → 400;不存在 → 404;请求体非法 → 400。
|
||||
// TestCardEndpointsErrorBranches 负责Test卡密Endpoints错误Branches相关处理。
|
||||
func TestCardEndpointsErrorBranches(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// 无效 card_id(非数字)→ 400。
|
||||
req := httptest.NewRequest(http.MethodGet, "/cards/abc", nil)
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
@@ -44,6 +54,7 @@ func TestCardEndpointsErrorBranches(t *testing.T) {
|
||||
// 合法但不存在的 ID → 404。
|
||||
req2 := httptest.NewRequest(http.MethodGet, "/cards/999999", nil)
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != http.StatusNotFound {
|
||||
@@ -53,6 +64,7 @@ func TestCardEndpointsErrorBranches(t *testing.T) {
|
||||
// 创建卡券请求体非法 → 400。
|
||||
req3 := httptest.NewRequest(http.MethodPost, "/cards", strings.NewReader("not-json"))
|
||||
req3.AddCookie(cookie)
|
||||
// rec3 保存rec3,供当前处理流程使用
|
||||
rec3 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec3, req3)
|
||||
if rec3.Code != http.StatusBadRequest {
|
||||
@@ -62,6 +74,7 @@ func TestCardEndpointsErrorBranches(t *testing.T) {
|
||||
// 创建卡券缺类型 → 400(decodeCard 校验)。
|
||||
req4 := httptest.NewRequest(http.MethodPost, "/cards", strings.NewReader(`{"name":"无类型卡"}`))
|
||||
req4.AddCookie(cookie)
|
||||
// rec4 保存rec4,供当前处理流程使用
|
||||
rec4 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec4, req4)
|
||||
if rec4.Code != http.StatusBadRequest {
|
||||
@@ -69,12 +82,17 @@ func TestCardEndpointsErrorBranches(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateCardRejectsUnsupportedAPITypeAndInvalidDelay 负责TestCreate卡密RejectsUnsupportedAPI类型AndInvalid延迟相关处理。
|
||||
func TestCreateCardRejectsUnsupportedAPITypeAndInvalidDelay(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// body 表示当前遍历过程中的请求体
|
||||
for _, body := range []string{
|
||||
`{"name":"API 卡密","type":"api","enabled":true}`,
|
||||
`{"name":"非法类型","type":"unknown","enabled":true}`,
|
||||
@@ -83,8 +101,10 @@ func TestCreateCardRejectsUnsupportedAPITypeAndInvalidDelay(t *testing.T) {
|
||||
`{"name":"空库存","type":"data","data_content":"\n","enabled":true}`,
|
||||
`{"name":"空图片","type":"image","image_url":"","enabled":true}`,
|
||||
} {
|
||||
// req 保存req,供当前处理流程使用
|
||||
req := httptest.NewRequest(http.MethodPost, "/cards", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
@@ -95,10 +115,13 @@ func TestCreateCardRejectsUnsupportedAPITypeAndInvalidDelay(t *testing.T) {
|
||||
|
||||
// TestProtectedEndpointsRequireAuth 受保护端点未登录应 401。
|
||||
func TestProtectedEndpointsRequireAuth(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
h := srv.Router()
|
||||
|
||||
// path 表示当前遍历过程中的路径
|
||||
for _, path := range []string{
|
||||
"/cards",
|
||||
"/api/orders",
|
||||
@@ -108,7 +131,9 @@ func TestProtectedEndpointsRequireAuth(t *testing.T) {
|
||||
"/admin/stats",
|
||||
"/ai-reply-settings",
|
||||
} {
|
||||
// req 保存req,供当前处理流程使用
|
||||
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
@@ -119,24 +144,31 @@ func TestProtectedEndpointsRequireAuth(t *testing.T) {
|
||||
|
||||
// TestAdminEndpointRejectsNonAdmin 非 admin 用户访问管理端点应 403。
|
||||
func TestAdminEndpointRejectsNonAdmin(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
h := srv.Router()
|
||||
|
||||
// 创建普通用户并登录。
|
||||
srv.Store.Users.Create(context.Background(), "user2", "u2@e.com", "pw")
|
||||
// body 保存请求体,供当前处理流程使用
|
||||
body := `{"username":"user2","password":"pw"}`
|
||||
// req 保存req,供当前处理流程使用
|
||||
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader(body))
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("普通用户登录 status=%d", rec.Code)
|
||||
}
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
cookie := rec.Result().Cookies()[0]
|
||||
|
||||
// 访问 admin 端点 → 403。
|
||||
req2 := httptest.NewRequest(http.MethodGet, "/admin/stats", nil)
|
||||
req2.AddCookie(cookie)
|
||||
// rec2 保存rec2,供当前处理流程使用
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != http.StatusForbidden {
|
||||
@@ -146,14 +178,19 @@ func TestAdminEndpointRejectsNonAdmin(t *testing.T) {
|
||||
|
||||
// TestOrderImportEmptyBody 空导入内容应 400。
|
||||
func TestOrderImportEmptyBody(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
h := srv.Router()
|
||||
// cookie 保存登录凭证,供当前处理流程使用
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// req 保存req,供当前处理流程使用
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/orders/import", strings.NewReader(""))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.AddCookie(cookie)
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
@@ -163,11 +200,15 @@ func TestOrderImportEmptyBody(t *testing.T) {
|
||||
|
||||
// TestLoginMalformedJSON 登录请求体非法 JSON 应不致命(400 或失败响应)。
|
||||
func TestLoginMalformedJSON(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 保存h,供当前处理流程使用
|
||||
h := srv.Router()
|
||||
|
||||
// req 保存req,供当前处理流程使用
|
||||
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader("not-json"))
|
||||
// rec 保存rec,供当前处理流程使用
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
|
||||
Reference in new issue
Block a user