github.com/mattermost/mattermost-server/v5@v5.39.3/api4/commands_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	_ "github.com/mattermost/mattermost-server/v5/app/slashcommands"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/mattermost/mattermost-server/v5/model"
    15  )
    16  
    17  func TestEchoCommand(t *testing.T) {
    18  	th := Setup(t).InitBasic()
    19  	defer th.TearDown()
    20  
    21  	Client := th.Client
    22  	channel1 := th.BasicChannel
    23  
    24  	echoTestString := "/echo test"
    25  
    26  	r1 := Client.Must(Client.ExecuteCommand(channel1.Id, echoTestString)).(*model.CommandResponse)
    27  	require.NotNil(t, r1, "Echo command failed to execute")
    28  
    29  	r1 = Client.Must(Client.ExecuteCommand(channel1.Id, "/echo ")).(*model.CommandResponse)
    30  	require.NotNil(t, r1, "Echo command failed to execute")
    31  
    32  	time.Sleep(100 * time.Millisecond)
    33  
    34  	p1 := Client.Must(Client.GetPostsForChannel(channel1.Id, 0, 2, "", false)).(*model.PostList)
    35  	require.Len(t, p1.Order, 2, "Echo command failed to send")
    36  }
    37  
    38  func TestGroupmsgCommands(t *testing.T) {
    39  	th := Setup(t).InitBasic()
    40  	defer th.TearDown()
    41  
    42  	Client := th.Client
    43  	team := th.BasicTeam
    44  	user1 := th.BasicUser
    45  	user2 := th.BasicUser2
    46  	user3 := th.CreateUser()
    47  	user4 := th.CreateUser()
    48  	user5 := th.CreateUser()
    49  	user6 := th.CreateUser()
    50  	user7 := th.CreateUser()
    51  	user8 := th.CreateUser()
    52  	user9 := th.CreateUser()
    53  	th.LinkUserToTeam(user3, team)
    54  	th.LinkUserToTeam(user4, team)
    55  
    56  	rs1 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+","+user3.Username)).(*model.CommandResponse)
    57  
    58  	group1 := model.GetGroupNameFromUserIds([]string{user1.Id, user2.Id, user3.Id})
    59  	require.True(t, strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+group1), "failed to create group channel")
    60  
    61  	rs2 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user3.Username+","+user4.Username+" foobar")).(*model.CommandResponse)
    62  	group2 := model.GetGroupNameFromUserIds([]string{user1.Id, user3.Id, user4.Id})
    63  
    64  	require.True(t, strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+group2), "failed to create second direct channel")
    65  
    66  	result := Client.Must(Client.SearchPosts(team.Id, "foobar", false)).(*model.PostList)
    67  	require.NotEqual(t, 0, len(result.Order), "post did not get sent to direct message")
    68  
    69  	rs3 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+","+user3.Username)).(*model.CommandResponse)
    70  	require.True(t, strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+group1), "failed to go back to existing group channel")
    71  
    72  	Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+" foobar"))
    73  	Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+","+user3.Username+","+user4.Username+","+user5.Username+","+user6.Username+","+user7.Username+","+user8.Username+","+user9.Username+" foobar"))
    74  	Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg junk foobar"))
    75  	Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg junk,junk2 foobar"))
    76  }
    77  
    78  func TestInvitePeopleCommand(t *testing.T) {
    79  	th := Setup(t).InitBasic()
    80  	defer th.TearDown()
    81  
    82  	Client := th.Client
    83  	channel := th.BasicChannel
    84  
    85  	r1 := Client.Must(Client.ExecuteCommand(channel.Id, "/invite_people test@example.com")).(*model.CommandResponse)
    86  	require.NotNil(t, r1, "Command failed to execute")
    87  
    88  	r2 := Client.Must(Client.ExecuteCommand(channel.Id, "/invite_people test1@example.com test2@example.com")).(*model.CommandResponse)
    89  	require.NotNil(t, r2, "Command failed to execute")
    90  
    91  	r3 := Client.Must(Client.ExecuteCommand(channel.Id, "/invite_people")).(*model.CommandResponse)
    92  	require.NotNil(t, r3, "Command failed to execute")
    93  }
    94  
    95  // also used to test /open (see command_open_test.go)
    96  func testJoinCommands(t *testing.T, alias string) {
    97  	th := Setup(t).InitBasic()
    98  	defer th.TearDown()
    99  
   100  	Client := th.Client
   101  	team := th.BasicTeam
   102  	user2 := th.BasicUser2
   103  
   104  	channel0 := &model.Channel{DisplayName: "00", Name: "00" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
   105  	channel0 = Client.Must(Client.CreateChannel(channel0)).(*model.Channel)
   106  
   107  	channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
   108  	channel1 = Client.Must(Client.CreateChannel(channel1)).(*model.Channel)
   109  	Client.Must(Client.RemoveUserFromChannel(channel1.Id, th.BasicUser.Id))
   110  
   111  	channel2 := &model.Channel{DisplayName: "BB", Name: "bb" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
   112  	channel2 = Client.Must(Client.CreateChannel(channel2)).(*model.Channel)
   113  	Client.Must(Client.RemoveUserFromChannel(channel2.Id, th.BasicUser.Id))
   114  
   115  	channel3 := Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user2.Id)).(*model.Channel)
   116  
   117  	rs5 := Client.Must(Client.ExecuteCommand(channel0.Id, "/"+alias+" "+channel2.Name)).(*model.CommandResponse)
   118  	require.True(t, strings.HasSuffix(rs5.GotoLocation, "/"+team.Name+"/channels/"+channel2.Name), "failed to join channel")
   119  
   120  	rs6 := Client.Must(Client.ExecuteCommand(channel0.Id, "/"+alias+" "+channel3.Name)).(*model.CommandResponse)
   121  	require.False(t, strings.HasSuffix(rs6.GotoLocation, "/"+team.Name+"/channels/"+channel3.Name), "should not have joined direct message channel")
   122  
   123  	c1 := Client.Must(Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, false, "")).([]*model.Channel)
   124  
   125  	found := false
   126  	for _, c := range c1 {
   127  		if c.Id == channel2.Id {
   128  			found = true
   129  		}
   130  	}
   131  	require.True(t, found, "did not join channel")
   132  
   133  	// test case insensitively
   134  	channel4 := &model.Channel{DisplayName: "BB", Name: "bb" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
   135  	channel4 = Client.Must(Client.CreateChannel(channel4)).(*model.Channel)
   136  	Client.Must(Client.RemoveUserFromChannel(channel4.Id, th.BasicUser.Id))
   137  	rs7 := Client.Must(Client.ExecuteCommand(channel0.Id, "/"+alias+" "+strings.ToUpper(channel4.Name))).(*model.CommandResponse)
   138  	require.True(t, strings.HasSuffix(rs7.GotoLocation, "/"+team.Name+"/channels/"+channel4.Name), "failed to join channel")
   139  }
   140  
   141  func TestJoinCommands(t *testing.T) {
   142  	testJoinCommands(t, "join")
   143  }
   144  
   145  func TestLoadTestHelpCommands(t *testing.T) {
   146  	th := Setup(t).InitBasic()
   147  	defer th.TearDown()
   148  
   149  	Client := th.Client
   150  	channel := th.BasicChannel
   151  
   152  	enableTesting := *th.App.Config().ServiceSettings.EnableTesting
   153  	defer func() {
   154  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = enableTesting })
   155  	}()
   156  
   157  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true })
   158  
   159  	rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test help")).(*model.CommandResponse)
   160  	require.True(t, strings.Contains(rs.Text, "Mattermost testing commands to help"), rs.Text)
   161  
   162  	time.Sleep(2 * time.Second)
   163  }
   164  
   165  func TestLoadTestSetupCommands(t *testing.T) {
   166  	th := Setup(t).InitBasic()
   167  	defer th.TearDown()
   168  
   169  	Client := th.Client
   170  	channel := th.BasicChannel
   171  
   172  	enableTesting := *th.App.Config().ServiceSettings.EnableTesting
   173  	defer func() {
   174  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = enableTesting })
   175  	}()
   176  
   177  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true })
   178  
   179  	rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test setup fuzz 1 1 1")).(*model.CommandResponse)
   180  	require.Equal(t, "Created environment", rs.Text, rs.Text)
   181  
   182  	time.Sleep(2 * time.Second)
   183  }
   184  
   185  func TestLoadTestUsersCommands(t *testing.T) {
   186  	th := Setup(t).InitBasic()
   187  	defer th.TearDown()
   188  
   189  	Client := th.Client
   190  	channel := th.BasicChannel
   191  
   192  	enableTesting := *th.App.Config().ServiceSettings.EnableTesting
   193  	defer func() {
   194  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = enableTesting })
   195  	}()
   196  
   197  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true })
   198  
   199  	rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test users fuzz 1 2")).(*model.CommandResponse)
   200  	require.Equal(t, "Added users", rs.Text, rs.Text)
   201  
   202  	time.Sleep(2 * time.Second)
   203  }
   204  
   205  func TestLoadTestChannelsCommands(t *testing.T) {
   206  	th := Setup(t).InitBasic()
   207  	defer th.TearDown()
   208  
   209  	Client := th.Client
   210  	channel := th.BasicChannel
   211  
   212  	enableTesting := *th.App.Config().ServiceSettings.EnableTesting
   213  	defer func() {
   214  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = enableTesting })
   215  	}()
   216  
   217  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true })
   218  
   219  	rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test channels fuzz 1 2")).(*model.CommandResponse)
   220  	require.Equal(t, "Added channels", rs.Text, rs.Text)
   221  
   222  	time.Sleep(2 * time.Second)
   223  }
   224  
   225  func TestLoadTestPostsCommands(t *testing.T) {
   226  	th := Setup(t).InitBasic()
   227  	defer th.TearDown()
   228  
   229  	Client := th.Client
   230  	channel := th.BasicChannel
   231  
   232  	enableTesting := *th.App.Config().ServiceSettings.EnableTesting
   233  	defer func() {
   234  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = enableTesting })
   235  	}()
   236  
   237  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true })
   238  
   239  	rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test posts fuzz 2 3 2")).(*model.CommandResponse)
   240  	require.Equal(t, "Added posts", rs.Text, rs.Text)
   241  
   242  	time.Sleep(2 * time.Second)
   243  }
   244  
   245  func TestLeaveCommands(t *testing.T) {
   246  	th := Setup(t).InitBasic()
   247  	defer th.TearDown()
   248  
   249  	Client := th.Client
   250  	team := th.BasicTeam
   251  	user2 := th.BasicUser2
   252  
   253  	channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
   254  	channel1 = Client.Must(Client.CreateChannel(channel1)).(*model.Channel)
   255  	Client.Must(Client.AddChannelMember(channel1.Id, th.BasicUser.Id))
   256  
   257  	channel2 := &model.Channel{DisplayName: "BB", Name: "bb" + model.NewId() + "a", Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
   258  	channel2 = Client.Must(Client.CreateChannel(channel2)).(*model.Channel)
   259  	Client.Must(Client.AddChannelMember(channel2.Id, th.BasicUser.Id))
   260  	Client.Must(Client.AddChannelMember(channel2.Id, user2.Id))
   261  
   262  	channel3 := Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user2.Id)).(*model.Channel)
   263  
   264  	rs1 := Client.Must(Client.ExecuteCommand(channel1.Id, "/leave")).(*model.CommandResponse)
   265  	require.True(t, strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL), "failed to leave open channel 1")
   266  
   267  	rs2 := Client.Must(Client.ExecuteCommand(channel2.Id, "/leave")).(*model.CommandResponse)
   268  	require.True(t, strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL), "failed to leave private channel 1")
   269  
   270  	_, err := Client.ExecuteCommand(channel3.Id, "/leave")
   271  	require.NotNil(t, err, "should fail leaving direct channel")
   272  
   273  	cdata := Client.Must(Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, false, "")).([]*model.Channel)
   274  
   275  	found := false
   276  	for _, c := range cdata {
   277  		if c.Id == channel1.Id || c.Id == channel2.Id {
   278  			found = true
   279  		}
   280  	}
   281  	require.False(t, found, "did not leave right channels")
   282  
   283  	for _, c := range cdata {
   284  		if c.Name == model.DEFAULT_CHANNEL {
   285  			_, err := Client.RemoveUserFromChannel(c.Id, th.BasicUser.Id)
   286  			require.NotNil(t, err, "should have errored on leaving default channel")
   287  			break
   288  		}
   289  	}
   290  }
   291  
   292  func TestLogoutTestCommand(t *testing.T) {
   293  	th := Setup(t).InitBasic()
   294  	defer th.TearDown()
   295  
   296  	th.Client.Must(th.Client.ExecuteCommand(th.BasicChannel.Id, "/logout"))
   297  }
   298  
   299  func TestMeCommand(t *testing.T) {
   300  	th := Setup(t).InitBasic()
   301  	defer th.TearDown()
   302  
   303  	Client := th.Client
   304  	channel := th.BasicChannel
   305  
   306  	testString := "/me hello"
   307  
   308  	r1 := Client.Must(Client.ExecuteCommand(channel.Id, testString)).(*model.CommandResponse)
   309  	require.NotNil(t, r1, "Command failed to execute")
   310  
   311  	time.Sleep(100 * time.Millisecond)
   312  
   313  	p1 := Client.Must(Client.GetPostsForChannel(channel.Id, 0, 2, "", false)).(*model.PostList)
   314  	require.Len(t, p1.Order, 2, "Command failed to send")
   315  
   316  	pt := p1.Posts[p1.Order[0]].Type
   317  	require.Equal(t, model.POST_ME, pt, "invalid post type")
   318  
   319  	msg := p1.Posts[p1.Order[0]].Message
   320  	want := "*hello*"
   321  	require.Equal(t, want, msg, "invalid me response")
   322  }
   323  
   324  func TestMsgCommands(t *testing.T) {
   325  	th := Setup(t).InitBasic()
   326  	defer th.TearDown()
   327  
   328  	Client := th.Client
   329  	team := th.BasicTeam
   330  	user1 := th.BasicUser
   331  	user2 := th.BasicUser2
   332  	user3 := th.CreateUser()
   333  	th.LinkUserToTeam(user3, team)
   334  
   335  	Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user2.Id))
   336  	Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user3.Id))
   337  
   338  	rs1 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+user2.Username)).(*model.CommandResponse)
   339  	require.Condition(t, func() bool {
   340  		return strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) ||
   341  			strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id)
   342  	}, "failed to create direct channel")
   343  
   344  	rs2 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+user3.Username+" foobar")).(*model.CommandResponse)
   345  	require.Condition(t, func() bool {
   346  		return strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user3.Id) ||
   347  			strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+user3.Id+"__"+user1.Id)
   348  	}, "failed to create second direct channel")
   349  
   350  	result := Client.Must(Client.SearchPosts(th.BasicTeam.Id, "foobar", false)).(*model.PostList)
   351  	require.NotEqual(t, 0, len(result.Order), "post did not get sent to direct message")
   352  
   353  	rs3 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+user2.Username)).(*model.CommandResponse)
   354  	require.Condition(t, func() bool {
   355  		return strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) ||
   356  			strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id)
   357  	}, "failed to go back to existing direct channel")
   358  
   359  	Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+th.BasicUser.Username+" foobar"))
   360  	Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg junk foobar"))
   361  }
   362  
   363  func TestOpenCommands(t *testing.T) {
   364  	testJoinCommands(t, "open")
   365  }
   366  
   367  func TestSearchCommand(t *testing.T) {
   368  	th := Setup(t).InitBasic()
   369  	defer th.TearDown()
   370  
   371  	th.Client.Must(th.Client.ExecuteCommand(th.BasicChannel.Id, "/search"))
   372  }
   373  
   374  func TestSettingsCommand(t *testing.T) {
   375  	th := Setup(t).InitBasic()
   376  	defer th.TearDown()
   377  
   378  	th.Client.Must(th.Client.ExecuteCommand(th.BasicChannel.Id, "/settings"))
   379  }
   380  
   381  func TestShortcutsCommand(t *testing.T) {
   382  	th := Setup(t).InitBasic()
   383  	defer th.TearDown()
   384  
   385  	th.Client.Must(th.Client.ExecuteCommand(th.BasicChannel.Id, "/shortcuts"))
   386  }
   387  
   388  func TestShrugCommand(t *testing.T) {
   389  	th := Setup(t).InitBasic()
   390  	defer th.TearDown()
   391  
   392  	Client := th.Client
   393  	channel := th.BasicChannel
   394  
   395  	testString := "/shrug"
   396  
   397  	r1 := Client.Must(Client.ExecuteCommand(channel.Id, testString)).(*model.CommandResponse)
   398  	require.NotNil(t, r1, "Command failed to execute")
   399  
   400  	time.Sleep(100 * time.Millisecond)
   401  
   402  	p1 := Client.Must(Client.GetPostsForChannel(channel.Id, 0, 2, "", false)).(*model.PostList)
   403  	require.Len(t, p1.Order, 2, "Command failed to send")
   404  	require.Equal(t, `¯\\\_(ツ)\_/¯`, p1.Posts[p1.Order[0]].Message, "invalid shrug response")
   405  }
   406  
   407  func TestStatusCommands(t *testing.T) {
   408  	th := Setup(t).InitBasic()
   409  	defer th.TearDown()
   410  
   411  	commandAndTest(t, th, "away")
   412  	commandAndTest(t, th, "offline")
   413  	commandAndTest(t, th, "online")
   414  }
   415  
   416  func commandAndTest(t *testing.T, th *TestHelper, status string) {
   417  	Client := th.Client
   418  	channel := th.BasicChannel
   419  	user := th.BasicUser
   420  
   421  	r1 := Client.Must(Client.ExecuteCommand(channel.Id, "/"+status)).(*model.CommandResponse)
   422  	require.NotEqual(t, "Command failed to execute", r1)
   423  
   424  	time.Sleep(1000 * time.Millisecond)
   425  
   426  	rstatus := Client.Must(Client.GetUserStatus(user.Id, "")).(*model.Status)
   427  	require.Equal(t, status, rstatus.Status, "Error setting status")
   428  }