github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/cmd/mattermost/commands/command_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 "testing" 8 9 "github.com/mattermost/mattermost-server/model" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestCreateCommand(t *testing.T) { 15 th := Setup().InitBasic() 16 defer th.TearDown() 17 18 config := th.Config() 19 *config.ServiceSettings.EnableCommands = true 20 th.SetConfig(config) 21 22 team := th.BasicTeam 23 adminUser := th.TeamAdminUser 24 user := th.BasicUser 25 26 testCases := []struct { 27 Description string 28 Args []string 29 ExpectedErr string 30 }{ 31 { 32 "nil error", 33 []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 34 "", 35 }, 36 { 37 "Team not specified", 38 []string{"command", "create", "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 39 "Error: requires at least 1 arg(s), only received 0", 40 }, 41 { 42 "Team not found", 43 []string{"command", "create", "fakeTeam", "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 44 "Error: unable to find team", 45 }, 46 { 47 "Creator not specified", 48 []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler"}, 49 `Error: required flag(s) "creator" not set`, 50 }, 51 { 52 "Creator not found", 53 []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", "fakeuser"}, 54 "unable to find user", 55 }, 56 { 57 "Creator not team admin", 58 []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, 59 "the creator must be a user who has permissions to manage slash commands", 60 }, 61 { 62 "Command not specified", 63 []string{"command", "", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 64 "Error: unknown flag: --trigger-word", 65 }, 66 { 67 "Trigger not specified", 68 []string{"command", "create", team.Name, "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 69 `Error: required flag(s) "trigger-word" not set`, 70 }, 71 { 72 "Blank trigger", 73 []string{"command", "create", team.Name, "--trigger-word", "", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 74 "Invalid trigger", 75 }, 76 { 77 "Trigger with space", 78 []string{"command", "create", team.Name, "--trigger-word", "test cmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 79 "Error: a trigger word must not contain spaces", 80 }, 81 { 82 "Trigger starting with /", 83 []string{"command", "create", team.Name, "--trigger-word", "/testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 84 "Error: a trigger word cannot begin with a /", 85 }, 86 { 87 "URL not specified", 88 []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--creator", adminUser.Username}, 89 `Error: required flag(s) "url" not set`, 90 }, 91 { 92 "Blank URL", 93 []string{"command", "create", team.Name, "--trigger-word", "testcmd2", "--url", "", "--creator", adminUser.Username}, 94 "Invalid URL", 95 }, 96 { 97 "Invalid URL", 98 []string{"command", "create", team.Name, "--trigger-word", "testcmd2", "--url", "localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 99 "Invalid URL", 100 }, 101 { 102 "Duplicate Command", 103 []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 104 "This trigger word is already in use", 105 }, 106 { 107 "Misspelled flag", 108 []string{"command", "create", team.Name, "--trigger-wor", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, 109 "Error: unknown flag:", 110 }, 111 } 112 113 for _, testCase := range testCases { 114 t.Run(testCase.Description, func(t *testing.T) { 115 actual, _ := th.RunCommandWithOutput(t, testCase.Args...) 116 117 cmds, _ := th.SystemAdminClient.ListCommands(team.Id, true) 118 119 if testCase.ExpectedErr == "" { 120 if len(cmds) == 0 || cmds[0].Trigger != "testcmd" { 121 t.Fatal("Failed to create command") 122 } 123 assert.Contains(t, string(actual), "PASS") 124 } else { 125 if len(cmds) > 1 { 126 t.Fatal("Created command that shouldn't have been created") 127 } 128 assert.Contains(t, string(actual), testCase.ExpectedErr) 129 } 130 }) 131 } 132 } 133 134 func TestShowCommand(t *testing.T) { 135 th := Setup().InitBasic() 136 defer th.TearDown() 137 138 url := "http://localhost:8000/test-command" 139 team := th.BasicTeam 140 user := th.BasicUser 141 th.LinkUserToTeam(user, team) 142 trigger := "trigger_" + model.NewId() 143 displayName := "dn_" + model.NewId() 144 145 c := &model.Command{ 146 DisplayName: displayName, 147 Method: "G", 148 TeamId: team.Id, 149 Username: user.Username, 150 CreatorId: user.Id, 151 URL: url, 152 Trigger: trigger, 153 } 154 155 t.Run("existing command", func(t *testing.T) { 156 command, err := th.App.CreateCommand(c) 157 require.Nil(t, err) 158 commands, err := th.App.ListTeamCommands(team.Id) 159 require.Nil(t, err) 160 assert.Equal(t, len(commands), 1) 161 162 output := th.CheckCommand(t, "command", "show", command.Id) 163 assert.Contains(t, string(output), command.Id) 164 assert.Contains(t, string(output), command.TeamId) 165 assert.Contains(t, string(output), trigger) 166 assert.Contains(t, string(output), displayName) 167 assert.Contains(t, string(output), user.Username) 168 }) 169 170 t.Run("not existing command", func(t *testing.T) { 171 assert.Error(t, th.RunCommand(t, "command", "show", "invalid")) 172 }) 173 174 t.Run("no commandID", func(t *testing.T) { 175 assert.Error(t, th.RunCommand(t, "command", "show")) 176 }) 177 } 178 179 func TestDeleteCommand(t *testing.T) { 180 // Skipped due to v5.6 RC build issues. 181 t.Skip() 182 183 th := Setup().InitBasic() 184 defer th.TearDown() 185 url := "http://localhost:8000/test-command" 186 team := th.BasicTeam 187 user := th.BasicUser 188 th.LinkUserToTeam(user, team) 189 190 c := &model.Command{ 191 DisplayName: "dn_" + model.NewId(), 192 Method: "G", 193 TeamId: team.Id, 194 Username: user.Username, 195 CreatorId: user.Id, 196 URL: url, 197 Trigger: "trigger_" + model.NewId(), 198 } 199 200 t.Run("existing command", func(t *testing.T) { 201 command, err := th.App.CreateCommand(c) 202 require.Nil(t, err) 203 commands, err := th.App.ListTeamCommands(team.Id) 204 require.Nil(t, err) 205 assert.Equal(t, len(commands), 1) 206 207 th.CheckCommand(t, "command", "delete", command.Id) 208 commands, err = th.App.ListTeamCommands(team.Id) 209 require.Nil(t, err) 210 assert.Equal(t, len(commands), 0) 211 }) 212 213 t.Run("not existing command", func(t *testing.T) { 214 assert.Error(t, th.RunCommand(t, "command", "delete", "invalid")) 215 }) 216 }