| 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 | |
| 12 | func TestPostWebhook_OK(t *testing.T) { |
| 13 | once.Do(startServer) |
| 14 | |
| 15 | var receivedPayload WebhookMessage |
| 16 | |
| 17 | http.HandleFunc("/webhook", func(rw http.ResponseWriter, r *http.Request) { |
| 18 | rw.Header().Set("Content-Type", "application/json") |
| 19 | |
| 20 | decoder := json.NewDecoder(r.Body) |
| 21 | err := decoder.Decode(&receivedPayload) |
| 22 | if err != nil { |
| 23 | t.Errorf("Request contained invalid JSON, %s", err) |
| 24 | } |
| 25 | |
| 26 | response := []byte(`{}`) |
| 27 | rw.Write(response) |
| 28 | }) |
| 29 | |
| 30 | url := "http://" + serverAddr + "/webhook" |
| 31 | |
| 32 | payload := &WebhookMessage{ |
| 33 | Text: "Test Text", |
| 34 | Attachments: []Attachment{ |
| 35 | { |
| 36 | Text: "Foo", |
| 37 | }, |
| 38 | }, |
| 39 | } |
| 40 | |
| 41 | err := PostWebhook(url, payload) |
| 42 | |
| 43 | if err != nil { |
| 44 | t.Errorf("Expected not to receive error: %s", err) |
| 45 | } |
| 46 | |
| 47 | if !reflect.DeepEqual(payload, &receivedPayload) { |
| 48 | t.Errorf("Payload did not match\nwant: %#v\n got: %#v", payload, receivedPayload) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestPostWebhook_NotOK(t *testing.T) { |
| 53 | once.Do(startServer) |
| 54 | |
| 55 | http.HandleFunc("/webhook2", func(rw http.ResponseWriter, r *http.Request) { |
| 56 | rw.WriteHeader(http.StatusInternalServerError) |
| 57 | rw.Write([]byte("500 - Something bad happened!")) |
| 58 | }) |
| 59 | |
| 60 | url := "http://" + serverAddr + "/webhook2" |
| 61 | |
| 62 | err := PostWebhook(url, &WebhookMessage{}) |
| 63 | |
| 64 | if err == nil { |
| 65 | t.Errorf("Expected to receive error") |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestWebhookMessage_WithBlocks(t *testing.T) { |
| 70 | textBlockObject := NewTextBlockObject("plain_text", "text", false, false) |
| 71 | sectionBlock := NewSectionBlock(textBlockObject, nil, nil) |
| 72 | |
| 73 | singleBlock := &Blocks{BlockSet: []Block{sectionBlock}} |
| 74 | twoBlocks := &Blocks{BlockSet: []Block{sectionBlock, sectionBlock}} |
| 75 | |
| 76 | msgSingleBlock := WebhookMessage{Blocks: singleBlock} |
| 77 | assert.Equal(t, 1, len(msgSingleBlock.Blocks.BlockSet)) |
| 78 | |
| 79 | msgJsonSingleBlock, _ := json.Marshal(msgSingleBlock) |
| 80 | assert.Equal(t, `{"blocks":[{"type":"section","text":{"type":"plain_text","text":"text"}}]}`, string(msgJsonSingleBlock)) |
| 81 | |
| 82 | msgTwoBlocks := WebhookMessage{Blocks: twoBlocks} |
| 83 | assert.Equal(t, 2, len(msgTwoBlocks.Blocks.BlockSet)) |
| 84 | |
| 85 | msgJsonTwoBlocks, _ := json.Marshal(msgTwoBlocks) |
| 86 | assert.Equal(t, `{"blocks":[{"type":"section","text":{"type":"plain_text","text":"text"}},{"type":"section","text":{"type":"plain_text","text":"text"}}]}`, string(msgJsonTwoBlocks)) |
| 87 | |
| 88 | msgNoBlocks := WebhookMessage{Text: "foo"} |
| 89 | msgJsonNoBlocks, _ := json.Marshal(msgNoBlocks) |
| 90 | assert.Equal(t, `{"text":"foo"}`, string(msgJsonNoBlocks)) |
| 91 | } |