阶段十:完成 Go Server 注释基线清零

This commit is contained in:
Christ committed 2026-08-15 04:56:57 +08:00
1 parent 6257fad453
commit c47271ae9c
64 files changed
+4217 -30552

No files matched your search

+130 -5
View File
@@ -13,15 +13,20 @@ import (
// TestListChannels 列出通知渠道。
func TestListChannels(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
srv, _, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
cookie := loginHelper(t, h)
// 先创建一个渠道。
body := `{"name":"钉钉","type":"dingtalk","config":"{}","enabled":true}`
// req 保存req,供当前处理流程使用
req := httptest.NewRequest(http.MethodPost, "/notification-channels", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
@@ -31,11 +36,13 @@ func TestListChannels(t *testing.T) {
// 列出。
req2 := httptest.NewRequest(http.MethodGet, "/notification-channels", nil)
req2.AddCookie(cookie)
// rec2 保存rec2,供当前处理流程使用
rec2 := httptest.NewRecorder()
h.ServeHTTP(rec2, req2)
if rec2.Code != 200 {
t.Fatalf("list status=%d", rec2.Code)
}
// arr 保存arr,供当前处理流程使用
var arr []map[string]any
json.Unmarshal(rec2.Body.Bytes(), &arr)
if len(arr) != 1 || arr[0]["name"] != "钉钉" {
@@ -45,14 +52,20 @@ func TestListChannels(t *testing.T) {
// TestCreateChannelMissingName 缺 name/type 400。
func TestCreateChannelMissingName(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
srv, _, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
cookie := loginHelper(t, h)
// body 保存请求体,供当前处理流程使用
body := `{"type":"dingtalk"}`
// req 保存req,供当前处理流程使用
req := httptest.NewRequest(http.MethodPost, "/notification-channels", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -62,13 +75,18 @@ func TestCreateChannelMissingName(t *testing.T) {
// TestCreateChannelBadJSON 非法 JSON 400。
func TestCreateChannelBadJSON(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, "/notification-channels", strings.NewReader("not-json"))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -78,30 +96,40 @@ func TestCreateChannelBadJSON(t *testing.T) {
// TestUpdateChannel 更新渠道。
func TestUpdateChannel(t *testing.T) {
// srv、store、cleanup 保存srv、store、cleanup,供当前处理流程使用
srv, store, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
cookie := loginHelper(t, h)
// 创建。
body := `{"name":"钉钉","type":"dingtalk","config":"{\"webhook_url\":\"https://example.com\"}","event_types":"[\"account_offline\"]","enabled":true}`
// req 保存req,供当前处理流程使用
req := httptest.NewRequest(http.MethodPost, "/notification-channels", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
// cr 保存cr,供当前处理流程使用
var cr map[string]any
json.Unmarshal(rec.Body.Bytes(), &cr)
// id 保存标识,供当前处理流程使用
id := int64(cr["id"].(float64))
// 部分更新:只切换 enabled,不应清空 name/type/config/event_types。
upd := `{"enabled":false}`
// req2 保存req2,供当前处理流程使用
req2 := httptest.NewRequest(http.MethodPut, "/notification-channels/"+itoa(id), strings.NewReader(upd))
req2.AddCookie(cookie)
// rec2 保存rec2,供当前处理流程使用
rec2 := httptest.NewRecorder()
h.ServeHTTP(rec2, req2)
if rec2.Code != 200 {
t.Fatalf("update status=%d body=%s", rec2.Code, rec2.Body.String())
}
// row、err 保存row、err,供当前处理流程使用
row, err := store.Notifications.GetChannelRowForUser(context.Background(), id, 1)
if err != nil {
t.Fatalf("get channel row: %v", err)
@@ -117,6 +145,7 @@ func TestUpdateChannel(t *testing.T) {
// 显式传空 event_types 表示恢复为接收全部事件,其它字段仍保留。
req3 := httptest.NewRequest(http.MethodPut, "/notification-channels/"+itoa(id), strings.NewReader(`{"event_types":""}`))
req3.AddCookie(cookie)
// rec3 保存rec3,供当前处理流程使用
rec3 := httptest.NewRecorder()
h.ServeHTTP(rec3, req3)
if rec3.Code != 200 {
@@ -133,13 +162,18 @@ func TestUpdateChannel(t *testing.T) {
// TestUpdateChannelBadID 无效 ID 400。
func TestUpdateChannelBadID(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.MethodPut, "/notification-channels/abc", strings.NewReader(`{}`))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -149,13 +183,18 @@ func TestUpdateChannelBadID(t *testing.T) {
// TestUpdateChannelBadJSON 非法 JSON 400。
func TestUpdateChannelBadJSON(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.MethodPut, "/notification-channels/1", strings.NewReader("not-json"))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -165,22 +204,32 @@ func TestUpdateChannelBadJSON(t *testing.T) {
// TestDeleteChannel 删除渠道。
func TestDeleteChannel(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
srv, _, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
cookie := loginHelper(t, h)
// body 保存请求体,供当前处理流程使用
body := `{"name":"钉钉","type":"dingtalk","config":"{}","enabled":true}`
// req 保存req,供当前处理流程使用
req := httptest.NewRequest(http.MethodPost, "/notification-channels", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
// cr 保存cr,供当前处理流程使用
var cr map[string]any
json.Unmarshal(rec.Body.Bytes(), &cr)
// id 保存标识,供当前处理流程使用
id := int64(cr["id"].(float64))
// req2 保存req2,供当前处理流程使用
req2 := httptest.NewRequest(http.MethodDelete, "/notification-channels/"+itoa(id), nil)
req2.AddCookie(cookie)
// rec2 保存rec2,供当前处理流程使用
rec2 := httptest.NewRecorder()
h.ServeHTTP(rec2, req2)
if rec2.Code != 200 {
@@ -190,13 +239,18 @@ func TestDeleteChannel(t *testing.T) {
// TestDeleteChannelBadID 无效 ID 400。
func TestDeleteChannelBadID(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.MethodDelete, "/notification-channels/abc", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -206,13 +260,18 @@ func TestDeleteChannelBadID(t *testing.T) {
// TestTestChannelNoNotifier 未注入通知器 503。
func TestTestChannelNoNotifier(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, "/notification-channels/1/test", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusServiceUnavailable {
@@ -222,13 +281,18 @@ func TestTestChannelNoNotifier(t *testing.T) {
// TestTestChannelBadID 无效 ID 400。
func TestTestChannelBadID(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, "/notification-channels/abc/test", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -238,22 +302,29 @@ func TestTestChannelBadID(t *testing.T) {
// TestListMessageNotifications 列出消息通知绑定。
func TestListMessageNotifications(t *testing.T) {
// srv、store、cleanup 保存srv、store、cleanup,供当前处理流程使用
srv, store, cleanup := newTestServer(t)
defer cleanup()
// ctx 保存ctx,供当前处理流程使用
ctx := context.Background()
// 创建渠道 + 绑定。
store.DB.ExecContext(ctx, `INSERT INTO notification_channels (name, type, config, enabled, user_id) VALUES ('钉钉','dingtalk','{}',1,1)`)
store.DB.ExecContext(ctx, `INSERT INTO message_notifications (cookie_id, channel_id, enabled) VALUES ('acc1',1,1)`)
// h 保存h,供当前处理流程使用
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
cookie := loginHelper(t, h)
// req 保存req,供当前处理流程使用
req := httptest.NewRequest(http.MethodGet, "/message-notifications", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
}
// m 保存m,供当前处理流程使用
var m map[string][]map[string]any
json.Unmarshal(rec.Body.Bytes(), &m)
if len(m["acc1"]) != 1 {
@@ -261,74 +332,97 @@ func TestListMessageNotifications(t *testing.T) {
}
}
// TestMessageNotificationsFilterCrossUserChannels 负责Test消息通知列表FilterCross用户渠道列表相关处理。
func TestMessageNotificationsFilterCrossUserChannels(t *testing.T) {
// srv、store、cleanup 保存srv、store、cleanup,供当前处理流程使用
srv, store, cleanup := newTestServer(t)
defer cleanup()
// ctx 保存ctx,供当前处理流程使用
ctx := context.Background()
if _, err := store.Users.Create(ctx, "notif2", "[email protected]", "pw"); err != nil {
if // err 保存err,供当前处理流程使用
_, err := store.Users.Create(ctx, "notif2", "[email protected]", "pw"); err != nil {
t.Fatalf("create user: %v", err)
}
// u2 保存u2,供当前处理流程使用
u2, _ := store.Users.GetByUsername(ctx, "notif2")
// ownID、err 保存ownID、err,供当前处理流程使用
ownID, err := store.Notifications.CreateChannel(ctx, &db.NotificationChannelRow{
Name: "own", Type: "webhook", Config: `{}`, Enabled: true, UserID: 1,
})
if err != nil {
t.Fatalf("create own channel: %v", err)
}
// otherID、err 保存otherID、err,供当前处理流程使用
otherID, err := store.Notifications.CreateChannel(ctx, &db.NotificationChannelRow{
Name: "other-secret", Type: "webhook", Config: `{}`, Enabled: true, UserID: u2.ID,
})
if err != nil {
t.Fatalf("create other channel: %v", err)
}
if _, err := store.DB.ExecContext(ctx, `INSERT INTO message_notifications (cookie_id, channel_id, enabled) VALUES ('acc1', ?, 1)`, ownID); err != nil {
if // err 保存err,供当前处理流程使用
_, err := store.DB.ExecContext(ctx, `INSERT INTO message_notifications (cookie_id, channel_id, enabled) VALUES ('acc1', ?, 1)`, ownID); err != nil {
t.Fatalf("insert own binding: %v", err)
}
if _, err := store.DB.ExecContext(ctx, `INSERT INTO message_notifications (cookie_id, channel_id, enabled) VALUES ('acc1', ?, 1)`, otherID); err != nil {
if // err 保存err,供当前处理流程使用
_, err := store.DB.ExecContext(ctx, `INSERT INTO message_notifications (cookie_id, channel_id, enabled) VALUES ('acc1', ?, 1)`, otherID); err != nil {
t.Fatalf("insert dirty binding: %v", err)
}
// h 保存h,供当前处理流程使用
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
cookie := loginHelper(t, h)
// listReq 保存listReq,供当前处理流程使用
listReq := httptest.NewRequest(http.MethodGet, "/message-notifications", nil)
listReq.AddCookie(cookie)
// listRec 保存listRec,供当前处理流程使用
listRec := httptest.NewRecorder()
h.ServeHTTP(listRec, listReq)
if listRec.Code != http.StatusOK {
t.Fatalf("list status=%d body=%s", listRec.Code, listRec.Body.String())
}
// listed 保存listed,供当前处理流程使用
var listed map[string][]map[string]any
if err := json.Unmarshal(listRec.Body.Bytes(), &listed); err != nil {
if // err 保存err,供当前处理流程使用
err := json.Unmarshal(listRec.Body.Bytes(), &listed); err != nil {
t.Fatalf("decode list: %v", err)
}
if len(listed["acc1"]) != 1 || listed["acc1"][0]["channel_name"] != "own" {
t.Fatalf("list should filter dirty binding: %+v", listed)
}
// getReq 保存getReq,供当前处理流程使用
getReq := httptest.NewRequest(http.MethodGet, "/message-notifications/acc1", nil)
getReq.AddCookie(cookie)
// getRec 保存getRec,供当前处理流程使用
getRec := httptest.NewRecorder()
h.ServeHTTP(getRec, getReq)
if getRec.Code != http.StatusOK {
t.Fatalf("get status=%d body=%s", getRec.Code, getRec.Body.String())
}
// got 保存got,供当前处理流程使用
var got map[string]any
if err := json.Unmarshal(getRec.Body.Bytes(), &got); err != nil {
if // err 保存err,供当前处理流程使用
err := json.Unmarshal(getRec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode get: %v", err)
}
// ids 保存ids,供当前处理流程使用
ids, _ := got["channel_ids"].([]any)
if len(ids) != 1 || int64(ids[0].(float64)) != ownID {
t.Fatalf("bindings should filter dirty binding: %+v", got)
}
// updateOtherReq 保存updateOtherReq,供当前处理流程使用
updateOtherReq := httptest.NewRequest(http.MethodPut, "/notification-channels/"+itoa(otherID), strings.NewReader(`{"enabled":false}`))
updateOtherReq.AddCookie(cookie)
// updateOtherRec 保存updateOtherRec,供当前处理流程使用
updateOtherRec := httptest.NewRecorder()
h.ServeHTTP(updateOtherRec, updateOtherReq)
if updateOtherRec.Code != http.StatusForbidden {
t.Fatalf("cross-user update should be 403, got %d body=%s", updateOtherRec.Code, updateOtherRec.Body.String())
}
// otherRow、err 保存otherRow、err,供当前处理流程使用
otherRow, err := store.Notifications.GetChannelRowForUser(ctx, otherID, u2.ID)
if err != nil {
t.Fatalf("get other channel: %v", err)
@@ -337,8 +431,10 @@ func TestMessageNotificationsFilterCrossUserChannels(t *testing.T) {
t.Fatalf("cross-user update should not mutate other channel: %+v", otherRow)
}
// deleteOtherReq 保存deleteOtherReq,供当前处理流程使用
deleteOtherReq := httptest.NewRequest(http.MethodDelete, "/notification-channels/"+itoa(otherID), nil)
deleteOtherReq.AddCookie(cookie)
// deleteOtherRec 保存deleteOtherRec,供当前处理流程使用
deleteOtherRec := httptest.NewRecorder()
h.ServeHTTP(deleteOtherRec, deleteOtherReq)
if deleteOtherRec.Code != http.StatusForbidden {
@@ -348,16 +444,22 @@ func TestMessageNotificationsFilterCrossUserChannels(t *testing.T) {
// TestDeleteMessageNotification 删除单条消息通知。
func TestDeleteMessageNotification(t *testing.T) {
// srv、store、cleanup 保存srv、store、cleanup,供当前处理流程使用
srv, store, cleanup := newTestServer(t)
defer cleanup()
// ctx 保存ctx,供当前处理流程使用
ctx := context.Background()
store.DB.ExecContext(ctx, `INSERT INTO notification_channels (name, type, config, enabled, user_id) VALUES ('钉钉','dingtalk','{}',1,1)`)
store.DB.ExecContext(ctx, `INSERT INTO message_notifications (cookie_id, channel_id, enabled) VALUES ('acc1',1,1)`)
// h 保存h,供当前处理流程使用
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
cookie := loginHelper(t, h)
// req 保存req,供当前处理流程使用
req := httptest.NewRequest(http.MethodDelete, "/message-notifications/1", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
@@ -367,13 +469,18 @@ func TestDeleteMessageNotification(t *testing.T) {
// TestDeleteMessageNotificationBadID 无效 ID 400。
func TestDeleteMessageNotificationBadID(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.MethodDelete, "/message-notifications/abc", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -383,16 +490,22 @@ func TestDeleteMessageNotificationBadID(t *testing.T) {
// TestDeleteAccountNotifications 删除账号的所有通知绑定。
func TestDeleteAccountNotifications(t *testing.T) {
// srv、store、cleanup 保存srv、store、cleanup,供当前处理流程使用
srv, store, cleanup := newTestServer(t)
defer cleanup()
// ctx 保存ctx,供当前处理流程使用
ctx := context.Background()
store.DB.ExecContext(ctx, `INSERT INTO notification_channels (name, type, config, enabled, user_id) VALUES ('钉钉','dingtalk','{}',1,1)`)
store.DB.ExecContext(ctx, `INSERT INTO message_notifications (cookie_id, channel_id, enabled) VALUES ('acc1',1,1)`)
// h 保存h,供当前处理流程使用
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
cookie := loginHelper(t, h)
// req 保存req,供当前处理流程使用
req := httptest.NewRequest(http.MethodDelete, "/message-notifications/account/acc1", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
@@ -402,13 +515,18 @@ func TestDeleteAccountNotifications(t *testing.T) {
// TestSetAccountBindingsBadJSON 非法 JSON 400。
func TestSetAccountBindingsBadJSON(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, "/message-notifications/acc1", strings.NewReader("not-json"))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -418,16 +536,23 @@ func TestSetAccountBindingsBadJSON(t *testing.T) {
// TestSetAccountBindingsSingleChannel 单渠道绑定。
func TestSetAccountBindingsSingleChannel(t *testing.T) {
// srv、store、cleanup 保存srv、store、cleanup,供当前处理流程使用
srv, store, cleanup := newTestServer(t)
defer cleanup()
// ctx 保存ctx,供当前处理流程使用
ctx := context.Background()
store.DB.ExecContext(ctx, `INSERT INTO notification_channels (name, type, config, enabled, user_id) VALUES ('钉钉','dingtalk','{}',1,1)`)
// h 保存h,供当前处理流程使用
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
cookie := loginHelper(t, h)
// body 保存请求体,供当前处理流程使用
body := `{"channel_id":1,"enabled":true}`
// req 保存req,供当前处理流程使用
req := httptest.NewRequest(http.MethodPost, "/message-notifications/acc1", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {