Skip to content

Commit

Permalink
Merge e883a04 into 4092836
Browse files Browse the repository at this point in the history
  • Loading branch information
saturninoabril committed Mar 30, 2017
2 parents 4092836 + e883a04 commit 8fb277c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
34 changes: 34 additions & 0 deletions api4/post.go
Expand Up @@ -26,6 +26,8 @@ func InitPost() {
BaseRoutes.Team.Handle("/posts/search", ApiSessionRequired(searchPosts)).Methods("POST")
BaseRoutes.Post.Handle("", ApiSessionRequired(updatePost)).Methods("PUT")
BaseRoutes.Post.Handle("/patch", ApiSessionRequired(patchPost)).Methods("PUT")
BaseRoutes.Post.Handle("/pin", ApiSessionRequired(pinPost)).Methods("POST")
BaseRoutes.Post.Handle("/unpin", ApiSessionRequired(unpinPost)).Methods("POST")
}

func createPost(c *Context, w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -273,6 +275,38 @@ func patchPost(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(patchedPost.ToJson()))
}

func saveIsPinnedPost(c *Context, w http.ResponseWriter, r *http.Request, isPinned bool) {
c.RequirePostId()
if c.Err != nil {
return
}

if !app.SessionHasPermissionToChannelByPost(c.Session, c.Params.PostId, model.PERMISSION_READ_CHANNEL) {
c.SetPermissionError(model.PERMISSION_READ_CHANNEL)
return
}

patch := &model.PostPatch{}
patch.IsPinned = new(bool)
*patch.IsPinned = isPinned

_, err := app.PatchPost(c.Params.PostId, patch)
if err != nil {
c.Err = err
return
}

ReturnStatusOK(w)
}

func pinPost(c *Context, w http.ResponseWriter, r *http.Request) {
saveIsPinnedPost(c, w, r, true)
}

func unpinPost(c *Context, w http.ResponseWriter, r *http.Request) {
saveIsPinnedPost(c, w, r, false)
}

func getFileInfosForPost(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequirePostId()
if c.Err != nil {
Expand Down
70 changes: 70 additions & 0 deletions api4/post_test.go
Expand Up @@ -269,6 +269,76 @@ func TestPatchPost(t *testing.T) {
CheckNoError(t, resp)
}

func TestPinPost(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Client := th.Client

post := th.BasicPost
pass, resp := Client.PinPost(post.Id)
CheckNoError(t, resp)

if !pass {
t.Fatal("should have passed")
}

if rpost, err := app.GetSinglePost(post.Id); err != nil && rpost.IsPinned != true {
t.Fatal("failed to pin post")
}

pass, resp = Client.PinPost("junk")
CheckBadRequestStatus(t, resp)

if pass {
t.Fatal("should have failed")
}

_, resp = Client.PinPost(GenerateTestId())
CheckForbiddenStatus(t, resp)

Client.Logout()
_, resp = Client.PinPost(post.Id)
CheckUnauthorizedStatus(t, resp)

_, resp = th.SystemAdminClient.PinPost(post.Id)
CheckNoError(t, resp)
}

func TestUnpinPost(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Client := th.Client

pinnedPost := th.CreatePinnedPost()
pass, resp := Client.UnpinPost(pinnedPost.Id)
CheckNoError(t, resp)

if !pass {
t.Fatal("should have passed")
}

if rpost, err := app.GetSinglePost(pinnedPost.Id); err != nil && rpost.IsPinned != false {
t.Fatal("failed to pin post")
}

pass, resp = Client.UnpinPost("junk")
CheckBadRequestStatus(t, resp)

if pass {
t.Fatal("should have failed")
}

_, resp = Client.UnpinPost(GenerateTestId())
CheckForbiddenStatus(t, resp)

Client.Logout()
_, resp = Client.UnpinPost(pinnedPost.Id)
CheckUnauthorizedStatus(t, resp)

_, resp = th.SystemAdminClient.UnpinPost(pinnedPost.Id)
CheckNoError(t, resp)
}

func TestGetPostsForChannel(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Expand Down
20 changes: 20 additions & 0 deletions model/client4.go
Expand Up @@ -1239,6 +1239,26 @@ func (c *Client4) PatchPost(postId string, patch *PostPatch) (*Post, *Response)
}
}

// PinPost pin a post based on provided post id string.
func (c *Client4) PinPost(postId string) (bool, *Response) {
if r, err := c.DoApiPost(c.GetPostRoute(postId)+"/pin", ""); err != nil {
return false, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return CheckStatusOK(r), BuildResponse(r)
}
}

// UnpinPost unpin a post based on provided post id string.
func (c *Client4) UnpinPost(postId string) (bool, *Response) {
if r, err := c.DoApiPost(c.GetPostRoute(postId)+"/unpin", ""); err != nil {
return false, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return CheckStatusOK(r), 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 {
Expand Down

0 comments on commit 8fb277c

Please sign in to comment.