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