Debian Codegraph @

socketmode/socketmode_test.go

raw symbols SCIP
1 package socketmode
2
3 import (
4 "bytes"
5 "encoding/json"
6 "errors"
7 "reflect"
8 "testing"
9
10 "github.com/slack-go/slack/slackevents"
11 )
12
13 const (
14 EventDisconnect = `{
15 "type": "disconnect",
16 "reason": "warning",
17 "debug_info": {
18 "host": "applink-7fc4fdbb64-4x5xq"
19 }
20 }
21 `
22 EventHello = `{
23 "type": "hello",
24 "num_connections": 4,
25 "debug_info": {
26 "host": "applink-7fc4fdbb64-4x5xq",
27 "build_number": 10,
28 "approximate_connection_time": 18060
29 },
30 "connection_info": {
31 "app_id": "A01K58AR4RF"
32 }
33 }
34 `
35
36 EventAppMention = `{
37 "envelope_id": "c67a03d0-4094-4744-90ca-d286e00a3ab1",
38 "payload": {
39 "token": "redacted",
40 "team_id": "redacted",
41 "api_app_id": "redacted",
42 "event": {
43 "client_msg_id": "c714568f-67df-42d7-a343-0a8e4d9c6030",
44 "type": "app_mention",
45 "text": "\u003c@U01JKSB8T7Y\u003e test",
46 "user": "redacted",
47 "ts": "1610927831.000200",
48 "team": "redacted",
49 "blocks": [
50 {
51 "type": "rich_text",
52 "block_id": "2Le",
53 "elements": [
54 {
55 "type": "rich_text_section",
56 "elements": [
57 {
58 "type": "user",
59 "user_id": "redacted"
60 },
61 {
62 "type": "text",
63 "text": " test39"
64 }
65 ]
66 }
67 ]
68 }
69 ],
70 "channel": "redacted",
71 "event_ts": "1610927831.000200"
72 },
73 "type": "event_callback",
74 "event_id": "Ev01JZ2T7S3U",
75 "event_time": 1610927831,
76 "authorizations": [
77 {
78 "enterprise_id": null,
79 "team_id": "redacted",
80 "user_id": "redacted",
81 "is_bot": true,
82 "is_enterprise_install": false
83 }
84 ],
85 "is_ext_shared_channel": false,
86 "event_context": "1-app_mention-redacted-redacted"
87 },
88 "type": "events_api",
89 "accepts_response_payload": false,
90 "retry_attempt": 0,
91 "retry_reason": ""
92 }`
93 )
94
95 func TestEventParsing(t *testing.T) {
96 testParsing(t,
97 EventHello,
98 &Event{
99 Type: EventTypeHello,
100 Request: &Request{
101 Type: RequestTypeHello,
102 NumConnections: 4,
103 DebugInfo: DebugInfo{
104 Host: "applink-7fc4fdbb64-4x5xq",
105 BuildNumber: 10,
106 ApproximateConnectionTime: 18060,
107 },
108 ConnectionInfo: ConnectionInfo{
109 AppID: "A01K58AR4RF",
110 },
111 },
112 })
113
114 testParsing(t,
115 EventDisconnect,
116 &Event{
117 Type: EventTypeDisconnect,
118 Request: &Request{
119 Type: RequestTypeDisconnect,
120 Reason: "warning",
121 DebugInfo: DebugInfo{
122 Host: "applink-7fc4fdbb64-4x5xq",
123 },
124 },
125 })
126
127 rawAppMention := json.RawMessage(`{
128 "client_msg_id": "c714568f-67df-42d7-a343-0a8e4d9c6030",
129 "type": "app_mention",
130 "text": "\u003c@U01JKSB8T7Y\u003e test",
131 "user": "redacted",
132 "ts": "1610927831.000200",
133 "team": "redacted",
134 "blocks": [
135 {
136 "type": "rich_text",
137 "block_id": "2Le",
138 "elements": [
139 {
140 "type": "rich_text_section",
141 "elements": [
142 {
143 "type": "user",
144 "user_id": "redacted"
145 },
146 {
147 "type": "text",
148 "text": " test39"
149 }
150 ]
151 }
152 ]
153 }
154 ],
155 "channel": "redacted",
156 "event_ts": "1610927831.000200"
157 }`)
158
159 rawAppMentionReqPayload := json.RawMessage(`{
160 "token": "redacted",
161 "team_id": "redacted",
162 "api_app_id": "redacted",
163 "event": {
164 "client_msg_id": "c714568f-67df-42d7-a343-0a8e4d9c6030",
165 "type": "app_mention",
166 "text": "\u003c@U01JKSB8T7Y\u003e test",
167 "user": "redacted",
168 "ts": "1610927831.000200",
169 "team": "redacted",
170 "blocks": [
171 {
172 "type": "rich_text",
173 "block_id": "2Le",
174 "elements": [
175 {
176 "type": "rich_text_section",
177 "elements": [
178 {
179 "type": "user",
180 "user_id": "redacted"
181 },
182 {
183 "type": "text",
184 "text": " test39"
185 }
186 ]
187 }
188 ]
189 }
190 ],
191 "channel": "redacted",
192 "event_ts": "1610927831.000200"
193 },
194 "type": "event_callback",
195 "event_id": "Ev01JZ2T7S3U",
196 "event_time": 1610927831,
197 "authorizations": [
198 {
199 "enterprise_id": null,
200 "team_id": "redacted",
201 "user_id": "redacted",
202 "is_bot": true,
203 "is_enterprise_install": false
204 }
205 ],
206 "is_ext_shared_channel": false,
207 "event_context": "1-app_mention-redacted-redacted"
208 }`)
209 testParsing(t,
210 EventAppMention,
211 &Event{
212 Type: EventTypeEventsAPI,
213 Data: slackevents.EventsAPIEvent{
214 Token: "redacted",
215 TeamID: "redacted",
216 Type: "event_callback",
217 APIAppID: "redacted",
218 Data: &slackevents.EventsAPICallbackEvent{
219 Type: "event_callback",
220 Token: "redacted",
221 TeamID: "redacted",
222 APIAppID: "redacted",
223 InnerEvent: &rawAppMention,
224 AuthedUsers: nil,
225 AuthedTeams: nil,
226 EventID: "Ev01JZ2T7S3U",
227 EventTime: 1610927831,
228 EventContext: "1-app_mention-redacted-redacted",
229 },
230 InnerEvent: slackevents.EventsAPIInnerEvent{
231 Type: string(slackevents.AppMention),
232 Data: &slackevents.AppMentionEvent{
233 Type: string(slackevents.AppMention),
234 User: "redacted",
235 Text: "<@U01JKSB8T7Y> test",
236 TimeStamp: "1610927831.000200",
237 ThreadTimeStamp: "",
238 Channel: "redacted",
239 EventTimeStamp: "1610927831.000200",
240 },
241 },
242 },
243 Request: &Request{
244 Type: RequestTypeEventsAPI,
245 EnvelopeID: "c67a03d0-4094-4744-90ca-d286e00a3ab1",
246 Payload: rawAppMentionReqPayload,
247 AcceptsResponsePayload: false,
248 RetryAttempt: 0,
249 RetryReason: "",
250 },
251 })
252 }
253
254 func testParsing(t *testing.T, raw string, want interface{}) {
255 t.Helper()
256
257 got, err := parse(raw)
258 if err != nil {
259 t.Fatalf("unexpected error parsing event %q: %v", raw, err)
260 }
261
262 if !reflect.DeepEqual(want, got) {
263 t.Fatalf("unexpected parse result: want %s, got %s", dump(t, want), dump(t, got))
264 }
265 }
266
267 func dump(t *testing.T, data interface{}) string {
268 t.Helper()
269
270 var buf bytes.Buffer
271
272 e := json.NewEncoder(&buf)
273 e.SetIndent("", " ")
274
275 if err := e.Encode(data); err != nil {
276 t.Fatalf("encoding data to json: %v", err)
277 }
278
279 return buf.String()
280 }
281
282 func parse(raw string) (*Event, error) {
283 c := &Client{}
284
285 evt, err := c.parseEvent(json.RawMessage([]byte(raw)))
286
287 if evt == nil {
288 return nil, errors.New("failed handling raw event: event was empty")
289 }
290
291 return evt, err
292 }