From aa47b4633f18eed917c70263d139376f15d8778c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Garc=C3=ADa=20Montoro?= Date: Wed, 25 Mar 2020 10:24:42 +0100 Subject: [PATCH] MM-23508: Fix plugin API's GetPostsForChannel (#14125) Add a test checking the basic behaviour of the function --- app/plugin_api.go | 2 +- app/plugin_api_test.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/plugin_api.go b/app/plugin_api.go index 34697e69af..c1653e426c 100644 --- a/app/plugin_api.go +++ b/app/plugin_api.go @@ -521,7 +521,7 @@ func (api *PluginAPI) GetPostsBefore(channelId, postId string, page, perPage int } func (api *PluginAPI) GetPostsForChannel(channelId string, page, perPage int) (*model.PostList, *model.AppError) { - return api.app.GetPostsPage(model.GetPostsOptions{ChannelId: channelId, Page: perPage, PerPage: page}) + return api.app.GetPostsPage(model.GetPostsOptions{ChannelId: channelId, Page: page, PerPage: perPage}) } func (api *PluginAPI) UpdatePost(post *model.Post) (*model.Post, *model.AppError) { diff --git a/app/plugin_api_test.go b/app/plugin_api_test.go index 737afbf5a7..52b443e22f 100644 --- a/app/plugin_api_test.go +++ b/app/plugin_api_test.go @@ -1409,3 +1409,46 @@ func TestApiMetrics(t *testing.T) { metricsMock.AssertExpectations(t) }) } + +func TestPluginAPIGetPostsForChannel(t *testing.T) { + require := require.New(t) + + th := Setup(t).InitBasic() + defer th.TearDown() + api := th.SetupPluginAPI() + + numPosts := 10 + + // GetPostsForChannel returns posts ordered with the most recent first, so we + // need to invert the expected slice, the oldest post being BasicPost + expectedPosts := make([]*model.Post, numPosts) + expectedPosts[numPosts-1] = th.BasicPost + for i := numPosts - 2; i >= 0; i-- { + expectedPosts[i] = th.CreatePost(th.BasicChannel) + } + // CreatePost does not add Metadata, but initializes the structure. GetPostsForChannel + // returns nil for an empty Metadata, so we need to match that behaviour + for _, post := range expectedPosts { + post.Metadata = nil + } + + postList, err := api.GetPostsForChannel(th.BasicChannel.Id, 0, 0) + require.Nil(err) + require.Nil(postList.ToSlice()) + + postList, err = api.GetPostsForChannel(th.BasicChannel.Id, 0, numPosts/2) + require.Nil(err) + require.Equal(expectedPosts[:numPosts/2], postList.ToSlice()) + + postList, err = api.GetPostsForChannel(th.BasicChannel.Id, 1, numPosts/2) + require.Nil(err) + require.Equal(expectedPosts[numPosts/2:], postList.ToSlice()) + + postList, err = api.GetPostsForChannel(th.BasicChannel.Id, 2, numPosts/2) + require.Nil(err) + require.Nil(postList.ToSlice()) + + postList, err = api.GetPostsForChannel(th.BasicChannel.Id, 0, numPosts+1) + require.Nil(err) + require.Equal(expectedPosts, postList.ToSlice()) +}