Debian Codegraph @

pins_test.go

raw symbols SCIP
1 package slack
2
3 import (
4 "fmt"
5 "net/http"
6 "reflect"
7 "testing"
8 )
9
10 type pinsHandler struct {
11 gotParams map[string]string
12 response string
13 }
14
15 func newPinsHandler() *pinsHandler {
16 return &pinsHandler{
17 gotParams: make(map[string]string),
18 response: `{ "ok": true }`,
19 }
20 }
21
22 func (rh *pinsHandler) accumulateFormValue(k string, r *http.Request) {
23 if v := r.FormValue(k); v != "" {
24 rh.gotParams[k] = v
25 }
26 }
27
28 func (rh *pinsHandler) 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("page", r)
34 rh.accumulateFormValue("timestamp", r)
35 w.Header().Set("Content-Type", "application/json")
36 w.Write([]byte(rh.response))
37 }
38
39 func TestSlack_AddPin(t *testing.T) {
40 once.Do(startServer)
41 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
42 tests := []struct {
43 channel string
44 ref ItemRef
45 wantParams map[string]string
46 }{
47 {
48 "ChannelID",
49 NewRefToMessage("ChannelID", "123"),
50 map[string]string{
51 "channel": "ChannelID",
52 "timestamp": "123",
53 },
54 },
55 {
56 "ChannelID",
57 NewRefToFile("FileID"),
58 map[string]string{
59 "channel": "ChannelID",
60 "file": "FileID",
61 },
62 },
63 {
64 "ChannelID",
65 NewRefToComment("FileCommentID"),
66 map[string]string{
67 "channel": "ChannelID",
68 "file_comment": "FileCommentID",
69 },
70 },
71 }
72 var rh *pinsHandler
73 http.HandleFunc("/pins.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
74 for i, test := range tests {
75 rh = newPinsHandler()
76 err := api.AddPin(test.channel, test.ref)
77 if err != nil {
78 t.Fatalf("%d: Unexpected error: %s", i, err)
79 }
80 if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
81 t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
82 }
83 }
84 }
85
86 func TestSlack_RemovePin(t *testing.T) {
87 once.Do(startServer)
88 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
89 tests := []struct {
90 channel string
91 ref ItemRef
92 wantParams map[string]string
93 }{
94 {
95 "ChannelID",
96 NewRefToMessage("ChannelID", "123"),
97 map[string]string{
98 "channel": "ChannelID",
99 "timestamp": "123",
100 },
101 },
102 {
103 "ChannelID",
104 NewRefToFile("FileID"),
105 map[string]string{
106 "channel": "ChannelID",
107 "file": "FileID",
108 },
109 },
110 {
111 "ChannelID",
112 NewRefToComment("FileCommentID"),
113 map[string]string{
114 "channel": "ChannelID",
115 "file_comment": "FileCommentID",
116 },
117 },
118 }
119 var rh *pinsHandler
120 http.HandleFunc("/pins.remove", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
121 for i, test := range tests {
122 rh = newPinsHandler()
123 err := api.RemovePin(test.channel, test.ref)
124 if err != nil {
125 t.Fatalf("%d: Unexpected error: %s", i, err)
126 }
127 if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
128 t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
129 }
130 }
131 }
132
133 func TestSlack_ListPins(t *testing.T) {
134 once.Do(startServer)
135 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
136 rh := newPinsHandler()
137 http.HandleFunc("/pins.list", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
138 rh.response = `{"ok": true,
139 "items": [
140 {
141 "type": "message",
142 "channel": "C1",
143 "message": {
144 "text": "hello",
145 "reactions": [
146 {
147 "name": "astonished",
148 "count": 3,
149 "users": [ "U1", "U2", "U3" ]
150 },
151 {
152 "name": "clock1",
153 "count": 3,
154 "users": [ "U1", "U2" ]
155 }
156 ]
157 }
158 },
159 {
160 "type": "file",
161 "file": {
162 "name": "toy",
163 "reactions": [
164 {
165 "name": "clock1",
166 "count": 3,
167 "users": [ "U1", "U2" ]
168 }
169 ]
170 }
171 },
172 {
173 "type": "file_comment",
174 "file": {
175 "name": "toy"
176 },
177 "comment": {
178 "comment": "cool toy",
179 "reactions": [
180 {
181 "name": "astonished",
182 "count": 3,
183 "users": [ "U1", "U2", "U3" ]
184 }
185 ]
186 }
187 }
188 ],
189 "paging": {
190 "count": 100,
191 "total": 4,
192 "page": 1,
193 "pages": 1
194 }}`
195 want := []Item{
196 NewMessageItem("C1", &Message{Msg: Msg{
197 Text: "hello",
198 Reactions: []ItemReaction{
199 {Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
200 {Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
201 },
202 }}),
203 NewFileItem(&File{Name: "toy"}),
204 NewFileCommentItem(&File{Name: "toy"}, &Comment{Comment: "cool toy"}),
205 }
206 wantParams := map[string]string{
207 "channel": "ChannelID",
208 }
209 got, paging, err := api.ListPins("ChannelID")
210 if err != nil {
211 t.Fatalf("Unexpected error: %s", err)
212 }
213 if !reflect.DeepEqual(got, want) {
214 t.Errorf("Got Pins %#v, want %#v", got, want)
215 for i, item := range got {
216 fmt.Printf("Item %d, Type: %s\n", i, item.Type)
217 fmt.Printf("Message %#v\n", item.Message)
218 fmt.Printf("File %#v\n", item.File)
219 fmt.Printf("Comment %#v\n", item.Comment)
220 }
221 }
222 if !reflect.DeepEqual(rh.gotParams, wantParams) {
223 t.Errorf("Got params %#v, want %#v", rh.gotParams, wantParams)
224 }
225 if reflect.DeepEqual(paging, Paging{}) {
226 t.Errorf("Want paging data, got empty struct")
227 }
228 }