2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
2017-02-13 10:52:50 -05:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package api4
|
|
|
|
|
|
|
|
|
|
import (
|
2017-07-18 15:43:31 -04:00
|
|
|
"encoding/json"
|
2017-02-13 10:52:50 -05:00
|
|
|
"net/http"
|
2017-07-18 15:43:31 -04:00
|
|
|
"net/http/httptest"
|
|
|
|
|
"net/url"
|
2017-03-29 11:06:51 -04:00
|
|
|
"reflect"
|
2017-02-13 10:52:50 -05:00
|
|
|
"strconv"
|
2017-07-18 15:43:31 -04:00
|
|
|
"strings"
|
2017-02-13 10:52:50 -05:00
|
|
|
"testing"
|
2017-02-21 07:36:52 -05:00
|
|
|
"time"
|
2017-02-13 10:52:50 -05:00
|
|
|
|
2017-09-07 02:05:10 -04:00
|
|
|
"github.com/mattermost/mattermost-server/app"
|
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
|
|
|
"github.com/mattermost/mattermost-server/utils"
|
2017-02-13 10:52:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCreatePost(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-13 10:52:50 -05:00
|
|
|
Client := th.Client
|
|
|
|
|
|
2017-10-20 13:38:26 -04:00
|
|
|
post := &model.Post{ChannelId: th.BasicChannel.Id, Message: "#hashtag a" + model.NewId() + "a", Props: model.StringInterface{model.PROPS_ADD_CHANNEL_MEMBER: "no good"}}
|
2017-02-13 10:52:50 -05:00
|
|
|
rpost, resp := Client.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
2017-03-31 09:56:20 -04:00
|
|
|
CheckCreatedStatus(t, resp)
|
2017-02-13 10:52:50 -05:00
|
|
|
|
|
|
|
|
if rpost.Message != post.Message {
|
|
|
|
|
t.Fatal("message didn't match")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if rpost.Hashtags != "#hashtag" {
|
|
|
|
|
t.Fatal("hashtag didn't match")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(rpost.FileIds) != 0 {
|
|
|
|
|
t.Fatal("shouldn't have files")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if rpost.EditAt != 0 {
|
|
|
|
|
t.Fatal("newly created post shouldn't have EditAt set")
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-20 13:38:26 -04:00
|
|
|
if rpost.Props[model.PROPS_ADD_CHANNEL_MEMBER] != nil {
|
|
|
|
|
t.Fatal("newly created post shouldn't have Props['add_channel_member'] set")
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 10:52:50 -05:00
|
|
|
post.RootId = rpost.Id
|
|
|
|
|
post.ParentId = rpost.Id
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
post.RootId = "junk"
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
post.RootId = rpost.Id
|
|
|
|
|
post.ParentId = "junk"
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
2017-06-19 12:08:52 -04:00
|
|
|
post2 := &model.Post{ChannelId: th.BasicChannel2.Id, Message: "zz" + model.NewId() + "a", CreateAt: 123}
|
2017-02-13 10:52:50 -05:00
|
|
|
rpost2, resp := Client.CreatePost(post2)
|
|
|
|
|
|
|
|
|
|
if rpost2.CreateAt == post2.CreateAt {
|
|
|
|
|
t.Fatal("create at should not match")
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 13:30:48 -04:00
|
|
|
post.RootId = ""
|
|
|
|
|
post.ParentId = ""
|
|
|
|
|
post.Type = model.POST_SYSTEM_GENERIC
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
post.Type = ""
|
2017-02-13 10:52:50 -05:00
|
|
|
post.RootId = rpost2.Id
|
|
|
|
|
post.ParentId = rpost2.Id
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
post.RootId = ""
|
|
|
|
|
post.ParentId = ""
|
|
|
|
|
post.ChannelId = "junk"
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
post.ChannelId = model.NewId()
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
if r, err := Client.DoApiPost("/posts", "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")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
post.ChannelId = th.BasicChannel.Id
|
|
|
|
|
post.CreateAt = 123
|
|
|
|
|
rpost, resp = th.SystemAdminClient.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if rpost.CreateAt != post.CreateAt {
|
|
|
|
|
t.Fatal("create at should match")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 15:43:31 -04:00
|
|
|
func testCreatePostWithOutgoingHook(
|
|
|
|
|
t *testing.T,
|
|
|
|
|
hookContentType, expectedContentType, message, triggerWord string,
|
|
|
|
|
fileIds []string,
|
|
|
|
|
triggerWhen int,
|
|
|
|
|
) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-07-18 15:43:31 -04:00
|
|
|
user := th.SystemAdminUser
|
|
|
|
|
team := th.BasicTeam
|
|
|
|
|
channel := th.BasicChannel
|
|
|
|
|
|
2017-10-18 18:36:43 -04:00
|
|
|
enableOutgoingHooks := th.App.Config().ServiceSettings.EnableOutgoingWebhooks
|
|
|
|
|
enableAdminOnlyHooks := th.App.Config().ServiceSettings.EnableOnlyAdminIntegrations
|
|
|
|
|
allowedInternalConnections := *th.App.Config().ServiceSettings.AllowedUntrustedInternalConnections
|
2017-07-18 15:43:31 -04:00
|
|
|
defer func() {
|
2017-10-18 18:36:43 -04:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOutgoingWebhooks = enableOutgoingHooks })
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOnlyAdminIntegrations = enableAdminOnlyHooks })
|
2017-07-18 15:43:31 -04:00
|
|
|
utils.SetDefaultRolesBasedOnConfig()
|
2017-10-18 18:36:43 -04:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
|
|
|
cfg.ServiceSettings.AllowedUntrustedInternalConnections = &allowedInternalConnections
|
|
|
|
|
})
|
2017-07-18 15:43:31 -04:00
|
|
|
}()
|
2017-10-18 18:36:43 -04:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOutgoingWebhooks = true })
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = true })
|
2017-07-18 15:43:31 -04:00
|
|
|
utils.SetDefaultRolesBasedOnConfig()
|
2017-10-18 18:36:43 -04:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
|
|
|
*cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost 127.0.0.1"
|
|
|
|
|
})
|
2017-07-18 15:43:31 -04:00
|
|
|
|
|
|
|
|
var hook *model.OutgoingWebhook
|
|
|
|
|
var post *model.Post
|
|
|
|
|
|
|
|
|
|
// Create a test server that is the target of the outgoing webhook. It will
|
|
|
|
|
// validate the webhook body fields and write to the success channel on
|
|
|
|
|
// success/failure.
|
|
|
|
|
success := make(chan bool)
|
|
|
|
|
wait := make(chan bool, 1)
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
<-wait
|
|
|
|
|
|
|
|
|
|
requestContentType := r.Header.Get("Content-Type")
|
|
|
|
|
if requestContentType != expectedContentType {
|
|
|
|
|
t.Logf("Content-Type is %s, should be %s", requestContentType, expectedContentType)
|
|
|
|
|
success <- false
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedPayload := &model.OutgoingWebhookPayload{
|
|
|
|
|
Token: hook.Token,
|
|
|
|
|
TeamId: hook.TeamId,
|
|
|
|
|
TeamDomain: team.Name,
|
|
|
|
|
ChannelId: post.ChannelId,
|
|
|
|
|
ChannelName: channel.Name,
|
|
|
|
|
Timestamp: post.CreateAt,
|
|
|
|
|
UserId: post.UserId,
|
|
|
|
|
UserName: user.Username,
|
|
|
|
|
PostId: post.Id,
|
|
|
|
|
Text: post.Message,
|
|
|
|
|
TriggerWord: triggerWord,
|
|
|
|
|
FileIds: strings.Join(post.FileIds, ","),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// depending on the Content-Type, we expect to find a JSON or form encoded payload
|
|
|
|
|
if requestContentType == "application/json" {
|
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
|
o := &model.OutgoingWebhookPayload{}
|
|
|
|
|
decoder.Decode(&o)
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedPayload, o) {
|
|
|
|
|
t.Logf("JSON payload is %+v, should be %+v", o, expectedPayload)
|
|
|
|
|
success <- false
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Logf("Error parsing form: %q", err)
|
|
|
|
|
success <- false
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedFormValues, _ := url.ParseQuery(expectedPayload.ToFormValues())
|
|
|
|
|
if !reflect.DeepEqual(expectedFormValues, r.Form) {
|
|
|
|
|
t.Logf("Form values are: %q\n, should be: %q\n", r.Form, expectedFormValues)
|
|
|
|
|
success <- false
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
success <- true
|
|
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
// create an outgoing webhook, passing it the test server URL
|
|
|
|
|
var triggerWords []string
|
|
|
|
|
if triggerWord != "" {
|
|
|
|
|
triggerWords = []string{triggerWord}
|
|
|
|
|
}
|
2017-07-31 12:59:32 -04:00
|
|
|
|
2017-07-18 15:43:31 -04:00
|
|
|
hook = &model.OutgoingWebhook{
|
|
|
|
|
ChannelId: channel.Id,
|
|
|
|
|
TeamId: team.Id,
|
|
|
|
|
ContentType: hookContentType,
|
|
|
|
|
TriggerWords: triggerWords,
|
|
|
|
|
TriggerWhen: triggerWhen,
|
|
|
|
|
CallbackURLs: []string{ts.URL},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hook, resp := th.SystemAdminClient.CreateOutgoingWebhook(hook)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
// create a post to trigger the webhook
|
|
|
|
|
post = &model.Post{
|
|
|
|
|
ChannelId: channel.Id,
|
|
|
|
|
Message: message,
|
|
|
|
|
FileIds: fileIds,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post, resp = th.SystemAdminClient.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
wait <- true
|
|
|
|
|
|
|
|
|
|
// We wait for the test server to write to the success channel and we make
|
|
|
|
|
// the test fail if that doesn't happen before the timeout.
|
|
|
|
|
select {
|
|
|
|
|
case ok := <-success:
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatal("Test server did send an invalid webhook.")
|
|
|
|
|
}
|
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
|
t.Fatal("Timeout, test server did not send the webhook.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCreatePostWithOutgoingHook_form_urlencoded(t *testing.T) {
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "application/x-www-form-urlencoded", "application/x-www-form-urlencoded", "triggerword lorem ipsum", "triggerword", []string{"file_id_1"}, app.TRIGGERWORDS_EXACT_MATCH)
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "application/x-www-form-urlencoded", "application/x-www-form-urlencoded", "triggerwordaaazzz lorem ipsum", "triggerword", []string{"file_id_1"}, app.TRIGGERWORDS_STARTS_WITH)
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "application/x-www-form-urlencoded", "application/x-www-form-urlencoded", "", "", []string{"file_id_1"}, app.TRIGGERWORDS_EXACT_MATCH)
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "application/x-www-form-urlencoded", "application/x-www-form-urlencoded", "", "", []string{"file_id_1"}, app.TRIGGERWORDS_STARTS_WITH)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCreatePostWithOutgoingHook_json(t *testing.T) {
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "application/json", "application/json", "triggerword lorem ipsum", "triggerword", []string{"file_id_1, file_id_2"}, app.TRIGGERWORDS_EXACT_MATCH)
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "application/json", "application/json", "triggerwordaaazzz lorem ipsum", "triggerword", []string{"file_id_1, file_id_2"}, app.TRIGGERWORDS_STARTS_WITH)
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "application/json", "application/json", "triggerword lorem ipsum", "", []string{"file_id_1"}, app.TRIGGERWORDS_EXACT_MATCH)
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "application/json", "application/json", "triggerwordaaazzz lorem ipsum", "", []string{"file_id_1"}, app.TRIGGERWORDS_STARTS_WITH)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// hooks created before we added the ContentType field should be considered as
|
|
|
|
|
// application/x-www-form-urlencoded
|
|
|
|
|
func TestCreatePostWithOutgoingHook_no_content_type(t *testing.T) {
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "", "application/x-www-form-urlencoded", "triggerword lorem ipsum", "triggerword", []string{"file_id_1"}, app.TRIGGERWORDS_EXACT_MATCH)
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "", "application/x-www-form-urlencoded", "triggerwordaaazzz lorem ipsum", "triggerword", []string{"file_id_1"}, app.TRIGGERWORDS_STARTS_WITH)
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "", "application/x-www-form-urlencoded", "triggerword lorem ipsum", "", []string{"file_id_1, file_id_2"}, app.TRIGGERWORDS_EXACT_MATCH)
|
|
|
|
|
testCreatePostWithOutgoingHook(t, "", "application/x-www-form-urlencoded", "triggerwordaaazzz lorem ipsum", "", []string{"file_id_1, file_id_2"}, app.TRIGGERWORDS_STARTS_WITH)
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 12:59:32 -04:00
|
|
|
func TestCreatePostPublic(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-07-31 12:59:32 -04:00
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
post := &model.Post{ChannelId: th.BasicChannel.Id, Message: "#hashtag a" + model.NewId() + "a"}
|
|
|
|
|
|
|
|
|
|
user := model.User{Email: GenerateTestEmail(), Nickname: "Joram Wilander", Password: "hello1", Username: GenerateTestUsername(), Roles: model.ROLE_SYSTEM_USER.Id}
|
|
|
|
|
|
|
|
|
|
ruser, resp := Client.CreateUser(&user)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Login(user.Email, user.Password)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
2017-10-31 12:00:21 -04:00
|
|
|
th.App.UpdateUserRoles(ruser.Id, model.ROLE_SYSTEM_USER.Id+" "+model.ROLE_SYSTEM_POST_ALL_PUBLIC.Id, false)
|
2017-09-06 18:12:54 -04:00
|
|
|
th.App.InvalidateAllCaches()
|
2017-07-31 12:59:32 -04:00
|
|
|
|
|
|
|
|
Client.Login(user.Email, user.Password)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
post.ChannelId = th.BasicPrivateChannel.Id
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
2017-10-31 12:00:21 -04:00
|
|
|
th.App.UpdateUserRoles(ruser.Id, model.ROLE_SYSTEM_USER.Id, false)
|
2017-09-06 18:12:54 -04:00
|
|
|
th.App.JoinUserToTeam(th.BasicTeam, ruser, "")
|
|
|
|
|
th.App.UpdateTeamMemberRoles(th.BasicTeam.Id, ruser.Id, model.ROLE_TEAM_USER.Id+" "+model.ROLE_TEAM_POST_ALL_PUBLIC.Id)
|
|
|
|
|
th.App.InvalidateAllCaches()
|
2017-07-31 12:59:32 -04:00
|
|
|
|
|
|
|
|
Client.Login(user.Email, user.Password)
|
|
|
|
|
|
|
|
|
|
post.ChannelId = th.BasicPrivateChannel.Id
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
post.ChannelId = th.BasicChannel.Id
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-01 11:06:53 -04:00
|
|
|
func TestCreatePostAll(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-08-01 11:06:53 -04:00
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
post := &model.Post{ChannelId: th.BasicChannel.Id, Message: "#hashtag a" + model.NewId() + "a"}
|
|
|
|
|
|
|
|
|
|
user := model.User{Email: GenerateTestEmail(), Nickname: "Joram Wilander", Password: "hello1", Username: GenerateTestUsername(), Roles: model.ROLE_SYSTEM_USER.Id}
|
|
|
|
|
|
2017-09-06 18:12:54 -04:00
|
|
|
directChannel, _ := th.App.CreateDirectChannel(th.BasicUser.Id, th.BasicUser2.Id)
|
2017-08-01 11:06:53 -04:00
|
|
|
|
|
|
|
|
ruser, resp := Client.CreateUser(&user)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Login(user.Email, user.Password)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
2017-10-31 12:00:21 -04:00
|
|
|
th.App.UpdateUserRoles(ruser.Id, model.ROLE_SYSTEM_USER.Id+" "+model.ROLE_SYSTEM_POST_ALL.Id, false)
|
2017-09-06 18:12:54 -04:00
|
|
|
th.App.InvalidateAllCaches()
|
2017-08-01 11:06:53 -04:00
|
|
|
|
|
|
|
|
Client.Login(user.Email, user.Password)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
post.ChannelId = th.BasicPrivateChannel.Id
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
post.ChannelId = directChannel.Id
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
2017-10-31 12:00:21 -04:00
|
|
|
th.App.UpdateUserRoles(ruser.Id, model.ROLE_SYSTEM_USER.Id, false)
|
2017-09-06 18:12:54 -04:00
|
|
|
th.App.JoinUserToTeam(th.BasicTeam, ruser, "")
|
|
|
|
|
th.App.UpdateTeamMemberRoles(th.BasicTeam.Id, ruser.Id, model.ROLE_TEAM_USER.Id+" "+model.ROLE_TEAM_POST_ALL.Id)
|
|
|
|
|
th.App.InvalidateAllCaches()
|
2017-08-01 11:06:53 -04:00
|
|
|
|
|
|
|
|
Client.Login(user.Email, user.Password)
|
|
|
|
|
|
|
|
|
|
post.ChannelId = th.BasicPrivateChannel.Id
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
post.ChannelId = th.BasicChannel.Id
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
post.ChannelId = directChannel.Id
|
|
|
|
|
_, resp = Client.CreatePost(post)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-20 13:38:26 -04:00
|
|
|
func TestCreatePostSendOutOfChannelMentions(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
|
|
|
|
defer th.TearDown()
|
|
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
WebSocketClient, err := th.CreateWebSocketClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
WebSocketClient.Listen()
|
|
|
|
|
|
|
|
|
|
inChannelUser := th.CreateUser()
|
|
|
|
|
th.LinkUserToTeam(inChannelUser, th.BasicTeam)
|
|
|
|
|
th.App.AddUserToChannel(inChannelUser, th.BasicChannel)
|
|
|
|
|
|
|
|
|
|
post1 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "@" + inChannelUser.Username}
|
|
|
|
|
_, resp := Client.CreatePost(post1)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
CheckCreatedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
timeout := time.After(300 * time.Millisecond)
|
|
|
|
|
waiting := true
|
|
|
|
|
for waiting {
|
|
|
|
|
select {
|
|
|
|
|
case event := <-WebSocketClient.EventChannel:
|
|
|
|
|
if event.Event == model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE {
|
|
|
|
|
t.Fatal("should not have ephemeral message event")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case <-timeout:
|
|
|
|
|
waiting = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outOfChannelUser := th.CreateUser()
|
|
|
|
|
th.LinkUserToTeam(outOfChannelUser, th.BasicTeam)
|
|
|
|
|
|
|
|
|
|
post2 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "@" + outOfChannelUser.Username}
|
|
|
|
|
_, resp = Client.CreatePost(post2)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
CheckCreatedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
timeout = time.After(300 * time.Millisecond)
|
|
|
|
|
waiting = true
|
|
|
|
|
for waiting {
|
|
|
|
|
select {
|
|
|
|
|
case event := <-WebSocketClient.EventChannel:
|
|
|
|
|
if event.Event != model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE {
|
|
|
|
|
// Ignore any other events
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpost := model.PostFromJson(strings.NewReader(event.Data["post"].(string)))
|
|
|
|
|
if acm, ok := wpost.Props[model.PROPS_ADD_CHANNEL_MEMBER].(map[string]interface{}); !ok {
|
|
|
|
|
t.Fatal("should have received ephemeral post with 'add_channel_member' in props")
|
|
|
|
|
} else {
|
|
|
|
|
if acm["post_id"] == nil || acm["user_ids"] == nil || acm["usernames"] == nil {
|
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
waiting = false
|
|
|
|
|
case <-timeout:
|
|
|
|
|
t.Fatal("timed out waiting for ephemeral message event")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-28 04:34:32 -05:00
|
|
|
func TestUpdatePost(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-28 04:34:32 -05:00
|
|
|
Client := th.Client
|
|
|
|
|
channel := th.BasicChannel
|
|
|
|
|
|
2017-08-16 12:51:45 -04:00
|
|
|
isLicensed := utils.IsLicensed()
|
|
|
|
|
license := utils.License()
|
2017-10-18 18:36:43 -04:00
|
|
|
allowEditPost := *th.App.Config().ServiceSettings.AllowEditPost
|
2017-02-28 04:34:32 -05:00
|
|
|
defer func() {
|
2017-08-16 12:51:45 -04:00
|
|
|
utils.SetIsLicensed(isLicensed)
|
|
|
|
|
utils.SetLicense(license)
|
2017-10-18 18:36:43 -04:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowEditPost = allowEditPost })
|
2017-02-28 04:34:32 -05:00
|
|
|
utils.SetDefaultRolesBasedOnConfig()
|
|
|
|
|
}()
|
2017-08-16 12:51:45 -04:00
|
|
|
utils.SetIsLicensed(true)
|
|
|
|
|
utils.SetLicense(&model.License{Features: &model.Features{}})
|
|
|
|
|
utils.License().Features.SetDefaults()
|
2017-02-28 04:34:32 -05:00
|
|
|
|
2017-10-18 18:36:43 -04:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowEditPost = model.ALLOW_EDIT_POST_ALWAYS })
|
2017-02-28 04:34:32 -05:00
|
|
|
utils.SetDefaultRolesBasedOnConfig()
|
|
|
|
|
|
2017-06-19 12:08:52 -04:00
|
|
|
post := &model.Post{ChannelId: channel.Id, Message: "zz" + model.NewId() + "a"}
|
2017-02-28 04:34:32 -05:00
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 12:08:52 -04:00
|
|
|
msg := "zz" + model.NewId() + " update post"
|
2017-02-28 04:34:32 -05:00
|
|
|
rpost.Message = msg
|
2017-06-26 16:05:50 -04:00
|
|
|
rpost.UserId = ""
|
|
|
|
|
|
2017-03-20 16:47:53 -04:00
|
|
|
rupost, resp := Client.UpdatePost(rpost.Id, rpost)
|
2017-02-28 04:34:32 -05:00
|
|
|
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
|
2017-10-20 13:38:26 -04:00
|
|
|
rpost.Props[model.PROPS_ADD_CHANNEL_MEMBER] = "no good"
|
2017-03-20 16:47:53 -04:00
|
|
|
rrupost, resp := Client.UpdatePost(rpost.Id, rpost)
|
2017-02-28 04:34:32 -05:00
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if rrupost.Message != msg1 && rrupost.Hashtags != "#hashtag" {
|
|
|
|
|
t.Fatal("failed to updates")
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-20 13:38:26 -04:00
|
|
|
if rrupost.Props[model.PROPS_ADD_CHANNEL_MEMBER] != nil {
|
|
|
|
|
t.Fatal("failed to sanitize Props['add_channel_member'], should be nil")
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 13:30:48 -04:00
|
|
|
rpost2, err := th.App.CreatePost(&model.Post{ChannelId: channel.Id, Message: "zz" + model.NewId() + "a", Type: model.POST_JOIN_LEAVE, UserId: th.BasicUser.Id}, channel, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2017-02-28 04:34:32 -05:00
|
|
|
|
2017-06-19 12:08:52 -04:00
|
|
|
up2 := &model.Post{Id: rpost2.Id, ChannelId: channel.Id, Message: "zz" + model.NewId() + " update post 2"}
|
2017-03-20 16:47:53 -04:00
|
|
|
_, resp = Client.UpdatePost(rpost2.Id, up2)
|
2017-02-28 04:34:32 -05:00
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
_, resp = Client.UpdatePost(rpost.Id, rpost)
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
2017-04-04 15:17:47 -04:00
|
|
|
|
|
|
|
|
th.LoginBasic2()
|
|
|
|
|
_, resp = Client.UpdatePost(rpost.Id, rpost)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
|
|
|
|
|
_, resp = th.SystemAdminClient.UpdatePost(rpost.Id, rpost)
|
|
|
|
|
CheckNoError(t, resp)
|
2017-02-28 04:34:32 -05:00
|
|
|
}
|
|
|
|
|
|
2017-03-29 11:06:51 -04:00
|
|
|
func TestPatchPost(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-03-29 11:06:51 -04:00
|
|
|
Client := th.Client
|
|
|
|
|
channel := th.BasicChannel
|
|
|
|
|
|
2017-08-16 12:51:45 -04:00
|
|
|
isLicensed := utils.IsLicensed()
|
|
|
|
|
license := utils.License()
|
2017-10-18 18:36:43 -04:00
|
|
|
allowEditPost := *th.App.Config().ServiceSettings.AllowEditPost
|
2017-03-29 11:06:51 -04:00
|
|
|
defer func() {
|
2017-08-16 12:51:45 -04:00
|
|
|
utils.SetIsLicensed(isLicensed)
|
|
|
|
|
utils.SetLicense(license)
|
2017-10-18 18:36:43 -04:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowEditPost = allowEditPost })
|
2017-03-29 11:06:51 -04:00
|
|
|
utils.SetDefaultRolesBasedOnConfig()
|
|
|
|
|
}()
|
2017-08-16 12:51:45 -04:00
|
|
|
utils.SetIsLicensed(true)
|
|
|
|
|
utils.SetLicense(&model.License{Features: &model.Features{}})
|
|
|
|
|
utils.License().Features.SetDefaults()
|
2017-03-29 11:06:51 -04:00
|
|
|
|
2017-10-18 18:36:43 -04:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowEditPost = model.ALLOW_EDIT_POST_ALWAYS })
|
2017-03-29 11:06:51 -04:00
|
|
|
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{}
|
|
|
|
|
|
2017-10-23 12:40:35 -04:00
|
|
|
patch.IsPinned = model.NewBool(false)
|
|
|
|
|
patch.Message = model.NewString("#otherhashtag other message")
|
2017-03-29 11:06:51 -04:00
|
|
|
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"}
|
2017-10-23 12:40:35 -04:00
|
|
|
patch.HasReactions = model.NewBool(false)
|
2017-03-29 11:06:51 -04:00
|
|
|
|
|
|
|
|
rpost, resp := Client.PatchPost(post.Id, patch)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
2017-10-30 12:57:24 -04:00
|
|
|
if rpost.IsPinned {
|
2017-03-29 11:06:51 -04:00
|
|
|
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")
|
|
|
|
|
}
|
2017-10-30 12:57:24 -04:00
|
|
|
if rpost.HasReactions {
|
2017-03-29 11:06:51 -04:00
|
|
|
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)
|
|
|
|
|
|
2017-04-04 15:17:47 -04:00
|
|
|
th.LoginBasic2()
|
|
|
|
|
_, resp = Client.PatchPost(post.Id, patch)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
2017-03-29 11:06:51 -04:00
|
|
|
th.LoginTeamAdmin()
|
|
|
|
|
_, resp = Client.PatchPost(post.Id, patch)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = th.SystemAdminClient.PatchPost(post.Id, patch)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-31 09:58:47 -04:00
|
|
|
func TestPinPost(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-03-31 09:58:47 -04:00
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
post := th.BasicPost
|
|
|
|
|
pass, resp := Client.PinPost(post.Id)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if !pass {
|
|
|
|
|
t.Fatal("should have passed")
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 12:57:24 -04:00
|
|
|
if rpost, err := th.App.GetSinglePost(post.Id); err != nil && !rpost.IsPinned {
|
2017-03-31 09:58:47 -04:00
|
|
|
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()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-03-31 09:58:47 -04:00
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
pinnedPost := th.CreatePinnedPost()
|
|
|
|
|
pass, resp := Client.UnpinPost(pinnedPost.Id)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if !pass {
|
|
|
|
|
t.Fatal("should have passed")
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 12:57:24 -04:00
|
|
|
if rpost, err := th.App.GetSinglePost(pinnedPost.Id); err != nil && rpost.IsPinned {
|
2017-03-31 09:58:47 -04:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 10:52:50 -05:00
|
|
|
func TestGetPostsForChannel(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-13 10:52:50 -05:00
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
post1 := th.CreatePost()
|
|
|
|
|
post2 := th.CreatePost()
|
2017-06-19 12:08:52 -04:00
|
|
|
post3 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "zz" + model.NewId() + "a", RootId: post1.Id}
|
2017-03-24 16:46:11 -04:00
|
|
|
post3, _ = Client.CreatePost(post3)
|
|
|
|
|
|
2017-06-20 17:05:13 -04:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
|
since := model.GetMillis()
|
|
|
|
|
time.Sleep(300 * time.Millisecond)
|
2017-03-24 16:46:11 -04:00
|
|
|
|
|
|
|
|
post4 := th.CreatePost()
|
2017-02-13 10:52:50 -05:00
|
|
|
|
|
|
|
|
posts, resp := Client.GetPostsForChannel(th.BasicChannel.Id, 0, 60, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if posts.Order[0] != post4.Id {
|
|
|
|
|
t.Fatal("wrong order")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts.Order[1] != post3.Id {
|
|
|
|
|
t.Fatal("wrong order")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts.Order[2] != post2.Id {
|
|
|
|
|
t.Fatal("wrong order")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts.Order[3] != post1.Id {
|
|
|
|
|
t.Fatal("wrong order")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 0, 3, resp.Etag)
|
|
|
|
|
CheckEtag(t, posts, resp)
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 0, 3, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(posts.Order) != 3 {
|
|
|
|
|
t.Fatal("wrong number returned")
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 16:46:11 -04:00
|
|
|
if _, ok := posts.Posts[post3.Id]; !ok {
|
2017-02-13 10:52:50 -05:00
|
|
|
t.Fatal("missing comment")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, ok := posts.Posts[post1.Id]; !ok {
|
|
|
|
|
t.Fatal("missing root post")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 1, 1, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if posts.Order[0] != post3.Id {
|
|
|
|
|
t.Fatal("wrong order")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.GetPostsForChannel(th.BasicChannel.Id, 10000, 10000, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(posts.Order) != 0 {
|
|
|
|
|
t.Fatal("should be no posts")
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 16:46:11 -04:00
|
|
|
post5 := th.CreatePost()
|
|
|
|
|
|
2017-06-20 17:05:13 -04:00
|
|
|
posts, resp = Client.GetPostsSince(th.BasicChannel.Id, since)
|
2017-03-24 16:46:11 -04:00
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
2017-05-15 11:44:04 -04:00
|
|
|
if len(posts.Posts) != 2 {
|
|
|
|
|
t.Log(posts.Posts)
|
|
|
|
|
t.Fatal("should return 2 posts")
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 16:46:11 -04:00
|
|
|
found := make([]bool, 2)
|
|
|
|
|
for _, p := range posts.Posts {
|
2017-06-20 17:05:13 -04:00
|
|
|
if p.CreateAt < since {
|
2017-03-24 16:46:11 -04:00
|
|
|
t.Fatal("bad create at for post returned")
|
|
|
|
|
}
|
|
|
|
|
if p.Id == post4.Id {
|
|
|
|
|
found[0] = true
|
|
|
|
|
} else if p.Id == post5.Id {
|
|
|
|
|
found[1] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, f := range found {
|
|
|
|
|
if !f {
|
|
|
|
|
t.Fatal("missing post")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 10:52:50 -05:00
|
|
|
_, resp = Client.GetPostsForChannel("", 0, 60, "")
|
2017-03-20 16:47:53 -04:00
|
|
|
CheckBadRequestStatus(t, resp)
|
2017-02-13 10:52:50 -05:00
|
|
|
|
|
|
|
|
_, resp = Client.GetPostsForChannel("junk", 0, 60, "")
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetPostsForChannel(model.NewId(), 0, 60, "")
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
_, resp = Client.GetPostsForChannel(model.NewId(), 0, 60, "")
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = th.SystemAdminClient.GetPostsForChannel(th.BasicChannel.Id, 0, 60, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-05 16:18:23 -04:00
|
|
|
func TestGetFlaggedPostsForUser(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-04-05 16:18:23 -04:00
|
|
|
Client := th.Client
|
|
|
|
|
user := th.BasicUser
|
|
|
|
|
team1 := th.BasicTeam
|
|
|
|
|
channel1 := th.BasicChannel
|
|
|
|
|
post1 := th.CreatePost()
|
|
|
|
|
channel2 := th.CreatePublicChannel()
|
|
|
|
|
post2 := th.CreatePostWithClient(Client, channel2)
|
|
|
|
|
|
|
|
|
|
preference := model.Preference{
|
|
|
|
|
UserId: user.Id,
|
|
|
|
|
Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
|
|
|
|
|
Name: post1.Id,
|
|
|
|
|
Value: "true",
|
|
|
|
|
}
|
|
|
|
|
Client.UpdatePreferences(user.Id, &model.Preferences{preference})
|
|
|
|
|
preference.Name = post2.Id
|
|
|
|
|
Client.UpdatePreferences(user.Id, &model.Preferences{preference})
|
|
|
|
|
|
|
|
|
|
opl := model.NewPostList()
|
|
|
|
|
opl.AddPost(post1)
|
|
|
|
|
opl.AddOrder(post1.Id)
|
|
|
|
|
|
|
|
|
|
rpl, resp := Client.GetFlaggedPostsForUserInChannel(user.Id, channel1.Id, 0, 10)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 1 {
|
|
|
|
|
t.Fatal("should have returned 1 post")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(rpl.Posts, opl.Posts) {
|
|
|
|
|
t.Fatal("posts should have matched")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInChannel(user.Id, channel1.Id, 0, 1)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 1 {
|
|
|
|
|
t.Fatal("should have returned 1 post")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInChannel(user.Id, channel1.Id, 1, 1)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 0 {
|
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInChannel(user.Id, GenerateTestId(), 0, 10)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 0 {
|
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInChannel(user.Id, "junk", 0, 10)
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
if rpl != nil {
|
|
|
|
|
t.Fatal("should be nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opl.AddPost(post2)
|
|
|
|
|
opl.AddOrder(post2.Id)
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInTeam(user.Id, team1.Id, 0, 10)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 2 {
|
|
|
|
|
t.Fatal("should have returned 2 posts")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(rpl.Posts, opl.Posts) {
|
|
|
|
|
t.Fatal("posts should have matched")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInTeam(user.Id, team1.Id, 0, 1)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 1 {
|
|
|
|
|
t.Fatal("should have returned 1 post")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInTeam(user.Id, team1.Id, 1, 1)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 1 {
|
|
|
|
|
t.Fatal("should have returned 1 post")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInTeam(user.Id, team1.Id, 1000, 10)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 0 {
|
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInTeam(user.Id, GenerateTestId(), 0, 10)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 0 {
|
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInTeam(user.Id, "junk", 0, 10)
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
if rpl != nil {
|
|
|
|
|
t.Fatal("should be nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
channel3 := th.CreatePrivateChannel()
|
|
|
|
|
post4 := th.CreatePostWithClient(Client, channel3)
|
|
|
|
|
|
|
|
|
|
preference.Name = post4.Id
|
|
|
|
|
Client.UpdatePreferences(user.Id, &model.Preferences{preference})
|
|
|
|
|
|
|
|
|
|
opl.AddPost(post4)
|
|
|
|
|
opl.AddOrder(post4.Id)
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUser(user.Id, 0, 10)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 3 {
|
|
|
|
|
t.Fatal("should have returned 3 posts")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(rpl.Posts, opl.Posts) {
|
|
|
|
|
t.Fatal("posts should have matched")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUser(user.Id, 0, 2)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 2 {
|
|
|
|
|
t.Fatal("should have returned 2 posts")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUser(user.Id, 2, 2)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 1 {
|
|
|
|
|
t.Fatal("should have returned 1 post")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUser(user.Id, 1000, 10)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(rpl.Posts) != 0 {
|
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetFlaggedPostsForUser("junk", 0, 10)
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetFlaggedPostsForUser(GenerateTestId(), 0, 10)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInChannel(user.Id, channel1.Id, 0, 10)
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUserInTeam(user.Id, team1.Id, 0, 10)
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
rpl, resp = Client.GetFlaggedPostsForUser(user.Id, 0, 10)
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
rpl, resp = th.SystemAdminClient.GetFlaggedPostsForUserInChannel(user.Id, channel1.Id, 0, 10)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
rpl, resp = th.SystemAdminClient.GetFlaggedPostsForUserInTeam(user.Id, team1.Id, 0, 10)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
rpl, resp = th.SystemAdminClient.GetFlaggedPostsForUser(user.Id, 0, 10)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 16:46:11 -04:00
|
|
|
func TestGetPostsAfterAndBefore(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-03-24 16:46:11 -04:00
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
post1 := th.CreatePost()
|
|
|
|
|
post2 := th.CreatePost()
|
|
|
|
|
post3 := th.CreatePost()
|
|
|
|
|
post4 := th.CreatePost()
|
|
|
|
|
post5 := th.CreatePost()
|
|
|
|
|
|
|
|
|
|
posts, resp := Client.GetPostsBefore(th.BasicChannel.Id, post3.Id, 0, 100, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
found := make([]bool, 2)
|
|
|
|
|
for _, p := range posts.Posts {
|
|
|
|
|
if p.Id == post1.Id {
|
|
|
|
|
found[0] = true
|
|
|
|
|
} else if p.Id == post2.Id {
|
|
|
|
|
found[1] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p.Id == post4.Id || p.Id == post5.Id {
|
|
|
|
|
t.Fatal("returned posts after")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, f := range found {
|
|
|
|
|
if !f {
|
|
|
|
|
t.Fatal("missing post")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.GetPostsBefore(th.BasicChannel.Id, post3.Id, 1, 1, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(posts.Posts) != 1 {
|
|
|
|
|
t.Fatal("too many posts returned")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.GetPostsBefore(th.BasicChannel.Id, "junk", 1, 1, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(posts.Posts) != 0 {
|
|
|
|
|
t.Fatal("should have no posts")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.GetPostsAfter(th.BasicChannel.Id, post3.Id, 0, 100, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
found = make([]bool, 2)
|
|
|
|
|
for _, p := range posts.Posts {
|
|
|
|
|
if p.Id == post4.Id {
|
|
|
|
|
found[0] = true
|
|
|
|
|
} else if p.Id == post5.Id {
|
|
|
|
|
found[1] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p.Id == post1.Id || p.Id == post2.Id {
|
|
|
|
|
t.Fatal("returned posts before")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, f := range found {
|
|
|
|
|
if !f {
|
|
|
|
|
t.Fatal("missing post")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.GetPostsAfter(th.BasicChannel.Id, post3.Id, 1, 1, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(posts.Posts) != 1 {
|
|
|
|
|
t.Fatal("too many posts returned")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.GetPostsAfter(th.BasicChannel.Id, "junk", 1, 1, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(posts.Posts) != 0 {
|
|
|
|
|
t.Fatal("should have no posts")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 10:52:50 -05:00
|
|
|
func TestGetPost(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-13 10:52:50 -05:00
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
post, resp := Client.GetPost(th.BasicPost.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if post.Id != th.BasicPost.Id {
|
|
|
|
|
t.Fatal("post ids don't match")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post, resp = Client.GetPost(th.BasicPost.Id, resp.Etag)
|
|
|
|
|
CheckEtag(t, post, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetPost("", "")
|
|
|
|
|
CheckNotFoundStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetPost("junk", "")
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetPost(model.NewId(), "")
|
2017-06-26 08:16:57 -04:00
|
|
|
CheckNotFoundStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id)
|
|
|
|
|
|
|
|
|
|
// Channel is public, should be able to read post
|
|
|
|
|
post, resp = Client.GetPost(th.BasicPost.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
privatePost := th.CreatePostWithClient(Client, th.BasicPrivateChannel)
|
|
|
|
|
|
|
|
|
|
post, resp = Client.GetPost(privatePost.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id)
|
|
|
|
|
|
|
|
|
|
// Channel is private, should not be able to read post
|
|
|
|
|
post, resp = Client.GetPost(privatePost.Id, "")
|
2017-02-13 10:52:50 -05:00
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
_, resp = Client.GetPost(model.NewId(), "")
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
post, resp = th.SystemAdminClient.GetPost(th.BasicPost.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 07:36:52 -05:00
|
|
|
func TestDeletePost(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-21 07:36:52 -05:00
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
_, resp := Client.DeletePost("")
|
|
|
|
|
CheckNotFoundStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.DeletePost("junk")
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.DeletePost(th.BasicPost.Id)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Login(th.TeamAdminUser.Email, th.TeamAdminUser.Password)
|
|
|
|
|
_, resp = Client.DeletePost(th.BasicPost.Id)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
post := th.CreatePost()
|
|
|
|
|
user := th.CreateUser()
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
Client.Login(user.Email, user.Password)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.DeletePost(post.Id)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
_, resp = Client.DeletePost(model.NewId())
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
status, resp := th.SystemAdminClient.DeletePost(post.Id)
|
2017-10-30 12:57:24 -04:00
|
|
|
if !status {
|
2017-02-21 07:36:52 -05:00
|
|
|
t.Fatal("post should return status OK")
|
|
|
|
|
}
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 10:52:50 -05:00
|
|
|
func TestGetPostThread(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-13 10:52:50 -05:00
|
|
|
Client := th.Client
|
|
|
|
|
|
2017-06-19 12:08:52 -04:00
|
|
|
post := &model.Post{ChannelId: th.BasicChannel.Id, Message: "zz" + model.NewId() + "a", RootId: th.BasicPost.Id}
|
2017-02-13 10:52:50 -05:00
|
|
|
post, _ = Client.CreatePost(post)
|
|
|
|
|
|
|
|
|
|
list, resp := Client.GetPostThread(th.BasicPost.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
var list2 *model.PostList
|
|
|
|
|
list2, resp = Client.GetPostThread(th.BasicPost.Id, resp.Etag)
|
|
|
|
|
CheckEtag(t, list2, resp)
|
|
|
|
|
|
|
|
|
|
if list.Order[0] != th.BasicPost.Id {
|
|
|
|
|
t.Fatal("wrong order")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, ok := list.Posts[th.BasicPost.Id]; !ok {
|
|
|
|
|
t.Fatal("should have had post")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, ok := list.Posts[post.Id]; !ok {
|
|
|
|
|
t.Fatal("should have had post")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetPostThread("junk", "")
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetPostThread(model.NewId(), "")
|
2017-06-26 08:16:57 -04:00
|
|
|
CheckNotFoundStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id)
|
|
|
|
|
|
|
|
|
|
// Channel is public, should be able to read post
|
|
|
|
|
_, resp = Client.GetPostThread(th.BasicPost.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
privatePost := th.CreatePostWithClient(Client, th.BasicPrivateChannel)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetPostThread(privatePost.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id)
|
|
|
|
|
|
|
|
|
|
// Channel is private, should not be able to read post
|
|
|
|
|
_, resp = Client.GetPostThread(privatePost.Id, "")
|
2017-02-13 10:52:50 -05:00
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
_, resp = Client.GetPostThread(model.NewId(), "")
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
list, resp = th.SystemAdminClient.GetPostThread(th.BasicPost.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
}
|
2017-02-21 07:36:52 -05:00
|
|
|
|
|
|
|
|
func TestSearchPosts(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-21 07:36:52 -05:00
|
|
|
th.LoginBasic()
|
|
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
message := "search for post1"
|
|
|
|
|
_ = th.CreateMessagePost(message)
|
|
|
|
|
|
|
|
|
|
message = "search for post2"
|
|
|
|
|
post2 := th.CreateMessagePost(message)
|
|
|
|
|
|
|
|
|
|
message = "#hashtag search for post3"
|
|
|
|
|
post3 := th.CreateMessagePost(message)
|
|
|
|
|
|
|
|
|
|
message = "hashtag for post4"
|
|
|
|
|
_ = th.CreateMessagePost(message)
|
|
|
|
|
|
|
|
|
|
posts, resp := Client.SearchPosts(th.BasicTeam.Id, "search", false)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
if len(posts.Order) != 3 {
|
|
|
|
|
t.Fatal("wrong search")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.SearchPosts(th.BasicTeam.Id, "post2", false)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
if len(posts.Order) != 1 && posts.Order[0] == post2.Id {
|
|
|
|
|
t.Fatal("wrong search")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.SearchPosts(th.BasicTeam.Id, "#hashtag", false)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
if len(posts.Order) != 1 && posts.Order[0] == post3.Id {
|
|
|
|
|
t.Fatal("wrong search")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, resp = Client.SearchPosts(th.BasicTeam.Id, "*", false); len(posts.Order) != 0 {
|
|
|
|
|
t.Fatal("searching for just * shouldn't return any results")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts, resp = Client.SearchPosts(th.BasicTeam.Id, "post1 post2", true)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
if len(posts.Order) != 2 {
|
|
|
|
|
t.Fatal("wrong search results")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, resp = Client.SearchPosts("junk", "#sgtitlereview", false)
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.SearchPosts(model.NewId(), "#sgtitlereview", false)
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.SearchPosts(th.BasicTeam.Id, "", false)
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
_, resp = Client.SearchPosts(th.BasicTeam.Id, "#sgtitlereview", false)
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSearchHashtagPosts(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-21 07:36:52 -05:00
|
|
|
th.LoginBasic()
|
|
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
message := "#sgtitlereview with space"
|
|
|
|
|
_ = th.CreateMessagePost(message)
|
|
|
|
|
|
|
|
|
|
message = "#sgtitlereview\n with return"
|
|
|
|
|
_ = th.CreateMessagePost(message)
|
|
|
|
|
|
|
|
|
|
message = "no hashtag"
|
|
|
|
|
_ = th.CreateMessagePost(message)
|
|
|
|
|
|
|
|
|
|
posts, resp := Client.SearchPosts(th.BasicTeam.Id, "#sgtitlereview", false)
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
if len(posts.Order) != 2 {
|
|
|
|
|
t.Fatal("wrong search results")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
_, resp = Client.SearchPosts(th.BasicTeam.Id, "#sgtitlereview", false)
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSearchPostsInChannel(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-21 07:36:52 -05:00
|
|
|
th.LoginBasic()
|
|
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
channel := th.CreatePublicChannel()
|
|
|
|
|
|
|
|
|
|
message := "sgtitlereview with space"
|
|
|
|
|
_ = th.CreateMessagePost(message)
|
|
|
|
|
|
|
|
|
|
message = "sgtitlereview\n with return"
|
|
|
|
|
_ = th.CreateMessagePostWithClient(Client, th.BasicChannel2, message)
|
|
|
|
|
|
|
|
|
|
message = "other message with no return"
|
|
|
|
|
_ = th.CreateMessagePostWithClient(Client, th.BasicChannel2, message)
|
|
|
|
|
|
|
|
|
|
message = "other message with no return"
|
|
|
|
|
_ = th.CreateMessagePostWithClient(Client, channel, message)
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "channel:", false); len(posts.Order) != 0 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "in:", false); len(posts.Order) != 0 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "channel:"+th.BasicChannel.Name, false); len(posts.Order) != 2 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "in:"+th.BasicChannel2.Name, false); len(posts.Order) != 2 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "channel:"+th.BasicChannel2.Name, false); len(posts.Order) != 2 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "ChAnNeL:"+th.BasicChannel2.Name, false); len(posts.Order) != 2 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "sgtitlereview", false); len(posts.Order) != 2 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "sgtitlereview channel:"+th.BasicChannel.Name, false); len(posts.Order) != 1 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "sgtitlereview in: "+th.BasicChannel2.Name, false); len(posts.Order) != 1 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "sgtitlereview channel: "+th.BasicChannel2.Name, false); len(posts.Order) != 1 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "channel: "+th.BasicChannel2.Name+" channel: "+channel.Name, false); len(posts.Order) != 3 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSearchPostsFromUser(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-21 07:36:52 -05:00
|
|
|
Client := th.Client
|
|
|
|
|
|
|
|
|
|
th.LoginTeamAdmin()
|
|
|
|
|
user := th.CreateUser()
|
2017-10-02 04:50:56 -04:00
|
|
|
th.LinkUserToTeam(user, th.BasicTeam)
|
2017-09-06 18:12:54 -04:00
|
|
|
th.App.AddUserToChannel(user, th.BasicChannel)
|
|
|
|
|
th.App.AddUserToChannel(user, th.BasicChannel2)
|
2017-02-21 07:36:52 -05:00
|
|
|
|
|
|
|
|
message := "sgtitlereview with space"
|
|
|
|
|
_ = th.CreateMessagePost(message)
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
th.LoginBasic2()
|
|
|
|
|
|
|
|
|
|
message = "sgtitlereview\n with return"
|
|
|
|
|
_ = th.CreateMessagePostWithClient(Client, th.BasicChannel2, message)
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "from: "+th.TeamAdminUser.Username, false); len(posts.Order) != 2 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "from: "+th.BasicUser2.Username, false); len(posts.Order) != 1 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "from: "+th.BasicUser2.Username+" sgtitlereview", false); len(posts.Order) != 1 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message = "hullo"
|
|
|
|
|
_ = th.CreateMessagePost(message)
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "from: "+th.BasicUser2.Username+" in:"+th.BasicChannel.Name, false); len(posts.Order) != 1 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client.Login(user.Email, user.Password)
|
|
|
|
|
|
|
|
|
|
// wait for the join/leave messages to be created for user3 since they're done asynchronously
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "from: "+th.BasicUser2.Username, false); len(posts.Order) != 2 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "from: "+th.BasicUser2.Username+" from: "+user.Username, false); len(posts.Order) != 2 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "from: "+th.BasicUser2.Username+" from: "+user.Username+" in:"+th.BasicChannel2.Name, false); len(posts.Order) != 1 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message = "coconut"
|
|
|
|
|
_ = th.CreateMessagePostWithClient(Client, th.BasicChannel2, message)
|
|
|
|
|
|
|
|
|
|
if posts, _ := Client.SearchPosts(th.BasicTeam.Id, "from: "+th.BasicUser2.Username+" from: "+user.Username+" in:"+th.BasicChannel2.Name+" coconut", false); len(posts.Order) != 1 {
|
|
|
|
|
t.Fatalf("wrong number of posts returned %v", len(posts.Order))
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-24 06:15:13 -05:00
|
|
|
|
|
|
|
|
func TestGetFileInfosForPost(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic().InitSystemAdmin()
|
2017-10-02 04:50:56 -04:00
|
|
|
defer th.TearDown()
|
2017-02-24 06:15:13 -05:00
|
|
|
Client := th.Client
|
|
|
|
|
|
2017-10-30 12:57:24 -04:00
|
|
|
fileIds := make([]string, 3)
|
2017-02-24 06:15:13 -05:00
|
|
|
if data, err := readTestFile("test.png"); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else {
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
|
fileResp, _ := Client.UploadFile(data, th.BasicChannel.Id, "test.png")
|
|
|
|
|
fileIds[i] = fileResp.FileInfos[0].Id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 12:08:52 -04:00
|
|
|
post := &model.Post{ChannelId: th.BasicChannel.Id, Message: "zz" + model.NewId() + "a", FileIds: fileIds}
|
2017-02-24 06:15:13 -05:00
|
|
|
post, _ = Client.CreatePost(post)
|
|
|
|
|
|
|
|
|
|
infos, resp := Client.GetFileInfosForPost(post.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(infos) != 3 {
|
|
|
|
|
t.Fatal("missing file infos")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
found := false
|
|
|
|
|
for _, info := range infos {
|
|
|
|
|
if info.Id == fileIds[0] {
|
|
|
|
|
found = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
|
t.Fatal("missing file info")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
infos, resp = Client.GetFileInfosForPost(post.Id, resp.Etag)
|
|
|
|
|
CheckEtag(t, infos, resp)
|
|
|
|
|
|
|
|
|
|
infos, resp = Client.GetFileInfosForPost(th.BasicPost.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
|
|
|
|
if len(infos) != 0 {
|
|
|
|
|
t.Fatal("should have no file infos")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetFileInfosForPost("junk", "")
|
|
|
|
|
CheckBadRequestStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = Client.GetFileInfosForPost(model.NewId(), "")
|
|
|
|
|
CheckForbiddenStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
Client.Logout()
|
|
|
|
|
_, resp = Client.GetFileInfosForPost(model.NewId(), "")
|
|
|
|
|
CheckUnauthorizedStatus(t, resp)
|
|
|
|
|
|
|
|
|
|
_, resp = th.SystemAdminClient.GetFileInfosForPost(th.BasicPost.Id, "")
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
}
|