bd8a9ff96d
通过 getUpdates 将群写入 Redis,供创建/编辑渠道时选择 chat_id,避免前端重复传递 token。
73 lines
2.2 KiB
Go
73 lines
2.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"aiaa-notification-service/internal/adapter"
|
|
"aiaa-notification-service/internal/model"
|
|
"aiaa-notification-service/internal/safew"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestListSafewChatsPOSTMissingToken(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
h := &ChannelHandler{chats: safew.NewWatcher(safew.NewMemStore(), func(string, int64, int) ([]adapter.SafewChat, int64, error) {
|
|
return nil, 0, nil
|
|
})}
|
|
r := gin.New()
|
|
r.POST("/api/v1/channels/safew/chats", h.ListSafewChats)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/channels/safew/chats", bytes.NewReader([]byte(`{}`)))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestListSafewChatsPOSTOk(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
st := safew.NewMemStore()
|
|
_ = st.MergeSafewChats(nil, "tok", []adapter.SafewChat{{ID: "10000778141", Type: "group", Title: "测试AI"}})
|
|
h := &ChannelHandler{chats: safew.NewWatcher(st, func(string, int64, int) ([]adapter.SafewChat, int64, error) {
|
|
return nil, 0, nil
|
|
})}
|
|
r := gin.New()
|
|
r.POST("/api/v1/channels/safew/chats", h.ListSafewChats)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/channels/safew/chats", bytes.NewReader([]byte(`{"token":"tok","q":"测试"}`)))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
var resp struct {
|
|
Data []adapter.SafewChat `json:"data"`
|
|
Total int `json:"total"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.Total != 1 || resp.Data[0].ID != "10000778141" {
|
|
t.Fatalf("%#v", resp)
|
|
}
|
|
}
|
|
|
|
func TestSafewTokenFromChannel(t *testing.T) {
|
|
raw := json.RawMessage(`{"token":"abc","chat_id":"1"}`)
|
|
ch := &model.Channel{Type: "safew", Config: &raw}
|
|
tok, err := safewTokenFromChannel(ch)
|
|
if err != nil || tok != "abc" {
|
|
t.Fatalf("%q %v", tok, err)
|
|
}
|
|
ch.Type = "bark"
|
|
if _, err := safewTokenFromChannel(ch); err == nil {
|
|
t.Fatal("expected not safew")
|
|
}
|
|
}
|