github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/cmd/mattermost/commands/channel_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/mattermost/mattermost-server/v5/model"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestJoinChannel(t *testing.T) {
    17  	th := Setup(t).InitBasic()
    18  	defer th.TearDown()
    19  
    20  	channel := th.CreatePublicChannel()
    21  
    22  	th.CheckCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    23  
    24  	// Joining twice should succeed
    25  	th.CheckCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    26  
    27  	// should fail because channel does not exist
    28  	require.Error(t, th.RunCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name+"asdf", th.BasicUser2.Email))
    29  }
    30  
    31  func TestRemoveChannel(t *testing.T) {
    32  	th := Setup(t).InitBasic()
    33  	defer th.TearDown()
    34  
    35  	channel := th.CreatePublicChannel()
    36  
    37  	t.Run("should fail because channel does not exist", func(t *testing.T) {
    38  		require.Error(t, th.RunCommand(t, "channel", "remove", th.BasicTeam.Name+":doesnotexist", th.BasicUser2.Email))
    39  	})
    40  
    41  	t.Run("should remove user from channel", func(t *testing.T) {
    42  		th.CheckCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    43  		isMember, _ := th.App.Srv().Store.Channel().UserBelongsToChannels(th.BasicUser2.Id, []string{channel.Id})
    44  		assert.True(t, isMember)
    45  
    46  		th.CheckCommand(t, "channel", "remove", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    47  		isMember, _ = th.App.Srv().Store.Channel().UserBelongsToChannels(th.BasicUser2.Id, []string{channel.Id})
    48  		assert.False(t, isMember)
    49  	})
    50  
    51  	t.Run("should not fail removing non member user from channel", func(t *testing.T) {
    52  		isMember, _ := th.App.Srv().Store.Channel().UserBelongsToChannels(th.BasicUser2.Id, []string{channel.Id})
    53  		assert.False(t, isMember)
    54  		th.CheckCommand(t, "channel", "remove", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    55  	})
    56  
    57  	t.Run("should throw error if both --all-users flag and user email are passed", func(t *testing.T) {
    58  		require.Error(t, th.RunCommand(t, "channel", "remove", "--all-users", th.BasicUser.Email))
    59  	})
    60  
    61  	t.Run("should remove all users from channel", func(t *testing.T) {
    62  		th.CheckCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser.Email)
    63  		th.CheckCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    64  		count, _ := th.App.Srv().Store.Channel().GetMemberCount(channel.Id, false)
    65  		assert.Equal(t, count, int64(2))
    66  
    67  		th.CheckCommand(t, "channel", "remove", th.BasicTeam.Name+":"+channel.Name, "--all-users")
    68  		count, _ = th.App.Srv().Store.Channel().GetMemberCount(channel.Id, false)
    69  		assert.Equal(t, count, int64(0))
    70  	})
    71  
    72  	t.Run("should remove multiple users from channel", func(t *testing.T) {
    73  		th.CheckCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser.Email)
    74  		th.CheckCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    75  		count, _ := th.App.Srv().Store.Channel().GetMemberCount(channel.Id, false)
    76  		assert.Equal(t, count, int64(2))
    77  
    78  		th.CheckCommand(t, "channel", "remove", th.BasicTeam.Name+":"+channel.Name, th.BasicUser.Email, th.BasicUser2.Email)
    79  		count, _ = th.App.Srv().Store.Channel().GetMemberCount(channel.Id, false)
    80  		assert.Equal(t, count, int64(0))
    81  	})
    82  }
    83  
    84  func TestMoveChannel(t *testing.T) {
    85  	th := Setup(t).InitBasic()
    86  	defer th.TearDown()
    87  
    88  	team1 := th.BasicTeam
    89  	team2 := th.CreateTeam()
    90  	user1 := th.BasicUser
    91  	th.LinkUserToTeam(user1, team2)
    92  	channel := th.BasicChannel
    93  
    94  	th.LinkUserToTeam(user1, team1)
    95  	th.LinkUserToTeam(user1, team2)
    96  
    97  	adminEmail := user1.Email
    98  	adminUsername := user1.Username
    99  	origin := team1.Name + ":" + channel.Name
   100  	dest := team2.Name
   101  
   102  	th.CheckCommand(t, "channel", "add", origin, adminEmail)
   103  
   104  	// should fail with nil because errors are logged instead of returned when a channel does not exist
   105  	th.CheckCommand(t, "channel", "move", dest, team1.Name+":doesnotexist", "--username", adminUsername)
   106  
   107  	th.CheckCommand(t, "channel", "move", dest, origin, "--username", adminUsername)
   108  }
   109  
   110  func TestListChannels(t *testing.T) {
   111  	th := Setup(t).InitBasic()
   112  	defer th.TearDown()
   113  
   114  	channel := th.CreatePublicChannel()
   115  	th.Client.Must(th.Client.DeleteChannel(channel.Id))
   116  	privateChannel := th.CreatePrivateChannel()
   117  
   118  	output := th.CheckCommand(t, "channel", "list", th.BasicTeam.Name)
   119  
   120  	require.True(t, strings.Contains(output, "town-square"), "should have channels")
   121  
   122  	require.True(t, strings.Contains(output, channel.Name+" (archived)"), "should have archived channel")
   123  
   124  	require.True(t, strings.Contains(output, privateChannel.Name+" (private)"), "should have private channel")
   125  
   126  	th.Client.Must(th.Client.DeleteChannel(privateChannel.Id))
   127  
   128  	output = th.CheckCommand(t, "channel", "list", th.BasicTeam.Name)
   129  
   130  	require.True(t, strings.Contains(output, privateChannel.Name+" (archived) (private)"), "should have a channel both archived and private")
   131  }
   132  
   133  func TestRestoreChannel(t *testing.T) {
   134  	th := Setup(t).InitBasic()
   135  	defer th.TearDown()
   136  
   137  	channel := th.CreatePublicChannel()
   138  	th.Client.Must(th.Client.DeleteChannel(channel.Id))
   139  
   140  	th.CheckCommand(t, "channel", "restore", th.BasicTeam.Name+":"+channel.Name)
   141  
   142  	// restoring twice should succeed
   143  	th.CheckCommand(t, "channel", "restore", th.BasicTeam.Name+":"+channel.Name)
   144  }
   145  
   146  func TestCreateChannel(t *testing.T) {
   147  	th := Setup(t).InitBasic()
   148  	defer th.TearDown()
   149  
   150  	id := model.NewId()
   151  	commonName := "name" + id
   152  	team, _ := th.App.Srv().Store.Team().GetByName(th.BasicTeam.Name)
   153  
   154  	t.Run("should create public channel", func(t *testing.T) {
   155  		th.CheckCommand(t, "channel", "create", "--display_name", commonName, "--team", th.BasicTeam.Name, "--name", commonName)
   156  		channel, _ := th.App.Srv().Store.Channel().GetByName(team.Id, commonName, false)
   157  		assert.Equal(t, commonName, channel.Name)
   158  		assert.Equal(t, model.CHANNEL_OPEN, channel.Type)
   159  	})
   160  
   161  	t.Run("should create private channel", func(t *testing.T) {
   162  		name := commonName + "-private"
   163  		th.CheckCommand(t, "channel", "create", "--display_name", name, "--team", th.BasicTeam.Name, "--name", name, "--private")
   164  		channel, _ := th.App.Srv().Store.Channel().GetByName(team.Id, name, false)
   165  		assert.Equal(t, name, channel.Name)
   166  		assert.Equal(t, model.CHANNEL_PRIVATE, channel.Type)
   167  	})
   168  
   169  	t.Run("should create channel with header and purpose", func(t *testing.T) {
   170  		name := commonName + "-withhp"
   171  		th.CheckCommand(t, "channel", "create", "--display_name", name, "--team", th.BasicTeam.Name, "--name", name, "--header", "this is a header", "--purpose", "this is the purpose")
   172  		channel, _ := th.App.Srv().Store.Channel().GetByName(team.Id, name, false)
   173  		assert.Equal(t, name, channel.Name)
   174  		assert.Equal(t, model.CHANNEL_OPEN, channel.Type)
   175  		assert.Equal(t, "this is a header", channel.Header)
   176  		assert.Equal(t, "this is the purpose", channel.Purpose)
   177  	})
   178  
   179  	t.Run("should not create channel if name already exists on the same team", func(t *testing.T) {
   180  		output, err := th.RunCommandWithOutput(t, "channel", "create", "--display_name", commonName, "--team", th.BasicTeam.Name, "--name", commonName)
   181  		require.Error(t, err)
   182  		require.Contains(t, output, "A channel with that name already exists on the same team.")
   183  	})
   184  
   185  	t.Run("should not create channel without display name", func(t *testing.T) {
   186  		output, err := th.RunCommandWithOutput(t, "channel", "create", "--display_name", "", "--team", th.BasicTeam.Name, "--name", commonName)
   187  		require.Error(t, err)
   188  		require.Contains(t, output, "Display Name is required")
   189  	})
   190  
   191  	t.Run("should not create channel without name", func(t *testing.T) {
   192  		output, err := th.RunCommandWithOutput(t, "channel", "create", "--display_name", commonName, "--team", th.BasicTeam.Name, "--name", "")
   193  		require.Error(t, err)
   194  		require.Contains(t, output, "Name is required")
   195  	})
   196  
   197  	t.Run("should not create channel without team", func(t *testing.T) {
   198  		output, err := th.RunCommandWithOutput(t, "channel", "create", "--display_name", commonName, "--team", "", "--name", commonName)
   199  		require.Error(t, err)
   200  		require.Contains(t, output, "Team is required")
   201  	})
   202  
   203  	t.Run("should not create channel with unexisting team", func(t *testing.T) {
   204  		output, err := th.RunCommandWithOutput(t, "channel", "create", "--display_name", commonName, "--team", th.BasicTeam.Name+"-unexisting", "--name", commonName)
   205  		require.Error(t, err)
   206  		require.Contains(t, output, "Unable to find team:")
   207  	})
   208  }
   209  
   210  func TestRenameChannel(t *testing.T) {
   211  	th := Setup(t).InitBasic()
   212  	defer th.TearDown()
   213  
   214  	channel := th.CreatePublicChannel()
   215  	th.CheckCommand(t, "channel", "rename", th.BasicTeam.Name+":"+channel.Name, "newchannelname10", "--display_name", "New Display Name")
   216  
   217  	// Get the channel from the DB
   218  	updatedChannel, _ := th.App.GetChannel(channel.Id)
   219  	assert.Equal(t, "newchannelname10", updatedChannel.Name)
   220  	assert.Equal(t, "New Display Name", updatedChannel.DisplayName)
   221  }
   222  
   223  func Test_searchChannelCmdF(t *testing.T) {
   224  	th := Setup(t).InitBasic()
   225  	defer th.TearDown()
   226  
   227  	channel := th.CreatePublicChannel()
   228  	channel2 := th.CreatePublicChannel()
   229  	channel3 := th.CreatePrivateChannel()
   230  	channel4 := th.CreatePrivateChannel()
   231  	th.Client.DeleteChannel(channel2.Id)
   232  	th.Client.DeleteChannel(channel4.Id)
   233  
   234  	tests := []struct {
   235  		Name     string
   236  		Args     []string
   237  		Expected string
   238  	}{
   239  		{
   240  			"Success find Channel in any team",
   241  			[]string{"channel", "search", channel.Name},
   242  			fmt.Sprintf("Channel Name: %s, Display Name: %s, Channel ID: %s", channel.Name, channel.DisplayName, channel.Id),
   243  		},
   244  		{
   245  			"Failed find Channel in any team",
   246  			[]string{"channel", "search", channel.Name + "404"},
   247  			fmt.Sprintf("Channel %s is not found in any team", channel.Name+"404"),
   248  		},
   249  		{
   250  			"Success find Channel with param team ID",
   251  			[]string{"channel", "search", "--team", channel.TeamId, channel.Name},
   252  			fmt.Sprintf("Channel Name: %s, Display Name: %s, Channel ID: %s", channel.Name, channel.DisplayName, channel.Id),
   253  		},
   254  		{
   255  			"Failed find Channel with param team ID",
   256  			[]string{"channel", "search", "--team", channel.TeamId, channel.Name + "404"},
   257  			fmt.Sprintf("Channel %s is not found in team %s", channel.Name+"404", channel.TeamId),
   258  		},
   259  		{
   260  			"Success find archived Channel in any team",
   261  			[]string{"channel", "search", channel2.Name},
   262  			fmt.Sprintf("Channel Name: %s, Display Name: %s, Channel ID: %s (archived)", channel2.Name, channel2.DisplayName, channel2.Id),
   263  		},
   264  		{
   265  			"Success find archived Channel with param team ID",
   266  			[]string{"channel", "search", "--team", channel2.TeamId, channel2.Name},
   267  			fmt.Sprintf("Channel Name: %s, Display Name: %s, Channel ID: %s (archived)", channel2.Name, channel2.DisplayName, channel2.Id),
   268  		},
   269  		{
   270  			"Success find private Channel in any team",
   271  			[]string{"channel", "search", channel3.Name},
   272  			fmt.Sprintf("Channel Name: %s, Display Name: %s, Channel ID: %s (private)", channel3.Name, channel3.DisplayName, channel3.Id),
   273  		},
   274  		{
   275  			"Success find private Channel with param team ID",
   276  			[]string{"channel", "search", "--team", channel3.TeamId, channel3.Name},
   277  			fmt.Sprintf("Channel Name: %s, Display Name: %s, Channel ID: %s (private)", channel3.Name, channel3.DisplayName, channel3.Id),
   278  		},
   279  		{
   280  			"Success find both archived and private Channel in any team",
   281  			[]string{"channel", "search", channel4.Name},
   282  			fmt.Sprintf("Channel Name: %s, Display Name: %s, Channel ID: %s (archived) (private)", channel4.Name, channel4.DisplayName, channel4.Id),
   283  		},
   284  		{
   285  			"Success find both archived and private Channel with param team ID",
   286  			[]string{"channel", "search", "--team", channel4.TeamId, channel4.Name},
   287  			fmt.Sprintf("Channel Name: %s, Display Name: %s, Channel ID: %s (archived) (private)", channel4.Name, channel4.DisplayName, channel4.Id),
   288  		},
   289  		{
   290  			"Failed find team",
   291  			[]string{"channel", "search", "--team", channel.TeamId + "404", channel.Name},
   292  			fmt.Sprintf("Team %s is not found", channel.TeamId+"404"),
   293  		},
   294  		{
   295  			"Success find Channel with param team ID",
   296  			[]string{"channel", "search", channel.Name, "--team", channel.TeamId},
   297  			fmt.Sprintf("Channel Name: %s, Display Name: %s, Channel ID: %s", channel.Name, channel.DisplayName, channel.Id),
   298  		},
   299  		{
   300  			"Success find Channel with param team ID",
   301  			[]string{"channel", "search", channel.Name, "--team=" + channel.TeamId},
   302  			fmt.Sprintf("Channel Name: %s, Display Name: %s, Channel ID: %s", channel.Name, channel.DisplayName, channel.Id),
   303  		},
   304  	}
   305  
   306  	for _, test := range tests {
   307  		t.Run(test.Name, func(t *testing.T) {
   308  			assert.Contains(t, th.CheckCommand(t, test.Args...), test.Expected)
   309  		})
   310  	}
   311  }
   312  
   313  func TestModifyChannel(t *testing.T) {
   314  	th := Setup(t).InitBasic()
   315  	defer th.TearDown()
   316  
   317  	channel1 := th.CreatePrivateChannel()
   318  	channel2 := th.CreatePrivateChannel()
   319  
   320  	th.CheckCommand(t, "channel", "modify", "--public", th.BasicTeam.Name+":"+channel1.Name, "--username", th.BasicUser2.Email)
   321  	res, err := th.App.Srv().Store.Channel().Get(channel1.Id, false)
   322  	require.Nil(t, err)
   323  	assert.Equal(t, model.CHANNEL_OPEN, res.Type)
   324  
   325  	// should fail because user doesn't exist
   326  	require.Error(t, th.RunCommand(t, "channel", "modify", "--public", th.BasicTeam.Name+":"+channel2.Name, "--username", "idonotexist"))
   327  
   328  	pchannel1 := th.CreatePublicChannel()
   329  	pchannel2 := th.CreatePublicChannel()
   330  
   331  	th.CheckCommand(t, "channel", "modify", "--private", th.BasicTeam.Name+":"+pchannel1.Name, "--username", th.BasicUser2.Email)
   332  	res, err = th.App.Srv().Store.Channel().Get(pchannel1.Id, false)
   333  	require.Nil(t, err)
   334  	assert.Equal(t, model.CHANNEL_PRIVATE, res.Type)
   335  
   336  	// should fail because user doesn't exist
   337  	require.Error(t, th.RunCommand(t, "channel", "modify", "--private", th.BasicTeam.Name+":"+pchannel2.Name, "--username", "idonotexist"))
   338  }