diff --git a/api4/commands_test.go b/api4/commands_test.go index 5ba0e5f918..7dce542648 100644 --- a/api4/commands_test.go +++ b/api4/commands_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/mattermost/mattermost-server/model" + "github.com/stretchr/testify/require" ) func TestEchoCommand(t *testing.T) { @@ -20,20 +21,16 @@ func TestEchoCommand(t *testing.T) { echoTestString := "/echo test" - if r1 := Client.Must(Client.ExecuteCommand(channel1.Id, echoTestString)).(*model.CommandResponse); r1 == nil { - t.Fatal("Echo command failed to execute") - } + r1 := Client.Must(Client.ExecuteCommand(channel1.Id, echoTestString)).(*model.CommandResponse) + require.NotNil(t, r1, "Echo command failed to execute") - if r1 := Client.Must(Client.ExecuteCommand(channel1.Id, "/echo ")).(*model.CommandResponse); r1 == nil { - t.Fatal("Echo command failed to execute") - } + r1 = Client.Must(Client.ExecuteCommand(channel1.Id, "/echo ")).(*model.CommandResponse) + require.NotNil(t, r1, "Echo command failed to execute") time.Sleep(100 * time.Millisecond) p1 := Client.Must(Client.GetPostsForChannel(channel1.Id, 0, 2, "")).(*model.PostList) - if len(p1.Order) != 2 { - t.Fatal("Echo command failed to send") - } + require.Len(t, p1.Order, 2, "Echo command failed to send") } func TestGroupmsgCommands(t *testing.T) { @@ -57,25 +54,18 @@ func TestGroupmsgCommands(t *testing.T) { rs1 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+","+user3.Username)).(*model.CommandResponse) group1 := model.GetGroupNameFromUserIds([]string{user1.Id, user2.Id, user3.Id}) - - if !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+group1) { - t.Fatal("failed to create group channel") - } + require.True(t, strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+group1), "failed to create group channel") rs2 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user3.Username+","+user4.Username+" foobar")).(*model.CommandResponse) group2 := model.GetGroupNameFromUserIds([]string{user1.Id, user3.Id, user4.Id}) - if !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+group2) { - t.Fatal("failed to create second direct channel") - } - if result := Client.Must(Client.SearchPosts(team.Id, "foobar", false)).(*model.PostList); len(result.Order) == 0 { - t.Fatal("post did not get sent to direct message") - } + require.True(t, strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+group2), "failed to create second direct channel") + + result := Client.Must(Client.SearchPosts(team.Id, "foobar", false)).(*model.PostList) + require.NotEqual(t, 0, len(result.Order), "post did not get sent to direct message") rs3 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+","+user3.Username)).(*model.CommandResponse) - if !strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+group1) { - t.Fatal("failed to go back to existing group channel") - } + require.True(t, strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+group1), "failed to go back to existing group channel") Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/groupmsg "+user2.Username+" foobar")) 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")) @@ -91,19 +81,13 @@ func TestInvitePeopleCommand(t *testing.T) { channel := th.BasicChannel r1 := Client.Must(Client.ExecuteCommand(channel.Id, "/invite_people test@example.com")).(*model.CommandResponse) - if r1 == nil { - t.Fatal("Command failed to execute") - } + require.NotNil(t, r1, "Command failed to execute") r2 := Client.Must(Client.ExecuteCommand(channel.Id, "/invite_people test1@example.com test2@example.com")).(*model.CommandResponse) - if r2 == nil { - t.Fatal("Command failed to execute") - } + require.NotNil(t, r2, "Command failed to execute") r3 := Client.Must(Client.ExecuteCommand(channel.Id, "/invite_people")).(*model.CommandResponse) - if r3 == nil { - t.Fatal("Command failed to execute") - } + require.NotNil(t, r3, "Command failed to execute") } // also used to test /open (see command_open_test.go) @@ -129,14 +113,10 @@ func testJoinCommands(t *testing.T, alias string) { channel3 := Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user2.Id)).(*model.Channel) rs5 := Client.Must(Client.ExecuteCommand(channel0.Id, "/"+alias+" "+channel2.Name)).(*model.CommandResponse) - if !strings.HasSuffix(rs5.GotoLocation, "/"+team.Name+"/channels/"+channel2.Name) { - t.Fatal("failed to join channel") - } + require.True(t, strings.HasSuffix(rs5.GotoLocation, "/"+team.Name+"/channels/"+channel2.Name), "failed to join channel") rs6 := Client.Must(Client.ExecuteCommand(channel0.Id, "/"+alias+" "+channel3.Name)).(*model.CommandResponse) - if strings.HasSuffix(rs6.GotoLocation, "/"+team.Name+"/channels/"+channel3.Name) { - t.Fatal("should not have joined direct message channel") - } + require.False(t, strings.HasSuffix(rs6.GotoLocation, "/"+team.Name+"/channels/"+channel3.Name), "should not have joined direct message channel") c1 := Client.Must(Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "")).([]*model.Channel) @@ -146,10 +126,7 @@ func testJoinCommands(t *testing.T, alias string) { found = true } } - - if !found { - t.Fatal("did not join channel") - } + require.True(t, found, "did not join channel") } func TestJoinCommands(t *testing.T) { @@ -171,9 +148,7 @@ func TestLoadTestHelpCommands(t *testing.T) { th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true }) rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test help")).(*model.CommandResponse) - if !strings.Contains(rs.Text, "Mattermost testing commands to help") { - t.Fatal(rs.Text) - } + require.True(t, strings.Contains(rs.Text, "Mattermost testing commands to help"), rs.Text) time.Sleep(2 * time.Second) } @@ -193,9 +168,7 @@ func TestLoadTestSetupCommands(t *testing.T) { th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true }) rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test setup fuzz 1 1 1")).(*model.CommandResponse) - if rs.Text != "Created environment" { - t.Fatal(rs.Text) - } + require.Equal(t, "Created environment", rs.Text, rs.Text) time.Sleep(2 * time.Second) } @@ -215,9 +188,7 @@ func TestLoadTestUsersCommands(t *testing.T) { th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true }) rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test users fuzz 1 2")).(*model.CommandResponse) - if rs.Text != "Added users" { - t.Fatal(rs.Text) - } + require.Equal(t, "Added users", rs.Text, rs.Text) time.Sleep(2 * time.Second) } @@ -237,9 +208,7 @@ func TestLoadTestChannelsCommands(t *testing.T) { th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true }) rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test channels fuzz 1 2")).(*model.CommandResponse) - if rs.Text != "Added channels" { - t.Fatal(rs.Text) - } + require.Equal(t, "Added channels", rs.Text, rs.Text) time.Sleep(2 * time.Second) } @@ -259,9 +228,7 @@ func TestLoadTestPostsCommands(t *testing.T) { th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableTesting = true }) rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test posts fuzz 2 3 2")).(*model.CommandResponse) - if rs.Text != "Added posts" { - t.Fatal(rs.Text) - } + require.Equal(t, "Added posts", rs.Text, rs.Text) time.Sleep(2 * time.Second) } @@ -286,19 +253,13 @@ func TestLeaveCommands(t *testing.T) { channel3 := Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user2.Id)).(*model.Channel) rs1 := Client.Must(Client.ExecuteCommand(channel1.Id, "/leave")).(*model.CommandResponse) - if !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL) { - t.Fatal("failed to leave open channel 1") - } + require.True(t, strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL), "failed to leave open channel 1") rs2 := Client.Must(Client.ExecuteCommand(channel2.Id, "/leave")).(*model.CommandResponse) - if !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL) { - t.Fatal("failed to leave private channel 1") - } + require.True(t, strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL), "failed to leave private channel 1") _, err := Client.ExecuteCommand(channel3.Id, "/leave") - if err == nil { - t.Fatal("should fail leaving direct channel") - } + require.NotNil(t, err, "should fail leaving direct channel") cdata := Client.Must(Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "")).([]*model.Channel) @@ -308,16 +269,12 @@ func TestLeaveCommands(t *testing.T) { found = true } } - - if found { - t.Fatal("did not leave right channels") - } + require.False(t, found, "did not leave right channels") for _, c := range cdata { if c.Name == model.DEFAULT_CHANNEL { - if _, err := Client.RemoveUserFromChannel(c.Id, th.BasicUser.Id); err == nil { - t.Fatal("should have errored on leaving default channel") - } + _, err := Client.RemoveUserFromChannel(c.Id, th.BasicUser.Id) + require.NotNil(t, err, "should have errored on leaving default channel") break } } @@ -340,28 +297,19 @@ func TestMeCommand(t *testing.T) { testString := "/me hello" r1 := Client.Must(Client.ExecuteCommand(channel.Id, testString)).(*model.CommandResponse) - if r1 == nil { - t.Fatal("Command failed to execute") - } + require.NotNil(t, r1, "Command failed to execute") time.Sleep(100 * time.Millisecond) p1 := Client.Must(Client.GetPostsForChannel(channel.Id, 0, 2, "")).(*model.PostList) - if len(p1.Order) != 2 { - t.Fatal("Command failed to send") - } else { - pt := p1.Posts[p1.Order[0]].Type - if pt != model.POST_ME { - t.Log(pt) - t.Fatalf("invalid post type, got '%s', wanted '%s'", pt, model.POST_ME) - } - msg := p1.Posts[p1.Order[0]].Message - want := "*hello*" - if msg != want { - t.Log(msg) - t.Fatalf("invalid me response message, got '%s', wanted '%s'", msg, want) - } - } + require.Len(t, p1.Order, 2, "Command failed to send") + + pt := p1.Posts[p1.Order[0]].Type + require.Equal(t, model.POST_ME, pt, "invalid post type") + + msg := p1.Posts[p1.Order[0]].Message + want := "*hello*" + require.Equal(t, want, msg, "invalid me response") } func TestMsgCommands(t *testing.T) { @@ -379,22 +327,25 @@ func TestMsgCommands(t *testing.T) { Client.Must(Client.CreateDirectChannel(th.BasicUser.Id, user3.Id)) rs1 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+user2.Username)).(*model.CommandResponse) - if !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) && !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id) { - t.Fatal("failed to create direct channel") - } + require.Condition(t, func() bool { + return strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) || + strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id) + }, "failed to create direct channel") rs2 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+user3.Username+" foobar")).(*model.CommandResponse) - if !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user3.Id) && !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+user3.Id+"__"+user1.Id) { - t.Fatal("failed to create second direct channel") - } - if result := Client.Must(Client.SearchPosts(th.BasicTeam.Id, "foobar", false)).(*model.PostList); len(result.Order) == 0 { - t.Fatalf("post did not get sent to direct message") - } + require.Condition(t, func() bool { + return strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user3.Id) || + strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+user3.Id+"__"+user1.Id) + }, "failed to create second direct channel") + + result := Client.Must(Client.SearchPosts(th.BasicTeam.Id, "foobar", false)).(*model.PostList) + require.NotEqual(t, 0, len(result.Order), "post did not get sent to direct message") rs3 := Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+user2.Username)).(*model.CommandResponse) - if !strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) && !strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id) { - t.Fatal("failed to go back to existing direct channel") - } + require.Condition(t, func() bool { + return strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) || + strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id) + }, "failed to go back to existing direct channel") Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg "+th.BasicUser.Username+" foobar")) Client.Must(Client.ExecuteCommand(th.BasicChannel.Id, "/msg junk foobar")) @@ -435,21 +386,13 @@ func TestShrugCommand(t *testing.T) { testString := "/shrug" r1 := Client.Must(Client.ExecuteCommand(channel.Id, testString)).(*model.CommandResponse) - if r1 == nil { - t.Fatal("Command failed to execute") - } + require.NotNil(t, r1, "Command failed to execute") time.Sleep(100 * time.Millisecond) p1 := Client.Must(Client.GetPostsForChannel(channel.Id, 0, 2, "")).(*model.PostList) - if len(p1.Order) != 2 { - t.Fatal("Command failed to send") - } else { - if p1.Posts[p1.Order[0]].Message != `¯\\\_(ツ)\_/¯` { - t.Log(p1.Posts[p1.Order[0]].Message) - t.Fatal("invalid shrug response") - } - } + require.Len(t, p1.Order, 2, "Command failed to send") + require.Equal(t, `¯\\\_(ツ)\_/¯`, p1.Posts[p1.Order[0]].Message, "invalid shrug response") } func TestStatusCommands(t *testing.T) { @@ -467,15 +410,10 @@ func commandAndTest(t *testing.T, th *TestHelper, status string) { user := th.BasicUser r1 := Client.Must(Client.ExecuteCommand(channel.Id, "/"+status)).(*model.CommandResponse) - if r1 == nil { - t.Fatal("Command failed to execute") - } + require.NotEqual(t, "Command failed to execute", r1) time.Sleep(1000 * time.Millisecond) rstatus := Client.Must(Client.GetUserStatus(user.Id, "")).(*model.Status) - - if rstatus.Status != status { - t.Fatal("Error setting status " + status) - } + require.Equal(t, status, rstatus.Status, "Error setting status") }