[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>
Этот коммит содержится в:
Ben Schumacher
2023-06-06 23:29:29 +02:00
коммит произвёл GitHub
родитель 7116e9267a
Коммит 6c82605df0
140 изменённых файлов: 7516 добавлений и 7333 удалений

Просмотреть файл

@@ -4,6 +4,7 @@
package api4
import (
"context"
"strings"
"testing"
@@ -33,7 +34,7 @@ func TestSaveReaction(t *testing.T) {
}
t.Run("successful-reaction", func(t *testing.T) {
rr, _, err := client.SaveReaction(reaction)
rr, _, err := client.SaveReaction(context.Background(), reaction)
require.NoError(t, err)
require.Equal(t, reaction.UserId, rr.UserId, "UserId did not match")
require.Equal(t, reaction.PostId, rr.PostId, "PostId did not match")
@@ -46,7 +47,7 @@ func TestSaveReaction(t *testing.T) {
})
t.Run("duplicated-reaction", func(t *testing.T) {
_, _, err := client.SaveReaction(reaction)
_, _, err := client.SaveReaction(context.Background(), reaction)
require.NoError(t, err)
reactions, appErr := th.App.GetReactionsForPost(postId)
require.Nil(t, appErr)
@@ -56,7 +57,7 @@ func TestSaveReaction(t *testing.T) {
t.Run("save-second-reaction", func(t *testing.T) {
reaction.EmojiName = "sad"
rr, _, err := client.SaveReaction(reaction)
rr, _, err := client.SaveReaction(context.Background(), reaction)
require.NoError(t, err)
require.Equal(t, rr.EmojiName, reaction.EmojiName, "EmojiName did not match")
@@ -68,7 +69,7 @@ func TestSaveReaction(t *testing.T) {
t.Run("saving-special-case", func(t *testing.T) {
reaction.EmojiName = "+1"
rr, _, err := client.SaveReaction(reaction)
rr, _, err := client.SaveReaction(context.Background(), reaction)
require.NoError(t, err)
require.Equal(t, reaction.EmojiName, rr.EmojiName, "EmojiName did not match")
@@ -80,7 +81,7 @@ func TestSaveReaction(t *testing.T) {
t.Run("react-to-not-existing-post-id", func(t *testing.T) {
reaction.PostId = GenerateTestId()
_, resp, err := client.SaveReaction(reaction)
_, resp, err := client.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
@@ -88,7 +89,7 @@ func TestSaveReaction(t *testing.T) {
t.Run("react-to-not-valid-post-id", func(t *testing.T) {
reaction.PostId = "junk"
_, resp, err := client.SaveReaction(reaction)
_, resp, err := client.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
})
@@ -97,7 +98,7 @@ func TestSaveReaction(t *testing.T) {
reaction.PostId = postId
reaction.UserId = GenerateTestId()
_, resp, err := client.SaveReaction(reaction)
_, resp, err := client.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
@@ -105,7 +106,7 @@ func TestSaveReaction(t *testing.T) {
t.Run("react-as-not-valid-user-id", func(t *testing.T) {
reaction.UserId = "junk"
_, resp, err := client.SaveReaction(reaction)
_, resp, err := client.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
})
@@ -114,7 +115,7 @@ func TestSaveReaction(t *testing.T) {
reaction.UserId = userId
reaction.EmojiName = ""
_, resp, err := client.SaveReaction(reaction)
_, resp, err := client.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
})
@@ -122,7 +123,7 @@ func TestSaveReaction(t *testing.T) {
t.Run("react-as-not-valid-emoji-name", func(t *testing.T) {
reaction.EmojiName = strings.Repeat("a", 65)
_, resp, err := client.SaveReaction(reaction)
_, resp, err := client.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
})
@@ -130,23 +131,23 @@ func TestSaveReaction(t *testing.T) {
t.Run("react-as-other-user", func(t *testing.T) {
reaction.EmojiName = "smile"
otherUser := th.CreateUser()
client.Logout()
client.Login(otherUser.Email, otherUser.Password)
client.Logout(context.Background())
client.Login(context.Background(), otherUser.Email, otherUser.Password)
_, resp, err := client.SaveReaction(reaction)
_, resp, err := client.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
t.Run("react-being-not-logged-in", func(t *testing.T) {
client.Logout()
_, resp, err := client.SaveReaction(reaction)
client.Logout(context.Background())
_, resp, err := client.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckUnauthorizedStatus(t, resp)
})
t.Run("react-as-other-user-being-system-admin", func(t *testing.T) {
_, resp, err := th.SystemAdminClient.SaveReaction(reaction)
_, resp, err := th.SystemAdminClient.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
@@ -155,7 +156,7 @@ func TestSaveReaction(t *testing.T) {
th.LoginBasic()
th.RemovePermissionFromRole(model.PermissionAddReaction.Id, model.ChannelUserRoleId)
_, resp, err := client.SaveReaction(reaction)
_, resp, err := client.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
@@ -180,7 +181,7 @@ func TestSaveReaction(t *testing.T) {
appErr := th.App.DeleteChannel(th.Context, channel, userId)
assert.Nil(t, appErr)
_, resp, err := client.SaveReaction(reaction)
_, resp, err := client.SaveReaction(context.Background(), reaction)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
@@ -235,7 +236,7 @@ func TestGetReactions(t *testing.T) {
}
t.Run("get-reactions", func(t *testing.T) {
rr, _, err := client.GetReactions(postId)
rr, _, err := client.GetReactions(context.Background(), postId)
require.NoError(t, err)
assert.Len(t, rr, 5)
@@ -245,7 +246,7 @@ func TestGetReactions(t *testing.T) {
})
t.Run("get-reactions-of-invalid-post-id", func(t *testing.T) {
rr, resp, err := client.GetReactions("junk")
rr, resp, err := client.GetReactions(context.Background(), "junk")
require.Error(t, err)
CheckBadRequestStatus(t, resp)
@@ -253,21 +254,21 @@ func TestGetReactions(t *testing.T) {
})
t.Run("get-reactions-of-not-existing-post-id", func(t *testing.T) {
_, resp, err := client.GetReactions(GenerateTestId())
_, resp, err := client.GetReactions(context.Background(), GenerateTestId())
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
t.Run("get-reactions-as-anonymous-user", func(t *testing.T) {
client.Logout()
client.Logout(context.Background())
_, resp, err := client.GetReactions(postId)
_, resp, err := client.GetReactions(context.Background(), postId)
require.Error(t, err)
CheckUnauthorizedStatus(t, resp)
})
t.Run("get-reactions-as-system-admin", func(t *testing.T) {
_, _, err := th.SystemAdminClient.GetReactions(postId)
_, _, err := th.SystemAdminClient.GetReactions(context.Background(), postId)
require.NoError(t, err)
})
}
@@ -316,7 +317,7 @@ func TestDeleteReaction(t *testing.T) {
require.Nil(t, appErr)
require.Equal(t, 1, len(reactions), "didn't save reaction correctly")
_, err := client.DeleteReaction(r1)
_, err := client.DeleteReaction(context.Background(), r1)
require.NoError(t, err)
reactions, appErr = th.App.GetReactionsForPost(postId)
@@ -331,7 +332,7 @@ func TestDeleteReaction(t *testing.T) {
require.Nil(t, appErr)
require.Equal(t, len(reactions), 2, "didn't save reactions correctly")
_, err := client.DeleteReaction(r2)
_, err := client.DeleteReaction(context.Background(), r2)
require.NoError(t, err)
reactions, appErr = th.App.GetReactionsForPost(postId)
@@ -346,7 +347,7 @@ func TestDeleteReaction(t *testing.T) {
require.Nil(t, appErr)
require.Equal(t, 2, len(reactions), "didn't save reactions correctly")
_, err := client.DeleteReaction(r3)
_, err := client.DeleteReaction(context.Background(), r3)
require.NoError(t, err)
reactions, appErr = th.App.GetReactionsForPost(postId)
@@ -364,7 +365,7 @@ func TestDeleteReaction(t *testing.T) {
th.LoginBasic()
resp, err := client.DeleteReaction(r4)
resp, err := client.DeleteReaction(context.Background(), r4)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
@@ -375,7 +376,7 @@ func TestDeleteReaction(t *testing.T) {
t.Run("delete-reaction-from-not-existing-post-id", func(t *testing.T) {
r1.PostId = GenerateTestId()
resp, err := client.DeleteReaction(r1)
resp, err := client.DeleteReaction(context.Background(), r1)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
@@ -383,7 +384,7 @@ func TestDeleteReaction(t *testing.T) {
t.Run("delete-reaction-from-not-valid-post-id", func(t *testing.T) {
r1.PostId = "junk"
resp, err := client.DeleteReaction(r1)
resp, err := client.DeleteReaction(context.Background(), r1)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
})
@@ -392,7 +393,7 @@ func TestDeleteReaction(t *testing.T) {
r1.PostId = postId
r1.UserId = GenerateTestId()
resp, err := client.DeleteReaction(r1)
resp, err := client.DeleteReaction(context.Background(), r1)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
@@ -400,7 +401,7 @@ func TestDeleteReaction(t *testing.T) {
t.Run("delete-reaction-from-not-valid-user-id", func(t *testing.T) {
r1.UserId = "junk"
resp, err := client.DeleteReaction(r1)
resp, err := client.DeleteReaction(context.Background(), r1)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
})
@@ -409,7 +410,7 @@ func TestDeleteReaction(t *testing.T) {
r1.UserId = userId
r1.EmojiName = ""
resp, err := client.DeleteReaction(r1)
resp, err := client.DeleteReaction(context.Background(), r1)
require.Error(t, err)
CheckNotFoundStatus(t, resp)
})
@@ -417,25 +418,25 @@ func TestDeleteReaction(t *testing.T) {
t.Run("delete-reaction-with-not-existing-name", func(t *testing.T) {
r1.EmojiName = strings.Repeat("a", 65)
resp, err := client.DeleteReaction(r1)
resp, err := client.DeleteReaction(context.Background(), r1)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
})
t.Run("delete-reaction-as-anonymous-user", func(t *testing.T) {
client.Logout()
client.Logout(context.Background())
r1.EmojiName = "smile"
resp, err := client.DeleteReaction(r1)
resp, err := client.DeleteReaction(context.Background(), r1)
require.Error(t, err)
CheckUnauthorizedStatus(t, resp)
})
t.Run("delete-reaction-as-system-admin", func(t *testing.T) {
_, err := th.SystemAdminClient.DeleteReaction(r1)
_, err := th.SystemAdminClient.DeleteReaction(context.Background(), r1)
require.NoError(t, err)
_, err = th.SystemAdminClient.DeleteReaction(r4)
_, err = th.SystemAdminClient.DeleteReaction(context.Background(), r4)
require.NoError(t, err)
reactions, appErr := th.App.GetReactionsForPost(postId)
@@ -449,7 +450,7 @@ func TestDeleteReaction(t *testing.T) {
th.RemovePermissionFromRole(model.PermissionRemoveReaction.Id, model.ChannelUserRoleId)
th.App.SaveReactionForPost(th.Context, r1)
resp, err := client.DeleteReaction(r1)
resp, err := client.DeleteReaction(context.Background(), r1)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
@@ -463,7 +464,7 @@ func TestDeleteReaction(t *testing.T) {
th.RemovePermissionFromRole(model.PermissionRemoveOthersReactions.Id, model.SystemAdminRoleId)
th.App.SaveReactionForPost(th.Context, r1)
resp, err := th.SystemAdminClient.DeleteReaction(r1)
resp, err := th.SystemAdminClient.DeleteReaction(context.Background(), r1)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
@@ -485,7 +486,7 @@ func TestDeleteReaction(t *testing.T) {
EmojiName: "smile",
}
r1, _, err := client.SaveReaction(reaction)
r1, _, err := client.SaveReaction(context.Background(), reaction)
require.NoError(t, err)
reactions, appErr := th.App.GetReactionsForPost(postId)
@@ -495,7 +496,7 @@ func TestDeleteReaction(t *testing.T) {
appErr = th.App.DeleteChannel(th.Context, channel, userId)
assert.Nil(t, appErr)
_, resp, err := client.SaveReaction(r1)
_, resp, err := client.SaveReaction(context.Background(), r1)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
@@ -518,11 +519,11 @@ func TestGetBulkReactions(t *testing.T) {
post4 := &model.Post{UserId: user2Id, ChannelId: th.BasicChannel.Id, Message: "zz" + model.NewId() + "a"}
post5 := &model.Post{UserId: user2Id, ChannelId: th.BasicChannel.Id, Message: "zz" + model.NewId() + "a"}
post1, _, _ = client.CreatePost(post1)
post2, _, _ = client.CreatePost(post2)
post3, _, _ = client.CreatePost(post3)
post4, _, _ = client.CreatePost(post4)
post5, _, _ = client.CreatePost(post5)
post1, _, _ = client.CreatePost(context.Background(), post1)
post2, _, _ = client.CreatePost(context.Background(), post2)
post3, _, _ = client.CreatePost(context.Background(), post3)
post4, _, _ = client.CreatePost(context.Background(), post4)
post5, _, _ = client.CreatePost(context.Background(), post5)
expectedPostIdsReactionsMap := make(map[string][]*model.Reaction)
expectedPostIdsReactionsMap[post1.Id] = []*model.Reaction{}
@@ -564,7 +565,7 @@ func TestGetBulkReactions(t *testing.T) {
postIds := []string{post1.Id, post2.Id, post3.Id, post4.Id, post5.Id}
t.Run("get-reactions", func(t *testing.T) {
postIdsReactionsMap, _, err := client.GetBulkReactions(postIds)
postIdsReactionsMap, _, err := client.GetBulkReactions(context.Background(), postIds)
require.NoError(t, err)
assert.ElementsMatch(t, expectedPostIdsReactionsMap[post1.Id], postIdsReactionsMap[post1.Id])
@@ -577,9 +578,9 @@ func TestGetBulkReactions(t *testing.T) {
})
t.Run("get-reactions-as-anonymous-user", func(t *testing.T) {
client.Logout()
client.Logout(context.Background())
_, resp, err := client.GetBulkReactions(postIds)
_, resp, err := client.GetBulkReactions(context.Background(), postIds)
require.Error(t, err)
CheckUnauthorizedStatus(t, resp)
})