github.com/mad-app/mattermost-server@v5.11.1+incompatible/api4/commands_test.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/mattermost/mattermost-server/model" 12 ) 13 14 func TestEchoCommand(t *testing.T) { 15 th := Setup().InitBasic() 16 defer th.TearDown() 17 18 Client := th.Client 19 channel1 := th.BasicChannel 20 21 echoTestString := "/echo test" 22 23 if r1 := Client.Must(Client.ExecuteCommand(channel1.Id, echoTestString)).(*model.CommandResponse); r1 == nil { 24 t.Fatal("Echo command failed to execute") 25 } 26 27 if r1 := Client.Must(Client.ExecuteCommand(channel1.Id, "/echo ")).(*model.CommandResponse); r1 == nil { 28 t.Fatal("Echo command failed to execute") 29 } 30 31 time.Sleep(100 * time.Millisecond) 32 33 p1 := Client.Must(Client.GetPostsForChannel(channel1.Id, 0, 2, "")).(*model.PostList) 34 if len(p1.Order) != 2 { 35 t.Fatal("Echo command failed to send") 36 } 37 } 38 39 func TestGroupmsgCommands(t *testing.T) { 40 th := Setup().InitBasic() 41 defer th.TearDown() 42 43 Client := th.Client 44 team := th.BasicTeam 45 user1 := th.BasicUser 46 user2 := th.BasicUser2 47 user3 := th.CreateUser() 48 user4 := th.CreateUser() 49 user5 := th.CreateUser() 50 user6 := th.CreateUser() 51 user7 := th.CreateUser() 52 user8 := th.CreateUser() 53 user9 := th.CreateUser() 54 th.LinkUserToTeam(user3, team) 55 th.LinkUserToTeam(user4, team) 56 57 rs1 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+","+user3.Username)).(*model.CommandResponse) 58 59 group1 := model.GetGroupNameFromUserIds([]string{user1.Id, user2.Id, user3.Id}) 60 61 if !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+group1) { 62 t.Fatal("failed to create group channel") 63 } 64 65 rs2 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user3.Username+","+user4.Username+" foobar")).(*model.CommandResponse) 66 group2 := model.GetGroupNameFromUserIds([]string{user1.Id, user3.Id, user4.Id}) 67 68 if !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+group2) { 69 t.Fatal("failed to create second direct channel") 70 } 71 if result := Client.Must(Client.SearchPosts(team.Id, "foobar", false)).(*model.PostList); len(result.Order) == 0 { 72 t.Fatal("post did not get sent to direct message") 73 } 74 75 rs3 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+","+user3.Username)).(*model.CommandResponse) 76 if !strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+group1) { 77 t.Fatal("failed to go back to existing group channel") 78 } 79 80 Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+" foobar")) 81 Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+","+user3.Username+","+user4.Username+","+user5.Username+","+user6.Username+","+user7.Username+","+user8.Username+","+user9.Username+" foobar")) 82 Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg junk foobar")) 83 Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg junk,junk2 foobar")) 84 } 85 86 func TestInvitePeopleCommand(t *testing.T) { 87 th := Setup().InitBasic() 88 defer th.TearDown() 89 90 Client := th.Client 91 channel := th.BasicChannel 92 93 r1 := Client.Must(Client.ExecuteCommand(channel.Id, "/invite_people test@example.com")).(*model.CommandResponse) 94 if r1 == nil { 95 t.Fatal("Command failed to execute") 96 } 97 98 r2 := Client.Must(Client.ExecuteCommand(channel.Id, "/invite_people test1@example.com test2@example.com")).(*model.CommandResponse) 99 if r2 == nil { 100 t.Fatal("Command failed to execute") 101 } 102 103 r3 := Client.Must(Client.ExecuteCommand(channel.Id, "/invite_people")).(*model.CommandResponse) 104 if r3 == nil { 105 t.Fatal("Command failed to execute") 106 } 107 } 108 109 // also used to test /open (see command_open_test.go) 110 func testJoinCommands(t *testing.T, alias string) { 111 th := Setup().InitBasic() 112 defer th.TearDown() 113 114 Client := th.Client 115 team := th.BasicTeam 116 user2 := th.BasicUser2 117 118 channel0 := &model.Channel{DisplayName: "00", Name: "00" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id} 119 channel0 = Client.Must(Client.CreateChannel(channel0)).(*model.Channel) 120 121 channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id} 122 channel1 = Client.Must(Client.CreateChannel(channel1)).(*model.Channel) 123 Client.Must(Client.RemoveUserFromChannel(channel1.Id, th.BasicUser.Id)) 124 125 channel2 := &model.Channel{DisplayName: "BB", Name: "bb" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id} 126 channel2 = Client.Must(Client.CreateChannel(channel2)).(*model.Channel) 127 Client.Must(Client.RemoveUserFromChannel(channel2.Id, th.BasicUser.Id)) 128 129 channel3 := Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user2.Id)).(*model.Channel) 130 131 rs5 := Client.Must(Client.ExecuteCommand(channel0.Id, "/"+alias+" "+channel2.Name)).(*model.CommandResponse) 132 if !strings.HasSuffix(rs5.GotoLocation, "/"+team.Name+"/channels/"+channel2.Name) { 133 t.Fatal("failed to join channel") 134 } 135 136 rs6 := Client.Must(Client.ExecuteCommand(channel0.Id, "/"+alias+" "+channel3.Name)).(*model.CommandResponse) 137 if strings.HasSuffix(rs6.GotoLocation, "/"+team.Name+"/channels/"+channel3.Name) { 138 t.Fatal("should not have joined direct message channel") 139 } 140 141 c1 := Client.Must(Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "")).([]*model.Channel) 142 143 found := false 144 for _, c := range c1 { 145 if c.Id == channel2.Id { 146 found = true 147 } 148 } 149 150 if !found { 151 t.Fatal("did not join channel") 152 } 153 } 154 155 func TestJoinCommands(t *testing.T) { 156 testJoinCommands(t, "join") 157 } 158 159 func TestLoadTestHelpCommands(t *testing.T) { 160 th := Setup().InitBasic() 161 defer th.TearDown() 162 163 Client := th.Client 164 channel := th.BasicChannel 165 166 enableTesting := *th.App.Config().ServiceSettings.EnableTesting 167 defer func() { 168 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = enableTesting }) 169 }() 170 171 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true }) 172 173 rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test help")).(*model.CommandResponse) 174 if !strings.Contains(rs.Text, "Mattermost testing commands to help") { 175 t.Fatal(rs.Text) 176 } 177 178 time.Sleep(2 * time.Second) 179 } 180 181 func TestLoadTestSetupCommands(t *testing.T) { 182 th := Setup().InitBasic() 183 defer th.TearDown() 184 185 Client := th.Client 186 channel := th.BasicChannel 187 188 enableTesting := *th.App.Config().ServiceSettings.EnableTesting 189 defer func() { 190 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = enableTesting }) 191 }() 192 193 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true }) 194 195 rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test setup fuzz 1 1 1")).(*model.CommandResponse) 196 if rs.Text != "Created environment" { 197 t.Fatal(rs.Text) 198 } 199 200 time.Sleep(2 * time.Second) 201 } 202 203 func TestLoadTestUsersCommands(t *testing.T) { 204 th := Setup().InitBasic() 205 defer th.TearDown() 206 207 Client := th.Client 208 channel := th.BasicChannel 209 210 enableTesting := *th.App.Config().ServiceSettings.EnableTesting 211 defer func() { 212 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = enableTesting }) 213 }() 214 215 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true }) 216 217 rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test users fuzz 1 2")).(*model.CommandResponse) 218 if rs.Text != "Added users" { 219 t.Fatal(rs.Text) 220 } 221 222 time.Sleep(2 * time.Second) 223 } 224 225 func TestLoadTestChannelsCommands(t *testing.T) { 226 th := Setup().InitBasic() 227 defer th.TearDown() 228 229 Client := th.Client 230 channel := th.BasicChannel 231 232 enableTesting := *th.App.Config().ServiceSettings.EnableTesting 233 defer func() { 234 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = enableTesting }) 235 }() 236 237 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true }) 238 239 rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test channels fuzz 1 2")).(*model.CommandResponse) 240 if rs.Text != "Added channels" { 241 t.Fatal(rs.Text) 242 } 243 244 time.Sleep(2 * time.Second) 245 } 246 247 func TestLoadTestPostsCommands(t *testing.T) { 248 th := Setup().InitBasic() 249 defer th.TearDown() 250 251 Client := th.Client 252 channel := th.BasicChannel 253 254 enableTesting := *th.App.Config().ServiceSettings.EnableTesting 255 defer func() { 256 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = enableTesting }) 257 }() 258 259 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true }) 260 261 rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test posts fuzz 2 3 2")).(*model.CommandResponse) 262 if rs.Text != "Added posts" { 263 t.Fatal(rs.Text) 264 } 265 266 time.Sleep(2 * time.Second) 267 } 268 269 func TestLeaveCommands(t *testing.T) { 270 th := Setup().InitBasic() 271 defer th.TearDown() 272 273 Client := th.Client 274 team := th.BasicTeam 275 user2 := th.BasicUser2 276 277 channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id} 278 channel1 = Client.Must(Client.CreateChannel(channel1)).(*model.Channel) 279 Client.Must(Client.AddChannelMember(channel1.Id, th.BasicUser.Id)) 280 281 channel2 := &model.Channel{DisplayName: "BB", Name: "bb" + model.NewId() + "a", Type: model.CHANNEL_PRIVATE, TeamId: team.Id} 282 channel2 = Client.Must(Client.CreateChannel(channel2)).(*model.Channel) 283 Client.Must(Client.AddChannelMember(channel2.Id, th.BasicUser.Id)) 284 Client.Must(Client.AddChannelMember(channel2.Id, user2.Id)) 285 286 channel3 := Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user2.Id)).(*model.Channel) 287 288 rs1 := Client.Must(Client.ExecuteCommand(channel1.Id, "/leave")).(*model.CommandResponse) 289 if !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL) { 290 t.Fatal("failed to leave open channel 1") 291 } 292 293 rs2 := Client.Must(Client.ExecuteCommand(channel2.Id, "/leave")).(*model.CommandResponse) 294 if !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL) { 295 t.Fatal("failed to leave private channel 1") 296 } 297 298 _, err := Client.ExecuteCommand(channel3.Id, "/leave") 299 if err == nil { 300 t.Fatal("should fail leaving direct channel") 301 } 302 303 cdata := Client.Must(Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "")).([]*model.Channel) 304 305 found := false 306 for _, c := range cdata { 307 if c.Id == channel1.Id || c.Id == channel2.Id { 308 found = true 309 } 310 } 311 312 if found { 313 t.Fatal("did not leave right channels") 314 } 315 316 for _, c := range cdata { 317 if c.Name == model.DEFAULT_CHANNEL { 318 if _, err := Client.RemoveUserFromChannel(c.Id, th.BasicUser.Id); err == nil { 319 t.Fatal("should have errored on leaving default channel") 320 } 321 break 322 } 323 } 324 } 325 326 func TestLogoutTestCommand(t *testing.T) { 327 th := Setup().InitBasic() 328 defer th.TearDown() 329 330 th.Client.Must(th.Client.ExecuteCommand(th.BasicChannel.Id, "/logout")) 331 } 332 333 func TestMeCommand(t *testing.T) { 334 th := Setup().InitBasic() 335 defer th.TearDown() 336 337 Client := th.Client 338 channel := th.BasicChannel 339 340 testString := "/me hello" 341 342 r1 := Client.Must(Client.ExecuteCommand(channel.Id, testString)).(*model.CommandResponse) 343 if r1 == nil { 344 t.Fatal("Command failed to execute") 345 } 346 347 time.Sleep(100 * time.Millisecond) 348 349 p1 := Client.Must(Client.GetPostsForChannel(channel.Id, 0, 2, "")).(*model.PostList) 350 if len(p1.Order) != 2 { 351 t.Fatal("Command failed to send") 352 } else { 353 if p1.Posts[p1.Order[0]].Message != `*hello*` { 354 t.Log(p1.Posts[p1.Order[0]].Message) 355 t.Fatal("invalid shrug response") 356 } 357 } 358 } 359 360 func TestMsgCommands(t *testing.T) { 361 th := Setup().InitBasic() 362 defer th.TearDown() 363 364 Client := th.Client 365 team := th.BasicTeam 366 user1 := th.BasicUser 367 user2 := th.BasicUser2 368 user3 := th.CreateUser() 369 th.LinkUserToTeam(user3, team) 370 371 Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user2.Id)) 372 Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user3.Id)) 373 374 rs1 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+user2.Username)).(*model.CommandResponse) 375 if !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) && !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id) { 376 t.Fatal("failed to create direct channel") 377 } 378 379 rs2 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+user3.Username+" foobar")).(*model.CommandResponse) 380 if !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user3.Id) && !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+user3.Id+"__"+user1.Id) { 381 t.Fatal("failed to create second direct channel") 382 } 383 if result := Client.Must(Client.SearchPosts(th.BasicTeam.Id, "foobar", false)).(*model.PostList); len(result.Order) == 0 { 384 t.Fatalf("post did not get sent to direct message") 385 } 386 387 rs3 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+user2.Username)).(*model.CommandResponse) 388 if !strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) && !strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id) { 389 t.Fatal("failed to go back to existing direct channel") 390 } 391 392 Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+th.BasicUser.Username+" foobar")) 393 Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg junk foobar")) 394 } 395 396 func TestOpenCommands(t *testing.T) { 397 testJoinCommands(t, "open") 398 } 399 400 func TestSearchCommand(t *testing.T) { 401 th := Setup().InitBasic() 402 defer th.TearDown() 403 404 th.Client.Must(th.Client.ExecuteCommand(th.BasicChannel.Id, "/search")) 405 } 406 407 func TestSettingsCommand(t *testing.T) { 408 th := Setup().InitBasic() 409 defer th.TearDown() 410 411 th.Client.Must(th.Client.ExecuteCommand(th.BasicChannel.Id, "/settings")) 412 } 413 414 func TestShortcutsCommand(t *testing.T) { 415 th := Setup().InitBasic() 416 defer th.TearDown() 417 418 th.Client.Must(th.Client.ExecuteCommand(th.BasicChannel.Id, "/shortcuts")) 419 } 420 421 func TestShrugCommand(t *testing.T) { 422 th := Setup().InitBasic() 423 defer th.TearDown() 424 425 Client := th.Client 426 channel := th.BasicChannel 427 428 testString := "/shrug" 429 430 r1 := Client.Must(Client.ExecuteCommand(channel.Id, testString)).(*model.CommandResponse) 431 if r1 == nil { 432 t.Fatal("Command failed to execute") 433 } 434 435 time.Sleep(100 * time.Millisecond) 436 437 p1 := Client.Must(Client.GetPostsForChannel(channel.Id, 0, 2, "")).(*model.PostList) 438 if len(p1.Order) != 2 { 439 t.Fatal("Command failed to send") 440 } else { 441 if p1.Posts[p1.Order[0]].Message != `¯\\\_(ツ)\_/¯` { 442 t.Log(p1.Posts[p1.Order[0]].Message) 443 t.Fatal("invalid shrug response") 444 } 445 } 446 } 447 448 func TestStatusCommands(t *testing.T) { 449 th := Setup().InitBasic() 450 defer th.TearDown() 451 452 commandAndTest(t, th, "away") 453 commandAndTest(t, th, "offline") 454 commandAndTest(t, th, "online") 455 } 456 457 func commandAndTest(t *testing.T, th *TestHelper, status string) { 458 Client := th.Client 459 channel := th.BasicChannel 460 user := th.BasicUser 461 462 r1 := Client.Must(Client.ExecuteCommand(channel.Id, "/"+status)).(*model.CommandResponse) 463 if r1 == nil { 464 t.Fatal("Command failed to execute") 465 } 466 467 time.Sleep(1000 * time.Millisecond) 468 469 rstatus := Client.Must(Client.GetUserStatus(user.Id, "")).(*model.Status) 470 471 if rstatus.Status != status { 472 t.Fatal("Error setting status " + status) 473 } 474 }