github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/command_response_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestCommandResponseFromHTTPBody(t *testing.T) { 14 for _, test := range []struct { 15 ContentType string 16 Body string 17 ExpectedText string 18 }{ 19 {"", "foo", "foo"}, 20 {"text/plain", "foo", "foo"}, 21 {"application/json", `{"text": "foo"}`, "foo"}, 22 {"application/json; charset=utf-8", `{"text": "foo"}`, "foo"}, 23 {"application/json", `{"text": "` + "```" + `haskell\nlet\n\nf1 = [ 3 | a <- [1]]\nf2 = [ 4 | b <- [2]]\nf3 = \\p -> 5\n\nin 1\n` + "```" + `", "skip_slack_parsing": true}`, 24 "```haskell\nlet\n\nf1 = [ 3 | a <- [1]]\nf2 = [ 4 | b <- [2]]\nf3 = \\p -> 5\n\nin 1\n```", 25 }, 26 } { 27 response, err := CommandResponseFromHTTPBody(test.ContentType, strings.NewReader(test.Body)) 28 assert.NoError(t, err) 29 assert.Equal(t, test.ExpectedText, response.Text) 30 } 31 } 32 33 func TestCommandResponseFromPlainText(t *testing.T) { 34 response := CommandResponseFromPlainText("foo") 35 assert.Equal(t, "foo", response.Text) 36 } 37 38 func TestCommandResponseFromJson(t *testing.T) { 39 t.Parallel() 40 41 testCases := []struct { 42 Description string 43 Json string 44 ExpectedCommandResponse *CommandResponse 45 ShouldError bool 46 }{ 47 { 48 "empty response", 49 "", 50 nil, 51 true, 52 }, 53 { 54 "malformed response", 55 `{"text": }`, 56 nil, 57 true, 58 }, 59 { 60 "invalid response", 61 `{"text": "test", "response_type": 5}`, 62 nil, 63 true, 64 }, 65 { 66 "ephemeral response", 67 `{ 68 "response_type": "ephemeral", 69 "text": "response text", 70 "username": "response username", 71 "channel_id": "response channel id", 72 "icon_url": "response icon url", 73 "goto_location": "response goto location", 74 "attachments": [{ 75 "text": "attachment 1 text", 76 "pretext": "attachment 1 pretext" 77 },{ 78 "text": "attachment 2 text", 79 "fields": [{ 80 "title": "field 1", 81 "value": "value 1", 82 "short": true 83 },{ 84 "title": "field 2", 85 "value": [], 86 "short": false 87 }] 88 }] 89 }`, 90 &CommandResponse{ 91 ResponseType: "ephemeral", 92 Text: "response text", 93 Username: "response username", 94 ChannelId: "response channel id", 95 IconURL: "response icon url", 96 GotoLocation: "response goto location", 97 Attachments: []*SlackAttachment{ 98 { 99 Text: "attachment 1 text", 100 Pretext: "attachment 1 pretext", 101 }, 102 { 103 Text: "attachment 2 text", 104 Fields: []*SlackAttachmentField{ 105 { 106 Title: "field 1", 107 Value: "value 1", 108 Short: true, 109 }, 110 { 111 Title: "field 2", 112 Value: "[]", 113 Short: false, 114 }, 115 }, 116 }, 117 }, 118 }, 119 false, 120 }, 121 { 122 "null array items", 123 `{"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}, null]}, null]}`, 124 &CommandResponse{ 125 Attachments: []*SlackAttachment{ 126 { 127 Fields: []*SlackAttachmentField{ 128 { 129 Title: "foo", 130 Value: "bar", 131 Short: true, 132 }, 133 }, 134 }, 135 }, 136 }, 137 false, 138 }, 139 { 140 "multiple responses returned", 141 ` 142 { 143 "text": "message 1", 144 "extra_responses": [ 145 {"text": "message 2"} 146 ] 147 } 148 `, 149 &CommandResponse{ 150 Text: "message 1", 151 ExtraResponses: []*CommandResponse{ 152 { 153 Text: "message 2", 154 }, 155 }, 156 }, 157 false, 158 }, 159 { 160 "multiple responses returned, with attachments", 161 ` 162 { 163 "text": "message 1", 164 "attachments":[{"fields":[{"title":"foo","value":"bar","short":true}]}], 165 "extra_responses": [ 166 { 167 "text": "message 2", 168 "attachments":[{"fields":[{"title":"foo 2","value":"bar 2","short":false}]}] 169 } 170 ] 171 }`, 172 &CommandResponse{ 173 Text: "message 1", 174 Attachments: []*SlackAttachment{ 175 { 176 Fields: []*SlackAttachmentField{ 177 { 178 Title: "foo", 179 Value: "bar", 180 Short: true, 181 }, 182 }, 183 }, 184 }, 185 ExtraResponses: []*CommandResponse{ 186 { 187 Text: "message 2", 188 Attachments: []*SlackAttachment{ 189 { 190 Fields: []*SlackAttachmentField{ 191 { 192 Title: "foo 2", 193 Value: "bar 2", 194 Short: false, 195 }, 196 }, 197 }, 198 }, 199 }, 200 }, 201 }, 202 false, 203 }, 204 } 205 206 for _, testCase := range testCases { 207 testCase := testCase 208 t.Run(testCase.Description, func(t *testing.T) { 209 t.Parallel() 210 211 response, err := CommandResponseFromJson(strings.NewReader(testCase.Json)) 212 if testCase.ShouldError { 213 assert.Nil(t, response) 214 } else { 215 assert.NoError(t, err) 216 if assert.NotNil(t, response) { 217 assert.Equal(t, testCase.ExpectedCommandResponse, response) 218 } 219 } 220 }) 221 } 222 }