ApiV4: PUT /posts/{post_id} (#5521)

This commit is contained in:
Andrei Stanciu 2017-02-28 11:34:32 +02:00 committed by George Goldberg
parent 76fa840b52
commit 6ff350380b
3 changed files with 106 additions and 0 deletions

View file

@ -24,6 +24,7 @@ func InitPost() {
BaseRoutes.PostsForChannel.Handle("", ApiSessionRequired(getPostsForChannel)).Methods("GET")
BaseRoutes.Team.Handle("/posts/search", ApiSessionRequired(searchPosts)).Methods("POST")
BaseRoutes.Post.Handle("", ApiSessionRequired(updatePost)).Methods("PUT")
}
func createPost(c *Context, w http.ResponseWriter, r *http.Request) {
@ -176,6 +177,31 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(posts.ToJson()))
}
func updatePost(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequirePostId()
post := model.PostFromJson(r.Body)
if post == nil {
c.SetInvalidParam("post")
return
}
if !app.SessionHasPermissionToChannelByPost(c.Session, c.Params.PostId, model.PERMISSION_EDIT_POST) {
c.SetPermissionError(model.PERMISSION_EDIT_POST)
return
}
post.UserId = c.Session.UserId
rpost, err := app.UpdatePost(post)
if err != nil {
c.Err = err
return
}
w.Write([]byte(rpost.ToJson()))
}
func getFileInfosForPost(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequirePostId()
if c.Err != nil {
@ -198,3 +224,4 @@ func getFileInfosForPost(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.FileInfosToJson(infos)))
}
}

View file

@ -11,6 +11,7 @@ import (
"github.com/mattermost/platform/app"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
)
func TestCreatePost(t *testing.T) {
@ -98,6 +99,74 @@ func TestCreatePost(t *testing.T) {
}
}
func TestUpdatePost(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Client := th.Client
channel := th.BasicChannel
isLicensed := utils.IsLicensed
license := utils.License
allowEditPost := *utils.Cfg.ServiceSettings.AllowEditPost
defer func() {
utils.IsLicensed = isLicensed
utils.License = license
*utils.Cfg.ServiceSettings.AllowEditPost = allowEditPost
utils.SetDefaultRolesBasedOnConfig()
}()
utils.IsLicensed = true
utils.License = &model.License{Features: &model.Features{}}
utils.License.Features.SetDefaults()
*utils.Cfg.ServiceSettings.AllowEditPost = model.ALLOW_EDIT_POST_ALWAYS
utils.SetDefaultRolesBasedOnConfig()
post := &model.Post{ChannelId: channel.Id, Message: "a" + model.NewId() + "a"}
rpost, resp := Client.CreatePost(post)
CheckNoError(t, resp)
if rpost.Message != post.Message {
t.Fatal("full name didn't match")
}
if rpost.EditAt != 0 {
t.Fatal("Newly created post shouldn't have EditAt set")
}
msg := "a" + model.NewId() + " update post"
rpost.Message = msg
rupost, resp := Client.UpdatePost(rpost.Id, rpost);
CheckNoError(t, resp)
if rupost.Message != msg {
t.Fatal("failed to updates")
}
if rupost.EditAt == 0 {
t.Fatal("EditAt not updated for post")
}
msg1 := "#hashtag a" + model.NewId() + " update post again"
rpost.Message = msg1
rrupost, resp := Client.UpdatePost(rpost.Id, rpost);
CheckNoError(t, resp)
if rrupost.Message != msg1 && rrupost.Hashtags != "#hashtag" {
t.Fatal("failed to updates")
}
post2 := &model.Post{ChannelId: channel.Id, Message: "a" + model.NewId() + "a", Type: model.POST_JOIN_LEAVE}
rpost2, resp := Client.CreatePost(post2)
CheckNoError(t, resp)
up2 := &model.Post{Id: rpost2.Id, ChannelId: channel.Id, Message: "a" + model.NewId() + " update post 2"}
_, resp = Client.UpdatePost(rpost2.Id, up2);
CheckBadRequestStatus(t, resp)
Client.Logout()
_, resp = Client.UpdatePost(rpost.Id, rpost)
CheckUnauthorizedStatus(t, resp)
}
func TestGetPostsForChannel(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()

View file

@ -731,6 +731,16 @@ func (c *Client4) CreatePost(post *Post) (*Post, *Response) {
}
}
// UpdatePost updates a post based on the provided post struct.
func (c *Client4) UpdatePost(postId string, post *Post) (*Post, *Response) {
if r, err := c.DoApiPut(c.GetPostRoute(postId), post.ToJson()); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return PostFromJson(r.Body), BuildResponse(r)
}
}
// GetPost gets a single post.
func (c *Client4) GetPost(postId string, etag string) (*Post, *Response) {
if r, err := c.DoApiGet(c.GetPostRoute(postId), etag); err != nil {