-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_v1_test.go
139 lines (123 loc) · 4.39 KB
/
event_v1_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package larkhertz
import (
"context"
"testing"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/go-lark/lark"
"github.com/stretchr/testify/assert"
)
func TestMessageStandard(t *testing.T) {
var (
r = server.Default()
middleware = NewLarkMiddleware()
ok bool
m *lark.EventMessage
)
r.Use(middleware.LarkMessageHandler())
r.POST("/", func(c context.Context, ctx *app.RequestContext) {
m, ok = middleware.GetMessage(ctx)
})
message := lark.EventMessage{
Timestamp: "",
Token: "",
EventType: "event_callback",
Event: lark.EventBody{
Type: "message",
ChatType: "private",
MsgType: "text",
OpenID: "ou_08198ccd6a37644b49f4789c92369c80",
Text: "tlb",
Title: "",
OpenMessageID: "",
ImageKey: "",
ImageURL: "",
},
}
performRequest(r, "POST", "/", message)
assert.True(t, ok)
assert.Equal(t, "tlb", m.Event.Text)
}
func TestMessageEncrypted(t *testing.T) {
var (
r = server.Default()
middleware = NewLarkMiddleware().WithEncryption("ocTiICyJdYyvxD6gLkYdsewM41Qc48bx")
ok bool
m *lark.EventMessage
encryptText = "dSb7qqosP3fKQfSk1+wHkhmKX6ucVAZg2/BrkAe5ETqFTP6X/gvzWkVt7YtecaGuN5PuuYECHHEr1xvSXeIJ0Ijr1g85o8EwK12IcatCNhyHb+rOoPRrm4/BYOHVEmp8nmnOzZBWR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SmSdZ+jv2jqgSj1oJpwZWwmwwzQCrHV6h7VbzhgP7XliJKEj6yIR4a5vfRpTalI="
)
r.Use(middleware.LarkMessageHandler())
r.POST("/", func(c context.Context, ctx *app.RequestContext) {
m, ok = middleware.GetMessage(ctx)
})
encryptMessage := lark.EncryptedReq{
Encrypt: encryptText,
}
performRequest(r, "POST", "/", encryptMessage)
assert.True(t, ok)
assert.Equal(t, "hello", m.Event.Text)
assert.Equal(t, "9378d8f0122244e0920644d114e761d5", m.Token)
}
func TestMessageRawFetch(t *testing.T) {
var (
r = server.Default()
middleware = NewLarkMiddleware()
ok bool
m lark.EventMessage
)
r.Use(middleware.LarkMessageHandler())
r.POST("/", func(c context.Context, ctx *app.RequestContext) {
message, _ := ctx.Get(DefaultLarkMessageKey)
m, ok = message.(lark.EventMessage)
})
message := lark.EventMessage{
Timestamp: "",
Token: "",
EventType: "event_callback",
Event: lark.EventBody{
Type: "message",
ChatType: "private",
MsgType: "text",
OpenID: "ou_08198ccd6a37644b49f4789c92369c80",
Text: "tlb",
Title: "",
OpenMessageID: "",
ImageKey: "",
ImageURL: "",
},
}
performRequest(r, "POST", "/", message)
assert.True(t, ok)
assert.Equal(t, "tlb", m.Event.Text)
}
func TestMessageWithTokenVerification(t *testing.T) {
var (
r = server.Default()
middleware = NewLarkMiddleware().WithTokenVerification("abc")
ok bool
m *lark.EventMessage
)
r.Use(middleware.LarkMessageHandler())
r.POST("/", func(c context.Context, ctx *app.RequestContext) {
m, ok = middleware.GetMessage(ctx)
})
message := lark.EventMessage{
Timestamp: "",
Token: "abc1",
EventType: "event_callback",
Event: lark.EventBody{
Type: "message",
ChatType: "private",
MsgType: "text",
OpenID: "ou_08198ccd6a37644b49f4789c92369c80",
Text: "tlb",
Title: "",
OpenMessageID: "",
ImageKey: "",
ImageURL: "",
},
}
performRequest(r, "POST", "/", message)
assert.False(t, ok)
assert.Nil(t, m)
}