github.com/vnforks/kid@v5.11.1+incompatible/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  	"time"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestJoinChannel(t *testing.T) {
    18  	th := Setup().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().InitBasic()
    34  	defer th.TearDown()
    35  
    36  	channel := th.CreatePublicChannel()
    37  
    38  	th.CheckCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    39  
    40  	// should fail because channel does not exist
    41  	require.Error(t, th.RunCommand(t, "channel", "remove", th.BasicTeam.Name+":doesnotexist", th.BasicUser2.Email))
    42  
    43  	time.Sleep(time.Second)
    44  
    45  	th.CheckCommand(t, "channel", "remove", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    46  
    47  	time.Sleep(time.Second)
    48  
    49  	// Leaving twice should succeed
    50  	th.CheckCommand(t, "channel", "remove", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    51  }
    52  
    53  func TestMoveChannel(t *testing.T) {
    54  	th := Setup().InitBasic()
    55  	defer th.TearDown()
    56  
    57  	team1 := th.BasicTeam
    58  	team2 := th.CreateTeam()
    59  	user1 := th.BasicUser
    60  	th.LinkUserToTeam(user1, team2)
    61  	channel := th.BasicChannel
    62  
    63  	th.LinkUserToTeam(user1, team1)
    64  	th.LinkUserToTeam(user1, team2)
    65  
    66  	adminEmail := user1.Email
    67  	adminUsername := user1.Username
    68  	origin := team1.Name + ":" + channel.Name
    69  	dest := team2.Name
    70  
    71  	th.CheckCommand(t, "channel", "add", origin, adminEmail)
    72  
    73  	// should fail with nil because errors are logged instead of returned when a channel does not exist
    74  	th.CheckCommand(t, "channel", "move", dest, team1.Name+":doesnotexist", "--username", adminUsername)
    75  
    76  	th.CheckCommand(t, "channel", "move", dest, origin, "--username", adminUsername)
    77  }
    78  
    79  func TestListChannels(t *testing.T) {
    80  	th := Setup().InitBasic()
    81  	defer th.TearDown()
    82  
    83  	channel := th.CreatePublicChannel()
    84  	th.Client.Must(th.Client.DeleteChannel(channel.Id))
    85  
    86  	output := th.CheckCommand(t, "channel", "list", th.BasicTeam.Name)
    87  
    88  	if !strings.Contains(string(output), "town-square") {
    89  		t.Fatal("should have channels")
    90  	}
    91  
    92  	if !strings.Contains(string(output), channel.Name+" (archived)") {
    93  		t.Fatal("should have archived channel")
    94  	}
    95  }
    96  
    97  func TestRestoreChannel(t *testing.T) {
    98  	th := Setup().InitBasic()
    99  	defer th.TearDown()
   100  
   101  	channel := th.CreatePublicChannel()
   102  	th.Client.Must(th.Client.DeleteChannel(channel.Id))
   103  
   104  	th.CheckCommand(t, "channel", "restore", th.BasicTeam.Name+":"+channel.Name)
   105  
   106  	// restoring twice should succeed
   107  	th.CheckCommand(t, "channel", "restore", th.BasicTeam.Name+":"+channel.Name)
   108  }
   109  
   110  func TestCreateChannel(t *testing.T) {
   111  	th := Setup().InitBasic()
   112  	defer th.TearDown()
   113  
   114  	id := model.NewId()
   115  	name := "name" + id
   116  
   117  	th.CheckCommand(t, "channel", "create", "--display_name", name, "--team", th.BasicTeam.Name, "--name", name)
   118  
   119  	name = name + "-private"
   120  	th.CheckCommand(t, "channel", "create", "--display_name", name, "--team", th.BasicTeam.Name, "--private", "--name", name)
   121  }
   122  
   123  func TestRenameChannel(t *testing.T) {
   124  	th := Setup().InitBasic()
   125  	defer th.TearDown()
   126  
   127  	channel := th.CreatePublicChannel()
   128  	th.CheckCommand(t, "channel", "rename", th.BasicTeam.Name+":"+channel.Name, "newchannelname10", "--display_name", "New Display Name")
   129  
   130  	// Get the channel from the DB
   131  	updatedChannel, _ := th.App.GetChannel(channel.Id)
   132  	assert.Equal(t, "newchannelname10", updatedChannel.Name)
   133  	assert.Equal(t, "New Display Name", updatedChannel.DisplayName)
   134  }
   135  
   136  func Test_searchChannelCmdF(t *testing.T) {
   137  	th := Setup().InitBasic()
   138  	defer th.TearDown()
   139  
   140  	channel := th.CreatePublicChannel()
   141  	channel2 := th.CreatePublicChannel()
   142  	th.Client.DeleteChannel(channel2.Id)
   143  
   144  	tests := []struct {
   145  		Name     string
   146  		Args     []string
   147  		Expected string
   148  	}{
   149  		{
   150  			"Success find Channel in any team",
   151  			[]string{"channel", "search", channel.Name},
   152  			fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s", channel.Name, channel.DisplayName, channel.Id),
   153  		},
   154  		{
   155  			"Failed find Channel in any team",
   156  			[]string{"channel", "search", channel.Name + "404"},
   157  			fmt.Sprintf("Channel %s is not found in any team", channel.Name+"404"),
   158  		},
   159  		{
   160  			"Success find Channel with param team ID",
   161  			[]string{"channel", "search", "--team", channel.TeamId, channel.Name},
   162  			fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s", channel.Name, channel.DisplayName, channel.Id),
   163  		},
   164  		{
   165  			"Failed find Channel with param team ID",
   166  			[]string{"channel", "search", "--team", channel.TeamId, channel.Name + "404"},
   167  			fmt.Sprintf("Channel %s is not found in team %s", channel.Name+"404", channel.TeamId),
   168  		},
   169  		{
   170  			"Success find archived Channel in any team",
   171  			[]string{"channel", "search", channel2.Name},
   172  			fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s (archived)", channel2.Name, channel2.DisplayName, channel2.Id),
   173  		},
   174  		{
   175  			"Success find archived Channel with param team ID",
   176  			[]string{"channel", "search", "--team", channel2.TeamId, channel2.Name},
   177  			fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s (archived)", channel2.Name, channel2.DisplayName, channel2.Id),
   178  		},
   179  		{
   180  			"Failed find team",
   181  			[]string{"channel", "search", "--team", channel.TeamId + "404", channel.Name},
   182  			fmt.Sprintf("Team %s is not found", channel.TeamId+"404"),
   183  		},
   184  		{
   185  			"Success find Channel with param team ID",
   186  			[]string{"channel", "search", channel.Name, "--team", channel.TeamId},
   187  			fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s", channel.Name, channel.DisplayName, channel.Id),
   188  		},
   189  		{
   190  			"Success find Channel with param team ID",
   191  			[]string{"channel", "search", channel.Name, "--team=" + channel.TeamId},
   192  			fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s", channel.Name, channel.DisplayName, channel.Id),
   193  		},
   194  	}
   195  
   196  	for _, test := range tests {
   197  		t.Run(test.Name, func(t *testing.T) {
   198  			assert.Contains(t, th.CheckCommand(t, test.Args...), test.Expected)
   199  		})
   200  	}
   201  }