| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io/ioutil" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func postMessageInvalidChannelHandler(rw http.ResponseWriter, r *http.Request) { |
| 13 | rw.Header().Set("Content-Type", "application/json") |
| 14 | response, _ := json.Marshal(chatResponseFull{ |
| 15 | SlackResponse: SlackResponse{Ok: false, Error: "channel_not_found"}, |
| 16 | }) |
| 17 | rw.Write(response) |
| 18 | } |
| 19 | |
| 20 | func TestPostMessageInvalidChannel(t *testing.T) { |
| 21 | http.DefaultServeMux = new(http.ServeMux) |
| 22 | http.HandleFunc("/chat.postMessage", postMessageInvalidChannelHandler) |
| 23 | once.Do(startServer) |
| 24 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 25 | _, _, err := api.PostMessage("CXXXXXXXX", MsgOptionText("hello", false)) |
| 26 | if err == nil { |
| 27 | t.Errorf("Expected error: channel_not_found; instead succeeded") |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | if err.Error() != "channel_not_found" { |
| 32 | t.Errorf("Expected error: channel_not_found; received: %s", err) |
| 33 | return |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestGetPermalink(t *testing.T) { |
| 38 | channel := "C1H9RESGA" |
| 39 | timeStamp := "p135854651500008" |
| 40 | |
| 41 | http.HandleFunc("/chat.getPermalink", func(rw http.ResponseWriter, r *http.Request) { |
| 42 | |
| 43 | if got, want := r.Header.Get("Content-Type"), "application/x-www-form-urlencoded"; got != want { |
| 44 | t.Errorf("request uses unexpected content type: got %s, want %s", got, want) |
| 45 | } |
| 46 | |
| 47 | if got, want := r.URL.Query().Get("channel"), channel; got != want { |
| 48 | t.Errorf("request contains unexpected channel: got %s, want %s", got, want) |
| 49 | } |
| 50 | |
| 51 | if got, want := r.URL.Query().Get("message_ts"), timeStamp; got != want { |
| 52 | t.Errorf("request contains unexpected message timestamp: got %s, want %s", got, want) |
| 53 | } |
| 54 | |
| 55 | rw.Header().Set("Content-Type", "application/json") |
| 56 | response := []byte("{\"ok\": true, \"channel\": \"" + channel + "\", \"permalink\": \"https://ghostbusters.slack.com/archives/" + channel + "/" + timeStamp + "\"}") |
| 57 | rw.Write(response) |
| 58 | }) |
| 59 | |
| 60 | once.Do(startServer) |
| 61 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 62 | pp := PermalinkParameters{Channel: channel, Ts: timeStamp} |
| 63 | pl, err := api.GetPermalink(&pp) |
| 64 | |
| 65 | if got, want := pl, "https://ghostbusters.slack.com/archives/C1H9RESGA/p135854651500008"; got != want { |
| 66 | t.Errorf("unexpected permalink: got %s, want %s", got, want) |
| 67 | } |
| 68 | |
| 69 | if err != nil { |
| 70 | t.Errorf("unexpected error returned: %v", err) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestPostMessage(t *testing.T) { |
| 75 | type messageTest struct { |
| 76 | endpoint string |
| 77 | opt []MsgOption |
| 78 | expected url.Values |
| 79 | } |
| 80 | |
| 81 | blocks := []Block{NewContextBlock("context", NewTextBlockObject(PlainTextType, "hello", false, false))} |
| 82 | blockStr := `[{"type":"context","block_id":"context","elements":[{"type":"plain_text","text":"hello"}]}]` |
| 83 | |
| 84 | tests := map[string]messageTest{ |
| 85 | "OnlyBasicProperties": { |
| 86 | endpoint: "/chat.postMessage", |
| 87 | opt: []MsgOption{}, |
| 88 | expected: url.Values{ |
| 89 | "channel": []string{"CXXX"}, |
| 90 | "token": []string{"testing-token"}, |
| 91 | }, |
| 92 | }, |
| 93 | "Blocks": { |
| 94 | endpoint: "/chat.postMessage", |
| 95 | opt: []MsgOption{ |
| 96 | MsgOptionBlocks(blocks...), |
| 97 | MsgOptionText("text", false), |
| 98 | }, |
| 99 | expected: url.Values{ |
| 100 | "blocks": []string{blockStr}, |
| 101 | "channel": []string{"CXXX"}, |
| 102 | "text": []string{"text"}, |
| 103 | "token": []string{"testing-token"}, |
| 104 | }, |
| 105 | }, |
| 106 | "Attachment": { |
| 107 | endpoint: "/chat.postMessage", |
| 108 | opt: []MsgOption{ |
| 109 | MsgOptionAttachments( |
| 110 | Attachment{ |
| 111 | Blocks: Blocks{BlockSet: blocks}, |
| 112 | }), |
| 113 | }, |
| 114 | expected: url.Values{ |
| 115 | "attachments": []string{`[{"blocks":` + blockStr + `}]`}, |
| 116 | "channel": []string{"CXXX"}, |
| 117 | "token": []string{"testing-token"}, |
| 118 | }, |
| 119 | }, |
| 120 | "Metadata": { |
| 121 | endpoint: "/chat.postMessage", |
| 122 | opt: []MsgOption{ |
| 123 | MsgOptionMetadata( |
| 124 | SlackMetadata{ |
| 125 | EventType: "testing-event", |
| 126 | EventPayload: map[string]interface{}{ |
| 127 | "id": 13, |
| 128 | "name": "testing-name", |
| 129 | }, |
| 130 | }), |
| 131 | }, |
| 132 | expected: url.Values{ |
| 133 | "metadata": []string{`{"event_type":"testing-event","event_payload":{"id":13,"name":"testing-name"}}`}, |
| 134 | "channel": []string{"CXXX"}, |
| 135 | "token": []string{"testing-token"}, |
| 136 | }, |
| 137 | }, |
| 138 | "Unfurl": { |
| 139 | endpoint: "/chat.unfurl", |
| 140 | opt: []MsgOption{ |
| 141 | MsgOptionUnfurl("123", map[string]Attachment{"something": {Text: "attachment-test"}}), |
| 142 | }, |
| 143 | expected: url.Values{ |
| 144 | "channel": []string{"CXXX"}, |
| 145 | "token": []string{"testing-token"}, |
| 146 | "ts": []string{"123"}, |
| 147 | "unfurls": []string{`{"something":{"text":"attachment-test","blocks":null}}`}, |
| 148 | }, |
| 149 | }, |
| 150 | "UnfurlAuthURL": { |
| 151 | endpoint: "/chat.unfurl", |
| 152 | opt: []MsgOption{ |
| 153 | MsgOptionUnfurlAuthURL("123", "https://auth-url.com"), |
| 154 | }, |
| 155 | expected: url.Values{ |
| 156 | "channel": []string{"CXXX"}, |
| 157 | "token": []string{"testing-token"}, |
| 158 | "ts": []string{"123"}, |
| 159 | "user_auth_url": []string{"https://auth-url.com"}, |
| 160 | }, |
| 161 | }, |
| 162 | "UnfurlAuthRequired": { |
| 163 | endpoint: "/chat.unfurl", |
| 164 | opt: []MsgOption{ |
| 165 | MsgOptionUnfurlAuthRequired("123"), |
| 166 | }, |
| 167 | expected: url.Values{ |
| 168 | "channel": []string{"CXXX"}, |
| 169 | "token": []string{"testing-token"}, |
| 170 | "ts": []string{"123"}, |
| 171 | "user_auth_required": []string{"true"}, |
| 172 | }, |
| 173 | }, |
| 174 | "UnfurlAuthMessage": { |
| 175 | endpoint: "/chat.unfurl", |
| 176 | opt: []MsgOption{ |
| 177 | MsgOptionUnfurlAuthMessage("123", "Please!"), |
| 178 | }, |
| 179 | expected: url.Values{ |
| 180 | "channel": []string{"CXXX"}, |
| 181 | "token": []string{"testing-token"}, |
| 182 | "ts": []string{"123"}, |
| 183 | "user_auth_message": []string{"Please!"}, |
| 184 | }, |
| 185 | }, |
| 186 | } |
| 187 | |
| 188 | once.Do(startServer) |
| 189 | api := New(validToken, OptionAPIURL("http://"+serverAddr+"/")) |
| 190 | |
| 191 | for name, test := range tests { |
| 192 | t.Run(name, func(t *testing.T) { |
| 193 | http.DefaultServeMux = new(http.ServeMux) |
| 194 | http.HandleFunc(test.endpoint, func(rw http.ResponseWriter, r *http.Request) { |
| 195 | body, err := ioutil.ReadAll(r.Body) |
| 196 | if err != nil { |
| 197 | t.Errorf("unexpected error: %v", err) |
| 198 | return |
| 199 | } |
| 200 | actual, err := url.ParseQuery(string(body)) |
| 201 | if err != nil { |
| 202 | t.Errorf("unexpected error: %v", err) |
| 203 | return |
| 204 | } |
| 205 | if !reflect.DeepEqual(actual, test.expected) { |
| 206 | t.Errorf("\nexpected: %s\n actual: %s", test.expected, actual) |
| 207 | return |
| 208 | } |
| 209 | }) |
| 210 | |
| 211 | _, _, _ = api.PostMessage("CXXX", test.opt...) |
| 212 | }) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | func TestPostMessageWithBlocksWhenMsgOptionResponseURLApplied(t *testing.T) { |
| 217 | expectedBlocks := []Block{NewContextBlock("context", NewTextBlockObject(PlainTextType, "hello", false, false))} |
| 218 | |
| 219 | http.DefaultServeMux = new(http.ServeMux) |
| 220 | http.HandleFunc("/response-url", func(rw http.ResponseWriter, r *http.Request) { |
| 221 | body, err := ioutil.ReadAll(r.Body) |
| 222 | if err != nil { |
| 223 | t.Errorf("unexpected error: %v", err) |
| 224 | return |
| 225 | } |
| 226 | var msg Msg |
| 227 | if err := json.Unmarshal(body, &msg); err != nil { |
| 228 | t.Errorf("unexpected error: %v", err) |
| 229 | return |
| 230 | } |
| 231 | actualBlocks := msg.Blocks.BlockSet |
| 232 | if !reflect.DeepEqual(expectedBlocks, actualBlocks) { |
| 233 | t.Errorf("expected: %#v, got: %#v", expectedBlocks, actualBlocks) |
| 234 | return |
| 235 | } |
| 236 | }) |
| 237 | |
| 238 | once.Do(startServer) |
| 239 | api := New(validToken, OptionAPIURL("http://"+serverAddr+"/")) |
| 240 | |
| 241 | responseURL := api.endpoint + "response-url" |
| 242 | |
| 243 | _, _, _ = api.PostMessage("CXXX", MsgOptionBlocks(expectedBlocks...), MsgOptionText("text", false), MsgOptionResponseURL(responseURL, ResponseTypeInChannel)) |
| 244 | } |
| 245 | |
| 246 | func TestPostMessageWhenMsgOptionReplaceOriginalApplied(t *testing.T) { |
| 247 | http.DefaultServeMux = new(http.ServeMux) |
| 248 | http.HandleFunc("/response-url", func(rw http.ResponseWriter, r *http.Request) { |
| 249 | body, err := ioutil.ReadAll(r.Body) |
| 250 | if err != nil { |
| 251 | t.Errorf("unexpected error: %v", err) |
| 252 | return |
| 253 | } |
| 254 | var msg Msg |
| 255 | if err := json.Unmarshal(body, &msg); err != nil { |
| 256 | t.Errorf("unexpected error: %v", err) |
| 257 | return |
| 258 | } |
| 259 | if msg.ReplaceOriginal != true { |
| 260 | t.Errorf("expected: true, got: %v", msg.ReplaceOriginal) |
| 261 | return |
| 262 | } |
| 263 | }) |
| 264 | |
| 265 | once.Do(startServer) |
| 266 | api := New(validToken, OptionAPIURL("http://"+serverAddr+"/")) |
| 267 | |
| 268 | responseURL := api.endpoint + "response-url" |
| 269 | |
| 270 | _, _, _ = api.PostMessage("CXXX", MsgOptionText("text", false), MsgOptionReplaceOriginal(responseURL)) |
| 271 | } |
| 272 | |
| 273 | func TestPostMessageWhenMsgOptionDeleteOriginalApplied(t *testing.T) { |
| 274 | http.DefaultServeMux = new(http.ServeMux) |
| 275 | http.HandleFunc("/response-url", func(rw http.ResponseWriter, r *http.Request) { |
| 276 | body, err := ioutil.ReadAll(r.Body) |
| 277 | if err != nil { |
| 278 | t.Errorf("unexpected error: %v", err) |
| 279 | return |
| 280 | } |
| 281 | var msg Msg |
| 282 | if err := json.Unmarshal(body, &msg); err != nil { |
| 283 | t.Errorf("unexpected error: %v", err) |
| 284 | return |
| 285 | } |
| 286 | if msg.DeleteOriginal != true { |
| 287 | t.Errorf("expected: true, got: %v", msg.DeleteOriginal) |
| 288 | return |
| 289 | } |
| 290 | }) |
| 291 | |
| 292 | once.Do(startServer) |
| 293 | api := New(validToken, OptionAPIURL("http://"+serverAddr+"/")) |
| 294 | |
| 295 | responseURL := api.endpoint + "response-url" |
| 296 | |
| 297 | _, _, _ = api.PostMessage("CXXX", MsgOptionDeleteOriginal(responseURL)) |
| 298 | } |