| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | dialogSubmissionCallback = `{ |
| 12 | "type": "dialog_submission", |
| 13 | "submission": { |
| 14 | "name": "Sigourney Dreamweaver", |
| 15 | "email": "[email protected]", |
| 16 | "phone": "+1 800-555-1212", |
| 17 | "meal": "burrito", |
| 18 | "comment": "No sour cream please", |
| 19 | "team_channel": "C0LFFBKPB", |
| 20 | "who_should_sing": "U0MJRG1AL" |
| 21 | }, |
| 22 | "callback_id": "employee_offsite_1138b", |
| 23 | "team": { |
| 24 | "id": "T1ABCD2E12", |
| 25 | "domain": "coverbands" |
| 26 | }, |
| 27 | "user": { |
| 28 | "id": "W12A3BCDEF", |
| 29 | "name": "dreamweaver" |
| 30 | }, |
| 31 | "channel": { |
| 32 | "id": "C1AB2C3DE", |
| 33 | "name": "coverthon-1999" |
| 34 | }, |
| 35 | "action_ts": "936893340.702759", |
| 36 | "token": "M1AqUUw3FqayAbqNtsGMch72", |
| 37 | "response_url": "https://hooks.slack.com/app/T012AB0A1/123456789/JpmK0yzoZDeRiqfeduTBYXWQ" |
| 38 | }` |
| 39 | actionCallback = `{}` |
| 40 | viewClosedCallback = `{ |
| 41 | "type": "view_closed", |
| 42 | "team": { |
| 43 | "id": "T1ABCD2E12", |
| 44 | "domain": "coverbands" |
| 45 | }, |
| 46 | "user": { |
| 47 | "id": "W12A3BCDEF", |
| 48 | "name": "dreamweaver" |
| 49 | }, |
| 50 | "view": { |
| 51 | "type": "modal", |
| 52 | "title": { |
| 53 | "type": "plain_text", |
| 54 | "text": "launch project" |
| 55 | }, |
| 56 | "blocks": [{ |
| 57 | "type": "section", |
| 58 | "text": { |
| 59 | "text": "*Sally* has requested you set the deadline for the Nano launch project", |
| 60 | "type": "mrkdwn" |
| 61 | }, |
| 62 | "accessory": { |
| 63 | "type": "datepicker", |
| 64 | "action_id": "datepicker123", |
| 65 | "initial_date": "1990-04-28", |
| 66 | "placeholder": { |
| 67 | "type": "plain_text", |
| 68 | "text": "Select a date" |
| 69 | } |
| 70 | } |
| 71 | }], |
| 72 | "app_installed_team_id": "T1ABCD2E12" |
| 73 | }, |
| 74 | "api_app_id": "A123ABC", |
| 75 | "is_cleared": false |
| 76 | }` |
| 77 | viewSubmissionCallback = `{ |
| 78 | "type": "view_submission", |
| 79 | "team": { |
| 80 | "id": "T1ABCD2E12", |
| 81 | "domain": "coverbands" |
| 82 | }, |
| 83 | "user": { |
| 84 | "id": "W12A3BCDEF", |
| 85 | "name": "dreamweaver" |
| 86 | }, |
| 87 | "channel": { |
| 88 | "id": "C1AB2C3DE", |
| 89 | "name": "coverthon-1999" |
| 90 | }, |
| 91 | "view": { |
| 92 | "type": "modal", |
| 93 | "title": { |
| 94 | "type": "plain_text", |
| 95 | "text": "meal choice" |
| 96 | }, |
| 97 | "blocks": [ |
| 98 | { |
| 99 | "type": "input", |
| 100 | "block_id": "multi-line", |
| 101 | "label": { |
| 102 | "type": "plain_text", |
| 103 | "text": "dietary restrictions" |
| 104 | }, |
| 105 | "element": { |
| 106 | "type": "plain_text_input", |
| 107 | "multiline": true, |
| 108 | "action_id": "ml-value" |
| 109 | } |
| 110 | }, |
| 111 | { |
| 112 | "type": "input", |
| 113 | "block_id": "target_channel", |
| 114 | "label": { |
| 115 | "type": "plain_text", |
| 116 | "text": "Select a channel to post the result on" |
| 117 | }, |
| 118 | "element": { |
| 119 | "type": "conversations_select", |
| 120 | "action_id": "target_select", |
| 121 | "default_to_current_conversation": true, |
| 122 | "response_url_enabled": true |
| 123 | } |
| 124 | } |
| 125 | ], |
| 126 | "state": { |
| 127 | "values": { |
| 128 | "multi-line": { |
| 129 | "ml-value": { |
| 130 | "type": "plain_text_input", |
| 131 | "value": "No onions" |
| 132 | } |
| 133 | }, |
| 134 | "target_channel": { |
| 135 | "target_select": { |
| 136 | "type": "conversations_select", |
| 137 | "value": "C1AB2C3DE" |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | }, |
| 142 | "app_installed_team_id": "T1ABCD2E12" |
| 143 | }, |
| 144 | "hash": "156663117.cd33ad1f", |
| 145 | "response_urls": [ |
| 146 | { |
| 147 | "block_id": "target_channel", |
| 148 | "action_id": "target_select", |
| 149 | "channel_id": "C1AB2C3DE", |
| 150 | "response_url": "https:\/\/hooks.slack.com\/app\/ABC12312\/1234567890\/A100B100C100d100" |
| 151 | } |
| 152 | ] |
| 153 | }` |
| 154 | ) |
| 155 | |
| 156 | func assertInteractionCallback(t *testing.T, callback InteractionCallback, encoded string) { |
| 157 | var decoded InteractionCallback |
| 158 | assert.Nil(t, json.Unmarshal([]byte(encoded), &decoded)) |
| 159 | assert.Equal(t, decoded, callback) |
| 160 | } |
| 161 | |
| 162 | func TestDialogCallback(t *testing.T) { |
| 163 | expected := InteractionCallback{ |
| 164 | Type: InteractionTypeDialogSubmission, |
| 165 | Token: "M1AqUUw3FqayAbqNtsGMch72", |
| 166 | CallbackID: "employee_offsite_1138b", |
| 167 | ResponseURL: "https://hooks.slack.com/app/T012AB0A1/123456789/JpmK0yzoZDeRiqfeduTBYXWQ", |
| 168 | ActionTs: "936893340.702759", |
| 169 | Team: Team{ID: "T1ABCD2E12", Name: "", Domain: "coverbands"}, |
| 170 | Channel: Channel{ |
| 171 | GroupConversation: GroupConversation{ |
| 172 | Conversation: Conversation{ |
| 173 | ID: "C1AB2C3DE", |
| 174 | }, |
| 175 | Name: "coverthon-1999", |
| 176 | }, |
| 177 | }, |
| 178 | User: User{ |
| 179 | ID: "W12A3BCDEF", |
| 180 | Name: "dreamweaver", |
| 181 | }, |
| 182 | DialogSubmissionCallback: DialogSubmissionCallback{ |
| 183 | Submission: map[string]string{ |
| 184 | "team_channel": "C0LFFBKPB", |
| 185 | "who_should_sing": "U0MJRG1AL", |
| 186 | "name": "Sigourney Dreamweaver", |
| 187 | "email": "[email protected]", |
| 188 | "phone": "+1 800-555-1212", |
| 189 | "meal": "burrito", |
| 190 | "comment": "No sour cream please", |
| 191 | }, |
| 192 | }, |
| 193 | } |
| 194 | assertInteractionCallback(t, expected, dialogSubmissionCallback) |
| 195 | } |
| 196 | |
| 197 | func TestActionCallback(t *testing.T) { |
| 198 | assertInteractionCallback(t, InteractionCallback{}, actionCallback) |
| 199 | } |
| 200 | |
| 201 | func TestViewClosedck(t *testing.T) { |
| 202 | expected := InteractionCallback{ |
| 203 | Type: InteractionTypeViewClosed, |
| 204 | Team: Team{ID: "T1ABCD2E12", Name: "", Domain: "coverbands"}, |
| 205 | User: User{ |
| 206 | ID: "W12A3BCDEF", |
| 207 | Name: "dreamweaver", |
| 208 | }, |
| 209 | View: View{ |
| 210 | Type: VTModal, |
| 211 | Title: NewTextBlockObject("plain_text", "launch project", false, false), |
| 212 | Blocks: Blocks{ |
| 213 | BlockSet: []Block{ |
| 214 | NewSectionBlock( |
| 215 | NewTextBlockObject("mrkdwn", "*Sally* has requested you set the deadline for the Nano launch project", false, false), |
| 216 | nil, |
| 217 | NewAccessory(&DatePickerBlockElement{ |
| 218 | Type: METDatepicker, |
| 219 | ActionID: "datepicker123", |
| 220 | InitialDate: "1990-04-28", |
| 221 | Placeholder: NewTextBlockObject("plain_text", "Select a date", false, false), |
| 222 | }), |
| 223 | ), |
| 224 | }, |
| 225 | }, |
| 226 | AppInstalledTeamID: "T1ABCD2E12", |
| 227 | }, |
| 228 | APIAppID: "A123ABC", |
| 229 | } |
| 230 | assertInteractionCallback(t, expected, viewClosedCallback) |
| 231 | } |
| 232 | |
| 233 | func TestViewSubmissionCallback(t *testing.T) { |
| 234 | expected := InteractionCallback{ |
| 235 | Type: InteractionTypeViewSubmission, |
| 236 | Team: Team{ID: "T1ABCD2E12", Name: "", Domain: "coverbands"}, |
| 237 | Channel: Channel{ |
| 238 | GroupConversation: GroupConversation{ |
| 239 | Conversation: Conversation{ |
| 240 | ID: "C1AB2C3DE", |
| 241 | }, |
| 242 | Name: "coverthon-1999", |
| 243 | }, |
| 244 | }, |
| 245 | User: User{ |
| 246 | ID: "W12A3BCDEF", |
| 247 | Name: "dreamweaver", |
| 248 | }, |
| 249 | View: View{ |
| 250 | Type: VTModal, |
| 251 | Title: NewTextBlockObject("plain_text", "meal choice", false, false), |
| 252 | Blocks: Blocks{ |
| 253 | BlockSet: []Block{ |
| 254 | NewInputBlock( |
| 255 | "multi-line", |
| 256 | NewTextBlockObject( |
| 257 | "plain_text", |
| 258 | "dietary restrictions", |
| 259 | false, |
| 260 | false, |
| 261 | ), |
| 262 | nil, |
| 263 | &PlainTextInputBlockElement{ |
| 264 | Type: "plain_text_input", |
| 265 | ActionID: "ml-value", |
| 266 | Multiline: true, |
| 267 | }, |
| 268 | ), |
| 269 | NewInputBlock( |
| 270 | "target_channel", |
| 271 | NewTextBlockObject( |
| 272 | "plain_text", |
| 273 | "Select a channel to post the result on", |
| 274 | false, |
| 275 | false, |
| 276 | ), |
| 277 | nil, |
| 278 | &SelectBlockElement{ |
| 279 | Type: "conversations_select", |
| 280 | ActionID: "target_select", |
| 281 | DefaultToCurrentConversation: true, |
| 282 | ResponseURLEnabled: true, |
| 283 | }, |
| 284 | ), |
| 285 | }, |
| 286 | }, |
| 287 | State: &ViewState{ |
| 288 | Values: map[string]map[string]BlockAction{ |
| 289 | "multi-line": map[string]BlockAction{ |
| 290 | "ml-value": BlockAction{ |
| 291 | Type: "plain_text_input", |
| 292 | Value: "No onions", |
| 293 | }, |
| 294 | }, |
| 295 | "target_channel": map[string]BlockAction{ |
| 296 | "target_select": BlockAction{ |
| 297 | Type: "conversations_select", |
| 298 | Value: "C1AB2C3DE", |
| 299 | }, |
| 300 | }, |
| 301 | }, |
| 302 | }, |
| 303 | AppInstalledTeamID: "T1ABCD2E12", |
| 304 | }, |
| 305 | ViewSubmissionCallback: ViewSubmissionCallback{ |
| 306 | Hash: "156663117.cd33ad1f", |
| 307 | ResponseURLs: []ViewSubmissionCallbackResponseURL{ |
| 308 | { |
| 309 | BlockID: "target_channel", |
| 310 | ActionID: "target_select", |
| 311 | ChannelID: "C1AB2C3DE", |
| 312 | ResponseURL: "https://hooks.slack.com/app/ABC12312/1234567890/A100B100C100d100", |
| 313 | }, |
| 314 | }, |
| 315 | }, |
| 316 | } |
| 317 | assertInteractionCallback(t, expected, viewSubmissionCallback) |
| 318 | } |
| 319 | |
| 320 | func TestInteractionCallbackJSONMarshalAndUnmarshal(t *testing.T) { |
| 321 | cb := &InteractionCallback{ |
| 322 | Type: InteractionTypeBlockActions, |
| 323 | Token: "token", |
| 324 | CallbackID: "", |
| 325 | ResponseURL: "responseURL", |
| 326 | TriggerID: "triggerID", |
| 327 | ActionTs: "actionTS", |
| 328 | Team: Team{ |
| 329 | ID: "teamid", |
| 330 | Name: "teamname", |
| 331 | }, |
| 332 | Channel: Channel{ |
| 333 | GroupConversation: GroupConversation{ |
| 334 | Name: "channelname", |
| 335 | Conversation: Conversation{ID: "channelid"}, |
| 336 | }, |
| 337 | }, |
| 338 | User: User{ |
| 339 | ID: "userid", |
| 340 | Name: "username", |
| 341 | Profile: UserProfile{RealName: "userrealname"}, |
| 342 | }, |
| 343 | OriginalMessage: Message{ |
| 344 | Msg: Msg{ |
| 345 | Text: "ogmsg text", |
| 346 | Timestamp: "ogmsg ts", |
| 347 | }, |
| 348 | }, |
| 349 | Message: Message{ |
| 350 | Msg: Msg{ |
| 351 | Text: "text", |
| 352 | Timestamp: "ts", |
| 353 | }, |
| 354 | }, |
| 355 | Name: "name", |
| 356 | Value: "value", |
| 357 | MessageTs: "messageTs", |
| 358 | AttachmentID: "attachmentID", |
| 359 | ActionCallback: ActionCallbacks{ |
| 360 | AttachmentActions: []*AttachmentAction{ |
| 361 | {Value: "value"}, |
| 362 | {Value: "value2"}, |
| 363 | }, |
| 364 | BlockActions: []*BlockAction{ |
| 365 | {ActionID: "id123"}, |
| 366 | {ActionID: "id456"}, |
| 367 | }, |
| 368 | }, |
| 369 | View: View{ |
| 370 | Type: VTModal, |
| 371 | Title: NewTextBlockObject("plain_text", "title", false, false), |
| 372 | Blocks: Blocks{ |
| 373 | BlockSet: []Block{NewDividerBlock()}, |
| 374 | }, |
| 375 | }, |
| 376 | DialogSubmissionCallback: DialogSubmissionCallback{State: ""}, |
| 377 | RawState: json.RawMessage(`{}`), |
| 378 | } |
| 379 | |
| 380 | cbJSONBytes, err := json.Marshal(cb) |
| 381 | assert.NoError(t, err) |
| 382 | |
| 383 | jsonCB := new(InteractionCallback) |
| 384 | err = json.Unmarshal(cbJSONBytes, jsonCB) |
| 385 | assert.NoError(t, err) |
| 386 | |
| 387 | assert.Equal(t, cb.Type, jsonCB.Type) |
| 388 | assert.Equal(t, cb.Token, jsonCB.Token) |
| 389 | assert.Equal(t, cb.CallbackID, jsonCB.CallbackID) |
| 390 | assert.Equal(t, cb.ResponseURL, jsonCB.ResponseURL) |
| 391 | assert.Equal(t, cb.TriggerID, jsonCB.TriggerID) |
| 392 | assert.Equal(t, cb.ActionTs, jsonCB.ActionTs) |
| 393 | assert.Equal(t, cb.Team.ID, jsonCB.Team.ID) |
| 394 | assert.Equal(t, cb.Team.Name, jsonCB.Team.Name) |
| 395 | assert.Equal(t, cb.Channel.ID, jsonCB.Channel.ID) |
| 396 | assert.Equal(t, cb.Channel.Name, jsonCB.Channel.Name) |
| 397 | assert.Equal(t, cb.Channel.Created, jsonCB.Channel.Created) |
| 398 | assert.Equal(t, cb.User.ID, jsonCB.User.ID) |
| 399 | assert.Equal(t, cb.User.Name, jsonCB.User.Name) |
| 400 | assert.Equal(t, cb.User.Profile.RealName, jsonCB.User.Profile.RealName) |
| 401 | assert.Equal(t, cb.OriginalMessage.Text, jsonCB.OriginalMessage.Text) |
| 402 | assert.Equal(t, cb.OriginalMessage.Timestamp, |
| 403 | jsonCB.OriginalMessage.Timestamp) |
| 404 | assert.Equal(t, cb.Message.Text, jsonCB.Message.Text) |
| 405 | assert.Equal(t, cb.Message.Timestamp, jsonCB.Message.Timestamp) |
| 406 | assert.Equal(t, cb.Name, jsonCB.Name) |
| 407 | assert.Equal(t, cb.Value, jsonCB.Value) |
| 408 | assert.Equal(t, cb.MessageTs, jsonCB.MessageTs) |
| 409 | assert.Equal(t, cb.AttachmentID, jsonCB.AttachmentID) |
| 410 | assert.Equal(t, len(cb.ActionCallback.AttachmentActions), |
| 411 | len(jsonCB.ActionCallback.AttachmentActions)) |
| 412 | assert.Equal(t, len(cb.ActionCallback.BlockActions), |
| 413 | len(jsonCB.ActionCallback.BlockActions)) |
| 414 | assert.Equal(t, cb.View.Type, jsonCB.View.Type) |
| 415 | assert.Equal(t, cb.View.Title, jsonCB.View.Title) |
| 416 | assert.Equal(t, cb.View.Blocks, jsonCB.View.Blocks) |
| 417 | assert.Equal(t, cb.DialogSubmissionCallback.State, |
| 418 | jsonCB.DialogSubmissionCallback.State) |
| 419 | } |
| 420 | |
| 421 | func TestInteractionCallback_InteractionTypeBlockActions_Unmarshal(t *testing.T) { |
| 422 | raw := []byte(`{ |
| 423 | "type": "block_actions", |
| 424 | "actions": [ |
| 425 | { |
| 426 | "type": "multi_conversations_select", |
| 427 | "action_id": "multi_convos", |
| 428 | "block_id": "test123", |
| 429 | "selected_conversations": ["G12345"] |
| 430 | } |
| 431 | ], |
| 432 | "container": { |
| 433 | "type": "view", |
| 434 | "view_id": "V12345" |
| 435 | }, |
| 436 | "state": { |
| 437 | "values": { |
| 438 | "section_block_id": { |
| 439 | "multi_convos": { |
| 440 | "type": "multi_conversations_select", |
| 441 | "selected_conversations": ["G12345"] |
| 442 | } |
| 443 | }, |
| 444 | "other_block_id": { |
| 445 | "other_action_id": { |
| 446 | "type": "plain_text_input", |
| 447 | "value": "test123" |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | }`) |
| 453 | var cb InteractionCallback |
| 454 | assert.NoError(t, json.Unmarshal(raw, &cb)) |
| 455 | assert.Equal(t, cb.State, "") |
| 456 | assert.Equal(t, |
| 457 | cb.BlockActionState.Values["section_block_id"]["multi_convos"].actionType(), |
| 458 | ActionType(MultiOptTypeConversations)) |
| 459 | assert.Equal(t, |
| 460 | cb.BlockActionState.Values["section_block_id"]["multi_convos"].SelectedConversations, |
| 461 | []string{"G12345"}) |
| 462 | } |
| 463 | |
| 464 | func TestInteractionCallback_Container_Marshal_And_Unmarshal(t *testing.T) { |
| 465 | // Contrived - you generally won't see all of the fields set in a single message |
| 466 | raw := []byte( |
| 467 | ` |
| 468 | { |
| 469 | "container": { |
| 470 | "type": "message", |
| 471 | "view_id": "viewID", |
| 472 | "message_ts": "messageTS", |
| 473 | "attachment_id": "123", |
| 474 | "channel_id": "channelID", |
| 475 | "is_ephemeral": false, |
| 476 | "is_app_unfurl": false |
| 477 | } |
| 478 | } |
| 479 | `) |
| 480 | |
| 481 | expected := &InteractionCallback{ |
| 482 | Container: Container{ |
| 483 | Type: "message", |
| 484 | ViewID: "viewID", |
| 485 | MessageTs: "messageTS", |
| 486 | AttachmentID: "123", |
| 487 | ChannelID: "channelID", |
| 488 | IsEphemeral: false, |
| 489 | IsAppUnfurl: false, |
| 490 | }, |
| 491 | RawState: json.RawMessage(`{}`), |
| 492 | } |
| 493 | |
| 494 | actual := new(InteractionCallback) |
| 495 | err := json.Unmarshal(raw, actual) |
| 496 | assert.NoError(t, err) |
| 497 | assert.Equal(t, expected.Container, actual.Container) |
| 498 | |
| 499 | expectedJSON := []byte(`{"type":"message","view_id":"viewID","message_ts":"messageTS","attachment_id":123,"channel_id":"channelID","is_ephemeral":false,"is_app_unfurl":false}`) |
| 500 | actualJSON, err := json.Marshal(actual.Container) |
| 501 | assert.NoError(t, err) |
| 502 | assert.Equal(t, expectedJSON, actualJSON) |
| 503 | } |
| 504 | |
| 505 | func TestInteractionCallback_In_Thread_Container_Marshal_And_Unmarshal(t *testing.T) { |
| 506 | // Contrived - you generally won't see all of the fields set in a single message |
| 507 | raw := []byte( |
| 508 | ` |
| 509 | { |
| 510 | "container": { |
| 511 | "type": "message", |
| 512 | "view_id": "viewID", |
| 513 | "message_ts": "messageTS", |
| 514 | "thread_ts": "threadTS", |
| 515 | "attachment_id": "123", |
| 516 | "channel_id": "channelID", |
| 517 | "is_ephemeral": false, |
| 518 | "is_app_unfurl": false |
| 519 | } |
| 520 | } |
| 521 | `) |
| 522 | |
| 523 | expected := &InteractionCallback{ |
| 524 | Container: Container{ |
| 525 | Type: "message", |
| 526 | ViewID: "viewID", |
| 527 | MessageTs: "messageTS", |
| 528 | ThreadTs: "threadTS", |
| 529 | AttachmentID: "123", |
| 530 | ChannelID: "channelID", |
| 531 | IsEphemeral: false, |
| 532 | IsAppUnfurl: false, |
| 533 | }, |
| 534 | RawState: json.RawMessage(`{}`), |
| 535 | } |
| 536 | |
| 537 | actual := new(InteractionCallback) |
| 538 | err := json.Unmarshal(raw, actual) |
| 539 | assert.NoError(t, err) |
| 540 | assert.Equal(t, expected.Container, actual.Container) |
| 541 | |
| 542 | expectedJSON := []byte(`{"type":"message","view_id":"viewID","message_ts":"messageTS","thread_ts":"threadTS","attachment_id":123,"channel_id":"channelID","is_ephemeral":false,"is_app_unfurl":false}`) |
| 543 | actualJSON, err := json.Marshal(actual.Container) |
| 544 | assert.NoError(t, err) |
| 545 | assert.Equal(t, expectedJSON, actualJSON) |
| 546 | } |