Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
279 lines
6.0 KiB
Go
279 lines
6.0 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package model
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCommandResponseFromHTTPBody(t *testing.T) {
|
|
for _, test := range []struct {
|
|
ContentType string
|
|
Body string
|
|
ExpectedText string
|
|
}{
|
|
{"", "foo", "foo"},
|
|
{"text/plain", "foo", "foo"},
|
|
{"application/json", `{"text": "foo"}`, "foo"},
|
|
{"application/json; charset=utf-8", `{"text": "foo"}`, "foo"},
|
|
{"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}`,
|
|
"```haskell\nlet\n\nf1 = [ 3 | a <- [1]]\nf2 = [ 4 | b <- [2]]\nf3 = \\p -> 5\n\nin 1\n```",
|
|
},
|
|
} {
|
|
response, err := CommandResponseFromHTTPBody(test.ContentType, strings.NewReader(test.Body))
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, test.ExpectedText, response.Text)
|
|
}
|
|
}
|
|
|
|
func TestCommandResponseFromPlainText(t *testing.T) {
|
|
response := CommandResponseFromPlainText("foo")
|
|
assert.Equal(t, "foo", response.Text)
|
|
}
|
|
|
|
func TestCommandResponseFromJSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
Description string
|
|
Json string
|
|
ExpectedCommandResponse *CommandResponse
|
|
ShouldError bool
|
|
ExpectInvalid bool
|
|
}{
|
|
{
|
|
"empty response",
|
|
"",
|
|
nil,
|
|
true,
|
|
false,
|
|
},
|
|
{
|
|
"malformed response",
|
|
`{"text": }`,
|
|
nil,
|
|
true,
|
|
false,
|
|
},
|
|
{
|
|
"invalid response",
|
|
`{"text": "test", "response_type": 5}`,
|
|
nil,
|
|
true,
|
|
false,
|
|
},
|
|
{
|
|
"ephemeral response",
|
|
`{
|
|
"response_type": "ephemeral",
|
|
"text": "response text",
|
|
"username": "response username",
|
|
"channel_id": "response channel id",
|
|
"icon_url": "http://example.com/icon.png",
|
|
"goto_location": "http://example.com/icon.png",
|
|
"attachments": [{
|
|
"text": "attachment 1 text",
|
|
"pretext": "attachment 1 pretext"
|
|
},{
|
|
"text": "attachment 2 text",
|
|
"fields": [{
|
|
"title": "field 1",
|
|
"value": "value 1",
|
|
"short": true
|
|
},{
|
|
"title": "field 2",
|
|
"value": [],
|
|
"short": false
|
|
}]
|
|
}]
|
|
}`,
|
|
&CommandResponse{
|
|
ResponseType: "ephemeral",
|
|
Text: "response text",
|
|
Username: "response username",
|
|
ChannelId: "response channel id",
|
|
IconURL: "http://example.com/icon.png",
|
|
GotoLocation: "http://example.com/icon.png",
|
|
Attachments: []*MessageAttachment{
|
|
{
|
|
Text: "attachment 1 text",
|
|
Pretext: "attachment 1 pretext",
|
|
},
|
|
{
|
|
Text: "attachment 2 text",
|
|
Fields: []*MessageAttachmentField{
|
|
{
|
|
Title: "field 1",
|
|
Value: "value 1",
|
|
Short: true,
|
|
},
|
|
{
|
|
Title: "field 2",
|
|
Value: "[]",
|
|
Short: false,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
false,
|
|
false,
|
|
},
|
|
{
|
|
"null array items",
|
|
`{"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}, null]}, null]}`,
|
|
&CommandResponse{
|
|
Attachments: []*MessageAttachment{
|
|
{
|
|
Fields: []*MessageAttachmentField{
|
|
{
|
|
Title: "foo",
|
|
Value: "bar",
|
|
Short: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
false,
|
|
false,
|
|
},
|
|
{
|
|
"multiple responses returned",
|
|
`
|
|
{
|
|
"text": "message 1",
|
|
"extra_responses": [
|
|
{"text": "message 2"}
|
|
]
|
|
}
|
|
`,
|
|
&CommandResponse{
|
|
Text: "message 1",
|
|
ExtraResponses: []*CommandResponse{
|
|
{
|
|
Text: "message 2",
|
|
},
|
|
},
|
|
},
|
|
false,
|
|
false,
|
|
},
|
|
{
|
|
"invalid response type should fail validation",
|
|
`{"text": "hello","response_type": "shliapa_type"}`,
|
|
&CommandResponse{
|
|
ResponseType: "shliapa_type",
|
|
Text: "hello",
|
|
},
|
|
false,
|
|
true,
|
|
},
|
|
{
|
|
"invalid response type inside extra_responses should fail validation",
|
|
`{
|
|
"text": "main ok",
|
|
"response_type": "in_channel",
|
|
"extra_responses": [
|
|
{"text": "nested bad", "response_type": "invalid_nested_type"}
|
|
]
|
|
}`,
|
|
&CommandResponse{
|
|
ResponseType: "in_channel",
|
|
Text: "main ok",
|
|
ExtraResponses: []*CommandResponse{
|
|
{
|
|
ResponseType: "invalid_nested_type",
|
|
Text: "nested bad",
|
|
},
|
|
},
|
|
},
|
|
false,
|
|
true,
|
|
},
|
|
{
|
|
"valid https goto_location should pass validation",
|
|
`{"text": "hello", "goto_location": "https://example.com"}`,
|
|
&CommandResponse{
|
|
Text: "hello",
|
|
GotoLocation: "https://example.com",
|
|
},
|
|
false,
|
|
false,
|
|
},
|
|
{
|
|
"multiple responses returned, with attachments",
|
|
`
|
|
{
|
|
"text": "message 1",
|
|
"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}]}],
|
|
"extra_responses": [
|
|
{
|
|
"text": "message 2",
|
|
"attachments":[{"fields":[{"title":"foo 2","value":"bar 2","short":false}]}]
|
|
}
|
|
]
|
|
}`,
|
|
&CommandResponse{
|
|
Text: "message 1",
|
|
Attachments: []*MessageAttachment{
|
|
{
|
|
Fields: []*MessageAttachmentField{
|
|
{
|
|
Title: "foo",
|
|
Value: "bar",
|
|
Short: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
ExtraResponses: []*CommandResponse{
|
|
{
|
|
Text: "message 2",
|
|
Attachments: []*MessageAttachment{
|
|
{
|
|
Fields: []*MessageAttachmentField{
|
|
{
|
|
Title: "foo 2",
|
|
Value: "bar 2",
|
|
Short: false,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
false,
|
|
false,
|
|
},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.Description, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
response, err := CommandResponseFromJSON(strings.NewReader(testCase.Json))
|
|
if testCase.ShouldError {
|
|
assert.Error(t, err)
|
|
assert.Nil(t, response)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
if assert.NotNil(t, response) {
|
|
assert.Equal(t, testCase.ExpectedCommandResponse, response)
|
|
|
|
if testCase.ExpectInvalid {
|
|
assert.NotNil(t, response.IsValid())
|
|
} else {
|
|
assert.Nil(t, response.IsValid())
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|