94 lines
2.7 KiB
Go
94 lines
2.7 KiB
Go
package dingtalk
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Tencent/WeKnora/internal/im"
|
|
)
|
|
|
|
func TestParseCallbackMessagePicture(t *testing.T) {
|
|
msg := &callbackMessage{
|
|
ConversationID: "cid-group",
|
|
ConversationType: dingtalkConvTypeGroup,
|
|
MsgID: "msg-picture-1",
|
|
Msgtype: "picture",
|
|
Content: []byte(`{"downloadCode":"download-code-1"}`),
|
|
SenderStaffId: "staff-1",
|
|
SenderNick: "Alice",
|
|
SessionWebhook: "https://example.invalid/session",
|
|
}
|
|
|
|
got := parseCallbackMessage(msg)
|
|
if got.MessageType != im.MessageTypeImage {
|
|
t.Fatalf("MessageType = %q, want image", got.MessageType)
|
|
}
|
|
if got.FileKey != "download-code-1" {
|
|
t.Fatalf("FileKey = %q, want download code", got.FileKey)
|
|
}
|
|
if got.FileName != "msg-picture-1.png" {
|
|
t.Fatalf("FileName = %q", got.FileName)
|
|
}
|
|
if got.Content != "" {
|
|
t.Fatalf("Content = %q, want empty", got.Content)
|
|
}
|
|
}
|
|
|
|
func TestAdapterDownloadFileUsesMessageFilesAPI(t *testing.T) {
|
|
var sawToken bool
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/v1.0/robot/messageFiles/download":
|
|
sawToken = r.Header.Get("x-acs-dingtalk-access-token") == "cached-access-token"
|
|
body, _ := io.ReadAll(r.Body)
|
|
if !strings.Contains(string(body), `"downloadCode":"download-code-1"`) || !strings.Contains(string(body), `"robotCode":"robot-code-1"`) {
|
|
t.Errorf("unexpected request body: %s", body)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"downloadUrl":"` + serverURLForRequest(r) + `/media/picture"}`))
|
|
case "/media/picture":
|
|
w.Header().Set("Content-Type", "image/png")
|
|
w.Header().Set("Content-Disposition", `attachment; filename="question.png"`)
|
|
_, _ = w.Write([]byte("PNGDATA"))
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
|
|
adapter := NewAdapter("robot-code-1", "secret", "")
|
|
adapter.token = "cached-access-token"
|
|
adapter.tokenExpAt = time.Now().Add(time.Hour)
|
|
adapter.messageFileDownloadURL = server.URL + "/v1.0/robot/messageFiles/download"
|
|
adapter.allowInsecureDownload = true
|
|
|
|
reader, fileName, err := adapter.DownloadFile(context.Background(), &im.IncomingMessage{
|
|
MessageType: im.MessageTypeImage,
|
|
MessageID: "msg-picture-1",
|
|
FileKey: "download-code-1",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("DownloadFile: %v", err)
|
|
}
|
|
defer reader.Close()
|
|
body, _ := io.ReadAll(reader)
|
|
if string(body) != "PNGDATA" {
|
|
t.Fatalf("body = %q", body)
|
|
}
|
|
if fileName != "question.png" {
|
|
t.Fatalf("fileName = %q", fileName)
|
|
}
|
|
if !sawToken {
|
|
t.Fatal("missing DingTalk access token header")
|
|
}
|
|
}
|
|
|
|
func serverURLForRequest(r *http.Request) string {
|
|
return "http://" + r.Host
|
|
}
|