Codebase list golang-github-nlopes-slack / 989a3d9
DRY GetReactions test Ryan Carver 8 years ago
1 changed file(s) with 61 addition(s) and 78 deletion(s). Raw diff Collapse all Expand all
140140 }
141141 }
142142
143 func TestSlack_GetReaction_ToMessage(t *testing.T) {
143 func TestSlack_GetReactions(t *testing.T) {
144144 once.Do(startServer)
145145 SLACK_API = "http://" + serverAddr + "/"
146146 api := New("testing-token")
147 getReactionRes = `{"ok": true,
147 tests := []struct {
148 params GetReactionParameters
149 wantParams map[string]string
150 json string
151 wantReactions []ItemReaction
152 }{
153 {
154
155 GetReactionParameters{ItemRef: NewRefToMessage("ChannelID", "123")},
156 map[string]string{
157 "channel": "ChannelID",
158 "timestamp": "123",
159 },
160 `{"ok": true,
148161 "message": {
149162 "type": "message",
150163 "message": {
161174 }
162175 ]
163176 }
164 }}`
165 want := []ItemReaction{
166 ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
167 ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
168 }
169 wantParams := map[string]string{
170 "channel": "ChannelID",
171 "timestamp": "123",
172 }
173 gotParams = map[string]string{}
174 params := NewGetReactionParameters(NewRefToMessage("ChannelID", "123"))
175 got, err := api.GetReactions(params)
176 if err != nil {
177 t.Fatalf("Unexpected error: %s", err)
178 }
179 if !reflect.DeepEqual(got, want) {
180 t.Errorf("Got reaction %#v, want %#v", got, want)
181 }
182 if !reflect.DeepEqual(gotParams, wantParams) {
183 t.Errorf("Got params %#v, want %#v", gotParams, wantParams)
184 }
185 }
186
187 func TestSlack_GetReaction_ToFile(t *testing.T) {
188 once.Do(startServer)
189 SLACK_API = "http://" + serverAddr + "/"
190 api := New("testing-token")
191 getReactionRes = `{"ok": true,
177 }}`,
178 []ItemReaction{
179 ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
180 ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
181 },
182 },
183 {
184 GetReactionParameters{ItemRef: NewRefToFile("FileID"), Full: true},
185 map[string]string{
186 "file": "FileID",
187 "full": "true",
188 },
189 `{"ok": true,
192190 "message": {
193191 "type": "file",
194192 "file": {
205203 }
206204 ]
207205 }
208 }}`
209 want := []ItemReaction{
210 ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
211 ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
212 }
213 wantParams := map[string]string{
214 "file": "FileID",
215 "full": "true",
216 }
217 gotParams = map[string]string{}
218 params := NewGetReactionParameters(NewRefToFile("FileID"))
219 params.Full = true
220 got, err := api.GetReactions(params)
221 if err != nil {
222 t.Fatalf("Unexpected error: %s", err)
223 }
224 if !reflect.DeepEqual(got, want) {
225 t.Errorf("Got reaction %#v, want %#v", got, want)
226 }
227 if !reflect.DeepEqual(gotParams, wantParams) {
228 t.Errorf("Got params %#v, want %#v", gotParams, wantParams)
229 }
230 }
231
232 func TestSlack_GetReaction_ToFileComment(t *testing.T) {
233 once.Do(startServer)
234 SLACK_API = "http://" + serverAddr + "/"
235 api := New("testing-token")
236 getReactionRes = `{"ok": true,
206 }}`,
207 []ItemReaction{
208 ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
209 ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
210 },
211 },
212 {
213
214 GetReactionParameters{ItemRef: NewRefToFileComment("FileCommentID")},
215 map[string]string{
216 "file_comment": "FileCommentID",
217 },
218 `{"ok": true,
237219 "message": {
238220 "type": "file_comment",
239221 "file_comment": {
252234 ]
253235 }
254236 }
255 }}`
256 want := []ItemReaction{
257 ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
258 ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
259 }
260 wantParams := map[string]string{
261 "file_comment": "FileCommentID",
262 }
263 gotParams = map[string]string{}
264 params := NewGetReactionParameters(NewRefToFileComment("FileCommentID"))
265 got, err := api.GetReactions(params)
266 if err != nil {
267 t.Fatalf("Unexpected error: %s", err)
268 }
269 if !reflect.DeepEqual(got, want) {
270 t.Errorf("Got reaction %#v, want %#v", got, want)
271 }
272 if !reflect.DeepEqual(gotParams, wantParams) {
273 t.Errorf("Got params %#v, want %#v", gotParams, wantParams)
237 }}`,
238 []ItemReaction{
239 ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
240 ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
241 },
242 },
243 }
244 for i, test := range tests {
245 gotParams = map[string]string{}
246 getReactionRes = test.json
247 got, err := api.GetReactions(test.params)
248 if err != nil {
249 t.Fatalf("%d: Unexpected error: %s", i, err)
250 }
251 if !reflect.DeepEqual(got, test.wantReactions) {
252 t.Errorf("%d: Got reaction %#v, want %#v", i, got, test.wantReactions)
253 }
254 if !reflect.DeepEqual(gotParams, test.wantParams) {
255 t.Errorf("%d: Got params %#v, want %#v", i, gotParams, test.wantParams)
256 }
274257 }
275258 }
276259