github.com/dschalla/mattermost-server@v4.8.1-rc1+incompatible/cmd/platform/channel_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package main 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/mattermost/mattermost-server/api" 11 "github.com/mattermost/mattermost-server/model" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestJoinChannel(t *testing.T) { 16 th := api.Setup().InitBasic() 17 defer th.TearDown() 18 19 channel := th.CreateChannel(th.BasicClient, th.BasicTeam) 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 := api.Setup().InitBasic() 32 defer th.TearDown() 33 34 channel := th.CreateChannel(th.BasicClient, th.BasicTeam) 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 := api.Setup().InitBasic() 49 defer th.TearDown() 50 51 client := th.BasicClient 52 team1 := th.BasicTeam 53 team2 := th.CreateTeam(client) 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 := api.Setup().InitBasic() 76 defer th.TearDown() 77 78 channel := th.CreateChannel(th.BasicClient, th.BasicTeam) 79 th.BasicClient.Must(th.BasicClient.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 := api.Setup().InitBasic() 94 defer th.TearDown() 95 96 channel := th.CreateChannel(th.BasicClient, th.BasicTeam) 97 th.BasicClient.Must(th.BasicClient.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 := api.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 }