-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
event_v2_test.go
83 lines (77 loc) · 2.79 KB
/
event_v2_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package lark
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPostEventV2(t *testing.T) {
message := EventV2{
Schema: "2.0",
Header: EventV2Header{
AppID: "666",
},
Event: EventBody{
Type: "message",
ChatType: "group",
MsgType: "text",
OpenID: testUserOpenID,
Text: "public event",
Title: "",
OpenMessageID: "",
ImageKey: "",
ImageURL: "",
},
}
w := performRequest(func(w http.ResponseWriter, r *http.Request) {
var m EventV2
json.NewDecoder(r.Body).Decode(&m)
w.Write([]byte(m.Schema))
}, "POST", "/", message)
assert.Equal(t, "2.0", string(w.Body.Bytes()))
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m, _ := json.Marshal(message)
w.Write([]byte(m))
}))
defer ts.Close()
resp, err := message.PostEvent(http.DefaultClient, ts.URL)
if assert.NoError(t, err) {
var event EventV2
body, err := ioutil.ReadAll(resp.Body)
if assert.NoError(t, err) {
defer resp.Body.Close()
_ = json.Unmarshal(body, &event)
assert.Equal(t, "2.0", event.Schema)
assert.Equal(t, "666", event.Header.AppID)
}
}
}
func TestEventTypes(t *testing.T) {
event := EventV2{
Header: EventV2Header{
EventType: EventTypeChatDisbanded,
},
EventRaw: json.RawMessage(`{ "message": { "chat_id": "oc_ae7f3952a9b28588aeac46c9853d25d3", "chat_type": "p2p", "content": "{\"text\":\"333\"}", "create_time": "1641385820771", "message_id": "om_6ff2cff41a3e9248bbb19bf0e4762e6e", "message_type": "text" }, "sender": { "sender_id": { "open_id": "ou_4f75b532aff410181e93552ad0532072", "union_id": "on_2312aab89ab7c87beb9a443b2f3b1342", "user_id": "4gbb63af" }, "sender_type": "user", "tenant_key": "736588c9260f175d" } }`),
}
m, e := event.GetMessageReceived()
assert.Error(t, e)
event.Header.EventType = EventTypeMessageReceived
m, e = event.GetMessageReceived()
assert.NoError(t, e)
assert.Equal(t, "p2p", m.Message.ChatType)
}
func TestGetEvent(t *testing.T) {
event := EventV2{
Header: EventV2Header{
EventType: EventTypeMessageReceived,
},
EventRaw: json.RawMessage(`{ "message": { "chat_id": "oc_ae7f3952a9b28588aeac46c9853d25d3", "chat_type": "p2p", "content": "{\"text\":\"333\"}", "create_time": "1641385820771", "message_id": "om_6ff2cff41a3e9248bbb19bf0e4762e6e", "message_type": "text" }, "sender": { "sender_id": { "open_id": "ou_4f75b532aff410181e93552ad0532072", "union_id": "on_2312aab89ab7c87beb9a443b2f3b1342", "user_id": "4gbb63af" }, "sender_type": "user", "tenant_key": "736588c9260f175d" } }`),
}
var ev EventV2MessageReceived
err := event.GetEvent(EventTypeMessageReceived, &ev)
if assert.NoError(t, err) {
assert.Equal(t, "oc_ae7f3952a9b28588aeac46c9853d25d3", ev.Message.ChatID)
}
}