MM-25071: local mode for getPostsForChannel (#14848)

Этот коммит содержится в:
Ashish Bhate
2020-06-22 14:06:37 +05:30
коммит произвёл GitHub
родитель e5addef19b
Коммит 0e714f350a
3 изменённых файлов: 102 добавлений и 95 удалений

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

@@ -305,6 +305,7 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app.
api.BaseRoutes.Posts = api.BaseRoutes.ApiRoot.PathPrefix("/posts").Subrouter() api.BaseRoutes.Posts = api.BaseRoutes.ApiRoot.PathPrefix("/posts").Subrouter()
api.BaseRoutes.Post = api.BaseRoutes.Posts.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter() api.BaseRoutes.Post = api.BaseRoutes.Posts.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter()
api.BaseRoutes.PostsForChannel = api.BaseRoutes.Channel.PathPrefix("/posts").Subrouter()
api.InitUserLocal() api.InitUserLocal()
api.InitTeamLocal() api.InitTeamLocal()

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

@@ -5,4 +5,6 @@ package api4
func (api *API) InitPostLocal() { func (api *API) InitPostLocal() {
api.BaseRoutes.Post.Handle("", api.ApiLocal(getPost)).Methods("GET") api.BaseRoutes.Post.Handle("", api.ApiLocal(getPost)).Methods("GET")
api.BaseRoutes.PostsForChannel.Handle("", api.ApiLocal(getPostsForChannel)).Methods("GET")
} }

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

@@ -1030,17 +1030,18 @@ func TestGetPostsForChannel(t *testing.T) {
post4 := th.CreatePost() post4 := th.CreatePost()
posts, resp := Client.GetPostsForChannel(th.BasicChannel.Id, 0, 60, "") th.TestForAllClients(t, func(t *testing.T, c *model.Client4) {
posts, resp := c.GetPostsForChannel(th.BasicChannel.Id, 0, 60, "")
CheckNoError(t, resp) CheckNoError(t, resp)
require.Equal(t, post4.Id, posts.Order[0], "wrong order") require.Equal(t, post4.Id, posts.Order[0], "wrong order")
require.Equal(t, post3.Id, posts.Order[1], "wrong order") require.Equal(t, post3.Id, posts.Order[1], "wrong order")
require.Equal(t, post2.Id, posts.Order[2], "wrong order") require.Equal(t, post2.Id, posts.Order[2], "wrong order")
require.Equal(t, post1.Id, posts.Order[3], "wrong order") require.Equal(t, post1.Id, posts.Order[3], "wrong order")
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 0, 3, resp.Etag) posts, resp = c.GetPostsForChannel(th.BasicChannel.Id, 0, 3, resp.Etag)
CheckEtag(t, posts, resp) CheckEtag(t, posts, resp)
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 0, 3, "") posts, resp = c.GetPostsForChannel(th.BasicChannel.Id, 0, 3, "")
CheckNoError(t, resp) CheckNoError(t, resp)
require.Len(t, posts.Order, 3, "wrong number returned") require.Len(t, posts.Order, 3, "wrong number returned")
@@ -1049,17 +1050,19 @@ func TestGetPostsForChannel(t *testing.T) {
_, ok = posts.Posts[post1.Id] _, ok = posts.Posts[post1.Id]
require.True(t, ok, "missing root post") require.True(t, ok, "missing root post")
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 1, 1, "") posts, resp = c.GetPostsForChannel(th.BasicChannel.Id, 1, 1, "")
CheckNoError(t, resp) CheckNoError(t, resp)
require.Equal(t, post3.Id, posts.Order[0], "wrong order") require.Equal(t, post3.Id, posts.Order[0], "wrong order")
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 10000, 10000, "") posts, resp = c.GetPostsForChannel(th.BasicChannel.Id, 10000, 10000, "")
CheckNoError(t, resp) CheckNoError(t, resp)
require.Empty(t, posts.Order, "should be no posts") require.Empty(t, posts.Order, "should be no posts")
})
post5 := th.CreatePost() post5 := th.CreatePost()
posts, resp = Client.GetPostsSince(th.BasicChannel.Id, since) th.TestForAllClients(t, func(t *testing.T, c *model.Client4) {
posts, resp := c.GetPostsSince(th.BasicChannel.Id, since)
CheckNoError(t, resp) CheckNoError(t, resp)
require.Len(t, posts.Posts, 2, "should return 2 posts") require.Len(t, posts.Posts, 2, "should return 2 posts")
@@ -1081,22 +1084,20 @@ func TestGetPostsForChannel(t *testing.T) {
require.True(t, f, "missing post") require.True(t, f, "missing post")
} }
_, resp = Client.GetPostsForChannel("", 0, 60, "") _, resp = c.GetPostsForChannel("", 0, 60, "")
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
_, resp = Client.GetPostsForChannel("junk", 0, 60, "") _, resp = c.GetPostsForChannel("junk", 0, 60, "")
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
})
_, resp = Client.GetPostsForChannel(model.NewId(), 0, 60, "") _, resp := Client.GetPostsForChannel(model.NewId(), 0, 60, "")
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
Client.Logout() Client.Logout()
_, resp = Client.GetPostsForChannel(model.NewId(), 0, 60, "") _, resp = Client.GetPostsForChannel(model.NewId(), 0, 60, "")
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
_, resp = th.SystemAdminClient.GetPostsForChannel(th.BasicChannel.Id, 0, 60, "")
CheckNoError(t, resp)
// more tests for next_post_id, prev_post_id, and order // more tests for next_post_id, prev_post_id, and order
// There are 12 posts composed of first 2 system messages and 10 created posts // There are 12 posts composed of first 2 system messages and 10 created posts
Client.Login(th.BasicUser.Email, th.BasicUser.Password) Client.Login(th.BasicUser.Email, th.BasicUser.Password)
@@ -1106,12 +1107,14 @@ func TestGetPostsForChannel(t *testing.T) {
th.CreatePost() // post9 th.CreatePost() // post9
post10 := th.CreatePost() post10 := th.CreatePost()
var posts *model.PostList
th.TestForAllClients(t, func(t *testing.T, c *model.Client4) {
// get the system post IDs posted before the created posts above // get the system post IDs posted before the created posts above
posts, resp = Client.GetPostsBefore(th.BasicChannel.Id, post1.Id, 0, 2, "") posts, resp = c.GetPostsBefore(th.BasicChannel.Id, post1.Id, 0, 2, "")
systemPostId1 := posts.Order[1] systemPostId1 := posts.Order[1]
// similar to '/posts' // similar to '/posts'
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 0, 60, "") posts, resp = c.GetPostsForChannel(th.BasicChannel.Id, 0, 60, "")
CheckNoError(t, resp) CheckNoError(t, resp)
require.Len(t, posts.Order, 12, "expected 12 posts") require.Len(t, posts.Order, 12, "expected 12 posts")
require.Equal(t, post10.Id, posts.Order[0], "posts not in order") require.Equal(t, post10.Id, posts.Order[0], "posts not in order")
@@ -1120,7 +1123,7 @@ func TestGetPostsForChannel(t *testing.T) {
require.Equal(t, "", posts.PrevPostId, "should return an empty PrevPostId") require.Equal(t, "", posts.PrevPostId, "should return an empty PrevPostId")
// similar to '/posts?per_page=3' // similar to '/posts?per_page=3'
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 0, 3, "") posts, resp = c.GetPostsForChannel(th.BasicChannel.Id, 0, 3, "")
CheckNoError(t, resp) CheckNoError(t, resp)
require.Len(t, posts.Order, 3, "expected 3 posts") require.Len(t, posts.Order, 3, "expected 3 posts")
require.Equal(t, post10.Id, posts.Order[0], "posts not in order") require.Equal(t, post10.Id, posts.Order[0], "posts not in order")
@@ -1129,7 +1132,7 @@ func TestGetPostsForChannel(t *testing.T) {
require.Equal(t, post7.Id, posts.PrevPostId, "should return post7.Id as PrevPostId") require.Equal(t, post7.Id, posts.PrevPostId, "should return post7.Id as PrevPostId")
// similar to '/posts?per_page=3&page=1' // similar to '/posts?per_page=3&page=1'
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 1, 3, "") posts, resp = c.GetPostsForChannel(th.BasicChannel.Id, 1, 3, "")
CheckNoError(t, resp) CheckNoError(t, resp)
require.Len(t, posts.Order, 3, "expected 3 posts") require.Len(t, posts.Order, 3, "expected 3 posts")
require.Equal(t, post7.Id, posts.Order[0], "posts not in order") require.Equal(t, post7.Id, posts.Order[0], "posts not in order")
@@ -1138,7 +1141,7 @@ func TestGetPostsForChannel(t *testing.T) {
require.Equal(t, post4.Id, posts.PrevPostId, "should return post4.Id as PrevPostId") require.Equal(t, post4.Id, posts.PrevPostId, "should return post4.Id as PrevPostId")
// similar to '/posts?per_page=3&page=2' // similar to '/posts?per_page=3&page=2'
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 2, 3, "") posts, resp = c.GetPostsForChannel(th.BasicChannel.Id, 2, 3, "")
CheckNoError(t, resp) CheckNoError(t, resp)
require.Len(t, posts.Order, 3, "expected 3 posts") require.Len(t, posts.Order, 3, "expected 3 posts")
require.Equal(t, post4.Id, posts.Order[0], "posts not in order") require.Equal(t, post4.Id, posts.Order[0], "posts not in order")
@@ -1147,7 +1150,7 @@ func TestGetPostsForChannel(t *testing.T) {
require.Equal(t, post1.Id, posts.PrevPostId, "should return post1.Id as PrevPostId") require.Equal(t, post1.Id, posts.PrevPostId, "should return post1.Id as PrevPostId")
// similar to '/posts?per_page=3&page=3' // similar to '/posts?per_page=3&page=3'
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 3, 3, "") posts, resp = c.GetPostsForChannel(th.BasicChannel.Id, 3, 3, "")
CheckNoError(t, resp) CheckNoError(t, resp)
require.Len(t, posts.Order, 3, "expected 3 posts") require.Len(t, posts.Order, 3, "expected 3 posts")
require.Equal(t, post1.Id, posts.Order[0], "posts not in order") require.Equal(t, post1.Id, posts.Order[0], "posts not in order")
@@ -1156,11 +1159,12 @@ func TestGetPostsForChannel(t *testing.T) {
require.Equal(t, "", posts.PrevPostId, "should return an empty PrevPostId") require.Equal(t, "", posts.PrevPostId, "should return an empty PrevPostId")
// similar to '/posts?per_page=3&page=4' // similar to '/posts?per_page=3&page=4'
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 4, 3, "") posts, resp = c.GetPostsForChannel(th.BasicChannel.Id, 4, 3, "")
CheckNoError(t, resp) CheckNoError(t, resp)
require.Empty(t, posts.Order, "should return 0 post") require.Empty(t, posts.Order, "should return 0 post")
require.Equal(t, "", posts.NextPostId, "should return an empty NextPostId") require.Equal(t, "", posts.NextPostId, "should return an empty NextPostId")
require.Equal(t, "", posts.PrevPostId, "should return an empty PrevPostId") require.Equal(t, "", posts.PrevPostId, "should return an empty PrevPostId")
})
} }
func TestGetFlaggedPostsForUser(t *testing.T) { func TestGetFlaggedPostsForUser(t *testing.T) {