github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/api/command_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api 5 6 import ( 7 "fmt" 8 "strings" 9 "testing" 10 "time" 11 12 "github.com/mattermost/mattermost-server/model" 13 ) 14 15 func TestListCommands(t *testing.T) { 16 th := Setup().InitBasic() 17 defer th.TearDown() 18 19 Client := th.BasicClient 20 21 if results, err := Client.ListCommands(); err != nil { 22 t.Fatal(err) 23 } else { 24 commands := results.Data.([]*model.Command) 25 foundEcho := false 26 27 for _, command := range commands { 28 if command.Trigger == "echo" { 29 foundEcho = true 30 } 31 } 32 33 if !foundEcho { 34 t.Fatal("Couldn't find echo command") 35 } 36 } 37 } 38 39 func TestCreateCommand(t *testing.T) { 40 th := Setup().InitBasic().InitSystemAdmin() 41 defer th.TearDown() 42 43 Client := th.BasicClient 44 user := th.SystemAdminUser 45 team := th.SystemAdminTeam 46 47 enableCommands := *th.App.Config().ServiceSettings.EnableCommands 48 defer func() { 49 th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableCommands = &enableCommands }) 50 }() 51 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true }) 52 53 cmd1 := &model.Command{ 54 CreatorId: user.Id, 55 TeamId: team.Id, 56 URL: "http://nowhere.com", 57 Method: model.COMMAND_METHOD_POST, 58 Trigger: "trigger"} 59 60 if _, err := Client.CreateCommand(cmd1); err == nil { 61 t.Fatal("should have failed because not admin") 62 } 63 64 Client = th.SystemAdminClient 65 66 cmd2 := &model.Command{ 67 CreatorId: user.Id, 68 TeamId: team.Id, 69 URL: "http://nowhere.com", 70 Method: model.COMMAND_METHOD_POST, 71 Trigger: "trigger"} 72 73 var rcmd *model.Command 74 if result, err := Client.CreateCommand(cmd2); err != nil { 75 t.Fatal(err) 76 } else { 77 rcmd = result.Data.(*model.Command) 78 } 79 80 if rcmd.CreatorId != user.Id { 81 t.Fatal("user ids didn't match") 82 } 83 84 if rcmd.TeamId != team.Id { 85 t.Fatal("team ids didn't match") 86 } 87 88 cmd3 := &model.Command{ 89 CreatorId: "123", 90 TeamId: "456", 91 URL: "http://nowhere.com", 92 Method: model.COMMAND_METHOD_POST, 93 Trigger: "trigger"} 94 if _, err := Client.CreateCommand(cmd3); err == nil { 95 t.Fatal("trigger cannot be duplicated") 96 } 97 98 cmd4 := cmd3 99 if _, err := Client.CreateCommand(cmd4); err == nil { 100 t.Fatal("command cannot be duplicated") 101 } 102 } 103 104 func TestListTeamCommands(t *testing.T) { 105 th := Setup().InitSystemAdmin() 106 defer th.TearDown() 107 108 Client := th.SystemAdminClient 109 110 enableCommands := *th.App.Config().ServiceSettings.EnableCommands 111 defer func() { 112 th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableCommands = &enableCommands }) 113 }() 114 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true }) 115 116 cmd1 := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST, Trigger: "trigger"} 117 cmd1 = Client.Must(Client.CreateCommand(cmd1)).Data.(*model.Command) 118 119 if result, err := Client.ListTeamCommands(); err != nil { 120 t.Fatal(err) 121 } else { 122 cmds := result.Data.([]*model.Command) 123 124 if len(cmds) != 1 { 125 t.Fatal("incorrect number of cmd") 126 } 127 } 128 } 129 130 func TestUpdateCommand(t *testing.T) { 131 th := Setup().InitSystemAdmin() 132 defer th.TearDown() 133 134 Client := th.SystemAdminClient 135 user := th.SystemAdminUser 136 team := th.SystemAdminTeam 137 138 enableCommands := *th.App.Config().ServiceSettings.EnableCommands 139 defer func() { 140 th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableCommands = &enableCommands }) 141 }() 142 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true }) 143 144 cmd1 := &model.Command{ 145 CreatorId: user.Id, 146 TeamId: team.Id, 147 URL: "http://nowhere.com", 148 Method: model.COMMAND_METHOD_POST, 149 Trigger: "trigger"} 150 151 cmd1 = Client.Must(Client.CreateCommand(cmd1)).Data.(*model.Command) 152 153 cmd2 := &model.Command{ 154 CreatorId: user.Id, 155 TeamId: team.Id, 156 URL: "http://nowhere.com", 157 Method: model.COMMAND_METHOD_POST, 158 Trigger: "trigger2", 159 Token: cmd1.Token, 160 Id: cmd1.Id} 161 162 if result, err := Client.UpdateCommand(cmd2); err != nil { 163 t.Fatal(err) 164 } else { 165 if result.Data.(*model.Command).Trigger == cmd1.Trigger { 166 t.Fatal("update didn't work properly") 167 } 168 } 169 } 170 171 func TestRegenToken(t *testing.T) { 172 th := Setup().InitSystemAdmin() 173 defer th.TearDown() 174 175 Client := th.SystemAdminClient 176 177 enableCommands := *th.App.Config().ServiceSettings.EnableCommands 178 defer func() { 179 th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableCommands = &enableCommands }) 180 }() 181 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true }) 182 183 cmd := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST, Trigger: "trigger"} 184 cmd = Client.Must(Client.CreateCommand(cmd)).Data.(*model.Command) 185 186 data := make(map[string]string) 187 data["id"] = cmd.Id 188 189 if result, err := Client.RegenCommandToken(data); err != nil { 190 t.Fatal(err) 191 } else { 192 if result.Data.(*model.Command).Token == cmd.Token { 193 t.Fatal("regen didn't work properly") 194 } 195 } 196 } 197 198 func TestDeleteCommand(t *testing.T) { 199 th := Setup().InitBasic().InitSystemAdmin() 200 defer th.TearDown() 201 202 Client := th.SystemAdminClient 203 204 enableCommands := *th.App.Config().ServiceSettings.EnableCommands 205 onlyAdminIntegration := *th.App.Config().ServiceSettings.EnableOnlyAdminIntegrations 206 defer func() { 207 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = enableCommands }) 208 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = onlyAdminIntegration }) 209 }() 210 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true }) 211 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = false }) 212 213 cmd := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST, Trigger: "trigger"} 214 cmd = Client.Must(Client.CreateCommand(cmd)).Data.(*model.Command) 215 216 data := make(map[string]string) 217 data["id"] = cmd.Id 218 219 if _, err := Client.DeleteCommand(data); err != nil { 220 t.Fatal(err) 221 } 222 223 cmds := Client.Must(Client.ListTeamCommands()).Data.([]*model.Command) 224 if len(cmds) != 0 { 225 t.Fatal("delete didn't work properly") 226 } 227 228 cmd2 := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST, Trigger: "trigger2"} 229 cmd2 = Client.Must(Client.CreateCommand(cmd2)).Data.(*model.Command) 230 231 data2 := make(map[string]string) 232 data2["id"] = cmd2.Id 233 if _, err := th.BasicClient.DeleteCommand(data2); err == nil { 234 t.Fatal("Should have errored. Your not allowed to delete other's commands") 235 } 236 237 cmds2 := Client.Must(Client.ListTeamCommands()).Data.([]*model.Command) 238 if len(cmds2) != 1 { 239 t.Fatal("Client was able to delete command without permission.") 240 } 241 } 242 243 func TestTestCommand(t *testing.T) { 244 th := Setup().InitSystemAdmin() 245 defer th.TearDown() 246 247 Client := th.SystemAdminClient 248 channel1 := th.SystemAdminChannel 249 250 enableCommands := *th.App.Config().ServiceSettings.EnableCommands 251 allowedInternalConnections := *th.App.Config().ServiceSettings.AllowedUntrustedInternalConnections 252 defer func() { 253 th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableCommands = &enableCommands }) 254 th.App.UpdateConfig(func(cfg *model.Config) { 255 cfg.ServiceSettings.AllowedUntrustedInternalConnections = &allowedInternalConnections 256 }) 257 }() 258 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true }) 259 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost" }) 260 261 cmd1 := &model.Command{ 262 URL: fmt.Sprintf("http://localhost:%v", th.App.Srv.ListenAddr.Port) + model.API_URL_SUFFIX_V3 + "/teams/command_test", 263 Method: model.COMMAND_METHOD_POST, 264 Trigger: "testcommand", 265 } 266 267 cmd1 = Client.Must(Client.CreateCommand(cmd1)).Data.(*model.Command) 268 269 r1 := Client.Must(Client.Command(channel1.Id, "/testcommand")).Data.(*model.CommandResponse) 270 if r1 == nil { 271 t.Fatal("Test command failed to execute") 272 } 273 274 time.Sleep(100 * time.Millisecond) 275 276 p1 := Client.Must(Client.GetPosts(channel1.Id, 0, 10, "")).Data.(*model.PostList) 277 // including system 'joined' message 278 if len(p1.Order) != 2 { 279 t.Fatal("Test command response failed to send") 280 } 281 282 cmdPosted := false 283 for _, post := range p1.Posts { 284 if strings.Contains(post.Message, "test command response") { 285 cmdPosted = true 286 break 287 } 288 } 289 290 if !cmdPosted { 291 t.Fatal("Test command response failed to post") 292 } 293 294 cmd2 := &model.Command{ 295 URL: fmt.Sprintf("http://localhost:%v", th.App.Srv.ListenAddr.Port) + model.API_URL_SUFFIX_V3 + "/teams/command_test", 296 Method: model.COMMAND_METHOD_GET, 297 Trigger: "test2", 298 } 299 300 cmd2 = Client.Must(Client.CreateCommand(cmd2)).Data.(*model.Command) 301 302 r2 := Client.Must(Client.Command(channel1.Id, "/test2")).Data.(*model.CommandResponse) 303 if r2 == nil { 304 t.Fatal("Test2 command failed to execute") 305 } 306 307 time.Sleep(100 * time.Millisecond) 308 309 p2 := Client.Must(Client.GetPosts(channel1.Id, 0, 10, "")).Data.(*model.PostList) 310 if len(p2.Order) != 3 { 311 t.Fatal("Test command failed to send") 312 } 313 }