[MM-49989] Pass a context.Context to Client4 methods (#22922)
* Migrate all method in model/client4.go to accept a context.Context * Fix th.*Client * Fix remaining issues * Empty commit to triger CI * Fix test * Add cancellation test * Test that returned error is context.Canceled * Fix bad merge * Update mmctl code --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
7116e9267a
Коммит
6c82605df0
@@ -4,6 +4,7 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
@@ -27,7 +28,7 @@ func (s *MmctlUnitTestSuite) TestBotCreateCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
CreateBot(&mockBot).
|
||||
CreateBot(context.Background(), &mockBot).
|
||||
Return(&mockBot, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
@@ -51,25 +52,25 @@ func (s *MmctlUnitTestSuite) TestBotCreateCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
CreateBot(&mockBot).
|
||||
CreateBot(context.Background(), &mockBot).
|
||||
Return(&mockBot, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("no user found with the given email")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(model.UserFromBot(&mockBot), &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
CreateUserAccessToken(mockBot.UserId, "autogenerated").
|
||||
CreateUserAccessToken(context.Background(), mockBot.UserId, "autogenerated").
|
||||
Return(&mockToken, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
@@ -88,7 +89,7 @@ func (s *MmctlUnitTestSuite) TestBotCreateCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
CreateBot(&mockBot).
|
||||
CreateBot(context.Background(), &mockBot).
|
||||
Return(nil, &model.Response{}, errors.New("some-error")).
|
||||
Times(1)
|
||||
|
||||
@@ -117,19 +118,19 @@ func (s *MmctlUnitTestSuite) TestBotUpdateCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(&mockUser, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
PatchBot(mockUser.Id, gomock.Any()).
|
||||
PatchBot(context.Background(), mockUser.Id, gomock.Any()).
|
||||
Return(&mockBot, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
@@ -149,19 +150,19 @@ func (s *MmctlUnitTestSuite) TestBotUpdateCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUser(botArg, "").
|
||||
GetUser(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -184,19 +185,19 @@ func (s *MmctlUnitTestSuite) TestBotUpdateCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(&mockUser, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
PatchBot(mockUser.Id, gomock.Any()).
|
||||
PatchBot(context.Background(), mockUser.Id, gomock.Any()).
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -220,13 +221,13 @@ func (s *MmctlUnitTestSuite) TestBotListCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetBotsIncludeDeleted(0, 200, "").
|
||||
GetBotsIncludeDeleted(context.Background(), 0, 200, "").
|
||||
Return([]*model.Bot{&mockBot}, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUsersByIds([]string{mockBot.OwnerId}).
|
||||
GetUsersByIds(context.Background(), []string{mockBot.OwnerId}).
|
||||
Return([]*model.User{&mockUser}, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
@@ -246,7 +247,7 @@ func (s *MmctlUnitTestSuite) TestBotListCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetBotsIncludeDeleted(0, 200, "").
|
||||
GetBotsIncludeDeleted(context.Background(), 0, 200, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -268,13 +269,13 @@ func (s *MmctlUnitTestSuite) TestBotListCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetBotsOrphaned(0, 200, "").
|
||||
GetBotsOrphaned(context.Background(), 0, 200, "").
|
||||
Return([]*model.Bot{&mockBot}, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUsersByIds([]string{mockBot.OwnerId}).
|
||||
GetUsersByIds(context.Background(), []string{mockBot.OwnerId}).
|
||||
Return([]*model.User{&mockUser}, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
@@ -294,7 +295,7 @@ func (s *MmctlUnitTestSuite) TestBotListCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetBotsOrphaned(0, 200, "").
|
||||
GetBotsOrphaned(context.Background(), 0, 200, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -316,13 +317,13 @@ func (s *MmctlUnitTestSuite) TestBotListCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetBots(0, 200, "").
|
||||
GetBots(context.Background(), 0, 200, "").
|
||||
Return([]*model.Bot{&mockBot}, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUsersByIds([]string{mockBot.OwnerId}).
|
||||
GetUsersByIds(context.Background(), []string{mockBot.OwnerId}).
|
||||
Return([]*model.User{&mockUser}, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
@@ -343,13 +344,13 @@ func (s *MmctlUnitTestSuite) TestBotListCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetBots(0, 200, "").
|
||||
GetBots(context.Background(), 0, 200, "").
|
||||
Return([]*model.Bot{&mockBot}, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUsersByIds([]string{mockBot.OwnerId}).
|
||||
GetUsersByIds(context.Background(), []string{mockBot.OwnerId}).
|
||||
Return([]*model.User{}, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
@@ -369,7 +370,7 @@ func (s *MmctlUnitTestSuite) TestBotListCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetBots(0, 200, "").
|
||||
GetBots(context.Background(), 0, 200, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -390,13 +391,13 @@ func (s *MmctlUnitTestSuite) TestBotListCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetBots(0, 200, "").
|
||||
GetBots(context.Background(), 0, 200, "").
|
||||
Return([]*model.Bot{&mockBot}, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUsersByIds([]string{mockBot.OwnerId}).
|
||||
GetUsersByIds(context.Background(), []string{mockBot.OwnerId}).
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -418,19 +419,19 @@ func (s *MmctlUnitTestSuite) TestBotDisableCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(&mockUser, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
DisableBot(mockUser.Id).
|
||||
DisableBot(context.Background(), mockUser.Id).
|
||||
Return(&mockBot, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
@@ -447,19 +448,19 @@ func (s *MmctlUnitTestSuite) TestBotDisableCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUser(botArg, "").
|
||||
GetUser(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -482,19 +483,19 @@ func (s *MmctlUnitTestSuite) TestBotDisableCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(&mockUser, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
DisableBot(mockUser.Id).
|
||||
DisableBot(context.Background(), mockUser.Id).
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -516,19 +517,19 @@ func (s *MmctlUnitTestSuite) TestBotEnableCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(&mockUser, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
EnableBot(mockUser.Id).
|
||||
EnableBot(context.Background(), mockUser.Id).
|
||||
Return(&mockBot, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
@@ -545,19 +546,19 @@ func (s *MmctlUnitTestSuite) TestBotEnableCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUser(botArg, "").
|
||||
GetUser(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -580,19 +581,19 @@ func (s *MmctlUnitTestSuite) TestBotEnableCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(&mockUser, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
EnableBot(mockUser.Id).
|
||||
EnableBot(context.Background(), mockUser.Id).
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -616,31 +617,31 @@ func (s *MmctlUnitTestSuite) TestBotAssignCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(&mockBotUser, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(userArg, "").
|
||||
GetUserByEmail(context.Background(), userArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(userArg, "").
|
||||
GetUserByUsername(context.Background(), userArg, "").
|
||||
Return(&mockNewOwner, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
AssignBot(mockBotUser.Id, mockNewOwner.Id).
|
||||
AssignBot(context.Background(), mockBotUser.Id, mockNewOwner.Id).
|
||||
Return(&mockBot, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
@@ -658,19 +659,19 @@ func (s *MmctlUnitTestSuite) TestBotAssignCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUser(botArg, "").
|
||||
GetUser(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -690,31 +691,31 @@ func (s *MmctlUnitTestSuite) TestBotAssignCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(&mockBotUser, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(userArg, "").
|
||||
GetUserByUsername(context.Background(), userArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUser(userArg, "").
|
||||
GetUser(context.Background(), userArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(userArg, "").
|
||||
GetUserByEmail(context.Background(), userArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
@@ -735,31 +736,31 @@ func (s *MmctlUnitTestSuite) TestBotAssignCmd() {
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(botArg, "").
|
||||
GetUserByEmail(context.Background(), botArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(botArg, "").
|
||||
GetUserByUsername(context.Background(), botArg, "").
|
||||
Return(&mockBotUser, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByEmail(userArg, "").
|
||||
GetUserByEmail(context.Background(), userArg, "").
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
GetUserByUsername(userArg, "").
|
||||
GetUserByUsername(context.Background(), userArg, "").
|
||||
Return(&mockNewOwner, &model.Response{}, nil).
|
||||
Times(1)
|
||||
|
||||
s.client.
|
||||
EXPECT().
|
||||
AssignBot(mockBotUser.Id, mockNewOwner.Id).
|
||||
AssignBot(context.Background(), mockBotUser.Id, mockNewOwner.Id).
|
||||
Return(nil, &model.Response{}, errors.New("mock error")).
|
||||
Times(1)
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user