Files
mattermost/app/command_test.go
Alejandro García Montoro 2bec92a404 MM-21987 Resolve mentions in slash commands (#13762)
* Create infrastructure to manage mentions

Two new files have been added (along with their tests); namely:

- model/at_mentions.go: utilities to parse and manage mentions; for the moment,
it just contains a regex and a couple of functions to parse possible mentions
and to post-process them, but it can be extended in the future.
- model/mention_map.go: it contains two new types (UserMentionMap and
ChannelMentionMap) that both have FromURLValues and ToURLValues. These types
can be used when adding the mentions to the payload of the plugin slash
commands.

* Extend custom commands payload with mentions

Two couples of new fields are added to the payload; namely:

- user_mentions and user_mentions_ids: two aligned arrays of the same length
containing all the different @-mentions found in the command: the i-th element
of user_mentions_ids is the user identifier of the i-th element of
user_mentions.
- channel_mentions and channel_mentions_ids: two aligned arrays of the same
length containing all the different ~-mentions found in the command: the i-th
element of channel_mentions_ids is the channel identifier of the i-th element
of channel_mentions.

* Fix shadowing of variables and redundant return

* Fix shadowing of variable

* Address review comments (HT @lieut-data)

- Improvements in mentionsToTeamMembers and mentionsToPublicChannels:
	- Scope implementation details inside the functions.
	- Improve goroutines synchronization by using a sync.WaitGroup.
	- Retry lookup of username only if the returned error is http.StatusCode,
	  so we can return early if the error is more severe.
- Invert check in PossibleAtMentions to improve readability.
- Make user and channel mention keys private to the module.
- Allow the specification of an empty map of mentions in
(Channel|User)MentionsFromURLValues when both mentions keys are absent.
- Replace custom functions in tests with require.Equal on maps.

* Test functions to parse mentions from messages

* Extend plugin commands payload with mentions

* Add functions to CommandArgs to add mentions

The functions make sure that the maps are initialized before adding any value.

* Address review comments (HT @lieut-data)

- Adds a mlog.Warn to avoid burying the error when the user is not found.
- Improve readability in loop populating the mention map by moving the
initialization of the map closer to the loop and by iterating over the channel
itself, not over its length.

* File was not gofmt-ed with -s

* Close channel when all goroutines are finished

* Again, all code should be checked with gofmt -s

* Refactor code out of a goroutine

This change helps improve the readability of the code and does not affect its
overall performance. Less complexity is always better.

* Close channel and iterate over its range

Adapt mentionsToPublicChannels to have the same structure in the management
of the mentions channel as in mentionsToTeamMembers.

* Adapt mentionsToTeamMembers to new App

Commit 17523fa changed the App structure, making the *Server field
private, which is now accessed through the Srv() function.

Co-authored-by: mattermod <mattermod@users.noreply.github.com>
2020-03-11 11:50:12 +01:00

558 lines
16 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/services/httpservice"
)
func TestMoveCommand(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
sourceTeam := th.CreateTeam()
targetTeam := th.CreateTeam()
command := &model.Command{}
command.CreatorId = model.NewId()
command.Method = model.COMMAND_METHOD_POST
command.TeamId = sourceTeam.Id
command.URL = "http://nowhere.com/"
command.Trigger = "trigger1"
command, err := th.App.CreateCommand(command)
assert.Nil(t, err)
defer func() {
th.App.PermanentDeleteTeam(sourceTeam)
th.App.PermanentDeleteTeam(targetTeam)
}()
// Move a command and check the team is updated.
assert.Nil(t, th.App.MoveCommand(targetTeam, command))
retrievedCommand, err := th.App.GetCommand(command.Id)
assert.Nil(t, err)
assert.EqualValues(t, targetTeam.Id, retrievedCommand.TeamId)
// Move it to the team it's already in. Nothing should change.
assert.Nil(t, th.App.MoveCommand(targetTeam, command))
retrievedCommand, err = th.App.GetCommand(command.Id)
assert.Nil(t, err)
assert.EqualValues(t, targetTeam.Id, retrievedCommand.TeamId)
}
func TestCreateCommandPost(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
post := &model.Post{
ChannelId: th.BasicChannel.Id,
UserId: th.BasicUser.Id,
Type: model.POST_SYSTEM_GENERIC,
}
resp := &model.CommandResponse{
Text: "some message",
}
skipSlackParsing := false
_, err := th.App.CreateCommandPost(post, th.BasicTeam.Id, resp, skipSlackParsing)
require.NotNil(t, err)
require.Equal(t, err.Id, "api.context.invalid_param.app_error")
}
func TestHandleCommandResponsePost(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
command := &model.Command{}
args := &model.CommandArgs{
ChannelId: th.BasicChannel.Id,
TeamId: th.BasicTeam.Id,
UserId: th.BasicUser.Id,
RootId: "",
ParentId: "",
}
resp := &model.CommandResponse{
Type: model.POST_DEFAULT,
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
Props: model.StringInterface{"some_key": "some value"},
Text: "some message",
}
builtIn := true
post, err := th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Nil(t, err)
assert.Equal(t, args.ChannelId, post.ChannelId)
assert.Equal(t, args.RootId, post.RootId)
assert.Equal(t, args.ParentId, post.ParentId)
assert.Equal(t, args.UserId, post.UserId)
assert.Equal(t, resp.Type, post.Type)
assert.Equal(t, resp.Props, post.Props)
assert.Equal(t, resp.Text, post.Message)
assert.Nil(t, post.Props["override_icon_url"])
assert.Nil(t, post.Props["override_username"])
assert.Nil(t, post.Props["from_webhook"])
// Command is not built in, so it is a bot command.
builtIn = false
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Equal(t, "true", post.Props["from_webhook"])
builtIn = true
// Channel id is specified by response, it should override the command args value.
channel := th.CreateChannel(th.BasicTeam)
resp.ChannelId = channel.Id
th.AddUserToChannel(th.BasicUser, channel)
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Nil(t, err)
assert.Equal(t, resp.ChannelId, post.ChannelId)
assert.NotEqual(t, args.ChannelId, post.ChannelId)
// Override username config is turned off. No override should occur.
*th.App.Config().ServiceSettings.EnablePostUsernameOverride = false
resp.ChannelId = ""
command.Username = "Command username"
resp.Username = "Response username"
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Nil(t, err)
assert.Nil(t, post.Props["override_username"])
*th.App.Config().ServiceSettings.EnablePostUsernameOverride = true
// Override username config is turned on. Override username through command property.
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Nil(t, err)
assert.Equal(t, command.Username, post.Props["override_username"])
assert.Equal(t, "true", post.Props["from_webhook"])
command.Username = ""
// Override username through response property.
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Nil(t, err)
assert.Equal(t, resp.Username, post.Props["override_username"])
assert.Equal(t, "true", post.Props["from_webhook"])
*th.App.Config().ServiceSettings.EnablePostUsernameOverride = false
// Override icon url config is turned off. No override should occur.
*th.App.Config().ServiceSettings.EnablePostIconOverride = false
command.IconURL = "Command icon url"
resp.IconURL = "Response icon url"
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Nil(t, err)
assert.Nil(t, post.Props["override_icon_url"])
*th.App.Config().ServiceSettings.EnablePostIconOverride = true
// Override icon url config is turned on. Override icon url through command property.
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Nil(t, err)
assert.Equal(t, command.IconURL, post.Props["override_icon_url"])
assert.Equal(t, "true", post.Props["from_webhook"])
command.IconURL = ""
// Override icon url through response property.
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Nil(t, err)
assert.Equal(t, resp.IconURL, post.Props["override_icon_url"])
assert.Equal(t, "true", post.Props["from_webhook"])
// Test Slack text conversion.
resp.Text = "<!channel>"
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Nil(t, err)
assert.Equal(t, "@channel", post.Message)
assert.Equal(t, "true", post.Props["from_webhook"])
// Test Slack attachments text conversion.
resp.Attachments = []*model.SlackAttachment{
{
Text: "<!here>",
},
}
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
assert.Nil(t, err)
assert.Equal(t, "@channel", post.Message)
if assert.Len(t, post.Attachments(), 1) {
assert.Equal(t, "@here", post.Attachments()[0].Text)
}
assert.Equal(t, "true", post.Props["from_webhook"])
channel = th.CreatePrivateChannel(th.BasicTeam)
resp.ChannelId = channel.Id
args.UserId = th.BasicUser2.Id
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
require.NotNil(t, err)
require.Equal(t, err.Id, "api.command.command_post.forbidden.app_error")
// Test that /code text is not converted with the Slack text conversion.
command.Trigger = "code"
resp.ChannelId = ""
resp.Text = "<test.com|test website>"
resp.Attachments = []*model.SlackAttachment{
{
Text: "<!here>",
},
}
// set and unset SkipSlackParsing here seems the nicest way as no separate response objects are created for every testcase.
resp.SkipSlackParsing = true
post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn)
resp.SkipSlackParsing = false
assert.Nil(t, err)
assert.Equal(t, resp.Text, post.Message, "/code text should not be converted to Slack links")
assert.Equal(t, "<!here>", resp.Attachments[0].Text)
}
func TestHandleCommandResponse(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
command := &model.Command{}
args := &model.CommandArgs{
Command: "/invite username",
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
}
resp := &model.CommandResponse{
Text: "message 1",
Type: model.POST_SYSTEM_GENERIC,
}
builtIn := true
_, err := th.App.HandleCommandResponse(command, args, resp, builtIn)
require.NotNil(t, err)
require.Equal(t, err.Id, "api.command.execute_command.create_post_failed.app_error")
resp = &model.CommandResponse{
Text: "message 1",
}
_, err = th.App.HandleCommandResponse(command, args, resp, builtIn)
assert.Nil(t, err)
resp = &model.CommandResponse{
Text: "message 1",
ExtraResponses: []*model.CommandResponse{
{
Text: "message 2",
},
{
Type: model.POST_SYSTEM_GENERIC,
Text: "message 3",
},
},
}
_, err = th.App.HandleCommandResponse(command, args, resp, builtIn)
require.NotNil(t, err)
require.Equal(t, err.Id, "api.command.execute_command.create_post_failed.app_error")
resp = &model.CommandResponse{
ExtraResponses: []*model.CommandResponse{
{},
{},
},
}
_, err = th.App.HandleCommandResponse(command, args, resp, builtIn)
assert.Nil(t, err)
}
func TestDoCommandRequest(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.ServiceSettings.AllowedUntrustedInternalConnections = model.NewString("127.0.0.1")
cfg.ServiceSettings.EnableCommands = model.NewBool(true)
})
t.Run("with a valid text response", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(w, strings.NewReader("Hello, World!"))
}))
defer server.Close()
_, resp, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
require.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "Hello, World!", resp.Text)
})
t.Run("with a valid json response", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
io.Copy(w, strings.NewReader(`{"text": "Hello, World!"}`))
}))
defer server.Close()
_, resp, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
require.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "Hello, World!", resp.Text)
})
t.Run("with a large text response", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(w, InfiniteReader{})
}))
defer server.Close()
// Since we limit the length of the response, no error will be returned and resp.Text will be a finite string
_, resp, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
require.Nil(t, err)
require.NotNil(t, resp)
})
t.Run("with a large, valid json response", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
io.Copy(w, io.MultiReader(strings.NewReader(`{"text": "`), InfiniteReader{}, strings.NewReader(`"}`)))
}))
defer server.Close()
_, _, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
require.NotNil(t, err)
require.Equal(t, "api.command.execute_command.failed.app_error", err.Id)
})
t.Run("with a large, invalid json response", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
io.Copy(w, InfiniteReader{})
}))
defer server.Close()
_, _, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
require.NotNil(t, err)
require.Equal(t, "api.command.execute_command.failed.app_error", err.Id)
})
t.Run("with a slow response", func(t *testing.T) {
done := make(chan bool)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-done
io.Copy(w, strings.NewReader(`{"text": "Hello, World!"}`))
}))
defer server.Close()
th.App.HTTPService().(*httpservice.HTTPServiceImpl).RequestTimeout = 100 * time.Millisecond
defer func() {
th.App.HTTPService().(*httpservice.HTTPServiceImpl).RequestTimeout = httpservice.RequestTimeout
}()
_, _, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
require.NotNil(t, err)
require.Equal(t, "api.command.execute_command.failed.app_error", err.Id)
close(done)
})
}
func TestMentionsToTeamMembers(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
otherTeam := th.CreateTeam()
otherUser := th.CreateUser()
th.LinkUserToTeam(otherUser, otherTeam)
fixture := []struct {
message string
inTeam string
expectedMap model.UserMentionMap
}{
{
fmt.Sprintf(""),
th.BasicTeam.Id,
model.UserMentionMap{},
},
{
fmt.Sprintf("/trigger"),
th.BasicTeam.Id,
model.UserMentionMap{},
},
{
fmt.Sprintf("/trigger 0 mentions"),
th.BasicTeam.Id,
model.UserMentionMap{},
},
{
fmt.Sprintf("/trigger 1 valid user @%s", th.BasicUser.Username),
th.BasicTeam.Id,
model.UserMentionMap{th.BasicUser.Username: th.BasicUser.Id},
},
{
fmt.Sprintf("/trigger 2 valid users @%s @%s",
th.BasicUser.Username, th.BasicUser2.Username,
),
th.BasicTeam.Id,
model.UserMentionMap{
th.BasicUser.Username: th.BasicUser.Id,
th.BasicUser2.Username: th.BasicUser2.Id,
},
},
{
fmt.Sprintf("/trigger 1 user from another team @%s", otherUser.Username),
th.BasicTeam.Id,
model.UserMentionMap{},
},
{
fmt.Sprintf("/trigger 2 valid users + 1 from another team @%s @%s @%s",
th.BasicUser.Username, th.BasicUser2.Username, otherUser.Username,
),
th.BasicTeam.Id,
model.UserMentionMap{
th.BasicUser.Username: th.BasicUser.Id,
th.BasicUser2.Username: th.BasicUser2.Id,
},
},
{
fmt.Sprintf("/trigger a valid channel ~%s", th.BasicChannel.Name),
th.BasicTeam.Id,
model.UserMentionMap{},
},
{
fmt.Sprintf("/trigger channel and mentions ~%s @%s",
th.BasicChannel.Name, th.BasicUser.Username),
th.BasicTeam.Id,
model.UserMentionMap{th.BasicUser.Username: th.BasicUser.Id},
},
{
fmt.Sprintf("/trigger repeated users @%s @%s @%s",
th.BasicUser.Username, th.BasicUser2.Username, th.BasicUser.Username),
th.BasicTeam.Id,
model.UserMentionMap{
th.BasicUser.Username: th.BasicUser.Id,
th.BasicUser2.Username: th.BasicUser2.Id,
},
},
}
for _, data := range fixture {
actualMap := th.App.mentionsToTeamMembers(data.message, data.inTeam)
require.Equal(t, actualMap, data.expectedMap)
}
}
func TestMentionsToPublicChannels(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
otherPublicChannel := th.CreateChannel(th.BasicTeam)
privateChannel := th.CreatePrivateChannel(th.BasicTeam)
fixture := []struct {
message string
inTeam string
expectedMap model.ChannelMentionMap
}{
{
fmt.Sprintf(""),
th.BasicTeam.Id,
model.ChannelMentionMap{},
},
{
fmt.Sprintf("/trigger"),
th.BasicTeam.Id,
model.ChannelMentionMap{},
},
{
fmt.Sprintf("/trigger 0 mentions"),
th.BasicTeam.Id,
model.ChannelMentionMap{},
},
{
fmt.Sprintf("/trigger 1 public channel ~%s", th.BasicChannel.Name),
th.BasicTeam.Id,
model.ChannelMentionMap{th.BasicChannel.Name: th.BasicChannel.Id},
},
{
fmt.Sprintf("/trigger 2 public channels ~%s ~%s",
th.BasicChannel.Name, otherPublicChannel.Name,
),
th.BasicTeam.Id,
model.ChannelMentionMap{
th.BasicChannel.Name: th.BasicChannel.Id,
otherPublicChannel.Name: otherPublicChannel.Id,
},
},
{
fmt.Sprintf("/trigger 1 private channel ~%s", privateChannel.Name),
th.BasicTeam.Id,
model.ChannelMentionMap{},
},
{
fmt.Sprintf("/trigger 2 public channel + 1 private ~%s ~%s ~%s",
th.BasicChannel.Name, otherPublicChannel.Name, privateChannel.Name,
),
th.BasicTeam.Id,
model.ChannelMentionMap{
th.BasicChannel.Name: th.BasicChannel.Id,
otherPublicChannel.Name: otherPublicChannel.Id,
},
},
{
fmt.Sprintf("/trigger a valid user @%s", th.BasicUser.Username),
th.BasicTeam.Id,
model.ChannelMentionMap{},
},
{
fmt.Sprintf("/trigger channel and mentions ~%s @%s",
th.BasicChannel.Name, th.BasicUser.Username),
th.BasicTeam.Id,
model.ChannelMentionMap{th.BasicChannel.Name: th.BasicChannel.Id},
},
{
fmt.Sprintf("/trigger repeated channels ~%s ~%s ~%s",
th.BasicChannel.Name, otherPublicChannel.Name, th.BasicChannel.Name),
th.BasicTeam.Id,
model.ChannelMentionMap{
th.BasicChannel.Name: th.BasicChannel.Id,
otherPublicChannel.Name: otherPublicChannel.Id,
},
},
}
for _, data := range fixture {
actualMap := th.App.mentionsToPublicChannels(data.message, data.inTeam)
require.Equal(t, actualMap, data.expectedMap)
}
}