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