Codebase list golang-github-nlopes-slack / 69368cc
implement RemoveReactionand DRY tests for AddReaction Ryan Carver 8 years ago
2 changed file(s) with 134 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
3131 // NewAddReactionParameters initialies the inputs to react to an item.
3232 func NewAddReactionParameters(name string, ref ItemRef) AddReactionParameters {
3333 return AddReactionParameters{Name: name, ItemRef: ref}
34 }
35
36 // RemoveReactionParameters is the inputs to remove an existing reaction.
37 type RemoveReactionParameters struct {
38 Name string
39 ItemRef
40 }
41
42 // NewAddReactionParameters initialies the inputs to react to an item.
43 func NewRemoveReactionParameters(name string, ref ItemRef) RemoveReactionParameters {
44 return RemoveReactionParameters{Name: name, ItemRef: ref}
3445 }
3546
3647 // GetReactionParameters is the inputs to get reactions to an item.
174185 return nil
175186 }
176187
188 // RemoveReaction removes a reaction emoji from a message, file or file comment.
189 func (api *Slack) RemoveReaction(params RemoveReactionParameters) error {
190 values := url.Values{
191 "token": {api.config.token},
192 }
193 if params.Name != "" {
194 values.Set("name", params.Name)
195 }
196 if params.ChannelId != "" {
197 values.Set("channel", string(params.ChannelId))
198 }
199 if params.Timestamp != "" {
200 values.Set("timestamp", string(params.Timestamp))
201 }
202 if params.FileId != "" {
203 values.Set("file", string(params.FileId))
204 }
205 if params.FileCommentId != "" {
206 values.Set("file_comment", string(params.FileCommentId))
207 }
208 response := &SlackResponse{}
209 if err := parseResponse("reactions.remove", values, response, api.debug); err != nil {
210 return err
211 }
212 if !response.Ok {
213 return errors.New(response.Error)
214 }
215 return nil
216 }
217
177218 // GetReactions returns details about the reactions on an item.
178219 func (api *Slack) GetReactions(params GetReactionParameters) ([]ItemReaction, error) {
179220 values := url.Values{
77 )
88
99 func init() {
10 http.HandleFunc("/reactions.add", addReactionHandler)
10 http.HandleFunc("/reactions.add", reactionHandler)
11 http.HandleFunc("/reactions.remove", reactionHandler)
1112 http.HandleFunc("/reactions.get", getReactionHandler)
1213 http.HandleFunc("/reactions.list", listReactionHandler)
1314 }
2425 }
2526 }
2627
27 func addReactionHandler(w http.ResponseWriter, r *http.Request) {
28 func reactionHandler(w http.ResponseWriter, r *http.Request) {
2829 accumulateFormValue("name", r)
30 accumulateFormValue("channel", r)
31 accumulateFormValue("timestamp", r)
2932 accumulateFormValue("file", r)
3033 accumulateFormValue("file_comment", r)
34 w.Header().Set("Content-Type", "application/json")
35 w.Write([]byte(`{ "ok": true }`))
36 }
37
38 func getReactionHandler(w http.ResponseWriter, r *http.Request) {
3139 accumulateFormValue("channel", r)
3240 accumulateFormValue("timestamp", r)
33 w.Header().Set("Content-Type", "application/json")
34 w.Write([]byte(`{ "ok": true }`))
35 }
36
37 func getReactionHandler(w http.ResponseWriter, r *http.Request) {
3841 accumulateFormValue("file", r)
3942 accumulateFormValue("file_comment", r)
40 accumulateFormValue("channel", r)
41 accumulateFormValue("timestamp", r)
4243 accumulateFormValue("full", r)
4344 w.Header().Set("Content-Type", "application/json")
4445 w.Write([]byte(getReactionRes))
5354 w.Write([]byte(listReactionRes))
5455 }
5556
56 func TestSlack_AddReaction_ToMessage(t *testing.T) {
57 once.Do(startServer)
58 SLACK_API = "http://" + serverAddr + "/"
59 api := New("testing-token")
60 wantParams := map[string]string{
61 "name": "thumbsup",
62 "channel": "ChannelID",
63 "timestamp": "123",
64 }
65 gotParams = map[string]string{}
66 params := NewAddReactionParameters("thumbsup", NewRefToMessage("ChannelID", "123"))
67 err := api.AddReaction(params)
68 if err != nil {
69 t.Fatalf("Unexpected error: %s", err)
70 }
71 if !reflect.DeepEqual(gotParams, wantParams) {
72 t.Errorf("Got params %#v, want %#v", gotParams, wantParams)
73 }
74 }
75
76 func TestSlack_AddReaction_ToFile(t *testing.T) {
77 once.Do(startServer)
78 SLACK_API = "http://" + serverAddr + "/"
79 api := New("testing-token")
80 wantParams := map[string]string{
81 "name": "thumbsup",
82 "file": "FileID",
83 }
84 gotParams = map[string]string{}
85 params := NewAddReactionParameters("thumbsup", NewRefToFile("FileID"))
86 err := api.AddReaction(params)
87 if err != nil {
88 t.Fatalf("Unexpected error: %s", err)
89 }
90 if !reflect.DeepEqual(gotParams, wantParams) {
91 t.Errorf("Got params %#v, want %#v", gotParams, wantParams)
92 }
93 }
94
95 func TestSlack_AddReaction_ToFileComment(t *testing.T) {
96 once.Do(startServer)
97 SLACK_API = "http://" + serverAddr + "/"
98 api := New("testing-token")
99 wantParams := map[string]string{
100 "name": "thumbsup",
101 "file_comment": "FileCommentID",
102 }
103 gotParams = map[string]string{}
104 params := NewAddReactionParameters("thumbsup", NewRefToFileComment("FileCommentID"))
105 err := api.AddReaction(params)
106 if err != nil {
107 t.Fatalf("Unexpected error: %s", err)
108 }
109 if !reflect.DeepEqual(gotParams, wantParams) {
110 t.Errorf("Got params %#v, want %#v", gotParams, wantParams)
57 func TestSlack_AddReaction(t *testing.T) {
58 once.Do(startServer)
59 SLACK_API = "http://" + serverAddr + "/"
60 api := New("testing-token")
61 tests := []struct {
62 params AddReactionParameters
63 wantParams map[string]string
64 }{
65 {
66 NewAddReactionParameters("thumbsup", NewRefToMessage("ChannelID", "123")),
67 map[string]string{
68 "name": "thumbsup",
69 "channel": "ChannelID",
70 "timestamp": "123",
71 },
72 },
73 {
74 NewAddReactionParameters("thumbsup", NewRefToFile("FileID")),
75 map[string]string{
76 "name": "thumbsup",
77 "file": "FileID",
78 },
79 },
80 {
81 NewAddReactionParameters("thumbsup", NewRefToFileComment("FileCommentID")),
82 map[string]string{
83 "name": "thumbsup",
84 "file_comment": "FileCommentID",
85 },
86 },
87 }
88 for i, test := range tests {
89 gotParams = map[string]string{}
90 err := api.AddReaction(test.params)
91 if err != nil {
92 t.Fatalf("%d: Unexpected error: %s", i, err)
93 }
94 if !reflect.DeepEqual(gotParams, test.wantParams) {
95 t.Errorf("%d: Got params %#v, want %#v", i, gotParams, test.wantParams)
96 }
97 }
98 }
99
100 func TestSlack_RemoveReaction(t *testing.T) {
101 once.Do(startServer)
102 SLACK_API = "http://" + serverAddr + "/"
103 api := New("testing-token")
104 tests := []struct {
105 params RemoveReactionParameters
106 wantParams map[string]string
107 }{
108 {
109 NewRemoveReactionParameters("thumbsup", NewRefToMessage("ChannelID", "123")),
110 map[string]string{
111 "name": "thumbsup",
112 "channel": "ChannelID",
113 "timestamp": "123",
114 },
115 },
116 {
117 NewRemoveReactionParameters("thumbsup", NewRefToFile("FileID")),
118 map[string]string{
119 "name": "thumbsup",
120 "file": "FileID",
121 },
122 },
123 {
124 NewRemoveReactionParameters("thumbsup", NewRefToFileComment("FileCommentID")),
125 map[string]string{
126 "name": "thumbsup",
127 "file_comment": "FileCommentID",
128 },
129 },
130 }
131 for i, test := range tests {
132 gotParams = map[string]string{}
133 err := api.RemoveReaction(test.params)
134 if err != nil {
135 t.Fatalf("%d: Unexpected error: %s", i, err)
136 }
137 if !reflect.DeepEqual(gotParams, test.wantParams) {
138 t.Errorf("%d: Got params %#v, want %#v", i, gotParams, test.wantParams)
139 }
111140 }
112141 }
113142