2019-11-29 12:59:40 +01:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2017-02-13 10:52:50 -05:00
package api4
import (
2021-03-31 09:40:35 +02:00
"context"
2017-07-19 03:43:31 +08:00
"encoding/json"
2020-09-29 10:33:15 +03:00
"errors"
2017-11-21 00:57:35 +01:00
"fmt"
2017-02-13 10:52:50 -05:00
"net/http"
2017-07-19 03:43:31 +08:00
"net/http/httptest"
"net/url"
2017-03-30 00:06:51 +09:00
"reflect"
2019-07-30 06:27:18 -03:00
"sort"
2017-07-19 03:43:31 +08: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
2018-09-26 07:27:04 -07:00
"github.com/stretchr/testify/assert"
2019-07-04 05:23:04 +08:00
"github.com/stretchr/testify/require"
2018-09-26 07:27:04 -07:00
2019-11-28 14:39:38 +01:00
"github.com/mattermost/mattermost-server/v5/app"
"github.com/mattermost/mattermost-server/v5/model"
2020-09-29 10:33:15 +03:00
"github.com/mattermost/mattermost-server/v5/plugin/plugintest/mock"
"github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
2019-11-28 14:39:38 +01:00
"github.com/mattermost/mattermost-server/v5/utils"
"github.com/mattermost/mattermost-server/v5/utils/testutils"
2017-02-13 10:52:50 -05:00
)
func TestCreatePost ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-02-13 10:52:50 -05:00
Client := th . Client
2017-10-21 01:38:26 +08: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
2019-11-29 07:41:51 +09:00
require . Equal ( t , post . Message , rpost . Message , "message didn't match" )
require . Equal ( t , "#hashtag" , rpost . Hashtags , "hashtag didn't match" )
2019-12-22 12:35:31 +01:00
require . Empty ( t , rpost . FileIds )
2019-11-29 07:41:51 +09:00
require . Equal ( t , 0 , int ( rpost . EditAt ) , "newly created post shouldn't have EditAt set" )
2020-03-13 21:12:20 +01:00
require . Nil ( t , rpost . GetProp ( model . PROPS_ADD_CHANNEL_MEMBER ) , "newly created post shouldn't have Props['add_channel_member'] set" )
2017-10-21 01:38:26 +08:00
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 09:08:52 -07:00
post2 := & model . Post { ChannelId : th . BasicChannel2 . Id , Message : "zz" + model . NewId ( ) + "a" , CreateAt : 123 }
2018-09-03 14:08:40 +02:00
rpost2 , _ := Client . CreatePost ( post2 )
2019-11-29 07:41:51 +09:00
require . NotEqual ( t , post2 . CreateAt , rpost2 . CreateAt , "create at should not match" )
2017-02-13 10:52:50 -05:00
2019-08-12 18:35:46 -03:00
t . Run ( "with file uploaded by same user" , func ( t * testing . T ) {
fileResp , subResponse := Client . UploadFile ( [ ] byte ( "data" ) , th . BasicChannel . Id , "test" )
CheckNoError ( t , subResponse )
fileId := fileResp . FileInfos [ 0 ] . Id
postWithFiles , subResponse := Client . CreatePost ( & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "with files" ,
FileIds : model . StringArray { fileId } ,
} )
CheckNoError ( t , subResponse )
assert . Equal ( t , model . StringArray { fileId } , postWithFiles . FileIds )
actualPostWithFiles , subResponse := Client . GetPost ( postWithFiles . Id , "" )
CheckNoError ( t , subResponse )
assert . Equal ( t , model . StringArray { fileId } , actualPostWithFiles . FileIds )
} )
t . Run ( "with file uploaded by different user" , func ( t * testing . T ) {
fileResp , subResponse := th . SystemAdminClient . UploadFile ( [ ] byte ( "data" ) , th . BasicChannel . Id , "test" )
CheckNoError ( t , subResponse )
fileId := fileResp . FileInfos [ 0 ] . Id
postWithFiles , subResponse := Client . CreatePost ( & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "with files" ,
FileIds : model . StringArray { fileId } ,
} )
CheckNoError ( t , subResponse )
assert . Empty ( t , postWithFiles . FileIds )
actualPostWithFiles , subResponse := Client . GetPost ( postWithFiles . Id , "" )
CheckNoError ( t , subResponse )
assert . Empty ( t , actualPostWithFiles . FileIds )
} )
t . Run ( "with file uploaded by nouser" , func ( t * testing . T ) {
fileInfo , err := th . App . UploadFile ( [ ] byte ( "data" ) , th . BasicChannel . Id , "test" )
require . Nil ( t , err )
fileId := fileInfo . Id
postWithFiles , subResponse := Client . CreatePost ( & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "with files" ,
FileIds : model . StringArray { fileId } ,
} )
CheckNoError ( t , subResponse )
assert . Equal ( t , model . StringArray { fileId } , postWithFiles . FileIds )
actualPostWithFiles , subResponse := Client . GetPost ( postWithFiles . Id , "" )
CheckNoError ( t , subResponse )
assert . Equal ( t , model . StringArray { fileId } , actualPostWithFiles . FileIds )
} )
2020-03-03 05:22:49 -05:00
t . Run ( "Create posts without the USE_CHANNEL_MENTIONS Permission - returns ephemeral message with mentions and no ephemeral message without mentions" , func ( t * testing . T ) {
WebSocketClient , err := th . CreateWebSocketClient ( )
WebSocketClient . Listen ( )
require . Nil ( t , err )
2020-02-12 10:45:34 -05:00
defer th . RestoreDefaultRolePermissions ( th . SaveDefaultRolePermissions ( ) )
th . RemovePermissionFromRole ( model . PERMISSION_USE_CHANNEL_MENTIONS . Id , model . CHANNEL_USER_ROLE_ID )
2020-03-03 05:22:49 -05:00
post . RootId = rpost . Id
post . ParentId = rpost . Id
post . Message = "a post with no channel mentions"
_ , resp = Client . CreatePost ( post )
CheckNoError ( t , resp )
// Message with no channel mentions should result in no ephemeral message
timeout := time . After ( 300 * time . Millisecond )
waiting := true
for waiting {
select {
case event := <- WebSocketClient . EventChannel :
require . NotEqual ( t , model . WEBSOCKET_EVENT_EPHEMERAL_MESSAGE , event . EventType ( ) , "should not have ephemeral message event" )
case <- timeout :
waiting = false
}
}
2020-02-12 10:45:34 -05:00
post . RootId = rpost . Id
post . ParentId = rpost . Id
post . Message = "a post with @channel"
_ , resp = Client . CreatePost ( post )
CheckNoError ( t , resp )
post . RootId = rpost . Id
post . ParentId = rpost . Id
post . Message = "a post with @all"
_ , resp = Client . CreatePost ( post )
CheckNoError ( t , resp )
post . RootId = rpost . Id
post . ParentId = rpost . Id
post . Message = "a post with @here"
_ , resp = Client . CreatePost ( post )
CheckNoError ( t , resp )
2020-03-03 05:22:49 -05:00
timeout = time . After ( 600 * time . Millisecond )
eventsToGo := 3 // 3 Posts created with @ mentions should result in 3 websocket events
for eventsToGo > 0 {
select {
case event := <- WebSocketClient . EventChannel :
if event . Event == model . WEBSOCKET_EVENT_EPHEMERAL_MESSAGE {
require . Equal ( t , model . WEBSOCKET_EVENT_EPHEMERAL_MESSAGE , event . Event )
eventsToGo = eventsToGo - 1
}
case <- timeout :
require . Fail ( t , "Should have received ephemeral message event and not timedout" )
eventsToGo = 0
}
}
2020-02-12 10:45:34 -05:00
} )
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 )
2019-11-29 07:41:51 +09:00
r , err := Client . DoApiPost ( "/posts" , "garbage" )
2021-02-17 09:52:18 +01:00
require . NotNil ( t , err )
2019-11-29 07:41:51 +09:00
require . Equal ( t , http . StatusBadRequest , r . StatusCode )
2017-02-13 10:52:50 -05:00
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 )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post . CreateAt , rpost . CreateAt , "create at should match" )
2017-02-13 10:52:50 -05:00
}
2018-04-17 14:20:47 +02:00
func TestCreatePostEphemeral ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2018-04-17 14:20:47 +02:00
defer th . TearDown ( )
Client := th . SystemAdminClient
ephemeralPost := & model . PostEphemeral {
UserID : th . BasicUser2 . Id ,
Post : & model . Post { ChannelId : th . BasicChannel . Id , Message : "a" + model . NewId ( ) + "a" , Props : model . StringInterface { model . PROPS_ADD_CHANNEL_MEMBER : "no good" } } ,
}
rpost , resp := Client . CreatePostEphemeral ( ephemeralPost )
CheckNoError ( t , resp )
CheckCreatedStatus ( t , resp )
2019-11-29 07:41:51 +09:00
require . Equal ( t , ephemeralPost . Post . Message , rpost . Message , "message didn't match" )
require . Equal ( t , 0 , int ( rpost . EditAt ) , "newly created ephemeral post shouldn't have EditAt set" )
2018-04-17 14:20:47 +02:00
2019-11-29 07:41:51 +09:00
r , err := Client . DoApiPost ( "/posts/ephemeral" , "garbage" )
2021-02-17 09:52:18 +01:00
require . NotNil ( t , err )
2019-11-29 07:41:51 +09:00
require . Equal ( t , http . StatusBadRequest , r . StatusCode )
2018-04-17 14:20:47 +02:00
Client . Logout ( )
_ , resp = Client . CreatePostEphemeral ( ephemeralPost )
CheckUnauthorizedStatus ( t , resp )
Client = th . Client
2018-09-03 14:08:40 +02:00
_ , resp = Client . CreatePostEphemeral ( ephemeralPost )
2018-04-17 14:20:47 +02:00
CheckForbiddenStatus ( t , resp )
}
2017-07-19 03:43:31 +08:00
func testCreatePostWithOutgoingHook (
t * testing . T ,
hookContentType , expectedContentType , message , triggerWord string ,
fileIds [ ] string ,
triggerWhen int ,
2017-11-21 00:57:35 +01:00
commentPostType bool ,
2017-07-19 03:43:31 +08:00
) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-07-19 03:43:31 +08:00
user := th . SystemAdminUser
team := th . BasicTeam
channel := th . BasicChannel
2019-01-31 08:12:01 -05:00
enableOutgoingWebhooks := * th . App . Config ( ) . ServiceSettings . EnableOutgoingWebhooks
allowedUntrustedInternalConnections := * th . App . Config ( ) . ServiceSettings . AllowedUntrustedInternalConnections
2018-07-11 12:58:16 +02:00
defer func ( ) {
2019-01-31 08:12:01 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . ServiceSettings . EnableOutgoingWebhooks = enableOutgoingWebhooks } )
2018-07-11 12:58:16 +02:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2019-01-31 08:12:01 -05:00
* cfg . ServiceSettings . AllowedUntrustedInternalConnections = allowedUntrustedInternalConnections
2018-07-11 12:58:16 +02:00
} )
} ( )
2019-01-31 08:12:01 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . ServiceSettings . EnableOutgoingWebhooks = true } )
2017-10-18 15:36:43 -07:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2019-07-17 16:04:09 +02:00
* cfg . ServiceSettings . AllowedUntrustedInternalConnections = "localhost,127.0.0.1"
2017-10-18 15:36:43 -07:00
} )
2017-07-19 03:43:31 +08: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 ( ) )
2017-11-21 00:57:35 +01:00
2017-07-19 03:43:31 +08:00
if ! reflect . DeepEqual ( expectedFormValues , r . Form ) {
t . Logf ( "Form values are: %q\n, should be: %q\n" , r . Form , expectedFormValues )
success <- false
return
}
}
2017-11-21 00:57:35 +01:00
respPostType := "" //if is empty or post will do a normal post.
if commentPostType {
respPostType = model . OUTGOING_HOOK_RESPONSE_TYPE_COMMENT
}
outGoingHookResponse := & model . OutgoingWebhookResponse {
Text : model . NewString ( "some test text" ) ,
Username : "TestCommandServer" ,
IconURL : "https://www.mattermost.org/wp-content/uploads/2016/04/icon.png" ,
Type : "custom_as" ,
ResponseType : respPostType ,
}
fmt . Fprintf ( w , outGoingHookResponse . ToJson ( ) )
2017-07-19 03:43:31 +08:00
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-19 03:43:31 +08: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 :
2019-11-29 07:41:51 +09:00
require . True ( t , ok , "Test server did send an invalid webhook." )
2017-07-19 03:43:31 +08:00
case <- time . After ( time . Second ) :
2019-11-29 07:41:51 +09:00
require . FailNow ( t , "Timeout, test server did not send the webhook." )
2017-07-19 03:43:31 +08:00
}
2017-11-21 00:57:35 +01:00
if commentPostType {
time . Sleep ( time . Millisecond * 100 )
2021-01-14 13:46:27 +02:00
postList , resp := th . SystemAdminClient . GetPostThread ( post . Id , "" , false )
2017-11-21 00:57:35 +01:00
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post . Id , postList . Order [ 0 ] , "wrong order" )
2017-11-21 00:57:35 +01:00
2019-11-29 07:41:51 +09:00
_ , ok := postList . Posts [ post . Id ]
require . True ( t , ok , "should have had post" )
require . Len ( t , postList . Posts , 2 , "should have 2 posts" )
2017-11-21 00:57:35 +01:00
}
2017-07-19 03:43:31 +08:00
}
func TestCreatePostWithOutgoingHook_form_urlencoded ( t * testing . T ) {
2021-01-04 11:32:29 +05:30
testCreatePostWithOutgoingHook ( t , "application/x-www-form-urlencoded" , "application/x-www-form-urlencoded" , "triggerword lorem ipsum" , "triggerword" , [ ] string { "file_id_1" } , app . TriggerwordsExactMatch , false )
testCreatePostWithOutgoingHook ( t , "application/x-www-form-urlencoded" , "application/x-www-form-urlencoded" , "triggerwordaaazzz lorem ipsum" , "triggerword" , [ ] string { "file_id_1" } , app . TriggerwordsStartsWith , false )
testCreatePostWithOutgoingHook ( t , "application/x-www-form-urlencoded" , "application/x-www-form-urlencoded" , "" , "" , [ ] string { "file_id_1" } , app . TriggerwordsExactMatch , false )
testCreatePostWithOutgoingHook ( t , "application/x-www-form-urlencoded" , "application/x-www-form-urlencoded" , "" , "" , [ ] string { "file_id_1" } , app . TriggerwordsStartsWith , false )
testCreatePostWithOutgoingHook ( t , "application/x-www-form-urlencoded" , "application/x-www-form-urlencoded" , "triggerword lorem ipsum" , "triggerword" , [ ] string { "file_id_1" } , app . TriggerwordsExactMatch , true )
testCreatePostWithOutgoingHook ( t , "application/x-www-form-urlencoded" , "application/x-www-form-urlencoded" , "triggerwordaaazzz lorem ipsum" , "triggerword" , [ ] string { "file_id_1" } , app . TriggerwordsStartsWith , true )
2017-07-19 03:43:31 +08:00
}
func TestCreatePostWithOutgoingHook_json ( t * testing . T ) {
2021-01-04 11:32:29 +05:30
testCreatePostWithOutgoingHook ( t , "application/json" , "application/json" , "triggerword lorem ipsum" , "triggerword" , [ ] string { "file_id_1, file_id_2" } , app . TriggerwordsExactMatch , false )
testCreatePostWithOutgoingHook ( t , "application/json" , "application/json" , "triggerwordaaazzz lorem ipsum" , "triggerword" , [ ] string { "file_id_1, file_id_2" } , app . TriggerwordsStartsWith , false )
testCreatePostWithOutgoingHook ( t , "application/json" , "application/json" , "triggerword lorem ipsum" , "" , [ ] string { "file_id_1" } , app . TriggerwordsExactMatch , false )
testCreatePostWithOutgoingHook ( t , "application/json" , "application/json" , "triggerwordaaazzz lorem ipsum" , "" , [ ] string { "file_id_1" } , app . TriggerwordsStartsWith , false )
testCreatePostWithOutgoingHook ( t , "application/json" , "application/json" , "triggerword lorem ipsum" , "triggerword" , [ ] string { "file_id_1, file_id_2" } , app . TriggerwordsExactMatch , true )
testCreatePostWithOutgoingHook ( t , "application/json" , "application/json" , "triggerwordaaazzz lorem ipsum" , "" , [ ] string { "file_id_1" } , app . TriggerwordsStartsWith , true )
2017-07-19 03:43:31 +08:00
}
// 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 ) {
2021-01-04 11:32:29 +05:30
testCreatePostWithOutgoingHook ( t , "" , "application/x-www-form-urlencoded" , "triggerword lorem ipsum" , "triggerword" , [ ] string { "file_id_1" } , app . TriggerwordsExactMatch , false )
testCreatePostWithOutgoingHook ( t , "" , "application/x-www-form-urlencoded" , "triggerwordaaazzz lorem ipsum" , "triggerword" , [ ] string { "file_id_1" } , app . TriggerwordsStartsWith , false )
testCreatePostWithOutgoingHook ( t , "" , "application/x-www-form-urlencoded" , "triggerword lorem ipsum" , "" , [ ] string { "file_id_1, file_id_2" } , app . TriggerwordsExactMatch , false )
testCreatePostWithOutgoingHook ( t , "" , "application/x-www-form-urlencoded" , "triggerwordaaazzz lorem ipsum" , "" , [ ] string { "file_id_1, file_id_2" } , app . TriggerwordsStartsWith , false )
testCreatePostWithOutgoingHook ( t , "" , "application/x-www-form-urlencoded" , "triggerword lorem ipsum" , "triggerword" , [ ] string { "file_id_1" } , app . TriggerwordsExactMatch , true )
testCreatePostWithOutgoingHook ( t , "" , "application/x-www-form-urlencoded" , "triggerword lorem ipsum" , "" , [ ] string { "file_id_1, file_id_2" } , app . TriggerwordsExactMatch , true )
2017-07-19 03:43:31 +08:00
}
2017-07-31 12:59:32 -04:00
func TestCreatePostPublic ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05: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" }
2018-01-17 12:38:37 -06:00
user := model . User { Email : th . GenerateTestEmail ( ) , Nickname : "Joram Wilander" , Password : "hello1" , Username : GenerateTestUsername ( ) , Roles : model . SYSTEM_USER_ROLE_ID }
2017-07-31 12:59:32 -04:00
ruser , resp := Client . CreateUser ( & user )
CheckNoError ( t , resp )
Client . Login ( user . Email , user . Password )
_ , resp = Client . CreatePost ( post )
CheckForbiddenStatus ( t , resp )
2017-11-21 13:08:32 -06:00
th . App . UpdateUserRoles ( ruser . Id , model . SYSTEM_USER_ROLE_ID + " " + model . SYSTEM_POST_ALL_PUBLIC_ROLE_ID , false )
2020-06-12 13:43:50 +02:00
th . App . Srv ( ) . 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-11-21 13:08:32 -06:00
th . App . UpdateUserRoles ( ruser . Id , model . SYSTEM_USER_ROLE_ID , false )
2017-09-06 17:12:54 -05:00
th . App . JoinUserToTeam ( th . BasicTeam , ruser , "" )
2017-11-21 13:08:32 -06:00
th . App . UpdateTeamMemberRoles ( th . BasicTeam . Id , ruser . Id , model . TEAM_USER_ROLE_ID + " " + model . TEAM_POST_ALL_PUBLIC_ROLE_ID )
2020-06-12 13:43:50 +02:00
th . App . Srv ( ) . 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 ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05: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" }
2018-01-17 12:38:37 -06:00
user := model . User { Email : th . GenerateTestEmail ( ) , Nickname : "Joram Wilander" , Password : "hello1" , Username : GenerateTestUsername ( ) , Roles : model . SYSTEM_USER_ROLE_ID }
2017-08-01 11:06:53 -04:00
2018-11-28 18:01:49 +01:00
directChannel , _ := th . App . GetOrCreateDirectChannel ( 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-11-21 13:08:32 -06:00
th . App . UpdateUserRoles ( ruser . Id , model . SYSTEM_USER_ROLE_ID + " " + model . SYSTEM_POST_ALL_ROLE_ID , false )
2020-06-12 13:43:50 +02:00
th . App . Srv ( ) . 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-11-21 13:08:32 -06:00
th . App . UpdateUserRoles ( ruser . Id , model . SYSTEM_USER_ROLE_ID , false )
2017-09-06 17:12:54 -05:00
th . App . JoinUserToTeam ( th . BasicTeam , ruser , "" )
2017-11-21 13:08:32 -06:00
th . App . UpdateTeamMemberRoles ( th . BasicTeam . Id , ruser . Id , model . TEAM_USER_ROLE_ID + " " + model . TEAM_POST_ALL_ROLE_ID )
2020-06-12 13:43:50 +02:00
th . App . Srv ( ) . 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-21 01:38:26 +08:00
func TestCreatePostSendOutOfChannelMentions ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-21 01:38:26 +08:00
defer th . TearDown ( )
Client := th . Client
WebSocketClient , err := th . CreateWebSocketClient ( )
2019-11-29 07:41:51 +09:00
require . Nil ( t , err )
2017-10-21 01:38:26 +08:00
WebSocketClient . Listen ( )
inChannelUser := th . CreateUser ( )
th . LinkUserToTeam ( inChannelUser , th . BasicTeam )
2021-04-02 14:33:23 +05:30
th . App . AddUserToChannel ( inChannelUser , th . BasicChannel , false )
2017-10-21 01:38:26 +08:00
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 :
2019-12-24 09:32:11 +01:00
require . NotEqual ( t , model . WEBSOCKET_EVENT_EPHEMERAL_MESSAGE , event . EventType ( ) , "should not have ephemeral message event" )
2017-10-21 01:38:26 +08:00
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 :
2019-12-24 09:32:11 +01:00
if event . EventType ( ) != model . WEBSOCKET_EVENT_EPHEMERAL_MESSAGE {
2017-10-21 01:38:26 +08:00
// Ignore any other events
continue
}
2019-12-24 09:32:11 +01:00
wpost := model . PostFromJson ( strings . NewReader ( event . GetData ( ) [ "post" ] . ( string ) ) )
2019-11-29 07:41:51 +09:00
2020-03-13 21:12:20 +01:00
acm , ok := wpost . GetProp ( model . PROPS_ADD_CHANNEL_MEMBER ) . ( map [ string ] interface { } )
2019-11-29 07:41:51 +09:00
require . True ( t , ok , "should have received ephemeral post with 'add_channel_member' in props" )
require . True ( t , acm [ "post_id" ] != nil , "should not be nil" )
require . True ( t , acm [ "user_ids" ] != nil , "should not be nil" )
require . True ( t , acm [ "usernames" ] != nil , "should not be nil" )
2017-10-21 01:38:26 +08:00
waiting = false
case <- timeout :
2019-11-29 07:41:51 +09:00
require . FailNow ( t , "timed out waiting for ephemeral message event" )
2017-10-21 01:38:26 +08:00
}
}
}
2020-01-13 20:20:56 +05:30
func TestCreatePostCheckOnlineStatus ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2020-01-13 20:20:56 +05:30
defer th . TearDown ( )
api := Init ( th . Server , th . Server . AppOptions , th . Server . Router )
session , _ := th . App . GetSession ( th . Client . AuthToken )
2020-05-27 17:35:02 +05:30
cli := th . CreateClient ( )
_ , loginResp := cli . Login ( th . BasicUser2 . Username , th . BasicUser2 . Password )
require . Nil ( t , loginResp . Error )
wsClient , err := th . CreateWebSocketClientWithClient ( cli )
require . Nil ( t , err )
2020-06-16 17:30:58 +05:30
defer wsClient . Close ( )
2020-05-27 17:35:02 +05:30
wsClient . Listen ( )
2020-06-16 17:30:58 +05:30
waitForEvent := func ( isSetOnline bool ) {
timeout := time . After ( 5 * time . Second )
for {
2020-06-04 12:52:40 +03:00
select {
case ev := <- wsClient . EventChannel :
if ev . EventType ( ) == model . WEBSOCKET_EVENT_POSTED {
2020-06-16 17:30:58 +05:30
assert . True ( t , ev . GetData ( ) [ "set_online" ] . ( bool ) == isSetOnline )
return
2020-05-27 17:35:02 +05:30
}
2020-06-04 12:52:40 +03:00
case <- timeout :
2020-06-30 08:25:47 +05:30
// We just skip the test instead of failing because waiting for more than 5 seconds
2021-04-01 13:44:56 -04:00
// to get a response does not make sense, and it will unnecessarily slow down
2020-06-30 08:25:47 +05:30
// the tests further in an already congested CI environment.
t . Skip ( "timed out waiting for event" )
2020-05-27 17:35:02 +05:30
}
}
2020-06-16 17:30:58 +05:30
}
2020-05-27 17:35:02 +05:30
2020-01-13 20:20:56 +05:30
handler := api . ApiHandler ( createPost )
resp := httptest . NewRecorder ( )
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "some message" ,
}
req := httptest . NewRequest ( "POST" , "/api/v4/posts?set_online=false" , strings . NewReader ( post . ToJson ( ) ) )
req . Header . Set ( model . HEADER_AUTH , "Bearer " + session . Token )
handler . ServeHTTP ( resp , req )
assert . Equal ( t , http . StatusCreated , resp . Code )
2020-06-16 17:30:58 +05:30
waitForEvent ( false )
2020-01-13 20:20:56 +05:30
2020-05-27 17:35:02 +05:30
_ , err = th . App . GetStatus ( th . BasicUser . Id )
2020-01-13 20:20:56 +05:30
require . NotNil ( t , err )
2020-08-13 01:42:08 -04:00
assert . Equal ( t , "app.status.get.missing.app_error" , err . Id )
2020-01-13 20:20:56 +05:30
req = httptest . NewRequest ( "POST" , "/api/v4/posts" , strings . NewReader ( post . ToJson ( ) ) )
req . Header . Set ( model . HEADER_AUTH , "Bearer " + session . Token )
handler . ServeHTTP ( resp , req )
assert . Equal ( t , http . StatusCreated , resp . Code )
2020-06-16 17:30:58 +05:30
waitForEvent ( true )
2020-01-13 20:20:56 +05:30
st , err := th . App . GetStatus ( th . BasicUser . Id )
require . Nil ( t , err )
assert . Equal ( t , "online" , st . Status )
}
2017-02-28 11:34:32 +02:00
func TestUpdatePost ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-02-28 11:34:32 +02:00
Client := th . Client
channel := th . BasicChannel
2020-06-12 13:43:50 +02:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2017-02-28 11:34:32 +02:00
2019-08-12 18:36:08 -03:00
fileIds := make ( [ ] string , 3 )
data , err := testutils . ReadTestFile ( "test.png" )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2019-08-12 18:36:08 -03:00
for i := 0 ; i < len ( fileIds ) ; i ++ {
fileResp , resp := Client . UploadFile ( data , channel . Id , "test.png" )
CheckNoError ( t , resp )
fileIds [ i ] = fileResp . FileInfos [ 0 ] . Id
2017-02-28 11:34:32 +02:00
}
2021-02-17 09:52:18 +01:00
rpost , appErr := th . App . CreatePost ( & model . Post {
2019-08-12 18:36:08 -03:00
UserId : th . BasicUser . Id ,
ChannelId : channel . Id ,
Message : "zz" + model . NewId ( ) + "a" ,
FileIds : fileIds ,
2020-05-27 17:35:02 +05:30
} , channel , false , true )
2021-02-17 09:52:18 +01:00
require . Nil ( t , appErr )
2019-08-12 18:36:08 -03:00
assert . Equal ( t , rpost . Message , rpost . Message , "full name didn't match" )
assert . EqualValues ( t , 0 , rpost . EditAt , "Newly created post shouldn't have EditAt set" )
assert . Equal ( t , model . StringArray ( fileIds ) , rpost . FileIds , "FileIds should have been set" )
t . Run ( "same message, fewer files" , func ( t * testing . T ) {
msg := "zz" + model . NewId ( ) + " update post"
rpost . Message = msg
rpost . UserId = ""
rupost , resp := Client . UpdatePost ( rpost . Id , & model . Post {
Id : rpost . Id ,
Message : rpost . Message ,
FileIds : fileIds [ 0 : 2 ] , // one fewer file id
} )
CheckNoError ( t , resp )
2017-02-28 11:34:32 +02:00
2019-08-12 18:36:08 -03:00
assert . Equal ( t , rupost . Message , msg , "failed to updates" )
assert . NotEqual ( t , 0 , rupost . EditAt , "EditAt not updated for post" )
assert . Equal ( t , model . StringArray ( fileIds ) , rupost . FileIds , "FileIds should have not have been updated" )
2017-06-27 04:05:50 +08:00
2019-08-12 18:36:08 -03:00
actual , resp := Client . GetPost ( rpost . Id , "" )
CheckNoError ( t , resp )
2017-02-28 11:34:32 +02:00
2019-08-12 18:36:08 -03:00
assert . Equal ( t , actual . Message , msg , "failed to updates" )
assert . NotEqual ( t , 0 , actual . EditAt , "EditAt not updated for post" )
assert . Equal ( t , model . StringArray ( fileIds ) , actual . FileIds , "FileIds should have not have been updated" )
} )
2017-02-28 11:34:32 +02:00
2019-08-12 18:36:08 -03:00
t . Run ( "new message, invalid props" , func ( t * testing . T ) {
msg1 := "#hashtag a" + model . NewId ( ) + " update post again"
rpost . Message = msg1
2020-03-13 21:12:20 +01:00
rpost . AddProp ( model . PROPS_ADD_CHANNEL_MEMBER , "no good" )
2019-08-12 18:36:08 -03:00
rrupost , resp := Client . UpdatePost ( rpost . Id , rpost )
CheckNoError ( t , resp )
2017-02-28 11:34:32 +02:00
2019-08-12 18:36:08 -03:00
assert . Equal ( t , msg1 , rrupost . Message , "failed to update message" )
assert . Equal ( t , "#hashtag" , rrupost . Hashtags , "failed to update hashtags" )
2020-03-13 21:12:20 +01:00
assert . Nil ( t , rrupost . GetProp ( model . PROPS_ADD_CHANNEL_MEMBER ) , "failed to sanitize Props['add_channel_member'], should be nil" )
2017-02-28 11:34:32 +02:00
2019-08-12 18:36:08 -03:00
actual , resp := Client . GetPost ( rpost . Id , "" )
CheckNoError ( t , resp )
2017-10-21 01:38:26 +08:00
2019-08-12 18:36:08 -03:00
assert . Equal ( t , msg1 , actual . Message , "failed to update message" )
assert . Equal ( t , "#hashtag" , actual . Hashtags , "failed to update hashtags" )
2020-03-13 21:12:20 +01:00
assert . Nil ( t , actual . GetProp ( model . PROPS_ADD_CHANNEL_MEMBER ) , "failed to sanitize Props['add_channel_member'], should be nil" )
2019-08-12 18:36:08 -03:00
} )
2017-02-28 11:34:32 +02:00
2019-08-12 18:36:08 -03:00
t . Run ( "join/leave post" , func ( t * testing . T ) {
rpost2 , err := th . App . CreatePost ( & model . Post {
ChannelId : channel . Id ,
Message : "zz" + model . NewId ( ) + "a" ,
Type : model . POST_JOIN_LEAVE ,
UserId : th . BasicUser . Id ,
2020-05-27 17:35:02 +05:30
} , channel , false , true )
2019-08-12 18:36:08 -03:00
require . Nil ( t , err )
2017-02-28 11:34:32 +02:00
2019-08-12 18:36:08 -03:00
up2 := & model . Post {
Id : rpost2 . Id ,
ChannelId : channel . Id ,
Message : "zz" + model . NewId ( ) + " update post 2" ,
}
_ , resp := Client . UpdatePost ( rpost2 . Id , up2 )
CheckBadRequestStatus ( t , resp )
} )
2019-04-04 20:01:21 +02:00
2021-02-17 09:52:18 +01:00
rpost3 , appErr := th . App . CreatePost ( & model . Post {
2019-08-12 18:36:08 -03:00
ChannelId : channel . Id ,
Message : "zz" + model . NewId ( ) + "a" ,
UserId : th . BasicUser . Id ,
2020-05-27 17:35:02 +05:30
} , channel , false , true )
2021-02-17 09:52:18 +01:00
require . Nil ( t , appErr )
2019-08-12 18:36:08 -03:00
t . Run ( "new message, add files" , func ( t * testing . T ) {
up3 := & model . Post {
Id : rpost3 . Id ,
ChannelId : channel . Id ,
Message : "zz" + model . NewId ( ) + " update post 3" ,
FileIds : fileIds [ 0 : 2 ] ,
}
rrupost3 , resp := Client . UpdatePost ( rpost3 . Id , up3 )
CheckNoError ( t , resp )
assert . Empty ( t , rrupost3 . FileIds )
2019-04-04 20:01:21 +02:00
2019-08-12 18:36:08 -03:00
actual , resp := Client . GetPost ( rpost . Id , "" )
CheckNoError ( t , resp )
assert . Equal ( t , model . StringArray ( fileIds ) , actual . FileIds )
2019-04-04 20:01:21 +02:00
} )
2019-08-12 18:36:08 -03:00
t . Run ( "add slack attachments" , func ( t * testing . T ) {
up4 := & model . Post {
Id : rpost3 . Id ,
ChannelId : channel . Id ,
Message : "zz" + model . NewId ( ) + " update post 3" ,
}
up4 . AddProp ( "attachments" , [ ] model . SlackAttachment {
{
Text : "Hello World" ,
} ,
} )
rrupost3 , resp := Client . UpdatePost ( rpost3 . Id , up4 )
CheckNoError ( t , resp )
assert . NotEqual ( t , rpost3 . EditAt , rrupost3 . EditAt )
assert . NotEqual ( t , rpost3 . Attachments ( ) , rrupost3 . Attachments ( ) )
} )
2017-04-04 15:17:47 -04:00
2019-08-12 18:36:08 -03:00
t . Run ( "logged out" , func ( t * testing . T ) {
Client . Logout ( )
_ , resp := Client . UpdatePost ( rpost . Id , rpost )
CheckUnauthorizedStatus ( t , resp )
} )
2017-04-04 15:17:47 -04:00
2019-08-12 18:36:08 -03:00
t . Run ( "different user" , func ( t * testing . T ) {
th . LoginBasic2 ( )
_ , resp := Client . UpdatePost ( rpost . Id , rpost )
CheckForbiddenStatus ( t , resp )
2017-04-04 15:17:47 -04:00
2019-08-12 18:36:08 -03:00
Client . Logout ( )
} )
2018-09-24 15:35:39 +02:00
2019-08-12 18:36:08 -03:00
t . Run ( "different user, but team admin" , func ( t * testing . T ) {
th . LoginTeamAdmin ( )
_ , resp := Client . UpdatePost ( rpost . Id , rpost )
CheckForbiddenStatus ( t , resp )
2018-09-24 15:35:39 +02:00
2019-08-12 18:36:08 -03:00
Client . Logout ( )
} )
t . Run ( "different user, but system admin" , func ( t * testing . T ) {
_ , resp := th . SystemAdminClient . UpdatePost ( rpost . Id , rpost )
CheckNoError ( t , resp )
} )
2017-02-28 11:34:32 +02:00
}
2018-09-27 20:15:47 +01:00
func TestUpdateOthersPostInDirectMessageChannel ( t * testing . T ) {
// This test checks that a sysadmin with the "EDIT_OTHERS_POSTS" permission can edit someone else's post in a
// channel without a team (DM/GM). This indirectly checks for the proper cascading all the way to system-wide roles
// on the user object of permissions based on a post in a channel with no team ID.
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2018-09-27 20:15:47 +01:00
defer th . TearDown ( )
dmChannel := th . CreateDmChannel ( th . SystemAdminUser )
post := & model . Post {
Message : "asd" ,
ChannelId : dmChannel . Id ,
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
UserId : th . BasicUser . Id ,
CreateAt : 0 ,
}
post , resp := th . Client . CreatePost ( post )
CheckNoError ( t , resp )
post . Message = "changed"
post , resp = th . SystemAdminClient . UpdatePost ( post . Id , post )
CheckNoError ( t , resp )
}
2017-03-30 00:06:51 +09:00
func TestPatchPost ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-03-30 00:06:51 +09:00
Client := th . Client
channel := th . BasicChannel
2020-06-12 13:43:50 +02:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2017-03-30 00:06:51 +09:00
2019-08-12 18:36:08 -03:00
fileIds := make ( [ ] string , 3 )
data , err := testutils . ReadTestFile ( "test.png" )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2019-08-12 18:36:08 -03:00
for i := 0 ; i < len ( fileIds ) ; i ++ {
fileResp , resp := Client . UploadFile ( data , channel . Id , "test.png" )
CheckNoError ( t , resp )
fileIds [ i ] = fileResp . FileInfos [ 0 ] . Id
}
2017-03-30 00:06:51 +09:00
post := & model . Post {
ChannelId : channel . Id ,
IsPinned : true ,
Message : "#hashtag a message" ,
Props : model . StringInterface { "channel_header" : "old_header" } ,
2019-08-12 18:36:08 -03:00
FileIds : fileIds [ 0 : 2 ] ,
2017-03-30 00:06:51 +09:00
HasReactions : true ,
}
post , _ = Client . CreatePost ( post )
2019-08-12 18:36:08 -03:00
var rpost * model . Post
t . Run ( "new message, props, files, HasReactions bit" , func ( t * testing . T ) {
patch := & model . PostPatch { }
2017-03-30 00:06:51 +09:00
2019-08-12 18:36:08 -03:00
patch . IsPinned = model . NewBool ( false )
patch . Message = model . NewString ( "#otherhashtag other message" )
patch . Props = & model . StringInterface { "channel_header" : "new_header" }
patchFileIds := model . StringArray ( fileIds ) // one extra file
patch . FileIds = & patchFileIds
patch . HasReactions = model . NewBool ( false )
2017-03-30 00:06:51 +09:00
2019-08-12 18:36:08 -03:00
var resp * model . Response
rpost , resp = Client . PatchPost ( post . Id , patch )
CheckNoError ( t , resp )
2017-03-30 00:06:51 +09:00
2019-08-12 18:36:08 -03:00
assert . False ( t , rpost . IsPinned , "IsPinned did not update properly" )
assert . Equal ( t , "#otherhashtag other message" , rpost . Message , "Message did not update properly" )
2020-03-13 21:12:20 +01:00
assert . Equal ( t , * patch . Props , rpost . GetProps ( ) , "Props did not update properly" )
2019-08-12 18:36:08 -03:00
assert . Equal ( t , "#otherhashtag" , rpost . Hashtags , "Message did not update properly" )
assert . Equal ( t , model . StringArray ( fileIds [ 0 : 2 ] ) , rpost . FileIds , "FileIds should not update" )
assert . False ( t , rpost . HasReactions , "HasReactions did not update properly" )
} )
2017-03-30 00:06:51 +09:00
2019-08-12 18:36:08 -03:00
t . Run ( "add slack attachments" , func ( t * testing . T ) {
patch2 := & model . PostPatch { }
attachments := [ ] model . SlackAttachment {
{
Text : "Hello World" ,
} ,
}
patch2 . Props = & model . StringInterface { "attachments" : attachments }
2019-04-04 20:01:21 +02:00
2019-08-12 18:36:08 -03:00
rpost2 , resp := Client . PatchPost ( post . Id , patch2 )
CheckNoError ( t , resp )
2020-03-13 21:12:20 +01:00
assert . NotEmpty ( t , rpost2 . GetProp ( "attachments" ) )
2019-08-12 18:36:08 -03:00
assert . NotEqual ( t , rpost . EditAt , rpost2 . EditAt )
} )
2019-04-04 20:01:21 +02:00
2019-08-12 18:36:08 -03:00
t . Run ( "invalid requests" , func ( t * testing . T ) {
r , err := Client . DoApiPut ( "/posts/" + post . Id + "/patch" , "garbage" )
2020-03-04 20:14:23 +01:00
require . EqualError ( t , err , ": Invalid or missing post in request body., " )
2019-08-12 18:36:08 -03:00
require . Equal ( t , http . StatusBadRequest , r . StatusCode , "wrong status code" )
2017-03-30 00:06:51 +09:00
2019-08-12 18:36:08 -03:00
patch := & model . PostPatch { }
_ , resp := Client . PatchPost ( "junk" , patch )
CheckBadRequestStatus ( t , resp )
} )
2017-03-30 00:06:51 +09:00
2019-08-12 18:36:08 -03:00
t . Run ( "unknown post" , func ( t * testing . T ) {
patch := & model . PostPatch { }
_ , resp := Client . PatchPost ( GenerateTestId ( ) , patch )
CheckForbiddenStatus ( t , resp )
} )
2017-03-30 00:06:51 +09:00
2019-08-12 18:36:08 -03:00
t . Run ( "logged out" , func ( t * testing . T ) {
Client . Logout ( )
patch := & model . PostPatch { }
_ , resp := Client . PatchPost ( post . Id , patch )
CheckUnauthorizedStatus ( t , resp )
} )
2017-03-30 00:06:51 +09:00
2019-08-12 18:36:08 -03:00
t . Run ( "different user" , func ( t * testing . T ) {
th . LoginBasic2 ( )
patch := & model . PostPatch { }
_ , resp := Client . PatchPost ( post . Id , patch )
CheckForbiddenStatus ( t , resp )
} )
2017-04-04 15:17:47 -04:00
2019-08-12 18:36:08 -03:00
t . Run ( "different user, but team admin" , func ( t * testing . T ) {
th . LoginTeamAdmin ( )
patch := & model . PostPatch { }
_ , resp := Client . PatchPost ( post . Id , patch )
CheckForbiddenStatus ( t , resp )
} )
2017-03-30 00:06:51 +09:00
2019-08-12 18:36:08 -03:00
t . Run ( "different user, but system admin" , func ( t * testing . T ) {
patch := & model . PostPatch { }
_ , resp := th . SystemAdminClient . PatchPost ( post . Id , patch )
CheckNoError ( t , resp )
} )
2020-06-23 16:08:55 -04:00
t . Run ( "edit others posts permission can function independently of edit own post" , func ( t * testing . T ) {
th . LoginBasic2 ( )
patch := & model . PostPatch { }
_ , resp := Client . PatchPost ( post . Id , patch )
CheckForbiddenStatus ( t , resp )
// Add permission to edit others'
defer th . RestoreDefaultRolePermissions ( th . SaveDefaultRolePermissions ( ) )
th . RemovePermissionFromRole ( model . PERMISSION_EDIT_POST . Id , model . CHANNEL_USER_ROLE_ID )
th . AddPermissionToRole ( model . PERMISSION_EDIT_OTHERS_POSTS . Id , model . CHANNEL_USER_ROLE_ID )
_ , resp = Client . PatchPost ( post . Id , patch )
CheckNoError ( t , resp )
} )
2017-03-30 00:06:51 +09:00
}
2017-03-31 22:58:47 +09:00
func TestPinPost ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-03-31 22:58:47 +09:00
Client := th . Client
post := th . BasicPost
pass , resp := Client . PinPost ( post . Id )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . True ( t , pass , "should have passed" )
rpost , err := th . App . GetSinglePost ( post . Id )
require . Nil ( t , err )
require . True ( t , rpost . IsPinned , "failed to pin post" )
2017-03-31 22:58:47 +09:00
pass , resp = Client . PinPost ( "junk" )
CheckBadRequestStatus ( t , resp )
2019-11-29 07:41:51 +09:00
require . False ( t , pass , "should have failed" )
2017-03-31 22:58:47 +09:00
_ , resp = Client . PinPost ( GenerateTestId ( ) )
CheckForbiddenStatus ( t , resp )
2019-01-28 19:42:49 +00:00
t . Run ( "unable-to-pin-post-in-read-only-town-square" , func ( t * testing . T ) {
2019-02-12 08:37:54 -05:00
townSquareIsReadOnly := * th . App . Config ( ) . TeamSettings . ExperimentalTownSquareIsReadOnly
2020-06-12 13:43:50 +02:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2019-01-28 19:42:49 +00:00
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . TeamSettings . ExperimentalTownSquareIsReadOnly = true } )
2020-06-12 13:43:50 +02:00
defer th . App . Srv ( ) . RemoveLicense ( )
2019-01-28 19:42:49 +00:00
defer th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . TeamSettings . ExperimentalTownSquareIsReadOnly = townSquareIsReadOnly } )
channel , err := th . App . GetChannelByName ( "town-square" , th . BasicTeam . Id , true )
assert . Nil ( t , err )
2019-01-31 12:16:16 +01:00
adminPost := th . CreatePostWithClient ( th . SystemAdminClient , channel )
2019-01-28 19:42:49 +00:00
2019-01-31 12:16:16 +01:00
_ , resp = Client . PinPost ( adminPost . Id )
2019-01-28 19:42:49 +00:00
CheckForbiddenStatus ( t , resp )
} )
2017-03-31 22:58:47 +09:00
Client . Logout ( )
_ , resp = Client . PinPost ( post . Id )
CheckUnauthorizedStatus ( t , resp )
_ , resp = th . SystemAdminClient . PinPost ( post . Id )
CheckNoError ( t , resp )
}
func TestUnpinPost ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-03-31 22:58:47 +09:00
Client := th . Client
pinnedPost := th . CreatePinnedPost ( )
pass , resp := Client . UnpinPost ( pinnedPost . Id )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . True ( t , pass , "should have passed" )
2017-03-31 22:58:47 +09:00
2019-11-29 07:41:51 +09:00
rpost , err := th . App . GetSinglePost ( pinnedPost . Id )
require . Nil ( t , err )
require . False ( t , rpost . IsPinned )
2017-03-31 22:58:47 +09:00
pass , resp = Client . UnpinPost ( "junk" )
CheckBadRequestStatus ( t , resp )
2019-11-29 07:41:51 +09:00
require . False ( t , pass , "should have failed" )
2017-03-31 22:58:47 +09:00
_ , 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 ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-02-13 10:52:50 -05:00
Client := th . Client
post1 := th . CreatePost ( )
post2 := th . CreatePost ( )
2017-06-19 09:08:52 -07: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-21 05:05:13 +08: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
2020-06-22 14:06:37 +05:30
th . TestForAllClients ( t , func ( t * testing . T , c * model . Client4 ) {
2021-01-14 13:46:27 +02:00
posts , resp := c . GetPostsForChannel ( th . BasicChannel . Id , 0 , 60 , "" , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Equal ( t , post4 . Id , posts . Order [ 0 ] , "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 , post1 . Id , posts . Order [ 3 ] , "wrong order" )
2017-02-13 10:52:50 -05:00
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsForChannel ( th . BasicChannel . Id , 0 , 3 , resp . Etag , false )
2020-06-22 14:06:37 +05:30
CheckEtag ( t , posts , resp )
2017-02-13 10:52:50 -05:00
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsForChannel ( th . BasicChannel . Id , 0 , 3 , "" , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Len ( t , posts . Order , 3 , "wrong number returned" )
2017-02-13 10:52:50 -05:00
2020-06-22 14:06:37 +05:30
_ , ok := posts . Posts [ post3 . Id ]
require . True ( t , ok , "missing comment" )
_ , ok = posts . Posts [ post1 . Id ]
require . True ( t , ok , "missing root post" )
2017-02-13 10:52:50 -05:00
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsForChannel ( th . BasicChannel . Id , 1 , 1 , "" , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Equal ( t , post3 . Id , posts . Order [ 0 ] , "wrong order" )
2017-02-13 10:52:50 -05:00
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsForChannel ( th . BasicChannel . Id , 10000 , 10000 , "" , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Empty ( t , posts . Order , "should be no posts" )
} )
2017-02-13 10:52:50 -05:00
2017-03-24 16:46:11 -04:00
post5 := th . CreatePost ( )
2020-06-22 14:06:37 +05:30
th . TestForAllClients ( t , func ( t * testing . T , c * model . Client4 ) {
2021-01-14 13:46:27 +02:00
posts , resp := c . GetPostsSince ( th . BasicChannel . Id , since , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Len ( t , posts . Posts , 2 , "should return 2 posts" )
2017-03-24 16:46:11 -04:00
2020-06-22 14:06:37 +05:30
// "since" query to return empty NextPostId and PrevPostId
require . Equal ( t , "" , posts . NextPostId , "should return an empty NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return an empty PrevPostId" )
2017-05-15 17:44:04 +02:00
2020-06-22 14:06:37 +05:30
found := make ( [ ] bool , 2 )
for _ , p := range posts . Posts {
require . LessOrEqual ( t , since , p . CreateAt , "bad create at for post returned" )
2019-11-29 07:41:51 +09:00
2020-06-22 14:06:37 +05:30
if p . Id == post4 . Id {
found [ 0 ] = true
} else if p . Id == post5 . Id {
found [ 1 ] = true
}
}
for _ , f := range found {
require . True ( t , f , "missing post" )
2017-03-24 16:46:11 -04:00
}
2021-01-14 13:46:27 +02:00
_ , resp = c . GetPostsForChannel ( "" , 0 , 60 , "" , false )
2020-06-22 14:06:37 +05:30
CheckBadRequestStatus ( t , resp )
2017-02-13 10:52:50 -05:00
2021-01-14 13:46:27 +02:00
_ , resp = c . GetPostsForChannel ( "junk" , 0 , 60 , "" , false )
2020-06-22 14:06:37 +05:30
CheckBadRequestStatus ( t , resp )
} )
2017-02-13 10:52:50 -05:00
2021-01-14 13:46:27 +02:00
_ , resp := Client . GetPostsForChannel ( model . NewId ( ) , 0 , 60 , "" , false )
2017-02-13 10:52:50 -05:00
CheckForbiddenStatus ( t , resp )
Client . Logout ( )
2021-01-14 13:46:27 +02:00
_ , resp = Client . GetPostsForChannel ( model . NewId ( ) , 0 , 60 , "" , false )
2017-02-13 10:52:50 -05:00
CheckUnauthorizedStatus ( t , resp )
2019-07-04 05:23:04 +08:00
// 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
Client . Login ( th . BasicUser . Email , th . BasicUser . Password )
th . CreatePost ( ) // post6
post7 := th . CreatePost ( )
post8 := th . CreatePost ( )
th . CreatePost ( ) // post9
post10 := th . CreatePost ( )
2020-06-22 14:06:37 +05:30
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
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsBefore ( th . BasicChannel . Id , post1 . Id , 0 , 2 , "" , false )
2020-06-22 14:06:37 +05:30
systemPostId1 := posts . Order [ 1 ]
2019-07-04 05:23:04 +08:00
2020-06-22 14:06:37 +05:30
// similar to '/posts'
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsForChannel ( th . BasicChannel . Id , 0 , 60 , "" , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Len ( t , posts . Order , 12 , "expected 12 posts" )
require . Equal ( t , post10 . Id , posts . Order [ 0 ] , "posts not in order" )
require . Equal ( t , systemPostId1 , posts . Order [ 11 ] , "posts not in order" )
require . Equal ( t , "" , posts . NextPostId , "should return an empty NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return an empty PrevPostId" )
// similar to '/posts?per_page=3'
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsForChannel ( th . BasicChannel . Id , 0 , 3 , "" , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Len ( t , posts . Order , 3 , "expected 3 posts" )
require . Equal ( t , post10 . Id , posts . Order [ 0 ] , "posts not in order" )
require . Equal ( t , post8 . Id , posts . Order [ 2 ] , "should return 3 posts and match order" )
require . Equal ( t , "" , posts . NextPostId , "should return an empty NextPostId" )
require . Equal ( t , post7 . Id , posts . PrevPostId , "should return post7.Id as PrevPostId" )
// similar to '/posts?per_page=3&page=1'
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsForChannel ( th . BasicChannel . Id , 1 , 3 , "" , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Len ( t , posts . Order , 3 , "expected 3 posts" )
require . Equal ( t , post7 . Id , posts . Order [ 0 ] , "posts not in order" )
require . Equal ( t , post5 . Id , posts . Order [ 2 ] , "posts not in order" )
require . Equal ( t , post8 . Id , posts . NextPostId , "should return post8.Id as NextPostId" )
require . Equal ( t , post4 . Id , posts . PrevPostId , "should return post4.Id as PrevPostId" )
// similar to '/posts?per_page=3&page=2'
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsForChannel ( th . BasicChannel . Id , 2 , 3 , "" , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Len ( t , posts . Order , 3 , "expected 3 posts" )
require . Equal ( t , post4 . Id , posts . Order [ 0 ] , "posts not in order" )
require . Equal ( t , post2 . Id , posts . Order [ 2 ] , "should return 3 posts and match order" )
require . Equal ( t , post5 . Id , posts . NextPostId , "should return post5.Id as NextPostId" )
require . Equal ( t , post1 . Id , posts . PrevPostId , "should return post1.Id as PrevPostId" )
// similar to '/posts?per_page=3&page=3'
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsForChannel ( th . BasicChannel . Id , 3 , 3 , "" , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Len ( t , posts . Order , 3 , "expected 3 posts" )
require . Equal ( t , post1 . Id , posts . Order [ 0 ] , "posts not in order" )
require . Equal ( t , systemPostId1 , posts . Order [ 2 ] , "should return 3 posts and match order" )
require . Equal ( t , post2 . Id , posts . NextPostId , "should return post2.Id as NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return an empty PrevPostId" )
// similar to '/posts?per_page=3&page=4'
2021-01-14 13:46:27 +02:00
posts , resp = c . GetPostsForChannel ( th . BasicChannel . Id , 4 , 3 , "" , false )
2020-06-22 14:06:37 +05:30
CheckNoError ( t , resp )
require . Empty ( t , posts . Order , "should return 0 post" )
require . Equal ( t , "" , posts . NextPostId , "should return an empty NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return an empty PrevPostId" )
} )
2017-02-13 10:52:50 -05:00
}
2017-04-06 05:18:23 +09:00
func TestGetFlaggedPostsForUser ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-04-06 05:18:23 +09: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" ,
}
2019-01-26 20:02:46 +01:00
_ , resp := Client . UpdatePreferences ( user . Id , & model . Preferences { preference } )
CheckNoError ( t , resp )
2017-04-06 05:18:23 +09:00
preference . Name = post2 . Id
2019-01-26 20:02:46 +01:00
_ , resp = Client . UpdatePreferences ( user . Id , & model . Preferences { preference } )
CheckNoError ( t , resp )
2017-04-06 05:18:23 +09:00
opl := model . NewPostList ( )
opl . AddPost ( post1 )
opl . AddOrder ( post1 . Id )
rpl , resp := Client . GetFlaggedPostsForUserInChannel ( user . Id , channel1 . Id , 0 , 10 )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , rpl . Posts , 1 , "should have returned 1 post" )
require . Equal ( t , opl . Posts , rpl . Posts , "posts should have matched" )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUserInChannel ( user . Id , channel1 . Id , 0 , 1 )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , rpl . Posts , 1 , "should have returned 1 post" )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUserInChannel ( user . Id , channel1 . Id , 1 , 1 )
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , rpl . Posts )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUserInChannel ( user . Id , GenerateTestId ( ) , 0 , 10 )
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , rpl . Posts )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUserInChannel ( user . Id , "junk" , 0 , 10 )
CheckBadRequestStatus ( t , resp )
2019-11-29 07:41:51 +09:00
require . Nil ( t , rpl )
2017-04-06 05:18:23 +09:00
opl . AddPost ( post2 )
opl . AddOrder ( post2 . Id )
rpl , resp = Client . GetFlaggedPostsForUserInTeam ( user . Id , team1 . Id , 0 , 10 )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , rpl . Posts , 2 , "should have returned 2 posts" )
require . Equal ( t , opl . Posts , rpl . Posts , "posts should have matched" )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUserInTeam ( user . Id , team1 . Id , 0 , 1 )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , rpl . Posts , 1 , "should have returned 1 post" )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUserInTeam ( user . Id , team1 . Id , 1 , 1 )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , rpl . Posts , 1 , "should have returned 1 post" )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUserInTeam ( user . Id , team1 . Id , 1000 , 10 )
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , rpl . Posts )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUserInTeam ( user . Id , GenerateTestId ( ) , 0 , 10 )
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , rpl . Posts )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUserInTeam ( user . Id , "junk" , 0 , 10 )
CheckBadRequestStatus ( t , resp )
2019-11-29 07:41:51 +09:00
require . Nil ( t , rpl )
2017-04-06 05:18:23 +09:00
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 )
2019-11-29 07:41:51 +09:00
require . Len ( t , rpl . Posts , 3 , "should have returned 3 posts" )
require . Equal ( t , opl . Posts , rpl . Posts , "posts should have matched" )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUser ( user . Id , 0 , 2 )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , rpl . Posts , 2 , "should have returned 2 posts" )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUser ( user . Id , 2 , 2 )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , rpl . Posts , 1 , "should have returned 1 post" )
2017-04-06 05:18:23 +09:00
rpl , resp = Client . GetFlaggedPostsForUser ( user . Id , 1000 , 10 )
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , rpl . Posts )
2017-04-06 05:18:23 +09:00
2019-01-26 20:02:46 +01:00
channel4 := th . CreateChannelWithClient ( th . SystemAdminClient , model . CHANNEL_PRIVATE )
post5 := th . CreatePostWithClient ( th . SystemAdminClient , channel4 )
preference . Name = post5 . Id
_ , resp = Client . UpdatePreferences ( user . Id , & model . Preferences { preference } )
CheckForbiddenStatus ( t , resp )
rpl , resp = Client . GetFlaggedPostsForUser ( user . Id , 0 , 10 )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , rpl . Posts , 3 , "should have returned 3 posts" )
require . Equal ( t , opl . Posts , rpl . Posts , "posts should have matched" )
2019-01-26 20:02:46 +01:00
th . AddUserToChannel ( user , channel4 )
_ , resp = Client . UpdatePreferences ( user . Id , & model . Preferences { preference } )
CheckNoError ( t , resp )
rpl , resp = Client . GetFlaggedPostsForUser ( user . Id , 0 , 10 )
CheckNoError ( t , resp )
opl . AddPost ( post5 )
opl . AddOrder ( post5 . Id )
2019-11-29 07:41:51 +09:00
require . Len ( t , rpl . Posts , 4 , "should have returned 4 posts" )
require . Equal ( t , opl . Posts , rpl . Posts , "posts should have matched" )
2019-01-26 20:02:46 +01:00
err := th . App . RemoveUserFromChannel ( user . Id , "" , channel4 )
2019-11-29 07:41:51 +09:00
assert . Nil ( t , err , "unable to remove user from channel" )
2019-01-26 20:02:46 +01:00
rpl , resp = Client . GetFlaggedPostsForUser ( user . Id , 0 , 10 )
CheckNoError ( t , resp )
opl2 := model . NewPostList ( )
opl2 . AddPost ( post1 )
opl2 . AddOrder ( post1 . Id )
opl2 . AddPost ( post2 )
opl2 . AddOrder ( post2 . Id )
opl2 . AddPost ( post4 )
opl2 . AddOrder ( post4 . Id )
2019-12-22 12:35:31 +01:00
require . Len ( t , rpl . Posts , 3 , "should have returned 3 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , opl2 . Posts , rpl . Posts , "posts should have matched" )
2019-01-26 20:02:46 +01:00
2017-04-06 05:18:23 +09:00
_ , resp = Client . GetFlaggedPostsForUser ( "junk" , 0 , 10 )
CheckBadRequestStatus ( t , resp )
_ , resp = Client . GetFlaggedPostsForUser ( GenerateTestId ( ) , 0 , 10 )
CheckForbiddenStatus ( t , resp )
Client . Logout ( )
2018-09-03 14:08:40 +02:00
_ , resp = Client . GetFlaggedPostsForUserInChannel ( user . Id , channel1 . Id , 0 , 10 )
2017-04-06 05:18:23 +09:00
CheckUnauthorizedStatus ( t , resp )
2018-09-03 14:08:40 +02:00
_ , resp = Client . GetFlaggedPostsForUserInTeam ( user . Id , team1 . Id , 0 , 10 )
2017-04-06 05:18:23 +09:00
CheckUnauthorizedStatus ( t , resp )
2018-09-03 14:08:40 +02:00
_ , resp = Client . GetFlaggedPostsForUser ( user . Id , 0 , 10 )
2017-04-06 05:18:23 +09:00
CheckUnauthorizedStatus ( t , resp )
2018-09-03 14:08:40 +02:00
_ , resp = th . SystemAdminClient . GetFlaggedPostsForUserInChannel ( user . Id , channel1 . Id , 0 , 10 )
2017-04-06 05:18:23 +09:00
CheckNoError ( t , resp )
2018-09-03 14:08:40 +02:00
_ , resp = th . SystemAdminClient . GetFlaggedPostsForUserInTeam ( user . Id , team1 . Id , 0 , 10 )
2017-04-06 05:18:23 +09:00
CheckNoError ( t , resp )
2018-09-03 14:08:40 +02:00
_ , resp = th . SystemAdminClient . GetFlaggedPostsForUser ( user . Id , 0 , 10 )
2017-04-06 05:18:23 +09:00
CheckNoError ( t , resp )
2020-09-29 10:33:15 +03:00
mockStore := mocks . Store { }
mockPostStore := mocks . PostStore { }
mockPostStore . On ( "GetFlaggedPosts" , mock . AnythingOfType ( "string" ) , mock . AnythingOfType ( "int" ) , mock . AnythingOfType ( "int" ) ) . Return ( nil , errors . New ( "some-error" ) )
mockPostStore . On ( "ClearCaches" ) . Return ( )
mockStore . On ( "Team" ) . Return ( th . App . Srv ( ) . Store . Team ( ) )
mockStore . On ( "Channel" ) . Return ( th . App . Srv ( ) . Store . Channel ( ) )
mockStore . On ( "User" ) . Return ( th . App . Srv ( ) . Store . User ( ) )
mockStore . On ( "Scheme" ) . Return ( th . App . Srv ( ) . Store . Scheme ( ) )
mockStore . On ( "Post" ) . Return ( & mockPostStore )
mockStore . On ( "FileInfo" ) . Return ( th . App . Srv ( ) . Store . FileInfo ( ) )
mockStore . On ( "Webhook" ) . Return ( th . App . Srv ( ) . Store . Webhook ( ) )
mockStore . On ( "System" ) . Return ( th . App . Srv ( ) . Store . System ( ) )
mockStore . On ( "License" ) . Return ( th . App . Srv ( ) . Store . License ( ) )
mockStore . On ( "Role" ) . Return ( th . App . Srv ( ) . Store . Role ( ) )
mockStore . On ( "Close" ) . Return ( nil )
th . App . Srv ( ) . Store = & mockStore
_ , resp = th . SystemAdminClient . GetFlaggedPostsForUser ( user . Id , 0 , 10 )
CheckInternalErrorStatus ( t , resp )
2017-04-06 05:18:23 +09:00
}
2019-07-04 05:23:04 +08:00
func TestGetPostsBefore ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05: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 ( )
2021-01-14 13:46:27 +02:00
posts , resp := Client . GetPostsBefore ( th . BasicChannel . Id , post3 . Id , 0 , 100 , "" , false )
2017-03-24 16:46:11 -04:00
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
}
2019-11-29 07:41:51 +09:00
require . NotEqual ( t , post4 . Id , p . Id , "returned posts after" )
require . NotEqual ( t , post5 . Id , p . Id , "returned posts after" )
2017-03-24 16:46:11 -04:00
}
for _ , f := range found {
2019-11-29 07:41:51 +09:00
require . True ( t , f , "missing post" )
2017-03-24 16:46:11 -04:00
}
2019-11-29 07:41:51 +09:00
require . Equal ( t , post3 . Id , posts . NextPostId , "should match NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should match empty PrevPostId" )
2019-07-04 05:23:04 +08:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , post4 . Id , 1 , 1 , "" , false )
2017-03-24 16:46:11 -04:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Posts , 1 , "too many posts returned" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post2 . Id , posts . Order [ 0 ] , "should match returned post" )
require . Equal ( t , post3 . Id , posts . NextPostId , "should match NextPostId" )
require . Equal ( t , post1 . Id , posts . PrevPostId , "should match PrevPostId" )
2017-03-24 16:46:11 -04:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , "junk" , 1 , 1 , "" , false )
2019-07-04 05:23:04 +08:00
CheckBadRequestStatus ( t , resp )
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , post5 . Id , 0 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Posts , 3 , "should match length of posts returned" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post4 . Id , posts . Order [ 0 ] , "should match returned post" )
require . Equal ( t , post2 . Id , posts . Order [ 2 ] , "should match returned post" )
require . Equal ( t , post5 . Id , posts . NextPostId , "should match NextPostId" )
require . Equal ( t , post1 . Id , posts . PrevPostId , "should match PrevPostId" )
2019-07-04 05:23:04 +08:00
// get the system post IDs posted before the created posts above
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , post1 . Id , 0 , 2 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
systemPostId2 := posts . Order [ 0 ]
systemPostId1 := posts . Order [ 1 ]
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , post5 . Id , 1 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Posts , 3 , "should match length of posts returned" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post1 . Id , posts . Order [ 0 ] , "should match returned post" )
require . Equal ( t , systemPostId2 , posts . Order [ 1 ] , "should match returned post" )
require . Equal ( t , systemPostId1 , posts . Order [ 2 ] , "should match returned post" )
require . Equal ( t , post2 . Id , posts . NextPostId , "should match NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return empty PrevPostId" )
2019-07-04 05:23:04 +08:00
// 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
post6 := th . CreatePost ( )
th . CreatePost ( ) // post7
post8 := th . CreatePost ( )
post9 := th . CreatePost ( )
th . CreatePost ( ) // post10
// similar to '/posts?before=post9'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , post9 . Id , 0 , 60 , "" , false )
2017-03-24 16:46:11 -04:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Order , 10 , "expected 10 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post8 . Id , posts . Order [ 0 ] , "posts not in order" )
require . Equal ( t , systemPostId1 , posts . Order [ 9 ] , "posts not in order" )
require . Equal ( t , post9 . Id , posts . NextPostId , "should return post9.Id as NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return an empty PrevPostId" )
2017-03-24 16:46:11 -04:00
2019-07-04 05:23:04 +08:00
// similar to '/posts?before=post9&per_page=3'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , post9 . Id , 0 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Order , 3 , "expected 3 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post8 . Id , posts . Order [ 0 ] , "posts not in order" )
require . Equal ( t , post6 . Id , posts . Order [ 2 ] , "should return 3 posts and match order" )
require . Equal ( t , post9 . Id , posts . NextPostId , "should return post9.Id as NextPostId" )
require . Equal ( t , post5 . Id , posts . PrevPostId , "should return post5.Id as PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?before=post9&per_page=3&page=1'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , post9 . Id , 1 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Order , 3 , "expected 3 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post5 . Id , posts . Order [ 0 ] , "posts not in order" )
require . Equal ( t , post3 . Id , posts . Order [ 2 ] , "posts not in order" )
require . Equal ( t , post6 . Id , posts . NextPostId , "should return post6.Id as NextPostId" )
require . Equal ( t , post2 . Id , posts . PrevPostId , "should return post2.Id as PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?before=post9&per_page=3&page=2'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , post9 . Id , 2 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Order , 3 , "expected 3 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post2 . Id , posts . Order [ 0 ] , "posts not in order" )
require . Equal ( t , systemPostId2 , posts . Order [ 2 ] , "posts not in order" )
require . Equal ( t , post3 . Id , posts . NextPostId , "should return post3.Id as NextPostId" )
require . Equal ( t , systemPostId1 , posts . PrevPostId , "should return systemPostId1 as PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?before=post1&per_page=3'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , post1 . Id , 0 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Order , 2 , "expected 2 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , systemPostId2 , posts . Order [ 0 ] , "posts not in order" )
require . Equal ( t , systemPostId1 , posts . Order [ 1 ] , "posts not in order" )
require . Equal ( t , post1 . Id , posts . NextPostId , "should return post1.Id as NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return an empty PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?before=systemPostId1'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , systemPostId1 , 0 , 60 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "should return 0 post" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , systemPostId1 , posts . NextPostId , "should return systemPostId1 as NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return an empty PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?before=systemPostId1&per_page=60&page=1'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , systemPostId1 , 1 , 60 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "should return 0 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , "" , posts . NextPostId , "should return an empty NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return an empty PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?before=non-existent-post'
nonExistentPostId := model . NewId ( )
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , nonExistentPostId , 0 , 60 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "should return 0 post" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , nonExistentPostId , posts . NextPostId , "should return nonExistentPostId as NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return an empty PrevPostId" )
2019-07-04 05:23:04 +08:00
}
func TestGetPostsAfter ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2019-07-04 05:23:04 +08:00
defer th . TearDown ( )
Client := th . Client
post1 := th . CreatePost ( )
post2 := th . CreatePost ( )
post3 := th . CreatePost ( )
post4 := th . CreatePost ( )
post5 := th . CreatePost ( )
2017-03-24 16:46:11 -04:00
2021-01-14 13:46:27 +02:00
posts , resp := Client . GetPostsAfter ( th . BasicChannel . Id , post3 . Id , 0 , 100 , "" , false )
2017-03-24 16:46:11 -04:00
CheckNoError ( t , resp )
2019-07-04 05:23:04 +08:00
found := make ( [ ] bool , 2 )
2017-03-24 16:46:11 -04:00
for _ , p := range posts . Posts {
if p . Id == post4 . Id {
found [ 0 ] = true
} else if p . Id == post5 . Id {
found [ 1 ] = true
}
2019-11-29 07:41:51 +09:00
require . NotEqual ( t , post1 . Id , p . Id , "returned posts before" )
require . NotEqual ( t , post2 . Id , p . Id , "returned posts before" )
2017-03-24 16:46:11 -04:00
}
for _ , f := range found {
2019-11-29 07:41:51 +09:00
require . True ( t , f , "missing post" )
2019-07-04 05:23:04 +08:00
}
2019-11-29 07:41:51 +09:00
require . Equal ( t , "" , posts . NextPostId , "should match empty NextPostId" )
require . Equal ( t , post3 . Id , posts . PrevPostId , "should match PrevPostId" )
2019-07-04 05:23:04 +08:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , post2 . Id , 1 , 1 , "" , false )
2017-03-24 16:46:11 -04:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Posts , 1 , "too many posts returned" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post4 . Id , posts . Order [ 0 ] , "should match returned post" )
require . Equal ( t , post5 . Id , posts . NextPostId , "should match NextPostId" )
require . Equal ( t , post3 . Id , posts . PrevPostId , "should match PrevPostId" )
2017-03-24 16:46:11 -04:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , "junk" , 1 , 1 , "" , false )
2019-07-04 05:23:04 +08:00
CheckBadRequestStatus ( t , resp )
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , post1 . Id , 0 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Posts , 3 , "should match length of posts returned" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post4 . Id , posts . Order [ 0 ] , "should match returned post" )
require . Equal ( t , post2 . Id , posts . Order [ 2 ] , "should match returned post" )
require . Equal ( t , post5 . Id , posts . NextPostId , "should match NextPostId" )
require . Equal ( t , post1 . Id , posts . PrevPostId , "should match PrevPostId" )
2019-07-04 05:23:04 +08:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , post1 . Id , 1 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Posts , 1 , "should match length of posts returned" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post5 . Id , posts . Order [ 0 ] , "should match returned post" )
require . Equal ( t , "" , posts . NextPostId , "should match NextPostId" )
require . Equal ( t , post4 . Id , posts . PrevPostId , "should match PrevPostId" )
2019-07-04 05:23:04 +08:00
// 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
post6 := th . CreatePost ( )
th . CreatePost ( ) // post7
post8 := th . CreatePost ( )
post9 := th . CreatePost ( )
post10 := th . CreatePost ( )
// similar to '/posts?after=post2'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , post2 . Id , 0 , 60 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Order , 8 , "expected 8 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post10 . Id , posts . Order [ 0 ] , "should match order" )
require . Equal ( t , post3 . Id , posts . Order [ 7 ] , "should match order" )
require . Equal ( t , "" , posts . NextPostId , "should return an empty NextPostId" )
require . Equal ( t , post2 . Id , posts . PrevPostId , "should return post2.Id as PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?after=post2&per_page=3'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , post2 . Id , 0 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Order , 3 , "expected 3 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post5 . Id , posts . Order [ 0 ] , "should match order" )
require . Equal ( t , post3 . Id , posts . Order [ 2 ] , "should return 3 posts and match order" )
require . Equal ( t , post6 . Id , posts . NextPostId , "should return post6.Id as NextPostId" )
require . Equal ( t , post2 . Id , posts . PrevPostId , "should return post2.Id as PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?after=post2&per_page=3&page=1'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , post2 . Id , 1 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Order , 3 , "expected 3 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post8 . Id , posts . Order [ 0 ] , "should match order" )
require . Equal ( t , post6 . Id , posts . Order [ 2 ] , "should match order" )
require . Equal ( t , post9 . Id , posts . NextPostId , "should return post9.Id as NextPostId" )
require . Equal ( t , post5 . Id , posts . PrevPostId , "should return post5.Id as PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?after=post2&per_page=3&page=2'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , post2 . Id , 2 , 3 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Len ( t , posts . Order , 2 , "expected 2 posts" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , post10 . Id , posts . Order [ 0 ] , "should match order" )
require . Equal ( t , post9 . Id , posts . Order [ 1 ] , "should match order" )
require . Equal ( t , "" , posts . NextPostId , "should return an empty NextPostId" )
require . Equal ( t , post8 . Id , posts . PrevPostId , "should return post8.Id as PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?after=post10'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , post10 . Id , 0 , 60 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "should return 0 post" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , "" , posts . NextPostId , "should return an empty NextPostId" )
require . Equal ( t , post10 . Id , posts . PrevPostId , "should return post10.Id as PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?after=post10&page=1'
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , post10 . Id , 1 , 60 , "" , false )
2017-03-24 16:46:11 -04:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "should return 0 post" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , "" , posts . NextPostId , "should return an empty NextPostId" )
require . Equal ( t , "" , posts . PrevPostId , "should return an empty PrevPostId" )
2019-07-04 05:23:04 +08:00
// similar to '/posts?after=non-existent-post'
nonExistentPostId := model . NewId ( )
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAfter ( th . BasicChannel . Id , nonExistentPostId , 0 , 60 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "should return 0 post" )
2019-11-29 07:41:51 +09:00
require . Equal ( t , "" , posts . NextPostId , "should return an empty NextPostId" )
require . Equal ( t , nonExistentPostId , posts . PrevPostId , "should return nonExistentPostId as PrevPostId" )
2019-07-04 05:23:04 +08:00
}
func TestGetPostsForChannelAroundLastUnread ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2019-07-04 05:23:04 +08:00
defer th . TearDown ( )
Client := th . Client
userId := th . BasicUser . Id
channelId := th . BasicChannel . Id
// 12 posts = 2 systems posts + 10 created posts below
post1 := th . CreatePost ( )
post2 := th . CreatePost ( )
post3 := th . CreatePost ( )
post4 := th . CreatePost ( )
2019-07-30 06:27:18 -03:00
post5 := th . CreatePost ( )
2019-07-18 02:05:43 +08:00
replyPost := & model . Post { ChannelId : channelId , Message : model . NewId ( ) , RootId : post4 . Id , ParentId : post4 . Id }
post6 , resp := Client . CreatePost ( replyPost )
CheckNoError ( t , resp )
post7 , resp := Client . CreatePost ( replyPost )
CheckNoError ( t , resp )
post8 , resp := Client . CreatePost ( replyPost )
CheckNoError ( t , resp )
post9 , resp := Client . CreatePost ( replyPost )
CheckNoError ( t , resp )
post10 , resp := Client . CreatePost ( replyPost )
CheckNoError ( t , resp )
2019-07-04 05:23:04 +08:00
2019-07-30 06:27:18 -03:00
postIdNames := map [ string ] string {
post1 . Id : "post1" ,
post2 . Id : "post2" ,
post3 . Id : "post3" ,
post4 . Id : "post4" ,
post5 . Id : "post5" ,
post6 . Id : "post6 (reply to post4)" ,
post7 . Id : "post7 (reply to post4)" ,
post8 . Id : "post8 (reply to post4)" ,
post9 . Id : "post9 (reply to post4)" ,
post10 . Id : "post10 (reply to post4)" ,
}
namePost := func ( postId string ) string {
name , ok := postIdNames [ postId ]
if ok {
return name
}
return fmt . Sprintf ( "unknown (%s)" , postId )
}
namePosts := func ( postIds [ ] string ) [ ] string {
namedPostIds := make ( [ ] string , 0 , len ( postIds ) )
for _ , postId := range postIds {
namedPostIds = append ( namedPostIds , namePost ( postId ) )
}
return namedPostIds
}
namePostsMap := func ( posts map [ string ] * model . Post ) [ ] string {
namedPostIds := make ( [ ] string , 0 , len ( posts ) )
for postId := range posts {
namedPostIds = append ( namedPostIds , namePost ( postId ) )
}
sort . Strings ( namedPostIds )
return namedPostIds
}
assertPostList := func ( t * testing . T , expected , actual * model . PostList ) {
t . Helper ( )
require . Equal ( t , namePosts ( expected . Order ) , namePosts ( actual . Order ) , "unexpected post order" )
require . Equal ( t , namePostsMap ( expected . Posts ) , namePostsMap ( actual . Posts ) , "unexpected posts" )
require . Equal ( t , namePost ( expected . NextPostId ) , namePost ( actual . NextPostId ) , "unexpected next post id" )
require . Equal ( t , namePost ( expected . PrevPostId ) , namePost ( actual . PrevPostId ) , "unexpected prev post id" )
}
2020-07-20 10:08:52 +02:00
// Setting limit_after to zero should fail with a 400 BadRequest.
2021-01-14 13:46:27 +02:00
posts , resp := Client . GetPostsAroundLastUnread ( userId , channelId , 20 , 0 , false )
2021-02-17 09:52:18 +01:00
require . NotNil ( t , resp . Error )
2020-07-20 10:08:52 +02:00
require . Equal ( t , "api.context.invalid_url_param.app_error" , resp . Error . Id )
require . Equal ( t , http . StatusBadRequest , resp . StatusCode )
2019-07-04 05:23:04 +08:00
// All returned posts are all read by the user, since it's created by the user itself.
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAroundLastUnread ( userId , channelId , 20 , 20 , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-07-30 06:27:18 -03:00
require . Len ( t , posts . Order , 12 , "Should return 12 posts only since there's no unread post" )
2019-07-04 05:23:04 +08:00
// Set channel member's last viewed to 0.
// All returned posts are latest posts as if all previous posts were already read by the user.
2021-03-31 09:40:35 +02:00
channelMember , err := th . App . Srv ( ) . Store . Channel ( ) . GetMember ( context . Background ( ) , channelId , userId )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2019-07-04 05:23:04 +08:00
channelMember . LastViewedAt = 0
2020-02-13 13:26:58 +01:00
_ , err = th . App . Srv ( ) . Store . Channel ( ) . UpdateMember ( channelMember )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2020-02-13 13:26:58 +01:00
th . App . Srv ( ) . Store . Post ( ) . InvalidateLastPostTimeCache ( channelId )
2019-07-04 05:23:04 +08:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAroundLastUnread ( userId , channelId , 20 , 20 , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-07-30 06:27:18 -03:00
require . Len ( t , posts . Order , 12 , "Should return 12 posts only since there's no unread post" )
2017-03-24 16:46:11 -04:00
2019-07-04 05:23:04 +08:00
// get the first system post generated before the created posts above
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsBefore ( th . BasicChannel . Id , post1 . Id , 0 , 2 , "" , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-07-30 06:27:18 -03:00
systemPost0 := posts . Posts [ posts . Order [ 0 ] ]
postIdNames [ systemPost0 . Id ] = "system post 0"
systemPost1 := posts . Posts [ posts . Order [ 1 ] ]
postIdNames [ systemPost1 . Id ] = "system post 1"
2019-07-04 05:23:04 +08:00
// Set channel member's last viewed before post1.
2021-03-31 09:40:35 +02:00
channelMember , err = th . App . Srv ( ) . Store . Channel ( ) . GetMember ( context . Background ( ) , channelId , userId )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2019-07-04 05:23:04 +08:00
channelMember . LastViewedAt = post1 . CreateAt - 1
2020-02-13 13:26:58 +01:00
_ , err = th . App . Srv ( ) . Store . Channel ( ) . UpdateMember ( channelMember )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2020-02-13 13:26:58 +01:00
th . App . Srv ( ) . Store . Post ( ) . InvalidateLastPostTimeCache ( channelId )
2019-07-04 05:23:04 +08:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAroundLastUnread ( userId , channelId , 3 , 3 , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-07-30 06:27:18 -03:00
assertPostList ( t , & model . PostList {
Order : [ ] string { post3 . Id , post2 . Id , post1 . Id , systemPost0 . Id , systemPost1 . Id } ,
Posts : map [ string ] * model . Post {
systemPost0 . Id : systemPost0 ,
systemPost1 . Id : systemPost1 ,
post1 . Id : post1 ,
post2 . Id : post2 ,
post3 . Id : post3 ,
} ,
NextPostId : post4 . Id ,
PrevPostId : "" ,
} , posts )
2019-07-04 05:23:04 +08:00
// Set channel member's last viewed before post6.
2021-03-31 09:40:35 +02:00
channelMember , err = th . App . Srv ( ) . Store . Channel ( ) . GetMember ( context . Background ( ) , channelId , userId )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2019-07-04 05:23:04 +08:00
channelMember . LastViewedAt = post6 . CreateAt - 1
2020-02-13 13:26:58 +01:00
_ , err = th . App . Srv ( ) . Store . Channel ( ) . UpdateMember ( channelMember )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2020-02-13 13:26:58 +01:00
th . App . Srv ( ) . Store . Post ( ) . InvalidateLastPostTimeCache ( channelId )
2019-07-04 05:23:04 +08:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAroundLastUnread ( userId , channelId , 3 , 3 , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-07-30 06:27:18 -03:00
assertPostList ( t , & model . PostList {
Order : [ ] string { post8 . Id , post7 . Id , post6 . Id , post5 . Id , post4 . Id , post3 . Id } ,
Posts : map [ string ] * model . Post {
post3 . Id : post3 ,
post4 . Id : post4 ,
post5 . Id : post5 ,
post6 . Id : post6 ,
post7 . Id : post7 ,
post8 . Id : post8 ,
post9 . Id : post9 ,
post10 . Id : post10 ,
} ,
NextPostId : post9 . Id ,
PrevPostId : post2 . Id ,
} , posts )
2019-07-04 05:23:04 +08:00
// Set channel member's last viewed before post10.
2021-03-31 09:40:35 +02:00
channelMember , err = th . App . Srv ( ) . Store . Channel ( ) . GetMember ( context . Background ( ) , channelId , userId )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2019-07-04 05:23:04 +08:00
channelMember . LastViewedAt = post10 . CreateAt - 1
2020-02-13 13:26:58 +01:00
_ , err = th . App . Srv ( ) . Store . Channel ( ) . UpdateMember ( channelMember )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2020-02-13 13:26:58 +01:00
th . App . Srv ( ) . Store . Post ( ) . InvalidateLastPostTimeCache ( channelId )
2019-07-04 05:23:04 +08:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAroundLastUnread ( userId , channelId , 3 , 3 , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-07-30 06:27:18 -03:00
assertPostList ( t , & model . PostList {
Order : [ ] string { post10 . Id , post9 . Id , post8 . Id , post7 . Id } ,
Posts : map [ string ] * model . Post {
post4 . Id : post4 ,
post6 . Id : post6 ,
post7 . Id : post7 ,
post8 . Id : post8 ,
post9 . Id : post9 ,
post10 . Id : post10 ,
} ,
NextPostId : "" ,
PrevPostId : post6 . Id ,
} , posts )
2019-07-04 05:23:04 +08:00
// Set channel member's last viewed equal to post10.
2021-03-31 09:40:35 +02:00
channelMember , err = th . App . Srv ( ) . Store . Channel ( ) . GetMember ( context . Background ( ) , channelId , userId )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2019-07-04 05:23:04 +08:00
channelMember . LastViewedAt = post10 . CreateAt
2020-02-13 13:26:58 +01:00
_ , err = th . App . Srv ( ) . Store . Channel ( ) . UpdateMember ( channelMember )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2020-02-13 13:26:58 +01:00
th . App . Srv ( ) . Store . Post ( ) . InvalidateLastPostTimeCache ( channelId )
2019-07-04 05:23:04 +08:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAroundLastUnread ( userId , channelId , 3 , 3 , false )
2019-07-04 05:23:04 +08:00
CheckNoError ( t , resp )
2019-07-30 06:27:18 -03:00
assertPostList ( t , & model . PostList {
Order : [ ] string { post10 . Id , post9 . Id , post8 . Id } ,
Posts : map [ string ] * model . Post {
post4 . Id : post4 ,
post6 . Id : post6 ,
post7 . Id : post7 ,
post8 . Id : post8 ,
post9 . Id : post9 ,
post10 . Id : post10 ,
} ,
NextPostId : "" ,
PrevPostId : post7 . Id ,
} , posts )
// Set channel member's last viewed to just before a new reply to a previous thread, not
// otherwise in the requested window.
post11 := th . CreatePost ( )
post12 , resp := Client . CreatePost ( & model . Post {
ChannelId : channelId ,
Message : model . NewId ( ) ,
RootId : post4 . Id ,
ParentId : post4 . Id ,
} )
CheckNoError ( t , resp )
post13 := th . CreatePost ( )
postIdNames [ post11 . Id ] = "post11"
postIdNames [ post12 . Id ] = "post12 (reply to post4)"
postIdNames [ post13 . Id ] = "post13"
2021-03-31 09:40:35 +02:00
channelMember , err = th . App . Srv ( ) . Store . Channel ( ) . GetMember ( context . Background ( ) , channelId , userId )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2019-07-30 06:27:18 -03:00
channelMember . LastViewedAt = post12 . CreateAt - 1
2020-02-13 13:26:58 +01:00
_ , err = th . App . Srv ( ) . Store . Channel ( ) . UpdateMember ( channelMember )
2021-02-17 09:52:18 +01:00
require . NoError ( t , err )
2020-02-13 13:26:58 +01:00
th . App . Srv ( ) . Store . Post ( ) . InvalidateLastPostTimeCache ( channelId )
2019-07-30 06:27:18 -03:00
2021-01-14 13:46:27 +02:00
posts , resp = Client . GetPostsAroundLastUnread ( userId , channelId , 1 , 2 , false )
2019-07-30 06:27:18 -03:00
CheckNoError ( t , resp )
assertPostList ( t , & model . PostList {
Order : [ ] string { post13 . Id , post12 . Id , post11 . Id } ,
Posts : map [ string ] * model . Post {
post4 . Id : post4 ,
post6 . Id : post6 ,
post7 . Id : post7 ,
post8 . Id : post8 ,
post9 . Id : post9 ,
post10 . Id : post10 ,
post11 . Id : post11 ,
post12 . Id : post12 ,
post13 . Id : post13 ,
} ,
NextPostId : "" ,
PrevPostId : post10 . Id ,
} , posts )
2017-03-24 16:46:11 -04:00
}
2017-02-13 10:52:50 -05:00
func TestGetPost ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2020-06-12 09:56:35 +05:30
// TODO: migrate this entirely to the subtest's client
// once the other methods are migrated too.
2017-02-13 10:52:50 -05:00
Client := th . Client
2020-06-12 09:56:35 +05:30
var privatePost * model . Post
th . TestForAllClients ( t , func ( t * testing . T , c * model . Client4 ) {
t . Helper ( )
2017-02-13 10:52:50 -05:00
2020-06-12 09:56:35 +05:30
post , resp := c . GetPost ( th . BasicPost . Id , "" )
CheckNoError ( t , resp )
2017-02-13 10:52:50 -05:00
2020-06-12 09:56:35 +05:30
require . Equal ( t , th . BasicPost . Id , post . Id , "post ids don't match" )
2017-02-13 10:52:50 -05:00
2020-06-12 09:56:35 +05:30
post , resp = c . GetPost ( th . BasicPost . Id , resp . Etag )
CheckEtag ( t , post , resp )
2017-02-13 10:52:50 -05:00
2020-06-12 09:56:35 +05:30
_ , resp = c . GetPost ( "" , "" )
CheckNotFoundStatus ( t , resp )
2017-02-13 10:52:50 -05:00
2020-06-12 09:56:35 +05:30
_ , resp = c . GetPost ( "junk" , "" )
CheckBadRequestStatus ( t , resp )
2017-06-26 08:16:57 -04:00
2020-06-12 09:56:35 +05:30
_ , resp = c . GetPost ( model . NewId ( ) , "" )
CheckNotFoundStatus ( t , resp )
2017-06-26 08:16:57 -04:00
2020-06-12 09:56:35 +05:30
Client . RemoveUserFromChannel ( th . BasicChannel . Id , th . BasicUser . Id )
2017-06-26 08:16:57 -04:00
2020-06-12 09:56:35 +05:30
// Channel is public, should be able to read post
_ , resp = c . GetPost ( th . BasicPost . Id , "" )
CheckNoError ( t , resp )
2017-06-26 08:16:57 -04:00
2020-06-12 09:56:35 +05:30
privatePost = th . CreatePostWithClient ( Client , th . BasicPrivateChannel )
_ , resp = c . GetPost ( privatePost . Id , "" )
CheckNoError ( t , resp )
} )
2017-06-26 08:16:57 -04:00
Client . RemoveUserFromChannel ( th . BasicPrivateChannel . Id , th . BasicUser . Id )
// Channel is private, should not be able to read post
2020-06-12 09:56:35 +05:30
_ , resp := Client . GetPost ( privatePost . Id , "" )
2017-02-13 10:52:50 -05:00
CheckForbiddenStatus ( t , resp )
2020-06-12 09:56:35 +05:30
// But local client should.
_ , resp = th . LocalClient . GetPost ( privatePost . Id , "" )
CheckNoError ( t , resp )
2017-02-13 10:52:50 -05:00
Client . Logout ( )
2020-06-12 09:56:35 +05:30
// Normal client should get unauthorized, but local client should get 404.
2017-02-13 10:52:50 -05:00
_ , resp = Client . GetPost ( model . NewId ( ) , "" )
CheckUnauthorizedStatus ( t , resp )
2020-06-12 09:56:35 +05:30
_ , resp = th . LocalClient . GetPost ( model . NewId ( ) , "" )
CheckNotFoundStatus ( t , resp )
2017-02-13 10:52:50 -05:00
}
2017-02-21 07:36:52 -05:00
func TestDeletePost ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05: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 )
2019-11-29 07:41:51 +09:00
require . True ( t , status , "post should return status OK" )
2017-02-21 07:36:52 -05:00
CheckNoError ( t , resp )
}
2020-11-16 18:56:29 +08:00
func TestDeletePostMessage ( t * testing . T ) {
th := Setup ( t ) . InitBasic ( )
th . LinkUserToTeam ( th . SystemAdminUser , th . BasicTeam )
2021-04-02 14:33:23 +05:30
th . App . AddUserToChannel ( th . SystemAdminUser , th . BasicChannel , false )
2020-11-16 18:56:29 +08:00
defer th . TearDown ( )
testCases := [ ] struct {
description string
client * model . Client4
delete_by interface { }
} {
{ "Do not send delete_by to regular user" , th . Client , nil } ,
{ "Send delete_by to system admin user" , th . SystemAdminClient , th . SystemAdminUser . Id } ,
}
for _ , tc := range testCases {
t . Run ( tc . description , func ( t * testing . T ) {
wsClient , err := th . CreateWebSocketClientWithClient ( tc . client )
require . Nil ( t , err )
defer wsClient . Close ( )
wsClient . Listen ( )
post := th . CreatePost ( )
status , resp := th . SystemAdminClient . DeletePost ( post . Id )
require . True ( t , status , "post should return status OK" )
CheckNoError ( t , resp )
timeout := time . After ( 5 * time . Second )
for {
select {
case ev := <- wsClient . EventChannel :
if ev . EventType ( ) == model . WEBSOCKET_EVENT_POST_DELETED {
assert . Equal ( t , tc . delete_by , ev . GetData ( ) [ "delete_by" ] )
return
}
case <- timeout :
// We just skip the test instead of failing because waiting for more than 5 seconds
2021-04-01 13:44:56 -04:00
// to get a response does not make sense, and it will unnecessarily slow down
2020-11-16 18:56:29 +08:00
// the tests further in an already congested CI environment.
t . Skip ( "timed out waiting for event" )
}
}
} )
}
}
2017-02-13 10:52:50 -05:00
func TestGetPostThread ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-02-13 10:52:50 -05:00
Client := th . Client
2017-06-19 09:08:52 -07: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 )
2021-01-14 13:46:27 +02:00
list , resp := Client . GetPostThread ( th . BasicPost . Id , "" , false )
2017-02-13 10:52:50 -05:00
CheckNoError ( t , resp )
var list2 * model . PostList
2021-01-14 13:46:27 +02:00
list2 , resp = Client . GetPostThread ( th . BasicPost . Id , resp . Etag , false )
2017-02-13 10:52:50 -05:00
CheckEtag ( t , list2 , resp )
2019-11-29 07:41:51 +09:00
require . Equal ( t , th . BasicPost . Id , list . Order [ 0 ] , "wrong order" )
2017-02-13 10:52:50 -05:00
2019-11-29 07:41:51 +09:00
_ , ok := list . Posts [ th . BasicPost . Id ]
require . True ( t , ok , "should have had post" )
2017-02-13 10:52:50 -05:00
2019-11-29 07:41:51 +09:00
_ , ok = list . Posts [ post . Id ]
require . True ( t , ok , "should have had post" )
2017-02-13 10:52:50 -05:00
2021-01-14 13:46:27 +02:00
_ , resp = Client . GetPostThread ( "junk" , "" , false )
2017-02-13 10:52:50 -05:00
CheckBadRequestStatus ( t , resp )
2021-01-14 13:46:27 +02:00
_ , resp = Client . GetPostThread ( model . NewId ( ) , "" , false )
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
2021-01-14 13:46:27 +02:00
_ , resp = Client . GetPostThread ( th . BasicPost . Id , "" , false )
2017-06-26 08:16:57 -04:00
CheckNoError ( t , resp )
privatePost := th . CreatePostWithClient ( Client , th . BasicPrivateChannel )
2021-01-14 13:46:27 +02:00
_ , resp = Client . GetPostThread ( privatePost . Id , "" , false )
2017-06-26 08:16:57 -04:00
CheckNoError ( t , resp )
Client . RemoveUserFromChannel ( th . BasicPrivateChannel . Id , th . BasicUser . Id )
// Channel is private, should not be able to read post
2021-01-14 13:46:27 +02:00
_ , resp = Client . GetPostThread ( privatePost . Id , "" , false )
2017-02-13 10:52:50 -05:00
CheckForbiddenStatus ( t , resp )
Client . Logout ( )
2021-01-14 13:46:27 +02:00
_ , resp = Client . GetPostThread ( model . NewId ( ) , "" , false )
2017-02-13 10:52:50 -05:00
CheckUnauthorizedStatus ( t , resp )
2021-01-14 13:46:27 +02:00
_ , resp = th . SystemAdminClient . GetPostThread ( th . BasicPost . Id , "" , false )
2017-02-13 10:52:50 -05:00
CheckNoError ( t , resp )
}
2017-02-21 07:36:52 -05:00
func TestSearchPosts ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2018-08-22 20:12:51 +01:00
experimentalViewArchivedChannels := * th . App . Config ( ) . TeamSettings . ExperimentalViewArchivedChannels
defer func ( ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) {
cfg . TeamSettings . ExperimentalViewArchivedChannels = & experimentalViewArchivedChannels
} )
} ( )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . TeamSettings . ExperimentalViewArchivedChannels = true
} )
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 )
2018-07-30 15:06:08 -04:00
archivedChannel := th . CreatePublicChannel ( )
_ = th . CreateMessagePostWithClient ( th . Client , archivedChannel , "#hashtag for post3" )
th . Client . DeleteChannel ( archivedChannel . Id )
2018-08-28 13:09:32 -04:00
terms := "search"
isOrSearch := false
timezoneOffset := 5
searchParams := model . SearchParameter {
Terms : & terms ,
IsOrSearch : & isOrSearch ,
TimeZoneOffset : & timezoneOffset ,
}
posts , resp := Client . SearchPostsWithParams ( th . BasicTeam . Id , & searchParams )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 3 , "wrong search" )
2018-08-28 13:09:32 -04:00
2018-09-26 07:27:04 -07:00
terms = "search"
page := 0
perPage := 2
searchParams = model . SearchParameter {
Terms : & terms ,
IsOrSearch : & isOrSearch ,
TimeZoneOffset : & timezoneOffset ,
Page : & page ,
PerPage : & perPage ,
}
posts2 , resp := Client . SearchPostsWithParams ( th . BasicTeam . Id , & searchParams )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
// We don't support paging for DB search yet, modify this when we do.
require . Len ( t , posts2 . Order , 3 , "Wrong number of posts" )
2018-09-26 07:27:04 -07:00
assert . Equal ( t , posts . Order [ 0 ] , posts2 . Order [ 0 ] )
assert . Equal ( t , posts . Order [ 1 ] , posts2 . Order [ 1 ] )
2018-09-27 12:11:19 -07:00
page = 1
searchParams = model . SearchParameter {
Terms : & terms ,
IsOrSearch : & isOrSearch ,
TimeZoneOffset : & timezoneOffset ,
Page : & page ,
PerPage : & perPage ,
}
posts2 , resp = Client . SearchPostsWithParams ( th . BasicTeam . Id , & searchParams )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
// We don't support paging for DB search yet, modify this when we do.
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts2 . Order , "Wrong number of posts" )
2018-09-27 12:11:19 -07:00
2018-08-28 13:09:32 -04:00
posts , resp = Client . SearchPosts ( th . BasicTeam . Id , "search" , false )
2017-02-21 07:36:52 -05:00
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 3 , "wrong search" )
2017-02-21 07:36:52 -05:00
posts , resp = Client . SearchPosts ( th . BasicTeam . Id , "post2" , false )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 1 , "wrong number of posts" )
require . Equal ( t , post2 . Id , posts . Order [ 0 ] , "wrong search" )
2017-02-21 07:36:52 -05:00
posts , resp = Client . SearchPosts ( th . BasicTeam . Id , "#hashtag" , false )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 1 , "wrong number of posts" )
require . Equal ( t , post3 . Id , posts . Order [ 0 ] , "wrong search" )
2017-02-21 07:36:52 -05:00
2018-09-26 07:27:04 -07:00
terms = "#hashtag"
includeDeletedChannels := true
searchParams = model . SearchParameter {
Terms : & terms ,
IsOrSearch : & isOrSearch ,
TimeZoneOffset : & timezoneOffset ,
IncludeDeletedChannels : & includeDeletedChannels ,
}
posts , resp = Client . SearchPostsWithParams ( th . BasicTeam . Id , & searchParams )
2018-07-30 15:06:08 -04:00
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 2 , "wrong search" )
2018-07-30 15:06:08 -04:00
2018-08-22 20:12:51 +01:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . TeamSettings . ExperimentalViewArchivedChannels = false
} )
2018-08-28 13:09:32 -04:00
2018-09-26 07:27:04 -07:00
posts , resp = Client . SearchPostsWithParams ( th . BasicTeam . Id , & searchParams )
2018-08-22 20:12:51 +01:00
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 1 , "wrong search" )
2018-08-22 20:12:51 +01:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "*" , false )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "searching for just * shouldn't return any results" )
2017-02-21 07:36:52 -05:00
posts , resp = Client . SearchPosts ( th . BasicTeam . Id , "post1 post2" , true )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 2 , "wrong search results" )
2017-02-21 07:36:52 -05:00
_ , 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 ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-02-21 07:36:52 -05:00
th . LoginBasic ( )
Client := th . Client
message := "#sgtitlereview with space"
2018-09-26 07:27:04 -07:00
assert . NotNil ( t , th . CreateMessagePost ( message ) )
2017-02-21 07:36:52 -05:00
message = "#sgtitlereview\n with return"
2018-09-26 07:27:04 -07:00
assert . NotNil ( t , th . CreateMessagePost ( message ) )
2017-02-21 07:36:52 -05:00
message = "no hashtag"
2018-09-26 07:27:04 -07:00
assert . NotNil ( t , th . CreateMessagePost ( message ) )
2017-02-21 07:36:52 -05:00
posts , resp := Client . SearchPosts ( th . BasicTeam . Id , "#sgtitlereview" , false )
CheckNoError ( t , resp )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 2 , "wrong search results" )
2017-02-21 07:36:52 -05:00
Client . Logout ( )
_ , resp = Client . SearchPosts ( th . BasicTeam . Id , "#sgtitlereview" , false )
CheckUnauthorizedStatus ( t , resp )
}
func TestSearchPostsInChannel ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05: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 )
2019-11-29 07:41:51 +09:00
posts , _ := Client . SearchPosts ( th . BasicTeam . Id , "channel:" , false )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "wrong number of posts for search 'channel:'" )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "in:" , false )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "wrong number of posts for search 'in:'" )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "channel:" + th . BasicChannel . Name , false )
require . Lenf ( t , posts . Order , 2 , "wrong number of posts returned for search 'channel:%v'" , th . BasicChannel . Name )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "in:" + th . BasicChannel2 . Name , false )
require . Lenf ( t , posts . Order , 2 , "wrong number of posts returned for search 'in:%v'" , th . BasicChannel2 . Name )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "channel:" + th . BasicChannel2 . Name , false )
require . Lenf ( t , posts . Order , 2 , "wrong number of posts for search 'channel:%v'" , th . BasicChannel2 . Name )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "ChAnNeL:" + th . BasicChannel2 . Name , false )
require . Lenf ( t , posts . Order , 2 , "wrong number of posts for search 'ChAnNeL:%v'" , th . BasicChannel2 . Name )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "sgtitlereview" , false )
require . Lenf ( t , posts . Order , 2 , "wrong number of posts for search 'sgtitlereview'" )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "sgtitlereview channel:" + th . BasicChannel . Name , false )
require . Lenf ( t , posts . Order , 1 , "wrong number of posts for search 'sgtitlereview channel:%v'" , th . BasicChannel . Name )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "sgtitlereview in: " + th . BasicChannel2 . Name , false )
require . Lenf ( t , posts . Order , 1 , "wrong number of posts for search 'sgtitlereview in: %v'" , th . BasicChannel2 . Name )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "sgtitlereview channel: " + th . BasicChannel2 . Name , false )
require . Lenf ( t , posts . Order , 1 , "wrong number of posts for search 'sgtitlereview channel: %v'" , th . BasicChannel2 . Name )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "channel: " + th . BasicChannel2 . Name + " channel: " + channel . Name , false )
require . Lenf ( t , posts . Order , 3 , "wrong number of posts for 'channel: %v channel: %v'" , th . BasicChannel2 . Name , channel . Name )
2017-02-21 07:36:52 -05:00
}
func TestSearchPostsFromUser ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-02-21 07:36:52 -05:00
Client := th . Client
th . LoginTeamAdmin ( )
user := th . CreateUser ( )
2017-10-02 03:50:56 -05:00
th . LinkUserToTeam ( user , th . BasicTeam )
2021-04-02 14:33:23 +05:30
th . App . AddUserToChannel ( user , th . BasicChannel , false )
th . App . AddUserToChannel ( user , th . BasicChannel2 , false )
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 )
2019-11-29 07:41:51 +09:00
posts , _ := Client . SearchPosts ( th . BasicTeam . Id , "from: " + th . TeamAdminUser . Username , false )
require . Lenf ( t , posts . Order , 2 , "wrong number of posts for search 'from: %v'" , th . TeamAdminUser . Username )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "from: " + th . BasicUser2 . Username , false )
require . Lenf ( t , posts . Order , 1 , "wrong number of posts for search 'from: %v" , th . BasicUser2 . Username )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "from: " + th . BasicUser2 . Username + " sgtitlereview" , false )
require . Lenf ( t , posts . Order , 1 , "wrong number of posts for search 'from: %v'" , th . BasicUser2 . Username )
2017-02-21 07:36:52 -05:00
message = "hullo"
_ = th . CreateMessagePost ( message )
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "from: " + th . BasicUser2 . Username + " in:" + th . BasicChannel . Name , false )
require . Len ( t , posts . Order , 1 , "wrong number of posts for search 'from: %v in:" , th . BasicUser2 . Username , th . BasicChannel . Name )
2017-02-21 07:36:52 -05:00
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 )
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "from: " + th . BasicUser2 . Username , false )
require . Lenf ( t , posts . Order , 2 , "wrong number of posts for search 'from: %v'" , th . BasicUser2 . Username )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "from: " + th . BasicUser2 . Username + " from: " + user . Username , false )
require . Lenf ( t , posts . Order , 2 , "wrong number of posts for search 'from: %v from: %v'" , th . BasicUser2 . Username , user . Username )
2017-02-21 07:36:52 -05:00
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "from: " + th . BasicUser2 . Username + " from: " + user . Username + " in:" + th . BasicChannel2 . Name , false )
require . Len ( t , posts . Order , 1 , "wrong number of posts" )
2017-02-21 07:36:52 -05:00
message = "coconut"
_ = th . CreateMessagePostWithClient ( Client , th . BasicChannel2 , message )
2019-11-29 07:41:51 +09:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "from: " + th . BasicUser2 . Username + " from: " + user . Username + " in:" + th . BasicChannel2 . Name + " coconut" , false )
require . Len ( t , posts . Order , 1 , "wrong number of posts" )
2017-02-21 07:36:52 -05:00
}
2017-02-24 06:15:13 -05:00
2018-08-28 13:09:32 -04:00
func TestSearchPostsWithDateFlags ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2018-08-28 13:09:32 -04:00
defer th . TearDown ( )
th . LoginBasic ( )
Client := th . Client
message := "sgtitlereview\n with return"
createDate := time . Date ( 2018 , 8 , 1 , 5 , 0 , 0 , 0 , time . UTC )
_ = th . CreateMessagePostNoClient ( th . BasicChannel , message , utils . MillisFromTime ( createDate ) )
message = "other message with no return"
createDate = time . Date ( 2018 , 8 , 2 , 5 , 0 , 0 , 0 , time . UTC )
_ = th . CreateMessagePostNoClient ( th . BasicChannel , message , utils . MillisFromTime ( createDate ) )
message = "other message with no return"
createDate = time . Date ( 2018 , 8 , 3 , 5 , 0 , 0 , 0 , time . UTC )
_ = th . CreateMessagePostNoClient ( th . BasicChannel , message , utils . MillisFromTime ( createDate ) )
posts , _ := Client . SearchPosts ( th . BasicTeam . Id , "return" , false )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 3 , "wrong number of posts" )
2018-08-28 13:09:32 -04:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "on:" , false )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "wrong number of posts" )
2018-08-28 13:09:32 -04:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "after:" , false )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "wrong number of posts" )
2018-08-28 13:09:32 -04:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "before:" , false )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "wrong number of posts" )
2018-08-28 13:09:32 -04:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "on:2018-08-01" , false )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 1 , "wrong number of posts" )
2018-08-28 13:09:32 -04:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "after:2018-08-01" , false )
resultCount := 0
for _ , post := range posts . Posts {
if post . UserId == th . BasicUser . Id {
resultCount = resultCount + 1
}
}
2019-11-29 07:41:51 +09:00
require . Equal ( t , 2 , resultCount , "wrong number of posts" )
2018-08-28 13:09:32 -04:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "before:2018-08-02" , false )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 1 , "wrong number of posts" )
2018-08-28 13:09:32 -04:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "before:2018-08-03 after:2018-08-02" , false )
2019-12-22 12:35:31 +01:00
require . Empty ( t , posts . Order , "wrong number of posts" )
2018-08-29 18:44:00 -04:00
posts , _ = Client . SearchPosts ( th . BasicTeam . Id , "before:2018-08-03 after:2018-08-01" , false )
2019-11-29 07:41:51 +09:00
require . Len ( t , posts . Order , 1 , "wrong number of posts" )
2018-08-28 13:09:32 -04:00
}
2017-02-24 06:15:13 -05:00
func TestGetFileInfosForPost ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2017-10-02 03:50:56 -05:00
defer th . TearDown ( )
2017-02-24 06:15:13 -05:00
Client := th . Client
2017-10-30 11:57:24 -05:00
fileIds := make ( [ ] string , 3 )
2019-11-29 07:41:51 +09:00
data , err := testutils . ReadTestFile ( "test.png" )
require . NoError ( t , err )
for i := 0 ; i < 3 ; i ++ {
fileResp , _ := Client . UploadFile ( data , th . BasicChannel . Id , "test.png" )
fileIds [ i ] = fileResp . FileInfos [ 0 ] . Id
2017-02-24 06:15:13 -05:00
}
2017-06-19 09:08:52 -07: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 )
2019-11-29 07:41:51 +09:00
require . Len ( t , infos , 3 , "missing file infos" )
2017-02-24 06:15:13 -05:00
found := false
for _ , info := range infos {
if info . Id == fileIds [ 0 ] {
found = true
}
}
2019-11-29 07:41:51 +09:00
require . True ( t , found , "missing file info" )
2017-02-24 06:15:13 -05:00
infos , resp = Client . GetFileInfosForPost ( post . Id , resp . Etag )
CheckEtag ( t , infos , resp )
infos , resp = Client . GetFileInfosForPost ( th . BasicPost . Id , "" )
CheckNoError ( t , resp )
2019-12-22 12:35:31 +01:00
require . Empty ( t , infos , "should have no file infos" )
2017-02-24 06:15:13 -05:00
_ , 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 )
}
2019-08-21 14:48:25 +02:00
func TestSetChannelUnread ( t * testing . T ) {
2020-02-10 19:31:41 +01:00
th := Setup ( t ) . InitBasic ( )
2019-08-21 14:48:25 +02:00
defer th . TearDown ( )
u1 := th . BasicUser
u2 := th . BasicUser2
s2 , _ := th . App . GetSession ( th . Client . AuthToken )
th . Client . Login ( u1 . Email , u1 . Password )
c1 := th . BasicChannel
c1toc2 := & model . ChannelView { ChannelId : th . BasicChannel2 . Id , PrevChannelId : c1 . Id }
now := utils . MillisFromTime ( time . Now ( ) )
th . CreateMessagePostNoClient ( c1 , "AAA" , now )
p2 := th . CreateMessagePostNoClient ( c1 , "BBB" , now + 10 )
th . CreateMessagePostNoClient ( c1 , "CCC" , now + 20 )
pp1 := th . CreateMessagePostNoClient ( th . BasicPrivateChannel , "Sssh!" , now )
pp2 := th . CreateMessagePostNoClient ( th . BasicPrivateChannel , "You Sssh!" , now + 10 )
require . NotNil ( t , pp1 )
require . NotNil ( t , pp2 )
// Ensure that post have been read
unread , err := th . App . GetChannelUnread ( c1 . Id , u1 . Id )
require . Nil ( t , err )
require . Equal ( t , int64 ( 4 ) , unread . MsgCount )
unread , err = th . App . GetChannelUnread ( c1 . Id , u2 . Id )
require . Nil ( t , err )
require . Equal ( t , int64 ( 4 ) , unread . MsgCount )
_ , err = th . App . ViewChannel ( c1toc2 , u2 . Id , s2 . Id )
require . Nil ( t , err )
unread , err = th . App . GetChannelUnread ( c1 . Id , u2 . Id )
require . Nil ( t , err )
require . Equal ( t , int64 ( 0 ) , unread . MsgCount )
t . Run ( "Unread last one" , func ( t * testing . T ) {
r := th . Client . SetPostUnread ( u1 . Id , p2 . Id )
checkHTTPStatus ( t , r , 200 , false )
unread , err := th . App . GetChannelUnread ( c1 . Id , u1 . Id )
require . Nil ( t , err )
2019-09-02 10:00:38 +02:00
assert . Equal ( t , int64 ( 2 ) , unread . MsgCount )
2019-08-21 14:48:25 +02:00
} )
t . Run ( "Unread on a private channel" , func ( t * testing . T ) {
r := th . Client . SetPostUnread ( u1 . Id , pp2 . Id )
assert . Equal ( t , 200 , r . StatusCode )
unread , err := th . App . GetChannelUnread ( th . BasicPrivateChannel . Id , u1 . Id )
require . Nil ( t , err )
2019-09-02 10:00:38 +02:00
assert . Equal ( t , int64 ( 1 ) , unread . MsgCount )
2019-08-21 14:48:25 +02:00
r = th . Client . SetPostUnread ( u1 . Id , pp1 . Id )
assert . Equal ( t , 200 , r . StatusCode )
unread , err = th . App . GetChannelUnread ( th . BasicPrivateChannel . Id , u1 . Id )
require . Nil ( t , err )
2019-09-02 10:00:38 +02:00
assert . Equal ( t , int64 ( 2 ) , unread . MsgCount )
2019-08-21 14:48:25 +02:00
} )
t . Run ( "Can't unread an imaginary post" , func ( t * testing . T ) {
r := th . Client . SetPostUnread ( u1 . Id , "invalid4ofngungryquinj976y" )
assert . Equal ( t , http . StatusForbidden , r . StatusCode )
} )
// let's create another user to test permissions
u3 := th . CreateUser ( )
c3 := th . CreateClient ( )
c3 . Login ( u3 . Email , u3 . Password )
t . Run ( "Can't unread channels you don't belong to" , func ( t * testing . T ) {
r := c3 . SetPostUnread ( u3 . Id , pp1 . Id )
assert . Equal ( t , http . StatusForbidden , r . StatusCode )
} )
t . Run ( "Can't unread users you don't have permission to edit" , func ( t * testing . T ) {
r := c3 . SetPostUnread ( u1 . Id , pp1 . Id )
assert . Equal ( t , http . StatusForbidden , r . StatusCode )
} )
t . Run ( "Can't unread if user is not logged in" , func ( t * testing . T ) {
th . Client . Logout ( )
response := th . Client . SetPostUnread ( u1 . Id , p2 . Id )
checkHTTPStatus ( t , response , http . StatusUnauthorized , true )
} )
}