summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSudheer <sudheer.105@gmail.com>2018-06-27 18:38:58 +0530
committerJesús Espino <jespinog@gmail.com>2018-06-27 15:08:58 +0200
commit9abd74c5e31625c1c11fcedafc44f8aadc4ecdb0 (patch)
tree4228353fab52052a2df7657fc56fc0519a6e6996
parent1e6994a2abf037c1fa8872ab34de1b0627cb1ab0 (diff)
downloadchat-9abd74c5e31625c1c11fcedafc44f8aadc4ecdb0.tar.gz
chat-9abd74c5e31625c1c11fcedafc44f8aadc4ecdb0.tar.bz2
chat-9abd74c5e31625c1c11fcedafc44f8aadc4ecdb0.zip
MM-10910 fix join and open channel commands to work with tilde (#8984)
* MM-10910 fix join and open channel commands to work with tilde * Fix lint * Add basic tests for command_join
-rw-r--r--app/command_join.go11
-rw-r--r--app/command_join_test.go88
2 files changed, 97 insertions, 2 deletions
diff --git a/app/command_join.go b/app/command_join.go
index 92084448e..61ed65ba6 100644
--- a/app/command_join.go
+++ b/app/command_join.go
@@ -6,6 +6,7 @@ package app
import (
"github.com/mattermost/mattermost-server/model"
goi18n "github.com/nicksnyder/go-i18n/i18n"
+ "strings"
)
type JoinProvider struct {
@@ -34,12 +35,18 @@ func (me *JoinProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Comman
}
func (me *JoinProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
- if result := <-a.Srv.Store.Channel().GetByName(args.TeamId, message, true); result.Err != nil {
+ channelName := message
+
+ if strings.HasPrefix(message, "~") {
+ channelName = message[1:]
+ }
+
+ if result := <-a.Srv.Store.Channel().GetByName(args.TeamId, channelName, true); result.Err != nil {
return &model.CommandResponse{Text: args.T("api.command_join.list.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
} else {
channel := result.Data.(*model.Channel)
- if channel.Name == message {
+ if channel.Name == channelName {
allowed := false
if (channel.Type == model.CHANNEL_PRIVATE && a.SessionHasPermissionToChannel(args.Session, channel.Id, model.PERMISSION_READ_CHANNEL)) || channel.Type == model.CHANNEL_OPEN {
allowed = true
diff --git a/app/command_join_test.go b/app/command_join_test.go
new file mode 100644
index 000000000..77574217b
--- /dev/null
+++ b/app/command_join_test.go
@@ -0,0 +1,88 @@
+// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package app
+
+import (
+ "testing"
+ "github.com/mattermost/mattermost-server/model"
+ "github.com/nicksnyder/go-i18n/i18n"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestJoinCommandNoChannel(t *testing.T) {
+ th := Setup().InitBasic()
+ defer th.TearDown()
+
+ if testing.Short() {
+ t.SkipNow()
+ }
+
+ cmd := &JoinProvider{}
+ resp := cmd.DoCommand(th.App, &model.CommandArgs{
+ T: i18n.IdentityTfunc(),
+ UserId: th.BasicUser2.Id,
+ SiteURL: "http://test.url",
+ TeamId: th.BasicTeam.Id,
+ }, "asdsad")
+
+ assert.Equal(t, "api.command_join.list.app_error", resp.Text)
+}
+
+func TestJoinCommandForExistingChannel(t *testing.T) {
+ th := Setup().InitBasic()
+ defer th.TearDown()
+
+ if testing.Short() {
+ t.SkipNow()
+ }
+
+ channel2, _ := th.App.CreateChannel(&model.Channel{
+ DisplayName: "AA",
+ Name: "aa" + model.NewId() + "a",
+ Type: model.CHANNEL_OPEN,
+ TeamId: th.BasicTeam.Id,
+ CreatorId: th.BasicUser.Id,
+ }, false)
+
+
+ cmd := &JoinProvider{}
+ resp := cmd.DoCommand(th.App, &model.CommandArgs{
+ T: i18n.IdentityTfunc(),
+ UserId: th.BasicUser2.Id,
+ SiteURL: "http://test.url",
+ TeamId: th.BasicTeam.Id,
+ }, channel2.Name)
+
+ assert.Equal(t, "", resp.Text)
+ assert.Equal(t, "http://test.url/"+th.BasicTeam.Name+"/channels/"+channel2.Name, resp.GotoLocation)
+}
+
+func TestJoinCommandWithTilde(t *testing.T) {
+ th := Setup().InitBasic()
+ defer th.TearDown()
+
+ if testing.Short() {
+ t.SkipNow()
+ }
+
+ channel2, _ := th.App.CreateChannel(&model.Channel{
+ DisplayName: "AA",
+ Name: "aa" + model.NewId() + "a",
+ Type: model.CHANNEL_OPEN,
+ TeamId: th.BasicTeam.Id,
+ CreatorId: th.BasicUser.Id,
+ }, false)
+
+
+ cmd := &JoinProvider{}
+ resp := cmd.DoCommand(th.App, &model.CommandArgs{
+ T: i18n.IdentityTfunc(),
+ UserId: th.BasicUser2.Id,
+ SiteURL: "http://test.url",
+ TeamId: th.BasicTeam.Id,
+ }, "~"+channel2.Name)
+
+ assert.Equal(t, "", resp.Text)
+ assert.Equal(t, "http://test.url/"+th.BasicTeam.Name+"/channels/"+channel2.Name, resp.GotoLocation)
+}