MM-20755: fix post actions in DMs/GMs (#13248)

* MM-20755: fix post actions in DMs/GMs

The fix for https://github.com/mattermost/mattermost-server/issues/12377 assumed that all channels have teams, but this is false for DMs and GMs.  Test for this and avoid failing on a missing team as such.

Fixes: https://mattermost.atlassian.net/browse/MM-20755

* tweak code for clarity
Этот коммит содержится в:
Jesse Hallam
2019-12-10 12:57:54 -04:00
коммит произвёл GitHub
родитель 354f20e7f0
Коммит bb1facb1f5
2 изменённых файлов: 244 добавлений и 201 удалений

Просмотреть файл

@@ -21,13 +21,14 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gorilla/mux"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
"strings" "strings"
"github.com/gorilla/mux"
"github.com/mattermost/mattermost-server/v5/model" "github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store" "github.com/mattermost/mattermost-server/v5/store"
"github.com/mattermost/mattermost-server/v5/utils" "github.com/mattermost/mattermost-server/v5/utils"
@@ -158,10 +159,17 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st
} }
teamChan := make(chan store.StoreResult, 1) teamChan := make(chan store.StoreResult, 1)
go func() { go func() {
defer close(teamChan)
// Direct and group channels won't have teams.
if upstreamRequest.TeamId == "" {
return
}
team, err := a.Srv.Store.Team().Get(upstreamRequest.TeamId) team, err := a.Srv.Store.Team().Get(upstreamRequest.TeamId)
teamChan <- store.StoreResult{Data: team, Err: err} teamChan <- store.StoreResult{Data: team, Err: err}
close(teamChan)
}() }()
ur := <-userChan ur := <-userChan
@@ -171,12 +179,15 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st
user := ur.Data.(*model.User) user := ur.Data.(*model.User)
upstreamRequest.UserName = user.Username upstreamRequest.UserName = user.Username
tr := <-teamChan tr, ok := <-teamChan
if ok {
if tr.Err != nil { if tr.Err != nil {
return "", tr.Err return "", tr.Err
} }
team := tr.Data.(*model.Team) team := tr.Data.(*model.Team)
upstreamRequest.TeamName = team.Name upstreamRequest.TeamName = team.Name
}
if upstreamRequest.Type == model.POST_ACTION_TYPE_SELECT { if upstreamRequest.Type == model.POST_ACTION_TYPE_SELECT {
if selectedOption != "" { if selectedOption != "" {

Просмотреть файл

@@ -68,9 +68,33 @@ func TestPostActionInvalidURL(t *testing.T) {
} }
func TestPostAction(t *testing.T) { func TestPostAction(t *testing.T) {
testCases := []struct {
Description string
Channel func(th *TestHelper) *model.Channel
}{
{"public channel", func(th *TestHelper) *model.Channel {
return th.BasicChannel
}},
{"direct channel", func(th *TestHelper) *model.Channel {
user1 := th.CreateUser()
return th.CreateDmChannel(user1)
}},
{"group channel", func(th *TestHelper) *model.Channel {
user1 := th.CreateUser()
user2 := th.CreateUser()
return th.CreateGroupChannel(user1, user2)
}},
}
for _, testCase := range testCases {
t.Run(testCase.Description, func(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() defer th.TearDown()
channel := testCase.Channel(th)
th.App.UpdateConfig(func(cfg *model.Config) { th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost,127.0.0.1" *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost,127.0.0.1"
}) })
@@ -81,10 +105,15 @@ func TestPostAction(t *testing.T) {
assert.Equal(t, request.UserId, th.BasicUser.Id) assert.Equal(t, request.UserId, th.BasicUser.Id)
assert.Equal(t, request.UserName, th.BasicUser.Username) assert.Equal(t, request.UserName, th.BasicUser.Username)
assert.Equal(t, request.ChannelId, th.BasicChannel.Id) assert.Equal(t, request.ChannelId, channel.Id)
assert.Equal(t, request.ChannelName, th.BasicChannel.Name) assert.Equal(t, request.ChannelName, channel.Name)
if channel.Type == model.CHANNEL_DIRECT || channel.Type == model.CHANNEL_GROUP {
assert.Empty(t, request.TeamId)
assert.Empty(t, request.TeamName)
} else {
assert.Equal(t, request.TeamId, th.BasicTeam.Id) assert.Equal(t, request.TeamId, th.BasicTeam.Id)
assert.Equal(t, request.TeamName, th.BasicTeam.Name) assert.Equal(t, request.TeamName, th.BasicTeam.Name)
}
assert.True(t, len(request.TriggerId) > 0) assert.True(t, len(request.TriggerId) > 0)
if request.Type == model.POST_ACTION_TYPE_SELECT { if request.Type == model.POST_ACTION_TYPE_SELECT {
assert.Equal(t, request.DataSource, "some_source") assert.Equal(t, request.DataSource, "some_source")
@@ -100,7 +129,7 @@ func TestPostAction(t *testing.T) {
interactivePost := model.Post{ interactivePost := model.Post{
Message: "Interactive post", Message: "Interactive post",
ChannelId: th.BasicChannel.Id, ChannelId: channel.Id,
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()), PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
UserId: th.BasicUser.Id, UserId: th.BasicUser.Id,
Props: model.StringInterface{ Props: model.StringInterface{
@@ -137,7 +166,7 @@ func TestPostAction(t *testing.T) {
menuPost := model.Post{ menuPost := model.Post{
Message: "Interactive post", Message: "Interactive post",
ChannelId: th.BasicChannel.Id, ChannelId: channel.Id,
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()), PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
UserId: th.BasicUser.Id, UserId: th.BasicUser.Id,
Props: model.StringInterface{ Props: model.StringInterface{
@@ -195,7 +224,7 @@ func TestPostAction(t *testing.T) {
interactivePostPlugin := model.Post{ interactivePostPlugin := model.Post{
Message: "Interactive post", Message: "Interactive post",
ChannelId: th.BasicChannel.Id, ChannelId: channel.Id,
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()), PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
UserId: th.BasicUser.Id, UserId: th.BasicUser.Id,
Props: model.StringInterface{ Props: model.StringInterface{
@@ -236,7 +265,7 @@ func TestPostAction(t *testing.T) {
interactivePostSiteURL := model.Post{ interactivePostSiteURL := model.Post{
Message: "Interactive post", Message: "Interactive post",
ChannelId: th.BasicChannel.Id, ChannelId: channel.Id,
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()), PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
UserId: th.BasicUser.Id, UserId: th.BasicUser.Id,
Props: model.StringInterface{ Props: model.StringInterface{
@@ -278,7 +307,7 @@ func TestPostAction(t *testing.T) {
interactivePostSubpath := model.Post{ interactivePostSubpath := model.Post{
Message: "Interactive post", Message: "Interactive post",
ChannelId: th.BasicChannel.Id, ChannelId: channel.Id,
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()), PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
UserId: th.BasicUser.Id, UserId: th.BasicUser.Id,
Props: model.StringInterface{ Props: model.StringInterface{
@@ -312,6 +341,9 @@ func TestPostAction(t *testing.T) {
_, err = th.App.DoPostAction(postSubpath.Id, attachmentsSubpath[0].Actions[0].Id, th.BasicUser.Id, "") _, err = th.App.DoPostAction(postSubpath.Id, attachmentsSubpath[0].Actions[0].Id, th.BasicUser.Id, "")
require.Nil(t, err) require.Nil(t, err)
})
}
} }
func TestPostActionProps(t *testing.T) { func TestPostActionProps(t *testing.T) {