github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/model/command_response_test.go (about) 1 // Copyright (c) 2016-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 } { 24 response, err := CommandResponseFromHTTPBody(test.ContentType, strings.NewReader(test.Body)) 25 assert.NoError(t, err) 26 assert.Equal(t, test.ExpectedText, response.Text) 27 } 28 } 29 30 func TestCommandResponseFromPlainText(t *testing.T) { 31 response := CommandResponseFromPlainText("foo") 32 assert.Equal(t, "foo", response.Text) 33 } 34 35 func TestCommandResponseFromJson(t *testing.T) { 36 t.Parallel() 37 38 testCases := []struct { 39 Description string 40 Json string 41 ExpectedCommandResponse *CommandResponse 42 ShouldError bool 43 }{ 44 { 45 "empty response", 46 "", 47 nil, 48 true, 49 }, 50 { 51 "malformed response", 52 `{"text": }`, 53 nil, 54 true, 55 }, 56 { 57 "invalid response", 58 `{"text": "test", "response_type": 5}`, 59 nil, 60 true, 61 }, 62 { 63 "ephemeral response", 64 `{ 65 "response_type": "ephemeral", 66 "text": "response text", 67 "username": "response username", 68 "icon_url": "response icon url", 69 "goto_location": "response goto location", 70 "attachments": [{ 71 "text": "attachment 1 text", 72 "pretext": "attachment 1 pretext" 73 },{ 74 "text": "attachment 2 text", 75 "fields": [{ 76 "title": "field 1", 77 "value": "value 1", 78 "short": true 79 },{ 80 "title": "field 2", 81 "value": [], 82 "short": false 83 }] 84 }] 85 }`, 86 &CommandResponse{ 87 ResponseType: "ephemeral", 88 Text: "response text", 89 Username: "response username", 90 IconURL: "response icon url", 91 GotoLocation: "response goto location", 92 Attachments: []*SlackAttachment{ 93 { 94 Text: "attachment 1 text", 95 Pretext: "attachment 1 pretext", 96 }, 97 { 98 Text: "attachment 2 text", 99 Fields: []*SlackAttachmentField{ 100 { 101 Title: "field 1", 102 Value: "value 1", 103 Short: true, 104 }, 105 { 106 Title: "field 2", 107 Value: "[]", 108 Short: false, 109 }, 110 }, 111 }, 112 }, 113 }, 114 false, 115 }, 116 { 117 "null array items", 118 `{"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}, null]}, null]}`, 119 &CommandResponse{ 120 Attachments: []*SlackAttachment{ 121 { 122 Fields: []*SlackAttachmentField{ 123 { 124 Title: "foo", 125 Value: "bar", 126 Short: true, 127 }, 128 }, 129 }, 130 }, 131 }, 132 false, 133 }, 134 } 135 136 for _, testCase := range testCases { 137 testCase := testCase 138 t.Run(testCase.Description, func(t *testing.T) { 139 t.Parallel() 140 141 response, err := CommandResponseFromJson(strings.NewReader(testCase.Json)) 142 if testCase.ShouldError { 143 assert.Nil(t, response) 144 } else { 145 assert.NoError(t, err) 146 if assert.NotNil(t, response) { 147 assert.Equal(t, testCase.ExpectedCommandResponse, response) 148 } 149 } 150 }) 151 } 152 }