chore: checkpoint repaired xianyu-go baseline
This commit is contained in:
1 parent
62705c524d
commit
3d37ff7917
119 files changed
+20086
-2085
No files matched your search
@@ -0,0 +1,386 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// TestListChannels 列出通知渠道。
|
||||
func TestListChannels(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// 先创建一个渠道。
|
||||
body := `{"name":"钉钉","type":"dingtalk","config":"{}","enabled":true}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/notification-channels", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("create status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// 列出。
|
||||
req2 := httptest.NewRequest(http.MethodGet, "/notification-channels", nil)
|
||||
req2.AddCookie(cookie)
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
t.Fatalf("list status=%d", rec2.Code)
|
||||
}
|
||||
var arr []map[string]any
|
||||
json.Unmarshal(rec2.Body.Bytes(), &arr)
|
||||
if len(arr) != 1 || arr[0]["name"] != "钉钉" {
|
||||
t.Fatalf("渠道列表异常: %+v", arr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateChannelMissingName 缺 name/type 400。
|
||||
func TestCreateChannelMissingName(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
body := `{"type":"dingtalk"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/notification-channels", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("缺 name 应 400,got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateChannelBadJSON 非法 JSON 400。
|
||||
func TestCreateChannelBadJSON(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/notification-channels", strings.NewReader("not-json"))
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("非法 JSON 应 400,got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateChannel 更新渠道。
|
||||
func TestUpdateChannel(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
// 创建。
|
||||
body := `{"name":"钉钉","type":"dingtalk","config":"{}","enabled":true}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/notification-channels", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
var cr map[string]any
|
||||
json.Unmarshal(rec.Body.Bytes(), &cr)
|
||||
id := int64(cr["id"].(float64))
|
||||
|
||||
// 更新。
|
||||
upd := `{"name":"改名钉钉","type":"dingtalk","config":"{}","enabled":false}`
|
||||
req2 := httptest.NewRequest(http.MethodPut, "/notification-channels/"+itoa(id), strings.NewReader(upd))
|
||||
req2.AddCookie(cookie)
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
t.Fatalf("update status=%d body=%s", rec2.Code, rec2.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateChannelBadID 无效 ID 400。
|
||||
func TestUpdateChannelBadID(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPut, "/notification-channels/abc", strings.NewReader(`{}`))
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("无效 ID 应 400,got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateChannelBadJSON 非法 JSON 400。
|
||||
func TestUpdateChannelBadJSON(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPut, "/notification-channels/1", strings.NewReader("not-json"))
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("非法 JSON 应 400,got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeleteChannel 删除渠道。
|
||||
func TestDeleteChannel(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
body := `{"name":"钉钉","type":"dingtalk","config":"{}","enabled":true}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/notification-channels", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
var cr map[string]any
|
||||
json.Unmarshal(rec.Body.Bytes(), &cr)
|
||||
id := int64(cr["id"].(float64))
|
||||
|
||||
req2 := httptest.NewRequest(http.MethodDelete, "/notification-channels/"+itoa(id), nil)
|
||||
req2.AddCookie(cookie)
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
t.Fatalf("delete status=%d", rec2.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeleteChannelBadID 无效 ID 400。
|
||||
func TestDeleteChannelBadID(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/notification-channels/abc", nil)
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("无效 ID 应 400,got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTestChannelNoNotifier 未注入通知器 503。
|
||||
func TestTestChannelNoNotifier(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/notification-channels/1/test", nil)
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("无通知器应 503,got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTestChannelBadID 无效 ID 400。
|
||||
func TestTestChannelBadID(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/notification-channels/abc/test", nil)
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("无效 ID 应 400,got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListMessageNotifications 列出消息通知绑定。
|
||||
func TestListMessageNotifications(t *testing.T) {
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
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 := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/message-notifications", nil)
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var m map[string][]map[string]any
|
||||
json.Unmarshal(rec.Body.Bytes(), &m)
|
||||
if len(m["acc1"]) != 1 {
|
||||
t.Fatalf("绑定列表异常: %+v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageNotificationsFilterCrossUserChannels(t *testing.T) {
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
if _, err := store.Users.Create(ctx, "notif2", "[email protected]", "pw"); err != nil {
|
||||
t.Fatalf("create user: %v", err)
|
||||
}
|
||||
u2, _ := store.Users.GetByUsername(ctx, "notif2")
|
||||
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 := 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 {
|
||||
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 {
|
||||
t.Fatalf("insert dirty binding: %v", err)
|
||||
}
|
||||
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
listReq := httptest.NewRequest(http.MethodGet, "/message-notifications", nil)
|
||||
listReq.AddCookie(cookie)
|
||||
listRec := httptest.NewRecorder()
|
||||
h.ServeHTTP(listRec, listReq)
|
||||
if listRec.Code != http.StatusOK {
|
||||
t.Fatalf("list status=%d body=%s", listRec.Code, listRec.Body.String())
|
||||
}
|
||||
var listed map[string][]map[string]any
|
||||
if 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 := httptest.NewRequest(http.MethodGet, "/message-notifications/acc1", nil)
|
||||
getReq.AddCookie(cookie)
|
||||
getRec := httptest.NewRecorder()
|
||||
h.ServeHTTP(getRec, getReq)
|
||||
if getRec.Code != http.StatusOK {
|
||||
t.Fatalf("get status=%d body=%s", getRec.Code, getRec.Body.String())
|
||||
}
|
||||
var got map[string]any
|
||||
if err := json.Unmarshal(getRec.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("decode get: %v", err)
|
||||
}
|
||||
ids, _ := got["channel_ids"].([]any)
|
||||
if len(ids) != 1 || int64(ids[0].(float64)) != ownID {
|
||||
t.Fatalf("bindings should filter dirty binding: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeleteMessageNotification 删除单条消息通知。
|
||||
func TestDeleteMessageNotification(t *testing.T) {
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
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 := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/message-notifications/1", nil)
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("delete status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeleteMessageNotificationBadID 无效 ID 400。
|
||||
func TestDeleteMessageNotificationBadID(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/message-notifications/abc", nil)
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("无效 ID 应 400,got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeleteAccountNotifications 删除账号的所有通知绑定。
|
||||
func TestDeleteAccountNotifications(t *testing.T) {
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
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 := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/message-notifications/account/acc1", nil)
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("delete status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetAccountBindingsBadJSON 非法 JSON 400。
|
||||
func TestSetAccountBindingsBadJSON(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/message-notifications/acc1", strings.NewReader("not-json"))
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("非法 JSON 应 400,got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetAccountBindingsSingleChannel 单渠道绑定。
|
||||
func TestSetAccountBindingsSingleChannel(t *testing.T) {
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
store.DB.ExecContext(ctx, `INSERT INTO notification_channels (name, type, config, enabled, user_id) VALUES ('钉钉','dingtalk','{}',1,1)`)
|
||||
h := srv.Router()
|
||||
cookie := loginHelper(t, h)
|
||||
|
||||
body := `{"channel_id":1,"enabled":true}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/message-notifications/acc1", strings.NewReader(body))
|
||||
req.AddCookie(cookie)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user