| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | |
| 11 | "github.com/slack-go/slack/internal/errorsx" |
| 12 | ) |
| 13 | |
| 14 | var dummySlackErr = errorsx.String("dummy_error_from_slack") |
| 15 | |
| 16 | type viewsHandler struct { |
| 17 | rawResponse string |
| 18 | } |
| 19 | |
| 20 | func (h *viewsHandler) handler(w http.ResponseWriter, r *http.Request) { |
| 21 | w.Header().Set("Content-Type", "application/json") |
| 22 | w.Write([]byte(h.rawResponse)) |
| 23 | } |
| 24 | |
| 25 | func TestSlack_OpenView(t *testing.T) { |
| 26 | once.Do(startServer) |
| 27 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 28 | |
| 29 | cases := []struct { |
| 30 | caseName string |
| 31 | triggerID string |
| 32 | modalViewRequest ModalViewRequest |
| 33 | rawResp string |
| 34 | expectedResp *ViewResponse |
| 35 | expectedErr error |
| 36 | }{ |
| 37 | { |
| 38 | caseName: "pass empty trigger_id", |
| 39 | triggerID: "", |
| 40 | modalViewRequest: ModalViewRequest{}, |
| 41 | rawResp: "", |
| 42 | expectedResp: nil, |
| 43 | expectedErr: ErrParametersMissing, |
| 44 | }, |
| 45 | { |
| 46 | caseName: "raise an error for not having a unique block id", |
| 47 | triggerID: "dummy_trigger_id", |
| 48 | modalViewRequest: ModalViewRequest{ |
| 49 | Blocks: Blocks{ |
| 50 | BlockSet: []Block{ |
| 51 | &InputBlock{ |
| 52 | BlockID: "example", |
| 53 | }, |
| 54 | &InputBlock{ |
| 55 | BlockID: "example", |
| 56 | }, |
| 57 | }, |
| 58 | }, |
| 59 | }, |
| 60 | rawResp: "", |
| 61 | expectedResp: nil, |
| 62 | expectedErr: ErrBlockIDNotUnique, |
| 63 | }, |
| 64 | { |
| 65 | caseName: "raise an error from Slack API", |
| 66 | triggerID: "dummy_trigger_id", |
| 67 | modalViewRequest: ModalViewRequest{}, |
| 68 | rawResp: `{ |
| 69 | "ok": false, |
| 70 | "error": "dummy_error_from_slack", |
| 71 | "response_metadata": { |
| 72 | "warnings": [ |
| 73 | "missing_charset" |
| 74 | ], |
| 75 | "messages": [ |
| 76 | "[WARN] A Content-Type HTTP header was presented but did not declare a charset, such as a 'utf-8'" |
| 77 | ] |
| 78 | } |
| 79 | }`, |
| 80 | expectedResp: &ViewResponse{ |
| 81 | SlackResponse{ |
| 82 | Ok: false, |
| 83 | Error: dummySlackErr.Error(), |
| 84 | ResponseMetadata: ResponseMetadata{ |
| 85 | Messages: []string{"[WARN] A Content-Type HTTP header was presented but did not declare a charset, such as a 'utf-8'"}, |
| 86 | Warnings: []string{"missing_charset"}, |
| 87 | }, |
| 88 | }, |
| 89 | View{}, |
| 90 | }, |
| 91 | expectedErr: dummySlackErr, |
| 92 | }, |
| 93 | { |
| 94 | caseName: "success", |
| 95 | triggerID: "dummy_trigger_id", |
| 96 | modalViewRequest: ModalViewRequest{}, |
| 97 | rawResp: `{ |
| 98 | "ok": true, |
| 99 | "view": { |
| 100 | "id": "VMHU10V25", |
| 101 | "team_id": "T8N4K1JN", |
| 102 | "type": "modal", |
| 103 | "title": { |
| 104 | "type": "plain_text", |
| 105 | "text": "Quite a plain modal" |
| 106 | }, |
| 107 | "submit": { |
| 108 | "type": "plain_text", |
| 109 | "text": "Create" |
| 110 | }, |
| 111 | "blocks": [ |
| 112 | { |
| 113 | "type": "input", |
| 114 | "block_id": "a_block_id", |
| 115 | "label": { |
| 116 | "type": "plain_text", |
| 117 | "text": "A simple label" |
| 118 | }, |
| 119 | "optional": false, |
| 120 | "element": { |
| 121 | "type": "plain_text_input", |
| 122 | "placeholder": { |
| 123 | "type": "plain_text", |
| 124 | "text": "Placeholder text" |
| 125 | }, |
| 126 | "action_id": "an_action_id" |
| 127 | } |
| 128 | } |
| 129 | ], |
| 130 | "private_metadata": "Shh it is a secret", |
| 131 | "callback_id": "identify_your_modals", |
| 132 | "external_id": "", |
| 133 | "state": { |
| 134 | "values": {} |
| 135 | }, |
| 136 | "hash": "156772938.1827394", |
| 137 | "clear_on_close": false, |
| 138 | "notify_on_close": false, |
| 139 | "root_view_id": "VMHU10V25", |
| 140 | "app_id": "AA4928AQ", |
| 141 | "bot_id": "BA13894H" |
| 142 | } |
| 143 | }`, |
| 144 | expectedResp: &ViewResponse{ |
| 145 | SlackResponse{ |
| 146 | Ok: true, |
| 147 | Error: "", |
| 148 | }, |
| 149 | View{ |
| 150 | ID: "VMHU10V25", |
| 151 | Type: VTModal, |
| 152 | TeamID: "T8N4K1JN", |
| 153 | Title: &TextBlockObject{ |
| 154 | Type: PlainTextType, |
| 155 | Text: "Quite a plain modal", |
| 156 | }, |
| 157 | Submit: &TextBlockObject{ |
| 158 | Type: PlainTextType, |
| 159 | Text: "Create", |
| 160 | }, |
| 161 | CallbackID: "identify_your_modals", |
| 162 | PrivateMetadata: "Shh it is a secret", |
| 163 | State: &ViewState{}, |
| 164 | Hash: "156772938.1827394", |
| 165 | RootViewID: "VMHU10V25", |
| 166 | AppID: "AA4928AQ", |
| 167 | BotID: "BA13894H", |
| 168 | Blocks: Blocks{ |
| 169 | BlockSet: []Block{ |
| 170 | NewInputBlock( |
| 171 | "a_block_id", |
| 172 | &TextBlockObject{ |
| 173 | Type: PlainTextType, |
| 174 | Text: "A simple label", |
| 175 | }, |
| 176 | &TextBlockObject{ |
| 177 | Type: PlainTextType, |
| 178 | Text: "A simple hint", |
| 179 | }, |
| 180 | NewPlainTextInputBlockElement( |
| 181 | &TextBlockObject{ |
| 182 | Type: PlainTextType, |
| 183 | Text: "Placeholder text", |
| 184 | }, |
| 185 | "an_action_id", |
| 186 | )), |
| 187 | }, |
| 188 | }, |
| 189 | }, |
| 190 | }, |
| 191 | expectedErr: nil, |
| 192 | }, |
| 193 | } |
| 194 | |
| 195 | h := &viewsHandler{} |
| 196 | http.HandleFunc("/views.open", h.handler) |
| 197 | for _, c := range cases { |
| 198 | t.Run(c.caseName, func(t *testing.T) { |
| 199 | h.rawResponse = c.rawResp |
| 200 | |
| 201 | resp, err := api.OpenView(c.triggerID, c.modalViewRequest) |
| 202 | if c.expectedErr == nil && err != nil { |
| 203 | t.Fatalf("unexpected error: %s\n", err) |
| 204 | } |
| 205 | if c.expectedErr != nil && err == nil { |
| 206 | t.Fatalf("expected %s, but did not raise an error", c.expectedErr) |
| 207 | } |
| 208 | if c.expectedErr != nil && err != nil && c.expectedErr.Error() != err.Error() { |
| 209 | t.Fatalf("expected %s as error but got %s\n", c.expectedErr, err) |
| 210 | } |
| 211 | if resp == nil || c.expectedResp == nil { |
| 212 | return |
| 213 | } |
| 214 | if !reflect.DeepEqual(resp.ResponseMetadata.Messages, c.expectedResp.ResponseMetadata.Messages) { |
| 215 | t.Fatalf("expected:\n\t%v\n but got:\n\t%v\n", c.expectedResp.ResponseMetadata.Messages, resp.ResponseMetadata.Messages) |
| 216 | } |
| 217 | if !reflect.DeepEqual(resp.ResponseMetadata.Warnings, c.expectedResp.ResponseMetadata.Warnings) { |
| 218 | t.Fatalf("expected:\n\t%v\n but got:\n\t%v\n", c.expectedResp.ResponseMetadata.Warnings, resp.ResponseMetadata.Warnings) |
| 219 | } |
| 220 | }) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | func TestSlack_View_PublishView(t *testing.T) { |
| 225 | once.Do(startServer) |
| 226 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 227 | |
| 228 | cases := []struct { |
| 229 | caseName string |
| 230 | userID string |
| 231 | rawResp string |
| 232 | expectedResp *ViewResponse |
| 233 | expectedErr error |
| 234 | }{ |
| 235 | { |
| 236 | caseName: "pass empty user_id", |
| 237 | userID: "", |
| 238 | rawResp: "", |
| 239 | expectedResp: nil, |
| 240 | expectedErr: ErrParametersMissing, |
| 241 | }, |
| 242 | { |
| 243 | caseName: "raise an error from Slack API", |
| 244 | userID: "dummy_user_id", |
| 245 | rawResp: `{ |
| 246 | "ok": false, |
| 247 | "error": "dummy_error_from_slack", |
| 248 | "response_metadata": { |
| 249 | "warnings": [ |
| 250 | "missing_charset" |
| 251 | ], |
| 252 | "messages": [ |
| 253 | "[WARN] A Content-Type HTTP header was presented but did not declare a charset, such as a 'utf-8'" |
| 254 | ] |
| 255 | } |
| 256 | }`, |
| 257 | expectedResp: &ViewResponse{ |
| 258 | SlackResponse{ |
| 259 | Ok: false, |
| 260 | Error: dummySlackErr.Error(), |
| 261 | ResponseMetadata: ResponseMetadata{ |
| 262 | Messages: []string{"[WARN] A Content-Type HTTP header was presented but did not declare a charset, such as a 'utf-8'"}, |
| 263 | Warnings: []string{"missing_charset"}, |
| 264 | }, |
| 265 | }, |
| 266 | View{}, |
| 267 | }, |
| 268 | expectedErr: dummySlackErr, |
| 269 | }, |
| 270 | { |
| 271 | caseName: "success", |
| 272 | userID: "dummy_user_id", |
| 273 | rawResp: `{ |
| 274 | "ok": true, |
| 275 | "view": { |
| 276 | "id": "VMHU10V25", |
| 277 | "team_id": "T8N4K1JN", |
| 278 | "type": "home", |
| 279 | "close": null, |
| 280 | "submit": null, |
| 281 | "blocks": [ |
| 282 | { |
| 283 | "type": "input", |
| 284 | "block_id": "a_block_id", |
| 285 | "label": { |
| 286 | "type": "plain_text", |
| 287 | "text": "A simple label" |
| 288 | }, |
| 289 | "optional": false, |
| 290 | "element": { |
| 291 | "type": "plain_text_input", |
| 292 | "placeholder": { |
| 293 | "type": "plain_text", |
| 294 | "text": "Placeholder text" |
| 295 | }, |
| 296 | "action_id": "an_action_id" |
| 297 | } |
| 298 | } |
| 299 | ], |
| 300 | "private_metadata": "Shh it is a secret", |
| 301 | "callback_id": "identify_your_home_tab", |
| 302 | "state": { |
| 303 | "values": {} |
| 304 | }, |
| 305 | "hash": "156772938.1827394", |
| 306 | "clear_on_close": false, |
| 307 | "notify_on_close": false, |
| 308 | "root_view_id": "VMHU10V25", |
| 309 | "previous_view_id": null, |
| 310 | "app_id": "AA4928AQ", |
| 311 | "external_id": "", |
| 312 | "bot_id": "BA13894H" |
| 313 | } |
| 314 | }`, |
| 315 | expectedResp: &ViewResponse{ |
| 316 | SlackResponse{ |
| 317 | Ok: true, |
| 318 | Error: "", |
| 319 | }, |
| 320 | View{ |
| 321 | ID: "VMHU10V25", |
| 322 | Type: VTHomeTab, |
| 323 | TeamID: "T8N4K1JN", |
| 324 | CallbackID: "identify_your_home_tab", |
| 325 | PrivateMetadata: "Shh it is a secret", |
| 326 | State: &ViewState{}, |
| 327 | Hash: "156772938.1827394", |
| 328 | RootViewID: "VMHU10V25", |
| 329 | AppID: "AA4928AQ", |
| 330 | BotID: "BA13894H", |
| 331 | Blocks: Blocks{ |
| 332 | BlockSet: []Block{ |
| 333 | NewInputBlock( |
| 334 | "a_block_id", |
| 335 | &TextBlockObject{ |
| 336 | Type: PlainTextType, |
| 337 | Text: "A simple label", |
| 338 | }, |
| 339 | &TextBlockObject{ |
| 340 | Type: PlainTextType, |
| 341 | Text: "A simple hint", |
| 342 | }, |
| 343 | NewPlainTextInputBlockElement( |
| 344 | &TextBlockObject{ |
| 345 | Type: PlainTextType, |
| 346 | Text: "Placeholder text", |
| 347 | }, |
| 348 | "an_action_id", |
| 349 | )), |
| 350 | }, |
| 351 | }, |
| 352 | }, |
| 353 | }, |
| 354 | expectedErr: nil, |
| 355 | }, |
| 356 | } |
| 357 | |
| 358 | h := &viewsHandler{} |
| 359 | http.HandleFunc("/views.publish", h.handler) |
| 360 | for _, c := range cases { |
| 361 | t.Run(c.caseName, func(t *testing.T) { |
| 362 | h.rawResponse = c.rawResp |
| 363 | |
| 364 | resp, err := api.PublishView(c.userID, HomeTabViewRequest{}, "dummy_hash") |
| 365 | if c.expectedErr == nil && err != nil { |
| 366 | t.Fatalf("unexpected error: %s\n", err) |
| 367 | } |
| 368 | if c.expectedErr != nil && err == nil { |
| 369 | t.Fatalf("expected %s, but did not raise an error", c.expectedErr) |
| 370 | } |
| 371 | if c.expectedErr != nil && err != nil && c.expectedErr.Error() != err.Error() { |
| 372 | t.Fatalf("expected %s as error but got %s\n", c.expectedErr, err) |
| 373 | } |
| 374 | if resp == nil || c.expectedResp == nil { |
| 375 | return |
| 376 | } |
| 377 | if !reflect.DeepEqual(resp.ResponseMetadata.Messages, c.expectedResp.ResponseMetadata.Messages) { |
| 378 | t.Fatalf("expected:\n\t%v\n but got:\n\t%v\n", c.expectedResp.ResponseMetadata.Messages, resp.ResponseMetadata.Messages) |
| 379 | } |
| 380 | if !reflect.DeepEqual(resp.ResponseMetadata.Warnings, c.expectedResp.ResponseMetadata.Warnings) { |
| 381 | t.Fatalf("expected:\n\t%v\n but got:\n\t%v\n", c.expectedResp.ResponseMetadata.Warnings, resp.ResponseMetadata.Warnings) |
| 382 | } |
| 383 | }) |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | func TestSlack_PushView(t *testing.T) { |
| 388 | once.Do(startServer) |
| 389 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 390 | |
| 391 | cases := []struct { |
| 392 | caseName string |
| 393 | triggerID string |
| 394 | rawResp string |
| 395 | expectedResp *ViewResponse |
| 396 | expectedErr error |
| 397 | }{ |
| 398 | { |
| 399 | caseName: "pass empty trigger_id", |
| 400 | triggerID: "", |
| 401 | rawResp: "", |
| 402 | expectedResp: nil, |
| 403 | expectedErr: ErrParametersMissing, |
| 404 | }, |
| 405 | { |
| 406 | caseName: "raise an error from Slack API", |
| 407 | triggerID: "dummy_trigger_id", |
| 408 | rawResp: `{ |
| 409 | "ok": false, |
| 410 | "error": "dummy_error_from_slack", |
| 411 | "response_metadata": { |
| 412 | "warnings": [ |
| 413 | "missing_charset" |
| 414 | ], |
| 415 | "messages": [ |
| 416 | "[WARN] A Content-Type HTTP header was presented but did not declare a charset, such as a 'utf-8'" |
| 417 | ] |
| 418 | } |
| 419 | }`, |
| 420 | expectedResp: &ViewResponse{ |
| 421 | SlackResponse{ |
| 422 | Ok: false, |
| 423 | Error: dummySlackErr.Error(), |
| 424 | ResponseMetadata: ResponseMetadata{ |
| 425 | Messages: []string{"[WARN] A Content-Type HTTP header was presented but did not declare a charset, such as a 'utf-8'"}, |
| 426 | Warnings: []string{"missing_charset"}, |
| 427 | }, |
| 428 | }, |
| 429 | View{}, |
| 430 | }, |
| 431 | expectedErr: dummySlackErr, |
| 432 | }, |
| 433 | { |
| 434 | caseName: "success", |
| 435 | triggerID: "dummy_trigger_id", |
| 436 | rawResp: `{ |
| 437 | "ok": true, |
| 438 | "view": { |
| 439 | "id": "VMHU10V25", |
| 440 | "team_id": "T8N4K1JN", |
| 441 | "type": "modal", |
| 442 | "title": { |
| 443 | "type": "plain_text", |
| 444 | "text": "Quite a plain modal" |
| 445 | }, |
| 446 | "submit": { |
| 447 | "type": "plain_text", |
| 448 | "text": "Create" |
| 449 | }, |
| 450 | "blocks": [ |
| 451 | { |
| 452 | "type": "input", |
| 453 | "block_id": "a_block_id", |
| 454 | "label": { |
| 455 | "type": "plain_text", |
| 456 | "text": "A simple label" |
| 457 | }, |
| 458 | "optional": false, |
| 459 | "element": { |
| 460 | "type": "plain_text_input", |
| 461 | "placeholder": { |
| 462 | "type": "plain_text", |
| 463 | "text": "Placeholder text" |
| 464 | }, |
| 465 | "action_id": "an_action_id" |
| 466 | } |
| 467 | } |
| 468 | ], |
| 469 | "private_metadata": "Shh it is a secret", |
| 470 | "callback_id": "identify_your_modals", |
| 471 | "external_id": "", |
| 472 | "state": { |
| 473 | "values": {} |
| 474 | }, |
| 475 | "hash": "156772938.1827394", |
| 476 | "clear_on_close": false, |
| 477 | "notify_on_close": false, |
| 478 | "root_view_id": "VMHU10V25", |
| 479 | "app_id": "AA4928AQ", |
| 480 | "bot_id": "BA13894H" |
| 481 | } |
| 482 | }`, |
| 483 | expectedResp: &ViewResponse{ |
| 484 | SlackResponse{ |
| 485 | Ok: true, |
| 486 | Error: "", |
| 487 | }, |
| 488 | View{ |
| 489 | ID: "VMHU10V25", |
| 490 | Type: VTModal, |
| 491 | TeamID: "T8N4K1JN", |
| 492 | Title: &TextBlockObject{ |
| 493 | Type: PlainTextType, |
| 494 | Text: "Quite a plain modal", |
| 495 | }, |
| 496 | Submit: &TextBlockObject{ |
| 497 | Type: PlainTextType, |
| 498 | Text: "Create", |
| 499 | }, |
| 500 | CallbackID: "identify_your_modals", |
| 501 | PrivateMetadata: "Shh it is a secret", |
| 502 | State: &ViewState{}, |
| 503 | Hash: "156772938.1827394", |
| 504 | RootViewID: "VMHU10V25", |
| 505 | AppID: "AA4928AQ", |
| 506 | BotID: "BA13894H", |
| 507 | Blocks: Blocks{ |
| 508 | BlockSet: []Block{ |
| 509 | NewInputBlock( |
| 510 | "a_block_id", |
| 511 | &TextBlockObject{ |
| 512 | Type: PlainTextType, |
| 513 | Text: "A simple label", |
| 514 | }, |
| 515 | &TextBlockObject{ |
| 516 | Type: PlainTextType, |
| 517 | Text: "A simple hint", |
| 518 | }, |
| 519 | NewPlainTextInputBlockElement( |
| 520 | &TextBlockObject{ |
| 521 | Type: PlainTextType, |
| 522 | Text: "Placeholder text", |
| 523 | }, |
| 524 | "an_action_id", |
| 525 | )), |
| 526 | }, |
| 527 | }, |
| 528 | }, |
| 529 | }, |
| 530 | expectedErr: nil, |
| 531 | }, |
| 532 | } |
| 533 | |
| 534 | h := &viewsHandler{} |
| 535 | http.HandleFunc("/views.push", h.handler) |
| 536 | for _, c := range cases { |
| 537 | t.Run(c.caseName, func(t *testing.T) { |
| 538 | h.rawResponse = c.rawResp |
| 539 | |
| 540 | resp, err := api.PushView(c.triggerID, ModalViewRequest{}) |
| 541 | if c.expectedErr == nil && err != nil { |
| 542 | t.Fatalf("unexpected error: %s\n", err) |
| 543 | } |
| 544 | if c.expectedErr != nil && err == nil { |
| 545 | t.Fatalf("expected %s, but did not raise an error", c.expectedErr) |
| 546 | } |
| 547 | if c.expectedErr != nil && err != nil && c.expectedErr.Error() != err.Error() { |
| 548 | t.Fatalf("expected %s as error but got %s\n", c.expectedErr, err) |
| 549 | } |
| 550 | if resp == nil || c.expectedResp == nil { |
| 551 | return |
| 552 | } |
| 553 | if !reflect.DeepEqual(resp.ResponseMetadata.Messages, c.expectedResp.ResponseMetadata.Messages) { |
| 554 | t.Fatalf("expected:\n\t%v\n but got:\n\t%v\n", c.expectedResp.ResponseMetadata.Messages, resp.ResponseMetadata.Messages) |
| 555 | } |
| 556 | if !reflect.DeepEqual(resp.ResponseMetadata.Warnings, c.expectedResp.ResponseMetadata.Warnings) { |
| 557 | t.Fatalf("expected:\n\t%v\n but got:\n\t%v\n", c.expectedResp.ResponseMetadata.Warnings, resp.ResponseMetadata.Warnings) |
| 558 | } |
| 559 | }) |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | func TestSlack_UpdateView(t *testing.T) { |
| 564 | once.Do(startServer) |
| 565 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 566 | |
| 567 | cases := []struct { |
| 568 | caseName string |
| 569 | externalID string |
| 570 | viewID string |
| 571 | rawResp string |
| 572 | expectedResp *ViewResponse |
| 573 | expectedErr error |
| 574 | }{ |
| 575 | { |
| 576 | caseName: "pass empty external_id and empty view_id", |
| 577 | externalID: "", |
| 578 | viewID: "", |
| 579 | rawResp: "", |
| 580 | expectedResp: nil, |
| 581 | expectedErr: ErrParametersMissing, |
| 582 | }, |
| 583 | { |
| 584 | caseName: "raise an error from Slack API", |
| 585 | externalID: "dummy_external_id", |
| 586 | viewID: "", |
| 587 | rawResp: `{ |
| 588 | "ok": false, |
| 589 | "error": "dummy_error_from_slack", |
| 590 | "response_metadata": { |
| 591 | "warnings": [ |
| 592 | "missing_charset" |
| 593 | ], |
| 594 | "messages": [ |
| 595 | "[WARN] A Content-Type HTTP header was presented but did not declare a charset, such as a 'utf-8'" |
| 596 | ] |
| 597 | } |
| 598 | }`, |
| 599 | expectedResp: &ViewResponse{ |
| 600 | SlackResponse{ |
| 601 | Ok: false, |
| 602 | Error: dummySlackErr.Error(), |
| 603 | ResponseMetadata: ResponseMetadata{ |
| 604 | Messages: []string{"[WARN] A Content-Type HTTP header was presented but did not declare a charset, such as a 'utf-8'"}, |
| 605 | Warnings: []string{"missing_charset"}, |
| 606 | }, |
| 607 | }, |
| 608 | View{}, |
| 609 | }, |
| 610 | expectedErr: dummySlackErr, |
| 611 | }, |
| 612 | { |
| 613 | caseName: "success", |
| 614 | externalID: "", |
| 615 | viewID: "dummy_view_id", |
| 616 | rawResp: `{ |
| 617 | "ok": true, |
| 618 | "view": { |
| 619 | "id": "VMHU10V25", |
| 620 | "team_id": "T8N4K1JN", |
| 621 | "type": "modal", |
| 622 | "title": { |
| 623 | "type": "plain_text", |
| 624 | "text": "Quite a plain modal" |
| 625 | }, |
| 626 | "submit": { |
| 627 | "type": "plain_text", |
| 628 | "text": "Create" |
| 629 | }, |
| 630 | "blocks": [ |
| 631 | { |
| 632 | "type": "input", |
| 633 | "block_id": "a_block_id", |
| 634 | "label": { |
| 635 | "type": "plain_text", |
| 636 | "text": "A simple label" |
| 637 | }, |
| 638 | "optional": false, |
| 639 | "element": { |
| 640 | "type": "plain_text_input", |
| 641 | "placeholder": { |
| 642 | "type": "plain_text", |
| 643 | "text": "Placeholder text" |
| 644 | }, |
| 645 | "action_id": "an_action_id" |
| 646 | } |
| 647 | } |
| 648 | ], |
| 649 | "private_metadata": "Shh it is a secret", |
| 650 | "callback_id": "identify_your_modals", |
| 651 | "external_id": "", |
| 652 | "state": { |
| 653 | "values": {} |
| 654 | }, |
| 655 | "hash": "156772938.1827394", |
| 656 | "clear_on_close": false, |
| 657 | "notify_on_close": false, |
| 658 | "root_view_id": "VMHU10V25", |
| 659 | "app_id": "AA4928AQ", |
| 660 | "bot_id": "BA13894H" |
| 661 | } |
| 662 | }`, |
| 663 | expectedResp: &ViewResponse{ |
| 664 | SlackResponse{ |
| 665 | Ok: true, |
| 666 | Error: "", |
| 667 | }, |
| 668 | View{ |
| 669 | ID: "VMHU10V25", |
| 670 | Type: VTModal, |
| 671 | TeamID: "T8N4K1JN", |
| 672 | Title: &TextBlockObject{ |
| 673 | Type: PlainTextType, |
| 674 | Text: "Quite a plain modal", |
| 675 | }, |
| 676 | Submit: &TextBlockObject{ |
| 677 | Type: PlainTextType, |
| 678 | Text: "Create", |
| 679 | }, |
| 680 | CallbackID: "identify_your_modals", |
| 681 | PrivateMetadata: "Shh it is a secret", |
| 682 | State: &ViewState{}, |
| 683 | Hash: "156772938.1827394", |
| 684 | RootViewID: "VMHU10V25", |
| 685 | AppID: "AA4928AQ", |
| 686 | BotID: "BA13894H", |
| 687 | Blocks: Blocks{ |
| 688 | BlockSet: []Block{ |
| 689 | NewInputBlock( |
| 690 | "a_block_id", |
| 691 | &TextBlockObject{ |
| 692 | Type: PlainTextType, |
| 693 | Text: "A simple label", |
| 694 | }, |
| 695 | &TextBlockObject{ |
| 696 | Type: PlainTextType, |
| 697 | Text: "A simple hint", |
| 698 | }, |
| 699 | NewPlainTextInputBlockElement( |
| 700 | &TextBlockObject{ |
| 701 | Type: PlainTextType, |
| 702 | Text: "Placeholder text", |
| 703 | }, |
| 704 | "an_action_id", |
| 705 | )), |
| 706 | }, |
| 707 | }, |
| 708 | }, |
| 709 | }, |
| 710 | expectedErr: nil, |
| 711 | }, |
| 712 | } |
| 713 | |
| 714 | h := &viewsHandler{} |
| 715 | http.HandleFunc("/views.update", h.handler) |
| 716 | for _, c := range cases { |
| 717 | t.Run(c.caseName, func(t *testing.T) { |
| 718 | h.rawResponse = c.rawResp |
| 719 | |
| 720 | resp, err := api.UpdateView(ModalViewRequest{}, c.externalID, "dummy_hash", c.viewID) |
| 721 | if c.expectedErr == nil && err != nil { |
| 722 | t.Fatalf("unexpected error: %s\n", err) |
| 723 | } |
| 724 | if c.expectedErr != nil && err == nil { |
| 725 | t.Fatalf("expected %s, but did not raise an error", c.expectedErr) |
| 726 | } |
| 727 | if c.expectedErr != nil && err != nil && c.expectedErr.Error() != err.Error() { |
| 728 | t.Fatalf("expected %s as error but got %s\n", c.expectedErr, err) |
| 729 | } |
| 730 | if resp == nil || c.expectedResp == nil { |
| 731 | return |
| 732 | } |
| 733 | if !reflect.DeepEqual(resp.ResponseMetadata.Messages, c.expectedResp.ResponseMetadata.Messages) { |
| 734 | t.Fatalf("expected:\n\t%v\n but got:\n\t%v\n", c.expectedResp.ResponseMetadata.Messages, resp.ResponseMetadata.Messages) |
| 735 | } |
| 736 | if !reflect.DeepEqual(resp.ResponseMetadata.Warnings, c.expectedResp.ResponseMetadata.Warnings) { |
| 737 | t.Fatalf("expected:\n\t%v\n but got:\n\t%v\n", c.expectedResp.ResponseMetadata.Warnings, resp.ResponseMetadata.Warnings) |
| 738 | } |
| 739 | }) |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | func assertViewSubmissionResponse(t *testing.T, resp *ViewSubmissionResponse, encoded string) { |
| 744 | var decoded *ViewSubmissionResponse |
| 745 | assert.Nil(t, json.Unmarshal([]byte(encoded), &decoded)) |
| 746 | assert.Equal(t, decoded, resp) |
| 747 | } |
| 748 | |
| 749 | func TestSlack_ClearViewSubmissionResponse(t *testing.T) { |
| 750 | resp := NewClearViewSubmissionResponse() |
| 751 | rawResp := `{ |
| 752 | "response_action": "clear" |
| 753 | }` |
| 754 | |
| 755 | assertViewSubmissionResponse(t, resp, rawResp) |
| 756 | } |
| 757 | |
| 758 | func TestSlack_UpdateViewSubmissionResponse(t *testing.T) { |
| 759 | resp := NewUpdateViewSubmissionResponse(&ModalViewRequest{ |
| 760 | Type: VTModal, |
| 761 | Title: NewTextBlockObject("plain_text", "Test update view submission response", false, false), |
| 762 | Blocks: Blocks{BlockSet: []Block{NewFileBlock("file_block_id", "external_string", "source_string")}}, |
| 763 | }) |
| 764 | rawResp := `{ |
| 765 | "response_action": "update", |
| 766 | "view": { |
| 767 | "type": "modal", |
| 768 | "title": { |
| 769 | "type": "plain_text", |
| 770 | "text": "Test update view submission response" |
| 771 | }, |
| 772 | "blocks": [ |
| 773 | { |
| 774 | "type": "file", |
| 775 | "block_id": "file_block_id", |
| 776 | "external_id": "external_string", |
| 777 | "source": "source_string" |
| 778 | } |
| 779 | ] |
| 780 | } |
| 781 | }` |
| 782 | |
| 783 | assertViewSubmissionResponse(t, resp, rawResp) |
| 784 | } |
| 785 | |
| 786 | func TestSlack_PushViewSubmissionResponse(t *testing.T) { |
| 787 | resp := NewPushViewSubmissionResponse(&ModalViewRequest{ |
| 788 | Type: VTModal, |
| 789 | Title: NewTextBlockObject("plain_text", "Test update view submission response", false, false), |
| 790 | Blocks: Blocks{ |
| 791 | BlockSet: []Block{ |
| 792 | NewContextBlock( |
| 793 | "context_block_id", |
| 794 | NewTextBlockObject("plain_text", "Context text", false, false), |
| 795 | NewImageBlockElement("image_url", "alt_text"), |
| 796 | ), |
| 797 | }, |
| 798 | }, |
| 799 | }) |
| 800 | rawResp := `{ |
| 801 | "response_action": "push", |
| 802 | "view": { |
| 803 | "type": "modal", |
| 804 | "title": { |
| 805 | "type": "plain_text", |
| 806 | "text": "Test update view submission response" |
| 807 | }, |
| 808 | "blocks": [ |
| 809 | { |
| 810 | "type": "context", |
| 811 | "block_id": "context_block_id", |
| 812 | "elements": [ |
| 813 | { |
| 814 | "type": "plain_text", |
| 815 | "text": "Context text" |
| 816 | }, |
| 817 | { |
| 818 | "type": "image", |
| 819 | "image_url": "image_url", |
| 820 | "alt_text": "alt_text" |
| 821 | } |
| 822 | ] |
| 823 | } |
| 824 | ] |
| 825 | } |
| 826 | }` |
| 827 | |
| 828 | assertViewSubmissionResponse(t, resp, rawResp) |
| 829 | } |
| 830 | |
| 831 | func TestSlack_ErrorsViewSubmissionResponse(t *testing.T) { |
| 832 | resp := NewErrorsViewSubmissionResponse(map[string]string{ |
| 833 | "input_text_action_id": "Please input a name that's at least 6 characters long", |
| 834 | "file_action_id": "File exceeded size limit of 5 KB", |
| 835 | }) |
| 836 | rawResp := `{ |
| 837 | "response_action": "errors", |
| 838 | "errors": { |
| 839 | "input_text_action_id": "Please input a name that's at least 6 characters long", |
| 840 | "file_action_id": "File exceeded size limit of 5 KB" |
| 841 | } |
| 842 | }` |
| 843 | |
| 844 | assertViewSubmissionResponse(t, resp, rawResp) |
| 845 | } |