Debian Codegraph @

reactions_test.go

raw symbols SCIP
1 package slack
2
3 import (
4 "fmt"
5 "net/http"
6 "reflect"
7 "testing"
8 )
9
10 type reactionsHandler struct {
11 gotParams map[string]string
12 response string
13 }
14
15 func newReactionsHandler() *reactionsHandler {
16 return &reactionsHandler{
17 gotParams: make(map[string]string),
18 response: `{ "ok": true }`,
19 }
20 }
21
22 func (rh *reactionsHandler) accumulateFormValue(k string, r *http.Request) {
23 if v := r.FormValue(k); v != "" {
24 rh.gotParams[k] = v
25 }
26 }
27
28 func (rh *reactionsHandler) handler(w http.ResponseWriter, r *http.Request) {
29 rh.accumulateFormValue("channel", r)
30 rh.accumulateFormValue("count", r)
31 rh.accumulateFormValue("file", r)
32 rh.accumulateFormValue("file_comment", r)
33 rh.accumulateFormValue("full", r)
34 rh.accumulateFormValue("name", r)
35 rh.accumulateFormValue("page", r)
36 rh.accumulateFormValue("timestamp", r)
37 rh.accumulateFormValue("user", r)
38 w.Header().Set("Content-Type", "application/json")
39 w.Write([]byte(rh.response))
40 }
41
42 func TestSlack_AddReaction(t *testing.T) {
43 once.Do(startServer)
44 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
45 tests := []struct {
46 name string
47 ref ItemRef
48 wantParams map[string]string
49 }{
50 {
51 "thumbsup",
52 NewRefToMessage("ChannelID", "123"),
53 map[string]string{
54 "name": "thumbsup",
55 "channel": "ChannelID",
56 "timestamp": "123",
57 },
58 },
59 {
60 "thumbsup",
61 NewRefToFile("FileID"),
62 map[string]string{
63 "name": "thumbsup",
64 "file": "FileID",
65 },
66 },
67 {
68 "thumbsup",
69 NewRefToComment("FileCommentID"),
70 map[string]string{
71 "name": "thumbsup",
72 "file_comment": "FileCommentID",
73 },
74 },
75 }
76 var rh *reactionsHandler
77 http.HandleFunc("/reactions.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
78 for i, test := range tests {
79 rh = newReactionsHandler()
80 err := api.AddReaction(test.name, test.ref)
81 if err != nil {
82 t.Fatalf("%d: Unexpected error: %s", i, err)
83 }
84 if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
85 t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
86 }
87 }
88 }
89
90 func TestSlack_RemoveReaction(t *testing.T) {
91 once.Do(startServer)
92 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
93 tests := []struct {
94 name string
95 ref ItemRef
96 wantParams map[string]string
97 }{
98 {
99 "thumbsup",
100 NewRefToMessage("ChannelID", "123"),
101 map[string]string{
102 "name": "thumbsup",
103 "channel": "ChannelID",
104 "timestamp": "123",
105 },
106 },
107 {
108 "thumbsup",
109 NewRefToFile("FileID"),
110 map[string]string{
111 "name": "thumbsup",
112 "file": "FileID",
113 },
114 },
115 {
116 "thumbsup",
117 NewRefToComment("FileCommentID"),
118 map[string]string{
119 "name": "thumbsup",
120 "file_comment": "FileCommentID",
121 },
122 },
123 }
124 var rh *reactionsHandler
125 http.HandleFunc("/reactions.remove", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
126 for i, test := range tests {
127 rh = newReactionsHandler()
128 err := api.RemoveReaction(test.name, test.ref)
129 if err != nil {
130 t.Fatalf("%d: Unexpected error: %s", i, err)
131 }
132 if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
133 t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
134 }
135 }
136 }
137
138 func TestSlack_GetReactions(t *testing.T) {
139 once.Do(startServer)
140 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
141 tests := []struct {
142 ref ItemRef
143 params GetReactionsParameters
144 wantParams map[string]string
145 json string
146 wantReactions []ItemReaction
147 }{
148 {
149 NewRefToMessage("ChannelID", "123"),
150 GetReactionsParameters{},
151 map[string]string{
152 "channel": "ChannelID",
153 "timestamp": "123",
154 },
155 `{"ok": true,
156 "type": "message",
157 "message": {
158 "reactions": [
159 {
160 "name": "astonished",
161 "count": 3,
162 "users": [ "U1", "U2", "U3" ]
163 },
164 {
165 "name": "clock1",
166 "count": 3,
167 "users": [ "U1", "U2" ]
168 }
169 ]
170 }}`,
171 []ItemReaction{
172 {Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
173 {Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
174 },
175 },
176 {
177 NewRefToFile("FileID"),
178 GetReactionsParameters{Full: true},
179 map[string]string{
180 "file": "FileID",
181 "full": "true",
182 },
183 `{"ok": true,
184 "type": "file",
185 "file": {
186 "reactions": [
187 {
188 "name": "astonished",
189 "count": 3,
190 "users": [ "U1", "U2", "U3" ]
191 },
192 {
193 "name": "clock1",
194 "count": 3,
195 "users": [ "U1", "U2" ]
196 }
197 ]
198 }}`,
199 []ItemReaction{
200 {Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
201 {Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
202 },
203 },
204 {
205
206 NewRefToComment("FileCommentID"),
207 GetReactionsParameters{},
208 map[string]string{
209 "file_comment": "FileCommentID",
210 },
211 `{"ok": true,
212 "type": "file_comment",
213 "file": {},
214 "comment": {
215 "reactions": [
216 {
217 "name": "astonished",
218 "count": 3,
219 "users": [ "U1", "U2", "U3" ]
220 },
221 {
222 "name": "clock1",
223 "count": 3,
224 "users": [ "U1", "U2" ]
225 }
226 ]
227 }}`,
228 []ItemReaction{
229 {Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
230 {Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
231 },
232 },
233 }
234 var rh *reactionsHandler
235 http.HandleFunc("/reactions.get", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
236 for i, test := range tests {
237 rh = newReactionsHandler()
238 rh.response = test.json
239 got, err := api.GetReactions(test.ref, test.params)
240 if err != nil {
241 t.Fatalf("%d: Unexpected error: %s", i, err)
242 }
243 if !reflect.DeepEqual(got, test.wantReactions) {
244 t.Errorf("%d: Got reaction %#v, want %#v", i, got, test.wantReactions)
245 }
246 if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
247 t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
248 }
249 }
250 }
251
252 func TestSlack_ListReactions(t *testing.T) {
253 once.Do(startServer)
254 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
255 rh := newReactionsHandler()
256 http.HandleFunc("/reactions.list", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
257 rh.response = `{"ok": true,
258 "items": [
259 {
260 "type": "message",
261 "channel": "C1",
262 "message": {
263 "text": "hello",
264 "reactions": [
265 {
266 "name": "astonished",
267 "count": 3,
268 "users": [ "U1", "U2", "U3" ]
269 },
270 {
271 "name": "clock1",
272 "count": 3,
273 "users": [ "U1", "U2" ]
274 }
275 ]
276 }
277 },
278 {
279 "type": "file",
280 "file": {
281 "name": "toy",
282 "reactions": [
283 {
284 "name": "clock1",
285 "count": 3,
286 "users": [ "U1", "U2" ]
287 }
288 ]
289 }
290 },
291 {
292 "type": "file_comment",
293 "file": {
294 "name": "toy"
295 },
296 "comment": {
297 "comment": "cool toy",
298 "reactions": [
299 {
300 "name": "astonished",
301 "count": 3,
302 "users": [ "U1", "U2", "U3" ]
303 }
304 ]
305 }
306 }
307 ],
308 "paging": {
309 "count": 100,
310 "total": 4,
311 "page": 1,
312 "pages": 1
313 }}`
314 want := []ReactedItem{
315 {
316 Item: NewMessageItem("C1", &Message{Msg: Msg{
317 Text: "hello",
318 Reactions: []ItemReaction{
319 {Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
320 {Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
321 },
322 }}),
323 Reactions: []ItemReaction{
324 {Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
325 {Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
326 },
327 },
328 {
329 Item: NewFileItem(&File{Name: "toy"}),
330 Reactions: []ItemReaction{
331 {Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
332 },
333 },
334 {
335 Item: NewFileCommentItem(&File{Name: "toy"}, &Comment{Comment: "cool toy"}),
336 Reactions: []ItemReaction{
337 {Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
338 },
339 },
340 }
341 wantParams := map[string]string{
342 "user": "User",
343 "count": "200",
344 "page": "2",
345 "full": "true",
346 }
347 params := NewListReactionsParameters()
348 params.User = "User"
349 params.Count = 200
350 params.Page = 2
351 params.Full = true
352 got, paging, err := api.ListReactions(params)
353 if err != nil {
354 t.Fatalf("Unexpected error: %s", err)
355 }
356 if !reflect.DeepEqual(got, want) {
357 t.Errorf("Got reaction %#v, want %#v", got, want)
358 for i, item := range got {
359 fmt.Printf("Item %d, Type: %s\n", i, item.Type)
360 fmt.Printf("Message %#v\n", item.Message)
361 fmt.Printf("File %#v\n", item.File)
362 fmt.Printf("Comment %#v\n", item.Comment)
363 fmt.Printf("Reactions %#v\n", item.Reactions)
364 }
365 }
366 if !reflect.DeepEqual(rh.gotParams, wantParams) {
367 t.Errorf("Got params %#v, want %#v", rh.gotParams, wantParams)
368 }
369 if reflect.DeepEqual(paging, Paging{}) {
370 t.Errorf("Want paging data, got empty struct")
371 }
372 }