From a22f02c8672bd502e6fde417cccee64a174aa56e Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Thu, 6 Aug 2020 16:18:47 -0700 Subject: [PATCH] MM-27412 Fixing /test command. (#15155) * Fixing /test command. * Add error handling to channels command. * Switch to using app instead of client in some places. * Fix lint being confused. * Join channels automatically. Co-authored-by: Mattermod --- app/auto_channels.go | 19 +++++++++++-------- app/auto_environment.go | 4 ++-- app/auto_posts.go | 26 +++++++++++++++----------- app/command_loadtest.go | 35 +++++++---------------------------- 4 files changed, 35 insertions(+), 49 deletions(-) diff --git a/app/auto_channels.go b/app/auto_channels.go index f0e1fc66ed..4378cbdcb9 100644 --- a/app/auto_channels.go +++ b/app/auto_channels.go @@ -9,7 +9,8 @@ import ( ) type AutoChannelCreator struct { - client *model.Client4 + a *App + userId string team *model.Team Fuzzy bool DisplayNameLen utils.Range @@ -19,10 +20,11 @@ type AutoChannelCreator struct { ChannelType string } -func NewAutoChannelCreator(client *model.Client4, team *model.Team) *AutoChannelCreator { +func NewAutoChannelCreator(a *App, team *model.Team, userId string) *AutoChannelCreator { return &AutoChannelCreator{ - client: client, + a: a, team: team, + userId: userId, Fuzzy: false, DisplayNameLen: CHANNEL_DISPLAY_NAME_LEN, DisplayNameCharset: utils.ALPHANUMERIC, @@ -45,12 +47,13 @@ func (cfg *AutoChannelCreator) createRandomChannel() (*model.Channel, error) { TeamId: cfg.team.Id, DisplayName: displayName, Name: name, - Type: cfg.ChannelType} + Type: cfg.ChannelType, + CreatorId: cfg.userId, + } - println(cfg.client.GetTeamRoute(cfg.team.Id)) - channel, resp := cfg.client.CreateChannel(channel) - if resp.Error != nil { - return nil, resp.Error + channel, err := cfg.a.CreateChannel(channel, true) + if err != nil { + return nil, err } return channel, nil } diff --git a/app/auto_environment.go b/app/auto_environment.go index a04c5a33f6..c94d50be46 100644 --- a/app/auto_environment.go +++ b/app/auto_environment.go @@ -65,7 +65,7 @@ func CreateTestEnvironmentInTeam(a *App, client *model.Client4, team *model.Team usernames[i] = user.Username } - channelCreator := NewAutoChannelCreator(client, team) + channelCreator := NewAutoChannelCreator(a, team, users[0].Id) channelCreator.Fuzzy = fuzzy channels, err := channelCreator.CreateTestChannels(rangeChannels) if err != nil { @@ -97,7 +97,7 @@ func CreateTestEnvironmentInTeam(a *App, client *model.Client4, team *model.Team } for i, channel := range channels { - postCreator := NewAutoPostCreator(client, channel.Id) + postCreator := NewAutoPostCreator(a, channel.Id, user.Id) postCreator.HasImage = i < numImages postCreator.Users = usernames postCreator.Fuzzy = fuzzy diff --git a/app/auto_posts.go b/app/auto_posts.go index 43eaa28845..67d82acd2c 100644 --- a/app/auto_posts.go +++ b/app/auto_posts.go @@ -15,8 +15,9 @@ import ( ) type AutoPostCreator struct { - client *model.Client4 + a *App channelid string + userid string Fuzzy bool TextLength utils.Range HasImage bool @@ -27,10 +28,11 @@ type AutoPostCreator struct { } // Automatic poster used for testing -func NewAutoPostCreator(client *model.Client4, channelid string) *AutoPostCreator { +func NewAutoPostCreator(a *App, channelid, userid string) *AutoPostCreator { return &AutoPostCreator{ - client: client, + a: a, channelid: channelid, + userid: userid, Fuzzy: false, TextLength: utils.Range{Begin: 100, End: 200}, HasImage: false, @@ -57,12 +59,12 @@ func (cfg *AutoPostCreator) UploadTestFile() ([]string, error) { return nil, err } - fileResp, resp := cfg.client.UploadFile(data.Bytes(), cfg.channelid, filename) - if resp.Error != nil { - return nil, resp.Error + fileResp, err2 := cfg.a.UploadFile(data.Bytes(), cfg.channelid, filename) + if err2 != nil { + return nil, err2 } - return []string{fileResp.FileInfos[0].Id}, nil + return []string{fileResp.Id}, nil } func (cfg *AutoPostCreator) CreateRandomPost() (*model.Post, error) { @@ -88,13 +90,15 @@ func (cfg *AutoPostCreator) CreateRandomPostNested(parentId, rootId string) (*mo post := &model.Post{ ChannelId: cfg.channelid, + UserId: cfg.userid, ParentId: parentId, RootId: rootId, Message: postText, - FileIds: fileIds} - rpost, resp := cfg.client.CreatePost(post) - if resp.Error != nil { - return nil, resp.Error + FileIds: fileIds, + } + rpost, err := cfg.a.CreatePostMissingChannel(post, true) + if err != nil { + return nil, err } return rpost, nil } diff --git a/app/command_loadtest.go b/app/command_loadtest.go index f343e399ea..fc494e288b 100644 --- a/app/command_loadtest.go +++ b/app/command_loadtest.go @@ -207,11 +207,6 @@ func (me *LoadTestProvider) SetupCommand(a *App, args *model.CommandArgs, messag } } client := model.NewAPIv4Client(args.SiteURL) - sessions, err := a.GetSessions(args.UserId) - if err != nil || len(sessions) == 0 { - return &model.CommandResponse{Text: "Failed to get sessions.", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}, err - } - client.SetToken(sessions[0].Token) if doTeams { if err := a.CreateBasicUser(client); err != nil { @@ -321,15 +316,11 @@ func (me *LoadTestProvider) ChannelsCommand(a *App, args *model.CommandArgs, mes return &model.CommandResponse{Text: "Failed to add channels", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}, err } - client := model.NewAPIv4Client(args.SiteURL) - sessions, err := a.GetSessions(args.UserId) - if err != nil || len(sessions) == 0 { - return &model.CommandResponse{Text: "Failed to get sessions.", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}, err - } - client.SetToken(sessions[0].Token) - channelCreator := NewAutoChannelCreator(client, team) + channelCreator := NewAutoChannelCreator(a, team, args.UserId) channelCreator.Fuzzy = doFuzz - channelCreator.CreateTestChannels(channelsr) + if _, err := channelCreator.CreateTestChannels(channelsr); err != nil { + return &model.CommandResponse{Text: "Failed to create test channels: " + err.Error(), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}, err + } return &model.CommandResponse{Text: "Added channels", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}, nil } @@ -346,18 +337,12 @@ func (me *LoadTestProvider) ThreadedPostCommand(a *App, args *model.CommandArgs, } } - client := model.NewAPIv4Client(args.SiteURL) - sessions, err := a.GetSessions(args.UserId) - if err != nil || len(sessions) == 0 { - return &model.CommandResponse{Text: "Failed to get sessions.", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}, err - } - client.MockSession(sessions[0].Token) - testPoster := NewAutoPostCreator(client, args.ChannelId) + testPoster := NewAutoPostCreator(a, args.ChannelId, args.UserId) testPoster.Fuzzy = true testPoster.Users = usernames rpost, err2 := testPoster.CreateRandomPost() if err2 != nil { - return &model.CommandResponse{Text: "Failed to create a post", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}, err + return &model.CommandResponse{Text: "Failed to create a post", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}, err2 } for i := 0; i < 1000; i++ { testPoster.CreateRandomPostNested(rpost.Id, rpost.Id) @@ -399,13 +384,7 @@ func (me *LoadTestProvider) PostsCommand(a *App, args *model.CommandArgs, messag } } - client := model.NewAPIv4Client(args.SiteURL) - sessions, err := a.GetSessions(args.UserId) - if err != nil || len(sessions) == 0 { - return &model.CommandResponse{Text: "Failed to get sessions.", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}, err - } - client.SetToken(sessions[0].Token) - testPoster := NewAutoPostCreator(client, args.ChannelId) + testPoster := NewAutoPostCreator(a, args.ChannelId, args.UserId) testPoster.Fuzzy = doFuzz testPoster.Users = usernames