summaryrefslogtreecommitdiffstats
path: root/api4/post_test.go
diff options
context:
space:
mode:
authorSaturnino Abril <saturnino.abril@gmail.com>2017-03-30 00:06:51 +0900
committerCorey Hulen <corey@hulen.com>2017-03-29 08:06:51 -0700
commit2f15523fe88c3a382abda1e64b2ef962c3ab5128 (patch)
treeb82d0f98d68c16fd23e886072f8a513d8450bef0 /api4/post_test.go
parent31830ffc7b578de643f5748227e4b783ee0782dd (diff)
downloadchat-2f15523fe88c3a382abda1e64b2ef962c3ab5128.tar.gz
chat-2f15523fe88c3a382abda1e64b2ef962c3ab5128.tar.bz2
chat-2f15523fe88c3a382abda1e64b2ef962c3ab5128.zip
APIv4 put /posts/{post_id}/patch (#5883)
* APIv4 put /posts/{post_id}/patch * Add props and edit permission
Diffstat (limited to 'api4/post_test.go')
-rw-r--r--api4/post_test.go102
1 files changed, 102 insertions, 0 deletions
diff --git a/api4/post_test.go b/api4/post_test.go
index 9e0880004..8f954efaa 100644
--- a/api4/post_test.go
+++ b/api4/post_test.go
@@ -5,6 +5,7 @@ package api4
import (
"net/http"
+ "reflect"
"strconv"
"testing"
"time"
@@ -167,6 +168,107 @@ func TestUpdatePost(t *testing.T) {
CheckUnauthorizedStatus(t, resp)
}
+func TestPatchPost(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,
+ IsPinned: true,
+ Message: "#hashtag a message",
+ Props: model.StringInterface{"channel_header": "old_header"},
+ FileIds: model.StringArray{"file1", "file2"},
+ HasReactions: true,
+ }
+ post, _ = Client.CreatePost(post)
+
+ patch := &model.PostPatch{}
+
+ patch.IsPinned = new(bool)
+ *patch.IsPinned = false
+ patch.Message = new(string)
+ *patch.Message = "#otherhashtag other message"
+ patch.Props = new(model.StringInterface)
+ *patch.Props = model.StringInterface{"channel_header": "new_header"}
+ patch.FileIds = new(model.StringArray)
+ *patch.FileIds = model.StringArray{"file1", "otherfile2", "otherfile3"}
+ patch.HasReactions = new(bool)
+ *patch.HasReactions = false
+
+ rpost, resp := Client.PatchPost(post.Id, patch)
+ CheckNoError(t, resp)
+
+ if rpost.IsPinned != false {
+ t.Fatal("IsPinned did not update properly")
+ }
+ if rpost.Message != "#otherhashtag other message" {
+ t.Fatal("Message did not update properly")
+ }
+ if len(rpost.Props) != 1 {
+ t.Fatal("Props did not update properly")
+ }
+ if !reflect.DeepEqual(rpost.Props, *patch.Props) {
+ t.Fatal("Props did not update properly")
+ }
+ if rpost.Hashtags != "#otherhashtag" {
+ t.Fatal("Message did not update properly")
+ }
+ if len(rpost.FileIds) != 3 {
+ t.Fatal("FileIds did not update properly")
+ }
+ if !reflect.DeepEqual(rpost.FileIds, *patch.FileIds) {
+ t.Fatal("FileIds did not update properly")
+ }
+ if rpost.HasReactions != false {
+ t.Fatal("HasReactions did not update properly")
+ }
+
+ if r, err := Client.DoApiPut("/posts/"+post.Id+"/patch", "garbage"); err == nil {
+ t.Fatal("should have errored")
+ } else {
+ if r.StatusCode != http.StatusBadRequest {
+ t.Log("actual: " + strconv.Itoa(r.StatusCode))
+ t.Log("expected: " + strconv.Itoa(http.StatusBadRequest))
+ t.Fatal("wrong status code")
+ }
+ }
+
+ _, resp = Client.PatchPost("junk", patch)
+ CheckBadRequestStatus(t, resp)
+
+ _, resp = Client.PatchPost(GenerateTestId(), patch)
+ CheckForbiddenStatus(t, resp)
+
+ Client.Logout()
+ _, resp = Client.PatchPost(post.Id, patch)
+ CheckUnauthorizedStatus(t, resp)
+
+ th.LoginTeamAdmin()
+ _, resp = Client.PatchPost(post.Id, patch)
+ CheckNoError(t, resp)
+
+ _, resp = th.SystemAdminClient.PatchPost(post.Id, patch)
+ CheckNoError(t, resp)
+}
+
func TestGetPostsForChannel(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()