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