github.com/jfrerich/mattermost-server@v5.8.0-rc2+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 TestDeleteCommand(t *testing.T) { 135 // Skipped due to v5.6 RC build issues. 136 t.Skip() 137 138 th := Setup().InitBasic() 139 defer th.TearDown() 140 url := "http://localhost:8000/test-command" 141 team := th.BasicTeam 142 user := th.BasicUser 143 th.LinkUserToTeam(user, team) 144 145 c := &model.Command{ 146 DisplayName: "dn_" + model.NewId(), 147 Method: "G", 148 TeamId: team.Id, 149 Username: user.Username, 150 CreatorId: user.Id, 151 URL: url, 152 Trigger: "trigger_" + model.NewId(), 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 th.CheckCommand(t, "command", "delete", command.Id) 163 commands, err = th.App.ListTeamCommands(team.Id) 164 require.Nil(t, err) 165 assert.Equal(t, len(commands), 0) 166 }) 167 168 t.Run("not existing command", func(t *testing.T) { 169 assert.Error(t, th.RunCommand(t, "command", "delete", "invalid")) 170 }) 171 }