github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+incompatible/cmd/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  	"strings"
     8  	"testing"
     9  
    10  	"github.com/mattermost/mattermost-server/api"
    11  	"github.com/mattermost/mattermost-server/cmd"
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestJoinChannel(t *testing.T) {
    17  	th := api.Setup().InitBasic()
    18  	defer th.TearDown()
    19  
    20  	channel := th.CreateChannel(th.BasicClient, th.BasicTeam)
    21  
    22  	cmd.CheckCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    23  
    24  	// Joining twice should succeed
    25  	cmd.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, cmd.RunCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name+"asdf", th.BasicUser2.Email))
    29  }
    30  
    31  func TestRemoveChannel(t *testing.T) {
    32  	th := api.Setup().InitBasic()
    33  	defer th.TearDown()
    34  
    35  	channel := th.CreateChannel(th.BasicClient, th.BasicTeam)
    36  
    37  	cmd.CheckCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    38  
    39  	// should fail because channel does not exist
    40  	require.Error(t, cmd.RunCommand(t, "channel", "remove", th.BasicTeam.Name+":doesnotexist", th.BasicUser2.Email))
    41  
    42  	cmd.CheckCommand(t, "channel", "remove", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    43  
    44  	// Leaving twice should succeed
    45  	cmd.CheckCommand(t, "channel", "remove", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email)
    46  }
    47  
    48  func TestMoveChannel(t *testing.T) {
    49  	th := api.Setup().InitBasic()
    50  	defer th.TearDown()
    51  
    52  	client := th.BasicClient
    53  	team1 := th.BasicTeam
    54  	team2 := th.CreateTeam(client)
    55  	user1 := th.BasicUser
    56  	th.LinkUserToTeam(user1, team2)
    57  	channel := th.BasicChannel
    58  
    59  	th.LinkUserToTeam(user1, team1)
    60  	th.LinkUserToTeam(user1, team2)
    61  
    62  	adminEmail := user1.Email
    63  	adminUsername := user1.Username
    64  	origin := team1.Name + ":" + channel.Name
    65  	dest := team2.Name
    66  
    67  	cmd.CheckCommand(t, "channel", "add", origin, adminEmail)
    68  
    69  	// should fail with nill because errors are logged instead of returned when a channel does not exist
    70  	require.Nil(t, cmd.RunCommand(t, "channel", "move", dest, team1.Name+":doesnotexist", "--username", adminUsername))
    71  
    72  	cmd.CheckCommand(t, "channel", "move", dest, origin, "--username", adminUsername)
    73  }
    74  
    75  func TestListChannels(t *testing.T) {
    76  	th := api.Setup().InitBasic()
    77  	defer th.TearDown()
    78  
    79  	channel := th.CreateChannel(th.BasicClient, th.BasicTeam)
    80  	th.BasicClient.Must(th.BasicClient.DeleteChannel(channel.Id))
    81  
    82  	output := cmd.CheckCommand(t, "channel", "list", th.BasicTeam.Name)
    83  
    84  	if !strings.Contains(string(output), "town-square") {
    85  		t.Fatal("should have channels")
    86  	}
    87  
    88  	if !strings.Contains(string(output), channel.Name+" (archived)") {
    89  		t.Fatal("should have archived channel")
    90  	}
    91  }
    92  
    93  func TestRestoreChannel(t *testing.T) {
    94  	th := api.Setup().InitBasic()
    95  	defer th.TearDown()
    96  
    97  	channel := th.CreateChannel(th.BasicClient, th.BasicTeam)
    98  	th.BasicClient.Must(th.BasicClient.DeleteChannel(channel.Id))
    99  
   100  	cmd.CheckCommand(t, "channel", "restore", th.BasicTeam.Name+":"+channel.Name)
   101  
   102  	// restoring twice should succeed
   103  	cmd.CheckCommand(t, "channel", "restore", th.BasicTeam.Name+":"+channel.Name)
   104  }
   105  
   106  func TestCreateChannel(t *testing.T) {
   107  	th := api.Setup().InitBasic()
   108  	defer th.TearDown()
   109  
   110  	id := model.NewId()
   111  	name := "name" + id
   112  
   113  	cmd.CheckCommand(t, "channel", "create", "--display_name", name, "--team", th.BasicTeam.Name, "--name", name)
   114  
   115  	name = name + "-private"
   116  	cmd.CheckCommand(t, "channel", "create", "--display_name", name, "--team", th.BasicTeam.Name, "--private", "--name", name)
   117  }