github.com/blend/go-sdk@v1.20220411.3/slack/webhook_sender_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package slack 9 10 import ( 11 "context" 12 "encoding/json" 13 "fmt" 14 "net/http" 15 "net/http/httptest" 16 "testing" 17 18 "github.com/blend/go-sdk/assert" 19 "github.com/blend/go-sdk/ex" 20 ) 21 22 func Test_WebhookSender_OK(t *testing.T) { 23 its := assert.New(t) 24 25 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 26 w.WriteHeader(http.StatusOK) 27 })) 28 defer ts.Close() 29 30 config := Config{ 31 Webhook: ts.URL, 32 } 33 34 sender := New(config) 35 err := sender.Send(context.TODO(), Message{ 36 Text: "this is only a test", 37 }) 38 its.Nil(err) 39 } 40 41 func Test_WebhookSender_BadStatusCode_ErrorMessage(t *testing.T) { 42 its := assert.New(t) 43 44 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 45 w.WriteHeader(http.StatusBadRequest) 46 fmt.Fprintf(w, "remote message here\n") 47 })) 48 defer ts.Close() 49 50 config := Config{ 51 Webhook: ts.URL, 52 } 53 54 sender := New(config) 55 err := sender.Send(context.TODO(), Message{ 56 Text: "this is only a test", 57 }) 58 its.NotNil(err) 59 its.True(ex.Is(err, ErrNon200)) 60 its.Equal("remote message here\n", ex.ErrMessage(err)) 61 } 62 63 func Test_WebhookSender_Defaults(t *testing.T) { 64 its := assert.New(t) 65 66 config := Config{ 67 Webhook: "http://foo.com", 68 Channel: "#bot-test", 69 Username: "default-test", 70 } 71 72 sender := New(config) 73 74 message := Message{ 75 Text: "this is only a test", 76 } 77 78 defaults := sender.MessageDefaults() 79 80 for _, option := range defaults { 81 option(&message) 82 } 83 84 its.Equal("this is only a test", message.Text) 85 its.Equal("#bot-test", message.Channel) 86 its.Equal("default-test", message.Username) 87 } 88 89 func Test_WebhookSend_DecodeResponse(t *testing.T) { 90 its := assert.New(t) 91 92 mockResponse := PostMessageResponse{ 93 OK: true, 94 Channel: "#bot-test", 95 Timestamp: "1503435956.000247", 96 Message: Message{ 97 Text: "Here's a message for you", 98 Username: "ecto1", 99 BotID: "B19LU7CSY", 100 Attachments: []MessageAttachment{ 101 { 102 Text: "This is an attachment", 103 }, 104 }, 105 Type: "message", 106 SubType: "bot_message", 107 Timestamp: "1503435956.000247", 108 }, 109 } 110 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 111 _ = json.NewEncoder(w).Encode(mockResponse) 112 })) 113 defer ts.Close() 114 115 config := Config{ 116 Webhook: ts.URL, 117 } 118 sender := New(config) 119 120 // Test: Successful send should return the response body 121 response, err := sender.SendAndReadResponse(context.TODO(), Message{ 122 Text: "this is only a test", 123 }) 124 its.Nil(err) 125 its.Equal(mockResponse, *response) 126 } 127 128 func Test_WebhookSender_ReadResponse_BadStatusCode(t *testing.T) { 129 its := assert.New(t) 130 131 mockResponse := PostMessageResponse{ 132 OK: false, 133 Error: "too_many_attachments", 134 } 135 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 136 w.WriteHeader(http.StatusBadRequest) 137 _ = json.NewEncoder(w).Encode(mockResponse) 138 })) 139 defer ts.Close() 140 141 config := Config{ 142 Webhook: ts.URL, 143 } 144 sender := New(config) 145 146 // Test: Non-200 http response should cause an error to be returned along with the response body 147 response, err := sender.SendAndReadResponse(context.TODO(), Message{ 148 Text: "this is only a test", 149 }) 150 its.NotNil(err) 151 its.Equal(mockResponse, *response) 152 } 153 154 func Test_WebhookSender_PostMessageAndReadResponse(t *testing.T) { 155 its := assert.New(t) 156 157 mockResponse := PostMessageResponse{ 158 OK: true, 159 Channel: "#bot-test", 160 Timestamp: "1503435956.000247", 161 Message: Message{ 162 Text: "Here's a message for you", 163 Username: "ecto1", 164 BotID: "B19LU7CSY", 165 Attachments: []MessageAttachment{ 166 { 167 Text: "This is an attachment", 168 }, 169 }, 170 Type: "message", 171 SubType: "bot_message", 172 Timestamp: "1503435956.000247", 173 }, 174 } 175 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 176 var received Message 177 err := json.NewDecoder(r.Body).Decode(&received) 178 if err != nil { 179 w.WriteHeader(http.StatusBadRequest) 180 } else { 181 mockResponse.Channel = received.Channel 182 mockResponse.Message.Text = received.Text 183 } 184 _ = json.NewEncoder(w).Encode(mockResponse) 185 })) 186 defer ts.Close() 187 188 config := Config{ 189 Webhook: ts.URL, 190 } 191 sender := New(config) 192 193 // Test: Channel and text parameters should be passed along in the request 194 expectedChannel, expectedText := "#test-channel", "Test test" 195 response, err := sender.PostMessageAndReadResponse(expectedChannel, expectedText) 196 its.Nil(err) 197 its.Equal(true, response.OK) 198 its.Equal(expectedChannel, response.Channel) 199 its.Equal(expectedText, response.Message.Text) 200 }