2019-11-29 06:59:40 -05:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2017-05-10 19:16:45 -04:00
package app
import (
2026-01-27 01:27:06 -05:00
"encoding/json"
2022-07-06 02:26:39 -04:00
"errors"
2017-08-29 17:14:59 -04:00
"fmt"
"net/http"
2025-12-11 01:59:50 -05:00
"os"
2023-05-03 06:42:57 -04:00
"strconv"
2018-12-17 18:16:57 -05:00
"sync"
2017-05-10 19:16:45 -04:00
"testing"
2017-07-04 15:17:54 -04:00
"time"
2017-05-10 19:16:45 -04:00
2017-08-29 17:14:59 -04:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2023-06-11 01:24:35 -04:00
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin/plugintest/mock"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/v8/channels/store"
storemocks "github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
"github.com/mattermost/mattermost/server/v8/channels/testlib"
"github.com/mattermost/mattermost/server/v8/platform/services/imageproxy"
"github.com/mattermost/mattermost/server/v8/platform/services/searchengine/mocks"
2017-05-10 19:16:45 -04:00
)
2025-12-11 01:59:50 -05:00
func enableBoRFeature ( th * TestHelper ) {
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
cfg . ServiceSettings . EnableBurnOnRead = model . NewPointer ( true )
} )
}
2025-05-20 09:05:10 -04:00
func makePendingPostId ( user * model . User ) string {
return fmt . Sprintf ( "%s:%s" , user . Id , strconv . FormatInt ( model . GetMillis ( ) , 10 ) )
}
2018-12-17 18:16:57 -05:00
func TestCreatePostDeduplicate ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2018-12-17 18:16:57 -05:00
t . Run ( "duplicate create post is idempotent" , func ( t * testing . T ) {
2025-05-20 09:05:10 -04:00
session := & model . Session {
UserId : th . BasicUser . Id ,
}
session , err := th . App . CreateSession ( th . Context , session )
require . Nil ( t , err )
pendingPostId := makePendingPostId ( th . BasicUser )
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePostAsUser ( th . Context . WithSession ( session ) , & model . Post {
2018-12-17 18:16:57 -05:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "message" ,
PendingPostId : pendingPostId ,
2025-05-20 09:05:10 -04:00
} , session . Id , true )
2018-12-17 18:16:57 -05:00
require . Nil ( t , err )
require . Equal ( t , "message" , post . Message )
2026-01-20 04:38:27 -05:00
duplicatePost , _ , err := th . App . CreatePostAsUser ( th . Context . WithSession ( session ) , & model . Post {
2018-12-17 18:16:57 -05:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "message" ,
PendingPostId : pendingPostId ,
2025-05-20 09:05:10 -04:00
} , session . Id , true )
2018-12-17 18:16:57 -05:00
require . Nil ( t , err )
require . Equal ( t , post . Id , duplicatePost . Id , "should have returned previously created post id" )
require . Equal ( t , "message" , duplicatePost . Message )
} )
t . Run ( "post rejected by plugin leaves cache ready for non-deduplicated try" , func ( t * testing . T ) {
2021-08-16 13:46:44 -04:00
setupPluginAPITest ( t , `
2018-12-17 18:16:57 -05:00
package main
import (
2023-06-11 01:24:35 -04:00
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/model"
2018-12-17 18:16:57 -05:00
)
type MyPlugin struct {
plugin . MattermostPlugin
allow bool
}
func ( p * MyPlugin ) MessageWillBePosted ( c * plugin . Context , post * model . Post ) ( * model . Post , string ) {
if ! p . allow {
p . allow = true
return nil , "rejected"
}
return nil , ""
}
func main ( ) {
plugin . ClientMain ( & MyPlugin { } )
}
2021-08-13 13:41:32 -04:00
` , ` { "id" : "testrejectfirstpost" , "server" : { "executable" : "backend.exe" } } ` , "testrejectfirstpost" , th . App , th . Context )
2018-12-17 18:16:57 -05:00
2025-05-20 09:05:10 -04:00
session := & model . Session {
UserId : th . BasicUser . Id ,
}
session , err := th . App . CreateSession ( th . Context , session )
require . Nil ( t , err )
pendingPostId := makePendingPostId ( th . BasicUser )
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePostAsUser ( th . Context . WithSession ( session ) , & model . Post {
2018-12-17 18:16:57 -05:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "message" ,
PendingPostId : pendingPostId ,
2025-05-20 09:05:10 -04:00
} , session . Id , true )
2018-12-17 18:16:57 -05:00
require . NotNil ( t , err )
require . Equal ( t , "Post rejected by plugin. rejected" , err . Id )
require . Nil ( t , post )
2026-01-20 04:38:27 -05:00
duplicatePost , _ , err := th . App . CreatePostAsUser ( th . Context . WithSession ( session ) , & model . Post {
2018-12-17 18:16:57 -05:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "message" ,
PendingPostId : pendingPostId ,
2025-05-20 09:05:10 -04:00
} , session . Id , true )
2018-12-17 18:16:57 -05:00
require . Nil ( t , err )
require . Equal ( t , "message" , duplicatePost . Message )
} )
t . Run ( "slow posting after cache entry blocks duplicate request" , func ( t * testing . T ) {
2021-08-16 13:46:44 -04:00
setupPluginAPITest ( t , `
2018-12-17 18:16:57 -05:00
package main
import (
2023-06-11 01:24:35 -04:00
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/model"
2018-12-17 18:16:57 -05:00
"time"
)
type MyPlugin struct {
plugin . MattermostPlugin
instant bool
}
func ( p * MyPlugin ) MessageWillBePosted ( c * plugin . Context , post * model . Post ) ( * model . Post , string ) {
if ! p . instant {
p . instant = true
time . Sleep ( 3 * time . Second )
}
return nil , ""
}
func main ( ) {
plugin . ClientMain ( & MyPlugin { } )
}
2021-08-13 13:41:32 -04:00
` , ` { "id" : "testdelayfirstpost" , "server" : { "executable" : "backend.exe" } } ` , "testdelayfirstpost" , th . App , th . Context )
2018-12-17 18:16:57 -05:00
2025-05-20 09:05:10 -04:00
session := & model . Session {
UserId : th . BasicUser . Id ,
}
session , err := th . App . CreateSession ( th . Context , session )
require . Nil ( t , err )
2018-12-17 18:16:57 -05:00
var post * model . Post
2025-05-20 09:05:10 -04:00
pendingPostId := makePendingPostId ( th . BasicUser )
2018-12-17 18:16:57 -05:00
wg := sync . WaitGroup { }
// Launch a goroutine to make the first CreatePost call that will get delayed
// by the plugin above.
2026-03-27 16:11:52 -04:00
wg . Go ( func ( ) {
2021-02-16 06:00:01 -05:00
var appErr * model . AppError
2026-01-20 04:38:27 -05:00
post , _ , appErr = th . App . CreatePostAsUser ( th . Context . WithSession ( session ) , & model . Post {
2018-12-17 18:16:57 -05:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "plugin delayed" ,
PendingPostId : pendingPostId ,
2025-05-20 09:05:10 -04:00
} , session . Id , true )
2021-02-16 06:00:01 -05:00
require . Nil ( t , appErr )
2018-12-17 18:16:57 -05:00
require . Equal ( t , post . Message , "plugin delayed" )
2026-03-27 16:11:52 -04:00
} )
2018-12-17 18:16:57 -05:00
// Give the goroutine above a chance to start and get delayed by the plugin.
time . Sleep ( 2 * time . Second )
// Try creating a duplicate post
2026-01-20 04:38:27 -05:00
duplicatePost , _ , err := th . App . CreatePostAsUser ( th . Context . WithSession ( session ) , & model . Post {
2018-12-17 18:16:57 -05:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "plugin delayed" ,
PendingPostId : pendingPostId ,
2025-05-20 09:05:10 -04:00
} , session . Id , true )
2018-12-17 18:16:57 -05:00
require . NotNil ( t , err )
require . Equal ( t , "api.post.deduplicate_create_post.pending" , err . Id )
require . Nil ( t , duplicatePost )
// Wait for the first CreatePost to finish to ensure assertions are made.
wg . Wait ( )
} )
t . Run ( "duplicate create post after cache expires is not idempotent" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
originalCacheTTL := pendingPostIDsCacheTTL
pendingPostIDsCacheTTL = time . Second
t . Cleanup ( func ( ) {
pendingPostIDsCacheTTL = originalCacheTTL
} )
2025-05-20 09:05:10 -04:00
session := & model . Session {
UserId : th . BasicUser . Id ,
}
session , err := th . App . CreateSession ( th . Context , session )
require . Nil ( t , err )
pendingPostId := makePendingPostId ( th . BasicUser )
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePostAsUser ( th . Context . WithSession ( session ) , & model . Post {
2018-12-17 18:16:57 -05:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "message" ,
PendingPostId : pendingPostId ,
2025-05-20 09:05:10 -04:00
} , session . Id , true )
2018-12-17 18:16:57 -05:00
require . Nil ( t , err )
require . Equal ( t , "message" , post . Message )
2025-05-30 07:58:26 -04:00
time . Sleep ( pendingPostIDsCacheTTL )
2018-12-17 18:16:57 -05:00
2026-01-20 04:38:27 -05:00
duplicatePost , _ , err := th . App . CreatePostAsUser ( th . Context . WithSession ( session ) , & model . Post {
2018-12-17 18:16:57 -05:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "message" ,
PendingPostId : pendingPostId ,
2025-05-20 09:05:10 -04:00
} , session . Id , true )
2018-12-17 18:16:57 -05:00
require . Nil ( t , err )
require . NotEqual ( t , post . Id , duplicatePost . Id , "should have created new post id" )
require . Equal ( t , "message" , duplicatePost . Message )
} )
2025-05-20 09:05:10 -04:00
t . Run ( "Permissison to post required to resolve from pending post cache" , func ( t * testing . T ) {
sessionBasicUser := & model . Session {
UserId : th . BasicUser . Id ,
}
sessionBasicUser , err := th . App . CreateSession ( th . Context , sessionBasicUser )
require . Nil ( t , err )
sessionBasicUser2 := & model . Session {
UserId : th . BasicUser2 . Id ,
}
sessionBasicUser2 , err = th . App . CreateSession ( th . Context , sessionBasicUser2 )
require . Nil ( t , err )
pendingPostId := makePendingPostId ( th . BasicUser )
2025-11-12 07:00:51 -05:00
privateChannel := th . CreatePrivateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , th . BasicUser , privateChannel )
2025-05-20 09:05:10 -04:00
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePostAsUser ( th . Context . WithSession ( sessionBasicUser ) , & model . Post {
2025-05-20 09:05:10 -04:00
UserId : th . BasicUser . Id ,
ChannelId : privateChannel . Id ,
Message : "message" ,
PendingPostId : pendingPostId ,
} , sessionBasicUser . Id , true )
require . Nil ( t , err )
require . Equal ( t , "message" , post . Message )
2026-01-20 04:38:27 -05:00
postAsDifferentUser , _ , err := th . App . CreatePostAsUser ( th . Context . WithSession ( sessionBasicUser2 ) , & model . Post {
2025-05-20 09:05:10 -04:00
UserId : th . BasicUser2 . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "message2" ,
PendingPostId : pendingPostId ,
} , sessionBasicUser2 . Id , true )
require . Nil ( t , err )
require . NotEqual ( t , post . Id , postAsDifferentUser . Id , "should have created new post id" )
require . Equal ( t , "message2" , postAsDifferentUser . Message )
// Both posts should exist unchanged
actualPost , err := th . App . GetSinglePost ( th . Context , post . Id , false )
require . Nil ( t , err )
assert . Equal ( t , "message" , actualPost . Message )
assert . Equal ( t , privateChannel . Id , actualPost . ChannelId )
actualPostAsDifferentUser , err := th . App . GetSinglePost ( th . Context , postAsDifferentUser . Id , false )
require . Nil ( t , err )
assert . Equal ( t , "message2" , actualPostAsDifferentUser . Message )
assert . Equal ( t , th . BasicChannel . Id , actualPostAsDifferentUser . ChannelId )
} )
2018-12-17 18:16:57 -05:00
}
2019-01-22 15:58:22 -05:00
func TestAttachFilesToPost ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2019-01-22 15:58:22 -05:00
t . Run ( "should attach files" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-01-22 15:58:22 -05:00
2023-12-04 12:34:57 -05:00
info1 , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . Save ( th . Context ,
& model . FileInfo {
CreatorId : th . BasicUser . Id ,
Path : "path.txt" ,
} )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err )
2019-05-15 16:07:03 -04:00
2023-12-04 12:34:57 -05:00
info2 , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . Save ( th . Context ,
& model . FileInfo {
CreatorId : th . BasicUser . Id ,
Path : "path.txt" ,
} )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err )
2019-01-22 15:58:22 -05:00
post := th . BasicPost
post . FileIds = [ ] string { info1 . Id , info2 . Id }
2025-12-11 01:59:50 -05:00
attachedFiles , appErr := th . App . attachFilesToPost ( th . Context , post , post . FileIds )
2021-02-16 06:00:01 -05:00
assert . Nil ( t , appErr )
2025-12-11 01:59:50 -05:00
assert . Len ( t , attachedFiles , 2 )
assert . Contains ( t , attachedFiles , info1 . Id )
assert . Contains ( t , attachedFiles , info2 . Id )
2019-01-22 15:58:22 -05:00
2023-11-07 04:04:16 -05:00
infos , _ , appErr := th . App . GetFileInfosForPost ( th . Context , post . Id , false , false )
2021-02-16 06:00:01 -05:00
assert . Nil ( t , appErr )
2019-01-22 15:58:22 -05:00
assert . Len ( t , infos , 2 )
} )
2025-12-11 01:59:50 -05:00
t . Run ( "should return only successfully attached files after failing to add files" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-01-22 15:58:22 -05:00
2023-12-04 12:34:57 -05:00
info1 , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . Save ( th . Context ,
& model . FileInfo {
CreatorId : th . BasicUser . Id ,
Path : "path.txt" ,
PostId : model . NewId ( ) ,
} )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err )
2019-05-15 16:07:03 -04:00
2023-12-04 12:34:57 -05:00
info2 , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . Save ( th . Context ,
& model . FileInfo {
CreatorId : th . BasicUser . Id ,
Path : "path.txt" ,
} )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err )
2019-01-22 15:58:22 -05:00
post := th . BasicPost
post . FileIds = [ ] string { info1 . Id , info2 . Id }
2025-12-11 01:59:50 -05:00
attachedFiles , appErr := th . App . attachFilesToPost ( th . Context , post , post . FileIds )
2021-02-16 06:00:01 -05:00
assert . Nil ( t , appErr )
2025-12-11 01:59:50 -05:00
assert . Len ( t , attachedFiles , 1 )
assert . Contains ( t , attachedFiles , info2 . Id )
2019-01-22 15:58:22 -05:00
2023-11-07 04:04:16 -05:00
infos , _ , appErr := th . App . GetFileInfosForPost ( th . Context , post . Id , false , false )
2021-02-16 06:00:01 -05:00
assert . Nil ( t , appErr )
2019-01-22 15:58:22 -05:00
assert . Len ( t , infos , 1 )
assert . Equal ( t , info2 . Id , infos [ 0 ] . Id )
2024-05-24 10:05:48 -04:00
updated , appErr := th . App . GetSinglePost ( th . Context , post . Id , false )
2021-02-16 06:00:01 -05:00
require . Nil ( t , appErr )
2019-01-22 15:58:22 -05:00
assert . Len ( t , updated . FileIds , 1 )
assert . Contains ( t , updated . FileIds , info2 . Id )
} )
}
2017-07-04 15:17:54 -04:00
func TestUpdatePostEditAt ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2017-07-04 15:17:54 -04:00
2021-04-28 10:43:41 -04:00
post := th . BasicPost . Clone ( )
2017-07-04 15:17:54 -04:00
post . IsPinned = true
2026-01-20 04:38:27 -05:00
saved , isMemberForPreviews , err := th . App . UpdatePost ( th . Context , post , & model . UpdatePostOptions { SafeUpdate : true } )
2019-09-24 09:23:30 -04:00
require . Nil ( t , err )
assert . Equal ( t , saved . EditAt , post . EditAt , "shouldn't have updated post.EditAt when pinning post" )
2026-01-20 04:38:27 -05:00
assert . True ( t , isMemberForPreviews )
2020-03-13 16:12:20 -04:00
post = saved . Clone ( )
2017-07-04 15:17:54 -04:00
time . Sleep ( time . Millisecond * 100 )
post . Message = model . NewId ( )
2026-01-20 04:38:27 -05:00
saved , isMemberForPreviews , err = th . App . UpdatePost ( th . Context , post , & model . UpdatePostOptions { SafeUpdate : true } )
2019-09-24 09:23:30 -04:00
require . Nil ( t , err )
assert . NotEqual ( t , saved . EditAt , post . EditAt , "should have updated post.EditAt when updating post message" )
2026-01-20 04:38:27 -05:00
assert . True ( t , isMemberForPreviews )
2018-06-25 15:33:13 -04:00
time . Sleep ( time . Millisecond * 200 )
2017-07-04 15:17:54 -04:00
}
2018-02-09 10:31:01 -05:00
func TestUpdatePostTimeLimit ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2018-02-09 10:31:01 -05:00
2021-04-28 10:43:41 -04:00
post := th . BasicPost . Clone ( )
2018-02-09 10:31:01 -05:00
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2018-02-09 10:31:01 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . PostEditTimeLimit = - 1
} )
2026-01-20 04:38:27 -05:00
_ , _ , err := th . App . UpdatePost ( th . Context , post , & model . UpdatePostOptions { SafeUpdate : true } )
2019-09-24 09:23:30 -04:00
require . Nil ( t , err )
2018-02-09 10:31:01 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . PostEditTimeLimit = 1000000000
} )
post . Message = model . NewId ( )
2019-09-24 09:23:30 -04:00
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . UpdatePost ( th . Context , post , & model . UpdatePostOptions { SafeUpdate : true } )
2019-09-24 09:23:30 -04:00
require . Nil ( t , err , "should allow you to edit the post" )
2018-02-09 10:31:01 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . PostEditTimeLimit = 1
} )
post . Message = model . NewId ( )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . UpdatePost ( th . Context , post , & model . UpdatePostOptions { SafeUpdate : true } )
2023-03-09 13:37:23 -05:00
require . Nil ( t , err , "should allow you to edit an old post because the time check is applied above in the call hierarchy" )
2018-02-09 10:31:01 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . PostEditTimeLimit = - 1
} )
}
2019-03-11 04:26:31 -04:00
func TestUpdatePostInArchivedChannel ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-03-11 04:26:31 -04:00
2025-11-12 07:00:51 -05:00
archivedChannel := th . CreateChannel ( t , th . BasicTeam )
post := th . CreatePost ( t , archivedChannel )
2025-04-29 02:50:47 -04:00
appErr := th . App . DeleteChannel ( th . Context , archivedChannel , "" )
require . Nil ( t , appErr )
2019-03-11 04:26:31 -04:00
2026-01-20 04:38:27 -05:00
_ , _ , err := th . App . UpdatePost ( th . Context , post , & model . UpdatePostOptions { SafeUpdate : true } )
2019-03-11 04:26:31 -04:00
require . NotNil ( t , err )
require . Equal ( t , "api.post.update_post.can_not_update_post_in_deleted.error" , err . Id )
}
2017-05-10 19:16:45 -04:00
func TestPostReplyToPostWhereRootPosterLeftChannel ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2017-05-10 19:16:45 -04:00
// This test ensures that when replying to a root post made by a user who has since left the channel, the reply
// post completes successfully. This is a regression test for PLT-6523.
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2017-05-10 19:16:45 -04:00
channel := th . BasicChannel
userInChannel := th . BasicUser2
userNotInChannel := th . BasicUser
rootPost := th . BasicPost
2022-07-14 05:01:29 -04:00
_ , err := th . App . AddUserToChannel ( th . Context , userInChannel , channel , false )
2019-09-24 09:23:30 -04:00
require . Nil ( t , err )
2017-05-10 19:16:45 -04:00
2021-05-11 06:00:44 -04:00
err = th . App . RemoveUserFromChannel ( th . Context , userNotInChannel . Id , "" , channel )
2019-09-24 09:23:30 -04:00
require . Nil ( t , err )
2017-05-10 19:16:45 -04:00
replyPost := model . Post {
2017-07-04 15:17:54 -04:00
Message : "asd" ,
ChannelId : channel . Id ,
RootId : rootPost . Id ,
2017-05-10 19:16:45 -04:00
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
2017-07-04 15:17:54 -04:00
UserId : userInChannel . Id ,
CreateAt : 0 ,
2017-05-10 19:16:45 -04:00
}
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePostAsUser ( th . Context , & replyPost , "" , true )
2019-09-24 09:23:30 -04:00
require . Nil ( t , err )
2017-05-10 19:16:45 -04:00
}
2017-08-29 17:14:59 -04:00
2019-03-05 05:52:33 -05:00
func TestPostAttachPostToChildPost ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-03-05 05:52:33 -05:00
channel := th . BasicChannel
user := th . BasicUser
rootPost := th . BasicPost
replyPost1 := model . Post {
Message : "reply one" ,
ChannelId : channel . Id ,
RootId : rootPost . Id ,
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
UserId : user . Id ,
CreateAt : 0 ,
}
2026-01-20 04:38:27 -05:00
res1 , _ , err := th . App . CreatePostAsUser ( th . Context , & replyPost1 , "" , true )
2019-09-24 09:23:30 -04:00
require . Nil ( t , err )
2019-03-05 05:52:33 -05:00
replyPost2 := model . Post {
Message : "reply two" ,
ChannelId : channel . Id ,
RootId : res1 . Id ,
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
UserId : user . Id ,
CreateAt : 0 ,
}
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePostAsUser ( th . Context , & replyPost2 , "" , true )
2019-09-24 09:23:30 -04:00
assert . Equalf ( t , err . StatusCode , http . StatusBadRequest , "Expected BadRequest error, got %v" , err )
2019-03-05 05:52:33 -05:00
replyPost3 := model . Post {
Message : "reply three" ,
ChannelId : channel . Id ,
RootId : rootPost . Id ,
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
UserId : user . Id ,
CreateAt : 0 ,
}
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePostAsUser ( th . Context , & replyPost3 , "" , true )
2019-09-24 09:23:30 -04:00
assert . Nil ( t , err )
2019-03-05 05:52:33 -05:00
}
2023-10-26 09:15:00 -04:00
func TestUpdatePostPluginHooks ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2023-10-26 09:15:00 -04:00
t . Run ( "Should stop processing at first reject" , func ( t * testing . T ) {
setupMultiPluginAPITest ( t , [ ] string {
`
package main
import (
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/model"
)
type MyPlugin struct {
plugin . MattermostPlugin
}
func ( p * MyPlugin ) MessageWillBeUpdated ( c * plugin . Context , newPost , oldPost * model . Post ) ( * model . Post , string ) {
return nil , "rejected"
}
func main ( ) {
plugin . ClientMain ( & MyPlugin { } )
}
` ,
`
package main
import (
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/model"
)
type MyPlugin struct {
plugin . MattermostPlugin
}
func ( p * MyPlugin ) MessageWillBeUpdated ( c * plugin . Context , newPost , oldPost * model . Post ) ( * model . Post , string ) {
if ( newPost == nil ) {
return nil , "nil post"
}
newPost . Message = newPost . Message + "fromplugin"
return newPost , ""
}
func main ( ) {
plugin . ClientMain ( & MyPlugin { } )
}
` ,
} , [ ] string {
` { "id": "testrejectfirstpost", "server": { "executable": "backend.exe"}} ` ,
` { "id": "testupdatepost", "server": { "executable": "backend.exe"}} ` ,
} , [ ] string {
"testrejectfirstpost" , "testupdatepost" ,
} , true , th . App , th . Context )
2025-05-20 09:05:10 -04:00
pendingPostId := makePendingPostId ( th . BasicUser )
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePostAsUser ( th . Context , & model . Post {
2023-10-26 09:15:00 -04:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "message" ,
PendingPostId : pendingPostId ,
} , "" , true )
require . Nil ( t , err )
post . Message = "new message"
2026-01-20 04:38:27 -05:00
updatedPost , _ , err := th . App . UpdatePost ( th . Context , post , nil )
2023-10-26 09:15:00 -04:00
require . Nil ( t , updatedPost )
require . NotNil ( t , err )
require . Equal ( t , "Post rejected by plugin. rejected" , err . Id )
} )
t . Run ( "Should update" , func ( t * testing . T ) {
setupMultiPluginAPITest ( t , [ ] string {
`
package main
import (
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/model"
)
type MyPlugin struct {
plugin . MattermostPlugin
}
func ( p * MyPlugin ) MessageWillBeUpdated ( c * plugin . Context , newPost , oldPost * model . Post ) ( * model . Post , string ) {
newPost . Message = newPost . Message + " 1"
return newPost , ""
}
func main ( ) {
plugin . ClientMain ( & MyPlugin { } )
}
` ,
`
package main
import (
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/model"
)
type MyPlugin struct {
plugin . MattermostPlugin
}
func ( p * MyPlugin ) MessageWillBeUpdated ( c * plugin . Context , newPost , oldPost * model . Post ) ( * model . Post , string ) {
2023-11-01 06:23:56 -04:00
newPost . Message = "2 " + newPost . Message
2023-10-26 09:15:00 -04:00
return newPost , ""
}
func main ( ) {
plugin . ClientMain ( & MyPlugin { } )
}
` ,
} , [ ] string {
` { "id": "testaddone", "server": { "executable": "backend.exe"}} ` ,
` { "id": "testaddtwo", "server": { "executable": "backend.exe"}} ` ,
} , [ ] string {
"testaddone" , "testaddtwo" ,
} , true , th . App , th . Context )
2025-05-20 09:05:10 -04:00
pendingPostId := makePendingPostId ( th . BasicUser )
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePostAsUser ( th . Context , & model . Post {
2023-10-26 09:15:00 -04:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "message" ,
PendingPostId : pendingPostId ,
} , "" , true )
require . Nil ( t , err )
post . Message = "new message"
2026-01-20 04:38:27 -05:00
updatedPost , isMemberForPreviews , err := th . App . UpdatePost ( th . Context , post , nil )
require . True ( t , isMemberForPreviews )
2023-10-26 09:15:00 -04:00
require . Nil ( t , err )
require . NotNil ( t , updatedPost )
2023-11-01 06:23:56 -04:00
require . Equal ( t , "2 new message 1" , updatedPost . Message )
2023-10-26 09:15:00 -04:00
} )
}
2017-11-28 16:02:56 -05:00
func TestPostChannelMentions ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2017-11-28 16:02:56 -05:00
channel := th . BasicChannel
user := th . BasicUser
2026-02-16 15:31:32 -05:00
// Create context with session for the user to properly test sanitization
ctx := th . Context . WithSession ( & model . Session { UserId : user . Id } )
2021-05-11 06:00:44 -04:00
channelToMention , err := th . App . CreateChannel ( th . Context , & model . Channel {
2017-11-28 16:02:56 -05:00
DisplayName : "Mention Test" ,
Name : "mention-test" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypeOpen ,
2017-11-28 16:02:56 -05:00
TeamId : th . BasicTeam . Id ,
} , false )
2019-09-24 09:23:30 -04:00
require . Nil ( t , err )
2025-04-29 02:50:47 -04:00
defer func ( ) {
appErr := th . App . PermanentDeleteChannel ( th . Context , channelToMention )
require . Nil ( t , appErr )
} ( )
2023-10-05 03:40:53 -04:00
channelToMention2 , err := th . App . CreateChannel ( th . Context , & model . Channel {
DisplayName : "Mention Test2" ,
Name : "mention-test2" ,
Type : model . ChannelTypeOpen ,
TeamId : th . BasicTeam . Id ,
} , false )
require . Nil ( t , err )
2025-04-29 02:50:47 -04:00
defer func ( ) {
appErr := th . App . PermanentDeleteChannel ( th . Context , channelToMention2 )
require . Nil ( t , appErr )
} ( )
2017-11-28 16:02:56 -05:00
2022-07-14 05:01:29 -04:00
_ , err = th . App . AddUserToChannel ( th . Context , user , channel , false )
2017-11-28 16:02:56 -05:00
require . Nil ( t , err )
post := & model . Post {
Message : fmt . Sprintf ( "hello, ~%v!" , channelToMention . Name ) ,
ChannelId : channel . Id ,
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
UserId : user . Id ,
CreateAt : 0 ,
}
2026-02-16 15:31:32 -05:00
post , _ , err = th . App . CreatePostAsUser ( ctx , post , "" , true )
2017-11-28 16:02:56 -05:00
require . Nil ( t , err )
2022-07-05 02:46:50 -04:00
assert . Equal ( t , map [ string ] any {
"mention-test" : map [ string ] any {
2017-11-28 16:02:56 -05:00
"display_name" : "Mention Test" ,
2020-03-31 09:25:53 -04:00
"team_name" : th . BasicTeam . Name ,
2017-11-28 16:02:56 -05:00
} ,
2025-03-20 07:53:50 -04:00
} , post . GetProp ( model . PostPropsChannelMentions ) )
2017-12-05 14:18:45 -05:00
2023-10-05 03:40:53 -04:00
post . Message = fmt . Sprintf ( "goodbye, ~%v!" , channelToMention2 . Name )
2026-02-16 15:31:32 -05:00
result , isMemberForPreviews , err := th . App . UpdatePost ( ctx , post , nil )
2026-01-20 04:38:27 -05:00
require . True ( t , isMemberForPreviews )
2017-12-05 14:18:45 -05:00
require . Nil ( t , err )
2022-07-05 02:46:50 -04:00
assert . Equal ( t , map [ string ] any {
2023-10-05 03:40:53 -04:00
"mention-test2" : map [ string ] any {
"display_name" : "Mention Test2" ,
2020-03-31 09:25:53 -04:00
"team_name" : th . BasicTeam . Name ,
2017-12-05 14:18:45 -05:00
} ,
2025-03-20 07:53:50 -04:00
} , result . GetProp ( model . PostPropsChannelMentions ) )
2023-10-05 03:40:53 -04:00
result . Message = "no more mentions!"
2026-02-16 15:31:32 -05:00
result , isMemberForPreviews , err = th . App . UpdatePost ( ctx , result , nil )
2026-01-20 04:38:27 -05:00
require . True ( t , isMemberForPreviews )
2023-10-05 03:40:53 -04:00
require . Nil ( t , err )
2025-03-20 07:53:50 -04:00
assert . Nil ( t , result . GetProp ( model . PostPropsChannelMentions ) )
2017-11-28 16:02:56 -05:00
}
2018-01-22 16:32:50 -05:00
func TestImageProxy ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2020-03-02 11:13:39 -05:00
th := SetupWithStoreMock ( t )
2018-01-22 16:32:50 -05:00
2022-10-06 04:04:21 -04:00
mockStore := th . App . Srv ( ) . Store ( ) . ( * storemocks . Store )
2020-03-02 11:13:39 -05:00
mockUserStore := storemocks . UserStore { }
mockUserStore . On ( "Count" , mock . Anything ) . Return ( int64 ( 10 ) , nil )
mockPostStore := storemocks . PostStore { }
mockPostStore . On ( "GetMaxPostSize" ) . Return ( 65535 , nil )
mockSystemStore := storemocks . SystemStore { }
2020-08-21 14:23:04 -04:00
mockSystemStore . On ( "GetByName" , "UpgradedFromTE" ) . Return ( & model . System { Name : "UpgradedFromTE" , Value : "false" } , nil )
2020-03-02 11:13:39 -05:00
mockSystemStore . On ( "GetByName" , "InstallationDate" ) . Return ( & model . System { Name : "InstallationDate" , Value : "10" } , nil )
2020-04-30 11:18:12 -04:00
mockSystemStore . On ( "GetByName" , "FirstServerRunTimestamp" ) . Return ( & model . System { Name : "FirstServerRunTimestamp" , Value : "10" } , nil )
2020-03-02 11:13:39 -05:00
mockStore . On ( "User" ) . Return ( & mockUserStore )
mockStore . On ( "Post" ) . Return ( & mockPostStore )
mockStore . On ( "System" ) . Return ( & mockSystemStore )
2022-03-15 09:39:23 -04:00
mockStore . On ( "GetDBSchemaVersion" ) . Return ( 1 , nil )
2020-03-02 11:13:39 -05:00
2018-02-09 16:41:06 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . SiteURL = "http://mymattermost.com"
} )
2022-08-24 03:10:56 -04:00
th . App . ch . imageProxy = imageproxy . MakeImageProxy ( th . Server . platform , th . Server . HTTPService ( ) , th . Server . Log ( ) )
2020-11-10 02:36:59 -05:00
2026-04-08 15:49:43 -04:00
testHMACKey := model . NewTestPassword ( )
2018-01-22 16:32:50 -05:00
for name , tc := range map [ string ] struct {
2020-11-10 02:36:59 -05:00
ProxyType string
ProxyURL string
ProxyOptions string
ImageURL string
ProxiedImageURL string
ProxiedRemovedImageURL string
2018-01-22 16:32:50 -05:00
} {
"atmos/camo" : {
2021-07-12 14:05:36 -04:00
ProxyType : model . ImageProxyTypeAtmosCamo ,
2020-11-10 02:36:59 -05:00
ProxyURL : "https://127.0.0.1" ,
2026-04-08 15:49:43 -04:00
ProxyOptions : testHMACKey ,
2020-11-10 02:36:59 -05:00
ImageURL : "http://mydomain.com/myimage" ,
ProxiedRemovedImageURL : "http://mydomain.com/myimage" ,
ProxiedImageURL : "http://mymattermost.com/api/v4/image?url=http%3A%2F%2Fmydomain.com%2Fmyimage" ,
2018-01-22 16:32:50 -05:00
} ,
2018-02-12 14:05:01 -05:00
"atmos/camo_SameSite" : {
2021-07-12 14:05:36 -04:00
ProxyType : model . ImageProxyTypeAtmosCamo ,
2020-11-10 02:36:59 -05:00
ProxyURL : "https://127.0.0.1" ,
2026-04-08 15:49:43 -04:00
ProxyOptions : testHMACKey ,
2020-11-10 02:36:59 -05:00
ImageURL : "http://mymattermost.com/myimage" ,
ProxiedRemovedImageURL : "http://mymattermost.com/myimage" ,
ProxiedImageURL : "http://mymattermost.com/myimage" ,
2018-02-09 16:41:06 -05:00
} ,
2018-02-12 14:05:01 -05:00
"atmos/camo_PathOnly" : {
2021-07-12 14:05:36 -04:00
ProxyType : model . ImageProxyTypeAtmosCamo ,
2020-11-10 02:36:59 -05:00
ProxyURL : "https://127.0.0.1" ,
2026-04-08 15:49:43 -04:00
ProxyOptions : testHMACKey ,
2020-11-10 02:36:59 -05:00
ImageURL : "/myimage" ,
ProxiedRemovedImageURL : "http://mymattermost.com/myimage" ,
ProxiedImageURL : "http://mymattermost.com/myimage" ,
2018-02-09 21:08:39 -05:00
} ,
2018-02-12 14:05:01 -05:00
"atmos/camo_EmptyImageURL" : {
2021-07-12 14:05:36 -04:00
ProxyType : model . ImageProxyTypeAtmosCamo ,
2020-11-10 02:36:59 -05:00
ProxyURL : "https://127.0.0.1" ,
2026-04-08 15:49:43 -04:00
ProxyOptions : testHMACKey ,
2020-11-10 02:36:59 -05:00
ImageURL : "" ,
ProxiedRemovedImageURL : "" ,
ProxiedImageURL : "" ,
2018-02-01 21:31:49 -05:00
} ,
2019-01-24 15:11:32 -05:00
"local" : {
2021-07-12 14:05:36 -04:00
ProxyType : model . ImageProxyTypeLocal ,
2020-11-10 02:36:59 -05:00
ImageURL : "http://mydomain.com/myimage" ,
ProxiedRemovedImageURL : "http://mydomain.com/myimage" ,
ProxiedImageURL : "http://mymattermost.com/api/v4/image?url=http%3A%2F%2Fmydomain.com%2Fmyimage" ,
2019-01-24 15:11:32 -05:00
} ,
"local_SameSite" : {
2021-07-12 14:05:36 -04:00
ProxyType : model . ImageProxyTypeLocal ,
2020-11-10 02:36:59 -05:00
ImageURL : "http://mymattermost.com/myimage" ,
ProxiedRemovedImageURL : "http://mymattermost.com/myimage" ,
ProxiedImageURL : "http://mymattermost.com/myimage" ,
2019-01-24 15:11:32 -05:00
} ,
"local_PathOnly" : {
2021-07-12 14:05:36 -04:00
ProxyType : model . ImageProxyTypeLocal ,
2020-11-10 02:36:59 -05:00
ImageURL : "/myimage" ,
ProxiedRemovedImageURL : "http://mymattermost.com/myimage" ,
ProxiedImageURL : "http://mymattermost.com/myimage" ,
2019-01-24 15:11:32 -05:00
} ,
"local_EmptyImageURL" : {
2021-07-12 14:05:36 -04:00
ProxyType : model . ImageProxyTypeLocal ,
2020-11-10 02:36:59 -05:00
ImageURL : "" ,
ProxiedRemovedImageURL : "" ,
ProxiedImageURL : "" ,
2019-01-24 15:11:32 -05:00
} ,
2018-01-22 16:32:50 -05:00
} {
t . Run ( name , func ( t * testing . T ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2024-08-05 23:45:00 -04:00
cfg . ImageProxySettings . Enable = model . NewPointer ( true )
cfg . ImageProxySettings . ImageProxyType = model . NewPointer ( tc . ProxyType )
cfg . ImageProxySettings . RemoteImageProxyOptions = model . NewPointer ( tc . ProxyOptions )
cfg . ImageProxySettings . RemoteImageProxyURL = model . NewPointer ( tc . ProxyURL )
2018-01-22 16:32:50 -05:00
} )
post := & model . Post {
Id : model . NewId ( ) ,
Message : "" ,
}
list := model . NewPostList ( )
list . Posts [ post . Id ] = post
assert . Equal ( t , "" , th . App . PostWithProxyAddedToImageURLs ( post ) . Message )
assert . Equal ( t , "" , th . App . PostWithProxyRemovedFromImageURLs ( post ) . Message )
post . Message = ""
2020-11-10 02:36:59 -05:00
assert . Equal ( t , "" , th . App . PostWithProxyRemovedFromImageURLs ( post ) . Message )
2018-08-01 11:43:58 -04:00
if tc . ImageURL != "" {
post . Message = ""
assert . Equal ( t , "" , th . App . PostWithProxyAddedToImageURLs ( post ) . Message )
assert . Equal ( t , "" , th . App . PostWithProxyRemovedFromImageURLs ( post ) . Message )
post . Message = ""
2020-11-10 02:36:59 -05:00
assert . Equal ( t , "" , th . App . PostWithProxyRemovedFromImageURLs ( post ) . Message )
2018-08-01 11:43:58 -04:00
}
2018-01-22 16:32:50 -05:00
} )
}
}
2018-11-14 13:01:44 -05:00
func TestDeletePostWithFileAttachments ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2018-11-14 13:01:44 -05:00
// Create a post with a file attachment.
2021-02-05 05:22:27 -05:00
teamID := th . BasicTeam . Id
2021-02-25 14:22:27 -05:00
channelID := th . BasicChannel . Id
2021-02-05 05:22:27 -05:00
userID := th . BasicUser . Id
2018-11-14 13:01:44 -05:00
filename := "test"
data := [ ] byte ( "abcd" )
2024-04-02 23:34:33 -04:00
info1 , err := th . App . DoUploadFile ( th . Context , time . Date ( 2007 , 2 , 4 , 1 , 2 , 3 , 4 , time . Local ) , teamID , channelID , userID , filename , data , true )
2019-09-24 09:23:30 -04:00
require . Nil ( t , err )
defer func ( ) {
2025-04-29 02:50:47 -04:00
err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . PermanentDelete ( th . Context , info1 . Id )
require . NoError ( t , err )
appErr := th . App . RemoveFile ( info1 . Path )
require . Nil ( t , appErr )
2019-09-24 09:23:30 -04:00
} ( )
2018-11-14 13:01:44 -05:00
post := & model . Post {
Message : "asd" ,
2021-02-25 14:22:27 -05:00
ChannelId : channelID ,
2018-11-14 13:01:44 -05:00
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
2021-02-05 05:22:27 -05:00
UserId : userID ,
2018-11-14 13:01:44 -05:00
CreateAt : 0 ,
FileIds : [ ] string { info1 . Id } ,
}
2026-01-20 04:38:27 -05:00
post , _ , err = th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2018-11-14 13:01:44 -05:00
assert . Nil ( t , err )
// Delete the post.
2022-07-14 05:01:29 -04:00
_ , err = th . App . DeletePost ( th . Context , post . Id , userID )
2018-11-14 13:01:44 -05:00
assert . Nil ( t , err )
// Wait for the cleanup routine to finish.
time . Sleep ( time . Millisecond * 100 )
// Check that the file can no longer be reached.
2023-11-07 04:04:16 -05:00
_ , err = th . App . GetFileInfo ( th . Context , info1 . Id )
2018-11-14 13:01:44 -05:00
assert . NotNil ( t , err )
}
2018-12-13 09:47:30 -05:00
2025-09-30 11:41:14 -04:00
func TestDeletePostWithRestrictedDM ( t * testing . T ) {
mainHelper . Parallel ( t )
t . Run ( "cannot delete post in restricted DM" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-09-30 11:41:14 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . TeamSettings . RestrictDirectMessage = model . DirectMessageTeam
} )
// Create a DM channel between two users who don't share a team
2025-11-12 07:00:51 -05:00
dmChannel := th . CreateDmChannel ( t , th . BasicUser2 )
2025-09-30 11:41:14 -04:00
// Ensure the two users do not share a team
teams , err := th . App . GetTeamsForUser ( th . BasicUser . Id )
require . Nil ( t , err )
for _ , team := range teams {
teamErr := th . App . RemoveUserFromTeam ( th . Context , team . Id , th . BasicUser . Id , th . SystemAdminUser . Id )
require . Nil ( t , teamErr )
}
teams , err = th . App . GetTeamsForUser ( th . BasicUser2 . Id )
require . Nil ( t , err )
for _ , team := range teams {
teamErr := th . App . RemoveUserFromTeam ( th . Context , team . Id , th . BasicUser2 . Id , th . SystemAdminUser . Id )
require . Nil ( t , teamErr )
}
// Create separate teams for each user
2025-11-12 07:00:51 -05:00
team1 := th . CreateTeam ( t )
team2 := th . CreateTeam ( t )
th . LinkUserToTeam ( t , th . BasicUser , team1 )
th . LinkUserToTeam ( t , th . BasicUser2 , team2 )
2025-09-30 11:41:14 -04:00
// Create a post in the DM channel
post := & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : dmChannel . Id ,
Message : "test post" ,
}
2026-01-20 04:38:27 -05:00
post , _ , err = th . App . CreatePost ( th . Context , post , dmChannel , model . CreatePostFlags { } )
2025-09-30 11:41:14 -04:00
require . Nil ( t , err )
// Try to delete the post
_ , appErr := th . App . DeletePost ( th . Context , post . Id , th . BasicUser . Id )
require . NotNil ( t , appErr )
require . Equal ( t , "api.post.delete_post.can_not_delete_from_restricted_dm.error" , appErr . Id )
require . Equal ( t , http . StatusBadRequest , appErr . StatusCode )
// Reset config
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . TeamSettings . RestrictDirectMessage = model . DirectMessageAny
} )
} )
}
2019-03-11 04:26:31 -04:00
func TestDeletePostInArchivedChannel ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-03-11 04:26:31 -04:00
2025-11-12 07:00:51 -05:00
archivedChannel := th . CreateChannel ( t , th . BasicTeam )
post := th . CreatePost ( t , archivedChannel )
2025-04-29 02:50:47 -04:00
appErr := th . App . DeleteChannel ( th . Context , archivedChannel , "" )
require . Nil ( t , appErr )
2019-03-11 04:26:31 -04:00
2022-07-14 05:01:29 -04:00
_ , err := th . App . DeletePost ( th . Context , post . Id , "" )
2019-03-11 04:26:31 -04:00
require . NotNil ( t , err )
require . Equal ( t , "api.post.delete_post.can_not_delete_post_in_deleted.error" , err . Id )
}
2018-12-13 09:47:30 -05:00
func TestCreatePost ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2018-12-13 09:47:30 -05:00
t . Run ( "call PreparePostForClient before returning" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2018-12-13 09:47:30 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2019-05-06 09:22:37 -04:00
* cfg . ServiceSettings . SiteURL = "http://mymattermost.com"
2019-01-24 15:11:32 -05:00
* cfg . ImageProxySettings . Enable = true
* cfg . ImageProxySettings . ImageProxyType = "atmos/camo"
* cfg . ImageProxySettings . RemoteImageProxyURL = "https://127.0.0.1"
2026-04-08 15:49:43 -04:00
* cfg . ImageProxySettings . RemoteImageProxyOptions = model . NewTestPassword ( )
2018-12-13 09:47:30 -05:00
} )
2022-08-24 03:10:56 -04:00
th . App . ch . imageProxy = imageproxy . MakeImageProxy ( th . Server . platform , th . Server . HTTPService ( ) , th . Server . Log ( ) )
2020-11-10 02:36:59 -05:00
2018-12-13 09:47:30 -05:00
imageURL := "http://mydomain.com/myimage"
2019-05-06 09:22:37 -04:00
proxiedImageURL := "http://mymattermost.com/api/v4/image?url=http%3A%2F%2Fmydomain.com%2Fmyimage"
2018-12-13 09:47:30 -05:00
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
rpost , _ , err := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2018-12-13 09:47:30 -05:00
require . Nil ( t , err )
assert . Equal ( t , "" , rpost . Message )
} )
2020-03-03 05:22:49 -05:00
t . Run ( "Sets prop MENTION_HIGHLIGHT_DISABLED when it should" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel )
2020-03-03 05:22:49 -05:00
t . Run ( "Does not set prop when user has USE_CHANNEL_MENTIONS" , func ( t * testing . T ) {
postWithNoMention := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "This post does not have mentions" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
rpost , _ , err := th . App . CreatePost ( th . Context , postWithNoMention , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2020-03-03 05:22:49 -05:00
require . Nil ( t , err )
2020-03-13 16:12:20 -04:00
assert . Equal ( t , rpost . GetProps ( ) , model . StringInterface { } )
2020-03-03 05:22:49 -05:00
postWithMention := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "This post has @here mention @all" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
rpost , _ , err = th . App . CreatePost ( th . Context , postWithMention , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2020-03-03 05:22:49 -05:00
require . Nil ( t , err )
2020-03-13 16:12:20 -04:00
assert . Equal ( t , rpost . GetProps ( ) , model . StringInterface { } )
2020-03-03 05:22:49 -05:00
} )
t . Run ( "Sets prop when post has mentions and user does not have USE_CHANNEL_MENTIONS" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionUseChannelMentions . Id , model . ChannelUserRoleId )
th . RemovePermissionFromRole ( t , model . PermissionUseChannelMentions . Id , model . ChannelAdminRoleId )
2020-03-03 05:22:49 -05:00
postWithNoMention := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "This post does not have mentions" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
rpost , _ , err := th . App . CreatePost ( th . Context , postWithNoMention , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2020-03-03 05:22:49 -05:00
require . Nil ( t , err )
2020-03-13 16:12:20 -04:00
assert . Equal ( t , rpost . GetProps ( ) , model . StringInterface { } )
2020-03-03 05:22:49 -05:00
postWithMention := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "This post has @here mention @all" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
rpost , _ , err = th . App . CreatePost ( th . Context , postWithMention , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2020-03-03 05:22:49 -05:00
require . Nil ( t , err )
2021-07-12 14:05:36 -04:00
assert . Equal ( t , rpost . GetProp ( model . PostPropsMentionHighlightDisabled ) , true )
2020-03-03 05:22:49 -05:00
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionUseChannelMentions . Id , model . ChannelUserRoleId )
th . AddPermissionToRole ( t , model . PermissionUseChannelMentions . Id , model . ChannelAdminRoleId )
2020-03-03 05:22:49 -05:00
} )
} )
2021-08-09 11:33:21 -04:00
t . Run ( "Sets PostPropsPreviewedPost when a permalink is the first link" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel )
2021-08-09 11:33:21 -04:00
referencedPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "hello world" ,
UserId : th . BasicUser . Id ,
}
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . SiteURL = "http://mymattermost.com"
} )
th . Context . Session ( ) . UserId = th . BasicUser . Id
2026-01-20 04:38:27 -05:00
referencedPost , _ , err := th . App . CreatePost ( th . Context , referencedPost , th . BasicChannel , model . CreatePostFlags { } )
2021-08-09 11:33:21 -04:00
require . Nil ( t , err )
permalink := fmt . Sprintf ( "%s/%s/pl/%s" , * th . App . Config ( ) . ServiceSettings . SiteURL , th . BasicTeam . Name , referencedPost . Id )
2025-11-12 07:00:51 -05:00
channelForPreview := th . CreateChannel ( t , th . BasicTeam )
2021-08-09 11:33:21 -04:00
previewPost := & model . Post {
ChannelId : channelForPreview . Id ,
Message : permalink ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
previewPost , _ , err = th . App . CreatePost ( th . Context , previewPost , channelForPreview , model . CreatePostFlags { } )
2021-08-09 11:33:21 -04:00
require . Nil ( t , err )
assert . Equal ( t , previewPost . GetProps ( ) , model . StringInterface { "previewed_post" : referencedPost . Id } )
} )
2021-09-16 13:54:38 -04:00
t . Run ( "creates a single record for a permalink preview post" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-09-16 13:54:38 -04:00
2025-11-12 07:00:51 -05:00
channelForPreview := th . CreateChannel ( t , th . BasicTeam )
2021-09-16 13:54:38 -04:00
referencedPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "hello world" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
referencedPost , _ , err := th . App . CreatePost ( th . Context , referencedPost , th . BasicChannel , model . CreatePostFlags { } )
2021-09-16 13:54:38 -04:00
require . Nil ( t , err )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . SiteURL = "http://foobar.com"
* cfg . ServiceSettings . EnablePermalinkPreviews = true
} )
permalink := fmt . Sprintf ( "%s/%s/pl/%s" , * th . App . Config ( ) . ServiceSettings . SiteURL , th . BasicTeam . Name , referencedPost . Id )
previewPost := & model . Post {
ChannelId : channelForPreview . Id ,
Message : permalink ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
previewPost , _ , err = th . App . CreatePost ( th . Context , previewPost , channelForPreview , model . CreatePostFlags { } )
2021-09-16 13:54:38 -04:00
require . Nil ( t , err )
sqlStore := th . GetSqlStore ( )
sql := fmt . Sprintf ( "select count(*) from Posts where Id = '%[1]s' or OriginalId = '%[1]s';" , previewPost . Id )
2022-02-16 12:00:27 -05:00
var val int64
2024-12-10 08:57:19 -05:00
err2 := sqlStore . GetMaster ( ) . Get ( & val , sql )
2021-09-16 13:54:38 -04:00
require . NoError ( t , err2 )
require . EqualValues ( t , int64 ( 1 ) , val )
} )
2021-11-22 03:54:19 -05:00
2022-04-07 17:32:18 -04:00
t . Run ( "sanitizes post metadata appropriately" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2022-04-07 17:32:18 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . SiteURL = "http://mymattermost.com"
} )
2025-11-12 07:00:51 -05:00
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel )
2022-04-07 17:32:18 -04:00
2025-11-12 07:00:51 -05:00
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
2022-07-14 05:01:29 -04:00
directChannel , err := th . App . createDirectChannel ( th . Context , user1 . Id , user2 . Id )
2022-04-07 17:32:18 -04:00
require . Nil ( t , err )
th . Context . Session ( ) . UserId = th . BasicUser . Id
testCases := [ ] struct {
Description string
Channel * model . Channel
Author string
2023-11-30 05:43:51 -05:00
Length int
2022-04-07 17:32:18 -04:00
} {
{
Description : "removes metadata from post for members who cannot read channel" ,
Channel : directChannel ,
Author : user1 . Id ,
2023-11-30 05:43:51 -05:00
Length : 0 ,
2022-04-07 17:32:18 -04:00
} ,
{
Description : "does not remove metadata from post for members who can read channel" ,
Channel : th . BasicChannel ,
Author : th . BasicUser . Id ,
2023-11-30 05:43:51 -05:00
Length : 1 ,
2022-04-07 17:32:18 -04:00
} ,
}
for _ , testCase := range testCases {
t . Run ( testCase . Description , func ( t * testing . T ) {
2023-11-30 05:43:51 -05:00
referencedPost := & model . Post {
2022-04-07 17:32:18 -04:00
ChannelId : testCase . Channel . Id ,
2023-11-30 05:43:51 -05:00
Message : "hello world" ,
2022-04-07 17:32:18 -04:00
UserId : testCase . Author ,
}
2026-01-20 04:38:27 -05:00
referencedPost , _ , err = th . App . CreatePost ( th . Context , referencedPost , testCase . Channel , model . CreatePostFlags { } )
2023-11-30 05:43:51 -05:00
require . Nil ( t , err )
permalink := fmt . Sprintf ( "%s/%s/pl/%s" , * th . App . Config ( ) . ServiceSettings . SiteURL , th . BasicTeam . Name , referencedPost . Id )
previewPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : permalink ,
UserId : th . BasicUser . Id ,
}
2022-04-07 17:32:18 -04:00
2026-01-20 04:38:27 -05:00
previewPost , _ , err = th . App . CreatePost ( th . Context , previewPost , th . BasicChannel , model . CreatePostFlags { } )
2022-04-07 17:32:18 -04:00
require . Nil ( t , err )
2023-11-30 05:43:51 -05:00
require . Len ( t , previewPost . Metadata . Embeds , testCase . Length )
2022-04-07 17:32:18 -04:00
} )
}
} )
2024-10-18 13:16:29 -04:00
t . Run ( "Should not allow to create posts on shared DMs" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := setupSharedChannels ( t ) . InitBasic ( t )
2024-10-18 13:16:29 -04:00
2025-11-12 07:00:51 -05:00
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
2025-04-29 02:50:47 -04:00
dm , appErr := th . App . createDirectChannel ( th . Context , user1 . Id , user2 . Id )
require . Nil ( t , appErr )
2024-10-18 13:16:29 -04:00
require . NotNil ( t , dm )
// we can't create direct channels with remote users, so we
// have to force the channel to be shared through the store to
// simulate preexisting shared DMs
sc := & model . SharedChannel {
ChannelId : dm . Id ,
Type : dm . Type ,
Home : true ,
ShareName : "shareddm" ,
CreatorId : user1 . Id ,
RemoteId : model . NewId ( ) ,
}
_ , scErr := th . Server . Store ( ) . SharedChannel ( ) . Save ( sc )
require . NoError ( t , scErr )
// and we update the channel to mark it as shared
dm . Shared = model . NewPointer ( true )
2025-04-29 02:50:47 -04:00
_ , err := th . Server . Store ( ) . Channel ( ) . Update ( th . Context , dm )
require . NoError ( t , err )
2024-10-18 13:16:29 -04:00
newPost := & model . Post {
ChannelId : dm . Id ,
Message : "hello world" ,
UserId : user1 . Id ,
}
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , newPost , dm , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . NotNil ( t , appErr )
2024-10-18 13:16:29 -04:00
require . Nil ( t , createdPost )
} )
t . Run ( "Should not allow to create posts on shared GMs" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := setupSharedChannels ( t ) . InitBasic ( t )
2024-10-18 13:16:29 -04:00
2025-11-12 07:00:51 -05:00
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
user3 := th . CreateUser ( t )
2025-06-13 06:43:30 -04:00
gm , appErr := th . App . createGroupChannel ( th . Context , [ ] string { user1 . Id , user2 . Id , user3 . Id } , user1 . Id )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2024-10-18 13:16:29 -04:00
require . NotNil ( t , gm )
// we can't create group channels with remote users, so we
// have to force the channel to be shared through the store to
// simulate preexisting shared GMs
sc := & model . SharedChannel {
ChannelId : gm . Id ,
Type : gm . Type ,
Home : true ,
ShareName : "sharedgm" ,
CreatorId : user1 . Id ,
RemoteId : model . NewId ( ) ,
}
2025-04-29 02:50:47 -04:00
_ , err := th . Server . Store ( ) . SharedChannel ( ) . Save ( sc )
require . NoError ( t , err )
2024-10-18 13:16:29 -04:00
// and we update the channel to mark it as shared
gm . Shared = model . NewPointer ( true )
2025-04-29 02:50:47 -04:00
_ , err = th . Server . Store ( ) . Channel ( ) . Update ( th . Context , gm )
require . NoError ( t , err )
2024-10-18 13:16:29 -04:00
newPost := & model . Post {
ChannelId : gm . Id ,
Message : "hello world" ,
UserId : user1 . Id ,
}
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , newPost , gm , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . NotNil ( t , appErr )
2024-10-18 13:16:29 -04:00
require . Nil ( t , createdPost )
} )
2021-11-22 03:54:19 -05:00
t . Run ( "MM-40016 should not panic with `concurrent map read and map write`" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-11-22 03:54:19 -05:00
2025-11-12 07:00:51 -05:00
channelForPreview := th . CreateChannel ( t , th . BasicTeam )
2021-11-22 03:54:19 -05:00
2025-07-18 06:54:51 -04:00
for range 20 {
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
th . LinkUserToTeam ( t , user , th . BasicTeam )
th . AddUserToChannel ( t , user , channelForPreview )
2021-11-22 03:54:19 -05:00
}
referencedPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "hello world" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
referencedPost , _ , err := th . App . CreatePost ( th . Context , referencedPost , th . BasicChannel , model . CreatePostFlags { } )
2021-11-22 03:54:19 -05:00
require . Nil ( t , err )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . SiteURL = "http://example.com"
* cfg . ServiceSettings . EnablePermalinkPreviews = true
} )
permalink := fmt . Sprintf ( "%s/%s/pl/%s" , * th . App . Config ( ) . ServiceSettings . SiteURL , th . BasicTeam . Name , referencedPost . Id )
previewPost := & model . Post {
ChannelId : channelForPreview . Id ,
Message : permalink ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
previewPost , _ , err = th . App . CreatePost ( th . Context , previewPost , channelForPreview , model . CreatePostFlags { } )
2021-11-22 03:54:19 -05:00
require . Nil ( t , err )
n := 1000
var wg sync . WaitGroup
wg . Add ( n )
2025-07-18 06:54:51 -04:00
for range n {
2021-11-22 03:54:19 -05:00
go func ( ) {
defer wg . Done ( )
post := previewPost . Clone ( )
2026-01-20 04:38:27 -05:00
_ , _ , appErr := th . App . UpdatePost ( th . Context , post , nil )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-11-22 03:54:19 -05:00
} ( )
}
wg . Wait ( )
} )
2024-11-08 07:57:06 -05:00
t . Run ( "should sanitize the force notifications prop if the flag is not set" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel )
2024-11-08 07:57:06 -05:00
postToCreate := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "hello world" ,
UserId : th . BasicUser . Id ,
}
postToCreate . AddProp ( model . PostPropsForceNotification , model . NewId ( ) )
2026-01-20 04:38:27 -05:00
createdPost , _ , err := th . App . CreatePost ( th . Context , postToCreate , th . BasicChannel , model . CreatePostFlags { } )
2024-11-08 07:57:06 -05:00
require . Nil ( t , err )
require . Empty ( t , createdPost . GetProp ( model . PostPropsForceNotification ) )
} )
t . Run ( "should add the force notifications prop if the flag is set" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel )
2024-11-08 07:57:06 -05:00
postToCreate := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "hello world" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
createdPost , _ , err := th . App . CreatePost ( th . Context , postToCreate , th . BasicChannel , model . CreatePostFlags { ForceNotification : true } )
2024-11-08 07:57:06 -05:00
require . Nil ( t , err )
require . NotEmpty ( t , createdPost . GetProp ( model . PostPropsForceNotification ) )
} )
2025-12-11 01:59:50 -05:00
Merge the Integrated Boards MVP feature branch (#35796)
* Add CreatedBy and UpdatedBy to the properties fields and values (#34485)
* Add CreatedBy and UpdatedBy to the properties fields and values
* Fix types
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Adds ObjectType to the property fields table (#34908)
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Update ObjectType migration setting an empty value and marking the column as not null (#34915)
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Adds uniqueness mechanisms to the property fields (#35058)
* Adds uniqueness mechanisms to the property fields
After adding ObjectType, this commit ensures that both the PSAv1 and
PSAv2 schemas are supported, and enforces property uniqueness through
both database indexes and a logical check when creating new property
fields.
* Adds uniqueness check to property updates
Updates are covered on this commit and we refactor as well the SQL
code to use the squirrel builder and work better with the conditional
addition of the `existingID` piece of the query.
* Add translations to error messages
* Fixing retrylayer mocks
* Remove retrylayer duplication
* Address review comments
* Fix comment to avoid linter issues
* Address PR comments
* Update server/channels/db/migrations/postgres/000157_add_object_type_to_property_fields.down.sql
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
* Update server/channels/db/migrations/postgres/000157_add_object_type_to_property_fields.up.sql
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
* Update server/channels/db/migrations/postgres/000157_add_object_type_to_property_fields.up.sql
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
* Update field validation to check only for valid target types
* Update migrations to avoid concurrent index creation within a transaction
* Update migrations to make all index ops concurrent
* Update tests to use valid PSAv2 property fields
* Adds a helper for valid PSAv2 TargetTypes
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
* Fix property tests (#35388)
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Adds Integrated Boards feature flag (#35378)
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Adds Integrated Boards MVP API changes (#34822)
This PR includes the necessary changes for channels and posts
endpoints and adds a set of generic endpoints to retrieve and manage
property fields and values following the new Property System approach.
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
Co-authored-by: Mattermost Build <build@mattermost.com>
* Property System Architecture permissions for v2 (#35113)
* Adds uniqueness mechanisms to the property fields
After adding ObjectType, this commit ensures that both the PSAv1 and
PSAv2 schemas are supported, and enforces property uniqueness through
both database indexes and a logical check when creating new property
fields.
* Adds uniqueness check to property updates
Updates are covered on this commit and we refactor as well the SQL
code to use the squirrel builder and work better with the conditional
addition of the `existingID` piece of the query.
* Add translations to error messages
* Add the permissions to the migrations, model and update the store calls
* Adds the property field and property group app layer
* Adds authorization helpers for property fields and values
* Make sure that users cannot lock themselves out of property fields
* Migrate permissions from a JSON column to three normalized columns
* Remove the audit comment
* Use target level constants in authorization
* Log authorization membership failures
* Rename admin to sysadmin
* Fix i18n sorting
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Add Views store and app layer (#35361)
* Add Views store and app layer for Integrated Boards
Implements the View entity (model, SQL store, service, app) as described
in the Integrated Boards tech spec. Views are channel-scoped board
configurations with typed props (board, kanban subviews) and soft-delete.
- public/model: View, ViewBoardProps, Subview, ViewPatch types with
PreSave/PreUpdate/IsValid/Patch/Clone/Auditable
- Migration 158: Views table with jsonb Props column and indexes
- SqlViewStore: CRUD with nil-safe Props marshaling (AppendBinaryFlag)
- ViewService: CreateView seeds default kanban subview and links the
boards property field; caches boardPropertyFieldID at startup
- App layer: CreateView/GetView/GetViewsForChannel/UpdateView/DeleteView
with channel-membership permission checks and WebSocket events
(view_created, view_updated, view_deleted)
- doSetupBoardsPropertyField: registers the Boards property group and
board field in NewServer() before ViewService construction
- GetFieldByName now returns store.ErrNotFound instead of raw sql.ErrNoRows
* Move permission checks out of App layer for views
- Remove HasPermissionToChannel calls from all App view methods
- Drop userID params from GetView, GetViewsForChannel, UpdateView, DeleteView
- Fix doSetupBoardsPropertyField to include required TargetType for PSAv2 field
* Make View service generic and enforce board validation in model
- Remove board-specific auto-setup from service and server startup
- Enforce that board views require Props, at least one subview, and at least one linked property in IsValid()
- Move default subview seeding out of app layer; callers must provide valid props
- Call PreSave on subviews during PreUpdate to assign IDs to new subviews
- Update all tests to reflect the new validation requirements
* Restore migrations files to match base branch
* Distinguish ErrNotFound from other errors in view store Get
* Use CONCURRENTLY and nontransactional for index operations in views migration
* Split views index creation into separate nontransactional migrations
* Update migrations.list
* Update i18n translations for views
* Fix makeView helper to include required Props for board view validation
* Rename ctx parameter from c to rctx in OAuthProvider mock
* Remove views service layer, call store directly from app
* Return 500 for unexpected DB errors in GetView, 404 only for not-found
* Harden View model: deep-copy Props, validate linked property IDs
- Add ViewBoardProps.Clone() to deep-copy LinkedProperties and Subviews
- Use it in View.Clone() and View.Patch() to prevent shared-slice aliasing
- Iterate over LinkedProperties in View.IsValid() and reject invalid IDs
with a dedicated i18n key
- Register ViewStore in storetest AssertExpectations so mock expectations
are enforced
- Add tests covering all new behaviours
* Restore autotranslation worker_stopped i18n translation
* Fix view store test IDs and improve error handling in app layer
- Use model.NewId() for linked property IDs in testUpdateView to fix
validation failure (IsValid rejects non-UUID strings)
- Fix import grouping in app/view.go (stdlib imports in one block)
- Return 404 instead of 500 when Update/Delete store calls return
ErrNotFound (e.g. concurrent deletion TOCTOU race)
* Add View store mock to retrylayer test genStore helper
The View store was added to the store interface but the genStore()
helper in retrylayer_test.go was not updated, causing TestRetry to panic.
Also removes the duplicate Recap mock registration.
* Refactor view deletion and websocket event handling; update SQL store methods to use query builder
* revert property field store
* Remove useless migrations
* Add cursor-based pagination to View store GetForChannel
- Add ViewQueryCursor and ViewQueryOpts types with validation
- Return (views, cursor, error) for caller-driven pagination
- PerPage clamping: <=0 defaults to 20, >200 clamps to 200
- Support IncludeDeleted filter
- Add comprehensive store tests for pagination, cursor edge cases,
PerPage clamping, and invalid input rejection
- Add app layer test for empty channelID → 400
- Update interface, retrylayer, timerlayer, and mock signatures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Refactor test loops in ViewStore tests for improved readability
* change pagination to limit/offset
* Add upper-bound limits on View Subviews and LinkedProperties
Defense-in-depth validation: cap Subviews at 50 and LinkedProperties
at 500 to prevent abuse below the 300KB payload limit.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* MM-67388, MM-66528, MM-67750: Add View REST API endpoints, websocket events, and sort order (#35442)
* Add Views store and app layer for Integrated Boards
Implements the View entity (model, SQL store, service, app) as described
in the Integrated Boards tech spec. Views are channel-scoped board
configurations with typed props (board, kanban subviews) and soft-delete.
- public/model: View, ViewBoardProps, Subview, ViewPatch types with
PreSave/PreUpdate/IsValid/Patch/Clone/Auditable
- Migration 158: Views table with jsonb Props column and indexes
- SqlViewStore: CRUD with nil-safe Props marshaling (AppendBinaryFlag)
- ViewService: CreateView seeds default kanban subview and links the
boards property field; caches boardPropertyFieldID at startup
- App layer: CreateView/GetView/GetViewsForChannel/UpdateView/DeleteView
with channel-membership permission checks and WebSocket events
(view_created, view_updated, view_deleted)
- doSetupBoardsPropertyField: registers the Boards property group and
board field in NewServer() before ViewService construction
- GetFieldByName now returns store.ErrNotFound instead of raw sql.ErrNoRows
* Move permission checks out of App layer for views
- Remove HasPermissionToChannel calls from all App view methods
- Drop userID params from GetView, GetViewsForChannel, UpdateView, DeleteView
- Fix doSetupBoardsPropertyField to include required TargetType for PSAv2 field
* Make View service generic and enforce board validation in model
- Remove board-specific auto-setup from service and server startup
- Enforce that board views require Props, at least one subview, and at least one linked property in IsValid()
- Move default subview seeding out of app layer; callers must provide valid props
- Call PreSave on subviews during PreUpdate to assign IDs to new subviews
- Update all tests to reflect the new validation requirements
* Restore migrations files to match base branch
* Distinguish ErrNotFound from other errors in view store Get
* Use CONCURRENTLY and nontransactional for index operations in views migration
* Split views index creation into separate nontransactional migrations
* Update migrations.list
* Update i18n translations for views
* Fix makeView helper to include required Props for board view validation
* Rename ctx parameter from c to rctx in OAuthProvider mock
* Remove views service layer, call store directly from app
* Return 500 for unexpected DB errors in GetView, 404 only for not-found
* Harden View model: deep-copy Props, validate linked property IDs
- Add ViewBoardProps.Clone() to deep-copy LinkedProperties and Subviews
- Use it in View.Clone() and View.Patch() to prevent shared-slice aliasing
- Iterate over LinkedProperties in View.IsValid() and reject invalid IDs
with a dedicated i18n key
- Register ViewStore in storetest AssertExpectations so mock expectations
are enforced
- Add tests covering all new behaviours
* Restore autotranslation worker_stopped i18n translation
* Fix view store test IDs and improve error handling in app layer
- Use model.NewId() for linked property IDs in testUpdateView to fix
validation failure (IsValid rejects non-UUID strings)
- Fix import grouping in app/view.go (stdlib imports in one block)
- Return 404 instead of 500 when Update/Delete store calls return
ErrNotFound (e.g. concurrent deletion TOCTOU race)
* Add View store mock to retrylayer test genStore helper
The View store was added to the store interface but the genStore()
helper in retrylayer_test.go was not updated, causing TestRetry to panic.
Also removes the duplicate Recap mock registration.
* Refactor view deletion and websocket event handling; update SQL store methods to use query builder
* revert property field store
* Add View API endpoints with OpenAPI spec, client methods, and i18n
Implement REST API for channel views (board-type) behind the
IntegratedBoards feature flag. Adds CRUD endpoints under
/api/v4/channels/{channel_id}/views with permission checks
matching the channel bookmark pattern.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Remove useless migrations
* Add cursor-based pagination to View store GetForChannel
- Add ViewQueryCursor and ViewQueryOpts types with validation
- Return (views, cursor, error) for caller-driven pagination
- PerPage clamping: <=0 defaults to 20, >200 clamps to 200
- Support IncludeDeleted filter
- Add comprehensive store tests for pagination, cursor edge cases,
PerPage clamping, and invalid input rejection
- Add app layer test for empty channelID → 400
- Update interface, retrylayer, timerlayer, and mock signatures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add cursor-based pagination to View API for channel views
* Enhance cursor handling in getViewsForChannel and update tests for pagination
* Refactor test loops in ViewStore tests for improved readability
* Refactor loop in TestGetViewsForChannel for improved readability
* change pagination to limit/offset
* switch to limit/offset pagination
* Add upper-bound limits on View Subviews and LinkedProperties
Defense-in-depth validation: cap Subviews at 50 and LinkedProperties
at 500 to prevent abuse below the 300KB payload limit.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add view sort order API endpoint
Add POST /api/v4/channels/{channel_id}/views/{view_id}/sort_order
endpoint following the channel bookmarks reorder pattern. Includes
store, app, and API layers with full test coverage at each layer.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add connectionId to view WebSocket events and sort_order API spec
Thread connectionId from request header through all view handlers
(create, update, delete, sort_order) to WebSocket events, matching
the channel bookmarks pattern. Add sort_order endpoint to OpenAPI
spec. Update minimum server version to 11.6.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Remove duplicate View/ViewPatch definitions from definitions.yaml
The merge from integrated-boards-mvp introduced duplicate View and
ViewPatch schema definitions that were already defined earlier in
the file with more detail (including ViewBoardProps ref and enums).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Update minimum server version to 11.6 in views API spec
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add missing translations for view sort order error messages
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Merge integrated-boards-mvp into ibmvp_api-views; remove spec files
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Fix flaky TestViewStore timestamp test on CI
Add sleep before UpdateSortOrder to ensure timestamps differ,
preventing same-millisecond comparisons on fast CI machines.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* remove duplicate views.yaml imclude
* Use c.boolString() for include_deleted query param in GetViewsForChannel
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Fix views.yaml sort order schema: use integer type and require body
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Refactor view sort order tests to use named IDs instead of array indices
Extract idA/idB/idC from views slice and add BEFORE/AFTER comments
to make stateful subtest ordering easier to follow.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Return 404 instead of 403 for view operations on deleted channels
Deleted channels should appear non-existent to callers rather than
revealing their existence via a 403. Detailed error text explains
the context for debugging.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* add missing channel deleteat checks
* Use c.Params.Page instead of manual page query param parsing in getViewsForChannel
c.Params already validates and defaults page/per_page, so the manual
parsing was redundant.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add support for total count in views retrieval
* Add tests for handling deleted views in GetViewsForChannel and GetView
* Short-circuit negative newIndex in UpdateSortOrder before opening transaction
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add per-channel limit on views to bound UpdateSortOrder cost
Without a cap, unbounded view creation makes sort-order updates
increasingly expensive (CASE WHEN per view, row locks). Adds
MaxViewsPerChannel=50 constant and enforces it in the app layer
before saving. Includes API and app layer tests.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Remove include_deleted support from views API
Soft-deleted views are structural metadata with low risk, but no other
similar endpoint (e.g. channel bookmarks) exposes deleted records without
an admin gate. Rather than adding an admin-only permission check for
consistency, remove the feature entirely since there is no current use case.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Update view permissions to require `create_post` instead of channel management permissions
* Remove obsolete view management error messages for direct and group messages
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* feat(migrations): add user tracking and object type to property fields
- Introduced user tracking columns (CreatedBy, UpdatedBy) to PropertyFields and PropertyValues.
- Added ObjectType column to PropertyFields with associated unique indexes for legacy and typed properties.
- Created new migration scripts for adding and dropping these features, including necessary indexes for data integrity.
- Established views for managing property fields with new attributes.
This update enhances the schema to support better tracking and categorization of property fields.
* Add Property System Architecture v2 API endpoints (#35583)
* Adds uniqueness mechanisms to the property fields
After adding ObjectType, this commit ensures that both the PSAv1 and
PSAv2 schemas are supported, and enforces property uniqueness through
both database indexes and a logical check when creating new property
fields.
* Adds uniqueness check to property updates
Updates are covered on this commit and we refactor as well the SQL
code to use the squirrel builder and work better with the conditional
addition of the `existingID` piece of the query.
* Add translations to error messages
* Add the permissions to the migrations, model and update the store calls
* Adds the property field and property group app layer
* Adds authorization helpers for property fields and values
* Make sure that users cannot lock themselves out of property fields
* Migrate permissions from a JSON column to three normalized columns
* Remove the audit comment
* Use target level constants in authorization
* Log authorization membership failures
* Rename admin to sysadmin
* Adds the Property System Architecture v2 API endpoints
* Adds permission checks to the create field endpoint
* Add target access checks to value endpoints
* Add default branches for object_type and target_type and extra guards for cursor client4 methods
* Fix vet API mismatch
* Fix error checks
* Fix linter
* Add merge semantics for property patch logic and API endpoint
* Fix i18n
* Fix duplicated patch elements and early return on bad cursor
* Update docs to use enums
* Fix i18n sorting
* Update app layer to return model.AppError
* Adds a limit to the number of property values that can be patched in the same request
* Require target_type filter when searching property fields
* Add objectType validation as part of field.IsValid()
* Fix linter
* Fix test with bad objecttpye
* Fix test grouping
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* MM-67968: Flatten view model — remove icon, subviews, typed board props (#35726)
* feat(views): flatten view model by removing icon, subview, and board props
Simplifies the View data model as part of MM-67968: removes Icon, Subview,
and ViewBoardProps types; renames ViewTypeBoard to ViewTypeKanban; replaces
typed Props with StringInterface (map[string]any); adds migration 000167
to drop the Icon column from the Views table.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
* feat(api): update views OpenAPI spec to reflect flattened model
Removes ViewBoardProps, Subview, and icon from the View and ViewPatch
schemas. Changes type enum from board to kanban. Replaces typed props
with a free-form StringInterface object. Aligns with MM-67968.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
* refactor(views): simplify store by dropping dbView and marshalViewProps
StringInterface already implements driver.Valuer and sql.Scanner, so the
manual JSON marshal/unmarshal and the dbView intermediate struct were
redundant. model.View now scans directly from the database. Also removes
the dead ViewMaxLinkedProperties constant and wraps the Commit() error in
UpdateSortOrder.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
* fix(api): allow arbitrary JSON in view props OpenAPI schema
The props field was restricted to string values via
additionalProperties: { type: string }, conflicting with the Go model's
StringInterface (map[string]any). Changed to additionalProperties: true
in View, ViewPatch, and inline POST schemas.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
* Adds basic implementation of the generic redux store for PSAv2 (#35512)
* Adds basic implementation of the generic redux store for PSAv2
* Add created_by and updated_by to the test fixtures
* Make target_id, target_type and object_type mandatory
* Wrap getPropertyFieldsByIds and getPropertyValuesForTargetByFieldIds with createSelector
* Address PR comments
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Adds websocket messages for the PSAv2 API events (#35696)
* Adds uniqueness mechanisms to the property fields
After adding ObjectType, this commit ensures that both the PSAv1 and
PSAv2 schemas are supported, and enforces property uniqueness through
both database indexes and a logical check when creating new property
fields.
* Adds uniqueness check to property updates
Updates are covered on this commit and we refactor as well the SQL
code to use the squirrel builder and work better with the conditional
addition of the `existingID` piece of the query.
* Add translations to error messages
* Add the permissions to the migrations, model and update the store calls
* Adds the property field and property group app layer
* Adds authorization helpers for property fields and values
* Make sure that users cannot lock themselves out of property fields
* Migrate permissions from a JSON column to three normalized columns
* Remove the audit comment
* Use target level constants in authorization
* Log authorization membership failures
* Rename admin to sysadmin
* Adds the Property System Architecture v2 API endpoints
* Adds permission checks to the create field endpoint
* Add target access checks to value endpoints
* Add default branches for object_type and target_type and extra guards for cursor client4 methods
* Fix vet API mismatch
* Fix error checks
* Fix linter
* Add merge semantics for property patch logic and API endpoint
* Fix i18n
* Fix duplicated patch elements and early return on bad cursor
* Update docs to use enums
* Fix i18n sorting
* Update app layer to return model.AppError
* Adds a limit to the number of property values that can be patched in the same request
* Adds websocket messages for the PSAv2 API events
* Add IsPSAv2 helper to the property field for clarity
* Add guard against nil returns on field deletion
* Add docs to the websocket endpoints
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* migrations: consolidate views migrations and reorder after master
- Merged 000165 (create Views) with 000167 (drop Icon) since Icon was never needed
- Renumbered branch migrations 159-166 → 160-167 so master's 000159 (deduplicate_policy_names) runs first
- Regenerated migrations.list
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Add API endpoint to retrieve posts for a specific view (#35604)
Automatic Merge
* Apply fixes after merge
* Return a more specific error from getting multiple fields
* Prevent getting broadcast params on field deletion if not needed
* Remove duplicated migration code
* Update property conflict code to always use master
* Adds nil guard when iterating on property fields
* Check that permission level is valid before getting rejected by the database
* Validate correctness on TargetID for PSAv2 fields
* Avoid PSAv1 using permissions or protected
* Fix test data after validation change
* Fix flaky search test
* Adds more posts for filter use cases to properly test exclusions
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
Co-authored-by: Julien Tant <julien@craftyx.fr>
Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: Julien Tant <785518+JulienTant@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-27 05:36:35 -04:00
t . Run ( "creates post with type card" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "card post" ,
Type : model . PostTypeCard ,
}
rpost , _ , appErr := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { } )
require . Nil ( t , appErr )
require . NotNil ( t , rpost )
assert . Equal ( t , model . PostTypeCard , rpost . Type )
assert . Equal ( t , "card post" , rpost . Message )
} )
2025-12-11 01:59:50 -05:00
t . Run ( "Should remove post file IDs for burn on read posts" , func ( t * testing . T ) {
th := Setup ( t ) . InitBasic ( t )
ci: enable fullyparallel mode for server tests (#35816)
* ci: enable fullyparallel mode for server tests
Replace os.Setenv, os.Chdir, and global state mutations with
parallel-safe alternatives (t.Setenv, t.Chdir, test hooks) across
37 files. Refactor GetLogRootPath and MM_INSTALL_TYPE to use
package-level test hooks instead of environment variables.
This enables gotestsum --fullparallel, allowing all test packages
to run with maximum parallelism within each shard.
Co-authored-by: Claude <claude@anthropic.com>
* ci: split fullyparallel from continue-on-error in workflow template
- Add new boolean input 'allow-failure' separate from 'fullyparallel'
- Change continue-on-error to use allow-failure instead of fullyparallel
- Update server-ci.yml to pass allow-failure: true for test coverage job
- Allows independent control of parallel execution and failure tolerance
Co-authored-by: Claude <claude@anthropic.com>
* fix: protect TestOverrideLogRootPath with sync.Mutex for parallel tests
- Replace global var TestOverrideLogRootPath with mutex-protected functions
- Add SetTestOverrideLogRootPath() and getTestOverrideLogRootPath() functions
- Update GetLogRootPath() to use thread-safe getter
- Update all test files to use SetTestOverrideLogRootPath() with t.Cleanup()
- Fixes race condition when running tests with t.Parallel()
Co-authored-by: Claude <claude@anthropic.com>
* fix: configure audit settings before server setup in tests
- Move ExperimentalAuditSettings from UpdateConfig() to config defaults
- Pass audit config via app.Config() option in SetupWithServerOptions()
- Fixes audit test setup ordering to configure BEFORE server initialization
- Resolves CodeRabbit's audit config timing issue in api4 tests
Co-authored-by: Claude <claude@anthropic.com>
* fix: implement SetTestOverrideLogRootPath mutex in logger.go
The previous commit updated test callers to use SetTestOverrideLogRootPath()
but didn't actually create the function in config/logger.go, causing build
failures across all CI shards. This commit:
- Replaces the exported var TestOverrideLogRootPath with mutex-protected
unexported state (testOverrideLogRootPath + testOverrideLogRootMu)
- Adds exported SetTestOverrideLogRootPath() setter
- Adds unexported getTestOverrideLogRootPath() getter
- Updates GetLogRootPath() to use the thread-safe getter
- Fixes log_test.go callers that were missed in the previous commit
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): use SetupConfig for access_control feature flag registration
InitAccessControlPolicy() checks FeatureFlags.AttributeBasedAccessControl
at route registration time during server startup. Setting the flag via
UpdateConfig after Setup() is too late — routes are never registered
and API calls return 404.
Use SetupConfig() to pass the feature flag in the initial config before
server startup, ensuring routes are properly registered.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore BurnOnRead flag state in TestRevealPost subtest
The 'feature not enabled' subtest disables BurnOnRead without restoring
it via t.Cleanup. Subsequent subtests inherit the disabled state, which
can cause 501 errors when they expect the feature to be available.
Add t.Cleanup to restore FeatureFlags.BurnOnRead = true after the
subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore EnableSharedChannelsMemberSync flag via t.Cleanup
The test disables EnableSharedChannelsMemberSync without restoring it.
If the subtest exits early (e.g., require failure), later sibling
subtests inherit a disabled flag and become flaky.
Add t.Cleanup to restore the flag after the subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* Fix test parallelism: use instance-scoped overrides and init-time audit config
Replace package-level test globals (TestOverrideInstallType,
SetTestOverrideLogRootPath) with fields on PlatformService so each test
gets its own instance without process-wide mutation. Fix three audit
tests (TestUserLoginAudit, TestLogoutAuditAuthStatus,
TestUpdatePasswordAudit) that configured the audit logger after server
init — the audit logger only reads config at startup, so pass audit
settings via app.Config() at init time instead.
Also revert the Go 1.24.13 downgrade and bump mattermost-govet to
v2.0.2 for Go 1.25.8 compatibility.
* Fix audit unit tests
* Fix MMCLOUDURL unit tests
* Fixed unit tests using MM_NOTIFY_ADMIN_COOL_OFF_DAYS
* Make app migrations idempotent for parallel test safety
Change System().Save() to System().SaveOrUpdate() in all migration
completion markers. When two parallel tests share a database pool entry,
both may race through the check-then-insert migration pattern. Save()
causes a duplicate key fatal crash; SaveOrUpdate() makes the second
write a harmless no-op.
* test: address review feedback on fullyparallel PR
- Use SetLogRootPathOverride() setter instead of direct field access
in platform/support_packet_test.go and platform/log_test.go (pvev)
- Restore TestGetLogRootPath in config/logger_test.go to keep
MM_LOG_PATH env var coverage; test uses t.Setenv so it runs
serially which is fine (pvev)
- Fix misleading comment in config_test.go: code uses t.Setenv,
not os.Setenv (jgheithcock)
Co-authored-by: Claude <claude@anthropic.com>
* fix: add missing os import in post_test.go
The os import was dropped during a merge conflict resolution while
burn-on-read shared channel tests from master still use os.Setenv.
Co-authored-by: Claude <claude@anthropic.com>
---------
Co-authored-by: Claude <claude@anthropic.com>
Co-authored-by: wiggin77 <wiggin77@warpmail.net>
Co-authored-by: Mattermost Build <build@mattermost.com>
2026-04-08 20:48:36 -04:00
// Enable BurnOnRead feature flag
th . App . UpdateConfig ( func ( cfg * model . Config ) { cfg . FeatureFlags . BurnOnRead = true } )
2025-12-11 01:59:50 -05:00
enableBoRFeature ( th )
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "hello world" ,
Type : model . PostTypeBurnOnRead ,
FileIds : [ ] string { model . NewId ( ) } ,
}
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { } )
2025-12-11 01:59:50 -05:00
require . Nil ( t , appErr )
require . Empty ( t , createdPost . FileIds )
} )
2026-04-01 05:30:35 -04:00
t . Run ( "should reject burn-on-read posts in shared channels" , func ( t * testing . T ) {
os . Setenv ( "MM_FEATUREFLAGS_BURNONREAD" , "true" )
t . Cleanup ( func ( ) {
os . Unsetenv ( "MM_FEATUREFLAGS_BURNONREAD" )
} )
th := setupSharedChannels ( t ) . InitBasic ( t )
enableBoRFeature ( th )
channel := th . CreateChannel ( t , th . BasicTeam )
sc := & model . SharedChannel {
ChannelId : channel . Id ,
TeamId : th . BasicTeam . Id ,
Type : channel . Type ,
Home : true ,
ShareName : "shared-bor-test" ,
CreatorId : th . BasicUser . Id ,
RemoteId : model . NewId ( ) ,
}
_ , scErr := th . Server . Store ( ) . SharedChannel ( ) . Save ( sc )
require . NoError ( t , scErr )
channel . Shared = model . NewPointer ( true )
_ , err := th . Server . Store ( ) . Channel ( ) . Update ( th . Context , channel )
require . NoError ( t , err )
post := & model . Post {
ChannelId : channel . Id ,
UserId : th . BasicUser . Id ,
Message : "burn-on-read in shared channel" ,
Type : model . PostTypeBurnOnRead ,
}
createdPost , _ , appErr := th . App . CreatePost ( th . Context , post , channel , model . CreatePostFlags { SetOnline : true } )
require . NotNil ( t , appErr )
require . Nil ( t , createdPost )
require . Equal ( t , "api.post.fill_in_post_props.burn_on_read.shared_channel.app_error" , appErr . Id )
require . Equal ( t , http . StatusBadRequest , appErr . StatusCode )
} )
t . Run ( "should allow burn-on-read posts in non-shared channels" , func ( t * testing . T ) {
os . Setenv ( "MM_FEATUREFLAGS_BURNONREAD" , "true" )
t . Cleanup ( func ( ) {
os . Unsetenv ( "MM_FEATUREFLAGS_BURNONREAD" )
} )
th := Setup ( t ) . InitBasic ( t )
enableBoRFeature ( th )
channel := th . CreateChannel ( t , th . BasicTeam )
require . False ( t , channel . IsShared ( ) )
post := & model . Post {
ChannelId : channel . Id ,
UserId : th . BasicUser . Id ,
Message : "burn-on-read in non-shared channel" ,
Type : model . PostTypeBurnOnRead ,
}
createdPost , _ , appErr := th . App . CreatePost ( th . Context , post , channel , model . CreatePostFlags { SetOnline : true } )
require . Nil ( t , appErr )
require . NotNil ( t , createdPost )
require . Equal ( t , model . PostTypeBurnOnRead , createdPost . Type )
} )
2018-12-13 09:47:30 -05:00
}
func TestPatchPost ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2018-12-13 09:47:30 -05:00
t . Run ( "call PreparePostForClient before returning" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2018-12-13 09:47:30 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2019-05-06 09:22:37 -04:00
* cfg . ServiceSettings . SiteURL = "http://mymattermost.com"
2019-01-24 15:11:32 -05:00
* cfg . ImageProxySettings . Enable = true
* cfg . ImageProxySettings . ImageProxyType = "atmos/camo"
* cfg . ImageProxySettings . RemoteImageProxyURL = "https://127.0.0.1"
2026-04-08 15:49:43 -04:00
* cfg . ImageProxySettings . RemoteImageProxyOptions = model . NewTestPassword ( )
2018-12-13 09:47:30 -05:00
} )
2022-08-24 03:10:56 -04:00
th . App . ch . imageProxy = imageproxy . MakeImageProxy ( th . Server . platform , th . Server . HTTPService ( ) , th . Server . Log ( ) )
2020-11-10 02:36:59 -05:00
2018-12-13 09:47:30 -05:00
imageURL := "http://mydomain.com/myimage"
2019-05-06 09:22:37 -04:00
proxiedImageURL := "http://mymattermost.com/api/v4/image?url=http%3A%2F%2Fmydomain.com%2Fmyimage"
2018-12-13 09:47:30 -05:00
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
rpost , _ , err := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2018-12-13 09:47:30 -05:00
require . Nil ( t , err )
assert . NotEqual ( t , "" , rpost . Message )
patch := & model . PostPatch {
2024-08-05 23:45:00 -04:00
Message : model . NewPointer ( "" ) ,
2018-12-13 09:47:30 -05:00
}
2026-01-20 04:38:27 -05:00
rpost , _ , err = th . App . PatchPost ( th . Context , rpost . Id , patch , nil )
2018-12-13 09:47:30 -05:00
require . Nil ( t , err )
assert . Equal ( t , "" , rpost . Message )
} )
2020-03-03 05:22:49 -05:00
t . Run ( "Sets Prop MENTION_HIGHLIGHT_DISABLED when it should" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2020-03-03 05:22:49 -05:00
2025-11-12 07:00:51 -05:00
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel )
2020-03-03 05:22:49 -05:00
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "This post does not have mentions" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
rpost , _ , err := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2020-03-03 05:22:49 -05:00
require . Nil ( t , err )
t . Run ( "Does not set prop when user has USE_CHANNEL_MENTIONS" , func ( t * testing . T ) {
2024-08-05 23:45:00 -04:00
patchWithNoMention := & model . PostPatch { Message : model . NewPointer ( "This patch has no channel mention" ) }
2020-03-03 05:22:49 -05:00
2026-01-20 04:38:27 -05:00
rpost , _ , err = th . App . PatchPost ( th . Context , rpost . Id , patchWithNoMention , nil )
2020-03-03 05:22:49 -05:00
require . Nil ( t , err )
2020-03-13 16:12:20 -04:00
assert . Equal ( t , rpost . GetProps ( ) , model . StringInterface { } )
2020-03-03 05:22:49 -05:00
2024-08-05 23:45:00 -04:00
patchWithMention := & model . PostPatch { Message : model . NewPointer ( "This patch has a mention now @here" ) }
2020-03-03 05:22:49 -05:00
2026-01-20 04:38:27 -05:00
rpost , _ , err = th . App . PatchPost ( th . Context , rpost . Id , patchWithMention , nil )
2020-03-03 05:22:49 -05:00
require . Nil ( t , err )
2020-03-13 16:12:20 -04:00
assert . Equal ( t , rpost . GetProps ( ) , model . StringInterface { } )
2020-03-03 05:22:49 -05:00
} )
t . Run ( "Sets prop when user does not have USE_CHANNEL_MENTIONS" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionUseChannelMentions . Id , model . ChannelUserRoleId )
th . RemovePermissionFromRole ( t , model . PermissionUseChannelMentions . Id , model . ChannelAdminRoleId )
2020-03-03 05:22:49 -05:00
2024-08-05 23:45:00 -04:00
patchWithNoMention := & model . PostPatch { Message : model . NewPointer ( "This patch still does not have a mention" ) }
2026-01-20 04:38:27 -05:00
rpost , _ , err = th . App . PatchPost ( th . Context , rpost . Id , patchWithNoMention , nil )
2020-03-03 05:22:49 -05:00
require . Nil ( t , err )
2020-03-13 16:12:20 -04:00
assert . Equal ( t , rpost . GetProps ( ) , model . StringInterface { } )
2020-03-03 05:22:49 -05:00
2024-08-05 23:45:00 -04:00
patchWithMention := & model . PostPatch { Message : model . NewPointer ( "This patch has a mention now @here" ) }
2020-03-03 05:22:49 -05:00
2026-01-20 04:38:27 -05:00
rpost , _ , err = th . App . PatchPost ( th . Context , rpost . Id , patchWithMention , nil )
2020-03-03 05:22:49 -05:00
require . Nil ( t , err )
2021-07-12 14:05:36 -04:00
assert . Equal ( t , rpost . GetProp ( model . PostPropsMentionHighlightDisabled ) , true )
2020-03-03 05:22:49 -05:00
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionUseChannelMentions . Id , model . ChannelUserRoleId )
th . AddPermissionToRole ( t , model . PermissionUseChannelMentions . Id , model . ChannelAdminRoleId )
2020-03-03 05:22:49 -05:00
} )
} )
2025-09-30 11:41:14 -04:00
t . Run ( "cannot patch post in restricted DM" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-09-30 11:41:14 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . TeamSettings . RestrictDirectMessage = model . DirectMessageTeam
} )
// Create a DM channel between two users who don't share a team
2025-11-12 07:00:51 -05:00
dmChannel := th . CreateDmChannel ( t , th . BasicUser2 )
2025-09-30 11:41:14 -04:00
// Ensure the two users do not share a team
teams , err := th . App . GetTeamsForUser ( th . BasicUser . Id )
require . Nil ( t , err )
for _ , team := range teams {
teamErr := th . App . RemoveUserFromTeam ( th . Context , team . Id , th . BasicUser . Id , th . SystemAdminUser . Id )
require . Nil ( t , teamErr )
}
teams , err = th . App . GetTeamsForUser ( th . BasicUser2 . Id )
require . Nil ( t , err )
for _ , team := range teams {
teamErr := th . App . RemoveUserFromTeam ( th . Context , team . Id , th . BasicUser2 . Id , th . SystemAdminUser . Id )
require . Nil ( t , teamErr )
}
// Create separate teams for each user
2025-11-12 07:00:51 -05:00
team1 := th . CreateTeam ( t )
team2 := th . CreateTeam ( t )
th . LinkUserToTeam ( t , th . BasicUser , team1 )
th . LinkUserToTeam ( t , th . BasicUser2 , team2 )
2025-09-30 11:41:14 -04:00
// Create a post in the DM channel
post := & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : dmChannel . Id ,
Message : "test post" ,
}
2026-01-20 04:38:27 -05:00
post , _ , err = th . App . CreatePost ( th . Context , post , dmChannel , model . CreatePostFlags { } )
2025-09-30 11:41:14 -04:00
require . Nil ( t , err )
// Try to patch the post
patch := & model . PostPatch {
Message : model . NewPointer ( "updated message" ) ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr := th . App . PatchPost ( th . Context , post . Id , patch , model . DefaultUpdatePostOptions ( ) )
2025-09-30 11:41:14 -04:00
require . NotNil ( t , appErr )
require . Equal ( t , "api.post.patch_post.can_not_update_post_in_restricted_dm.error" , appErr . Id )
require . Equal ( t , http . StatusBadRequest , appErr . StatusCode )
// Reset config
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . TeamSettings . RestrictDirectMessage = model . DirectMessageAny
} )
} )
2018-12-13 09:47:30 -05:00
}
2020-04-22 11:28:31 -04:00
func TestCreatePostAsUser ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2020-04-22 11:28:31 -04:00
t . Run ( "marks channel as viewed for regular user" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2020-04-22 11:28:31 -04:00
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "test" ,
UserId : th . BasicUser . Id ,
}
2025-09-18 10:14:24 -04:00
channelMemberBefore , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err )
2020-04-22 11:28:31 -04:00
time . Sleep ( 1 * time . Millisecond )
2026-01-20 04:38:27 -05:00
_ , _ , appErr := th . App . CreatePostAsUser ( th . Context , post , "" , true )
2020-04-22 11:28:31 -04:00
require . Nil ( t , appErr )
2025-09-18 10:14:24 -04:00
channelMemberAfter , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err )
2020-04-22 11:28:31 -04:00
require . Greater ( t , channelMemberAfter . LastViewedAt , channelMemberBefore . LastViewedAt )
} )
t . Run ( "does not mark channel as viewed for webhook from user" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2020-04-22 11:28:31 -04:00
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "test" ,
UserId : th . BasicUser . Id ,
}
2025-03-20 07:53:50 -04:00
post . AddProp ( model . PostPropsFromWebhook , "true" )
2020-04-22 11:28:31 -04:00
2025-09-18 10:14:24 -04:00
channelMemberBefore , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err )
2020-04-22 11:28:31 -04:00
time . Sleep ( 1 * time . Millisecond )
2026-01-20 04:38:27 -05:00
_ , _ , appErr := th . App . CreatePostAsUser ( th . Context , post , "" , true )
2020-04-22 11:28:31 -04:00
require . Nil ( t , appErr )
2025-09-18 10:14:24 -04:00
channelMemberAfter , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err )
2020-04-22 11:28:31 -04:00
require . Equal ( t , channelMemberAfter . LastViewedAt , channelMemberBefore . LastViewedAt )
} )
t . Run ( "does not mark channel as viewed for bot user in channel" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2020-04-22 11:28:31 -04:00
2025-11-12 07:00:51 -05:00
bot := th . CreateBot ( t )
2020-04-22 11:28:31 -04:00
botUser , appErr := th . App . GetUser ( bot . UserId )
require . Nil ( t , appErr )
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , botUser , th . BasicTeam )
th . AddUserToChannel ( t , botUser , th . BasicChannel )
2020-04-22 11:28:31 -04:00
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "test" ,
UserId : bot . UserId ,
}
2025-09-18 10:14:24 -04:00
channelMemberBefore , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2025-04-29 02:50:47 -04:00
require . NoError ( t , err )
2020-04-22 11:28:31 -04:00
time . Sleep ( 1 * time . Millisecond )
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePostAsUser ( th . Context , post , "" , true )
2020-04-22 11:28:31 -04:00
require . Nil ( t , appErr )
2025-09-18 10:14:24 -04:00
channelMemberAfter , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2025-04-29 02:50:47 -04:00
require . NoError ( t , err )
2020-04-22 11:28:31 -04:00
require . Equal ( t , channelMemberAfter . LastViewedAt , channelMemberBefore . LastViewedAt )
} )
t . Run ( "does not log warning for bot user not in channel" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2020-04-22 11:28:31 -04:00
2025-11-12 07:00:51 -05:00
bot := th . CreateBot ( t )
2020-04-22 11:28:31 -04:00
botUser , appErr := th . App . GetUser ( bot . UserId )
require . Nil ( t , appErr )
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , botUser , th . BasicTeam )
2020-04-22 11:28:31 -04:00
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "test" ,
UserId : bot . UserId ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePostAsUser ( th . Context , post , "" , true )
2020-04-22 11:28:31 -04:00
require . Nil ( t , appErr )
2022-06-16 04:47:01 -04:00
require . NoError ( t , th . TestLogger . Flush ( ) )
2021-08-17 16:08:04 -04:00
testlib . AssertNoLog ( t , th . LogBuffer , mlog . LvlWarn . Name , "Failed to get membership" )
2020-04-22 11:28:31 -04:00
} )
2021-07-29 05:17:28 -04:00
t . Run ( "marks channel as viewed for reply post when CRT is off" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-07-29 05:17:28 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . CollapsedThreads = model . CollapsedThreadsDefaultOff
} )
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "test" ,
UserId : th . BasicUser2 . Id ,
}
2026-01-20 04:38:27 -05:00
rootPost , _ , appErr := th . App . CreatePostAsUser ( th . Context , post , "" , true )
2021-07-29 05:17:28 -04:00
require . Nil ( t , appErr )
2025-09-18 10:14:24 -04:00
channelMemberBefore , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2025-04-29 02:50:47 -04:00
require . NoError ( t , err )
2021-07-29 05:17:28 -04:00
time . Sleep ( 1 * time . Millisecond )
replyPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "test reply" ,
UserId : th . BasicUser . Id ,
RootId : rootPost . Id ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePostAsUser ( th . Context , replyPost , "" , true )
2021-07-29 05:17:28 -04:00
require . Nil ( t , appErr )
2025-09-18 10:14:24 -04:00
channelMemberAfter , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2025-04-29 02:50:47 -04:00
require . NoError ( t , err )
2021-07-29 05:17:28 -04:00
require . NotEqual ( t , channelMemberAfter . LastViewedAt , channelMemberBefore . LastViewedAt )
} )
t . Run ( "does not mark channel as viewed for reply post when CRT is on" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-07-29 05:17:28 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . ThreadAutoFollow = true
* cfg . ServiceSettings . CollapsedThreads = model . CollapsedThreadsDefaultOn
} )
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "test" ,
UserId : th . BasicUser2 . Id ,
}
2026-01-20 04:38:27 -05:00
rootPost , _ , appErr := th . App . CreatePostAsUser ( th . Context , post , "" , true )
2021-07-29 05:17:28 -04:00
require . Nil ( t , appErr )
2025-09-18 10:14:24 -04:00
channelMemberBefore , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2025-04-29 02:50:47 -04:00
require . NoError ( t , err )
2021-07-29 05:17:28 -04:00
time . Sleep ( 1 * time . Millisecond )
replyPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "test reply" ,
UserId : th . BasicUser . Id ,
RootId : rootPost . Id ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePostAsUser ( th . Context , replyPost , "" , true )
2021-07-29 05:17:28 -04:00
require . Nil ( t , appErr )
2025-09-18 10:14:24 -04:00
channelMemberAfter , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2025-04-29 02:50:47 -04:00
require . NoError ( t , err )
2021-07-29 05:17:28 -04:00
require . Equal ( t , channelMemberAfter . LastViewedAt , channelMemberBefore . LastViewedAt )
} )
2025-09-30 11:41:14 -04:00
t . Run ( "cannot create post as user in restricted DM" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-09-30 11:41:14 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . TeamSettings . RestrictDirectMessage = model . DirectMessageTeam
} )
// Create a DM channel between two users who don't share a team
2025-11-12 07:00:51 -05:00
dmChannel := th . CreateDmChannel ( t , th . BasicUser2 )
2025-09-30 11:41:14 -04:00
// Ensure the two users do not share a team
teams , err := th . App . GetTeamsForUser ( th . BasicUser . Id )
require . Nil ( t , err )
for _ , team := range teams {
teamErr := th . App . RemoveUserFromTeam ( th . Context , team . Id , th . BasicUser . Id , th . SystemAdminUser . Id )
require . Nil ( t , teamErr )
}
teams , err = th . App . GetTeamsForUser ( th . BasicUser2 . Id )
require . Nil ( t , err )
for _ , team := range teams {
teamErr := th . App . RemoveUserFromTeam ( th . Context , team . Id , th . BasicUser2 . Id , th . SystemAdminUser . Id )
require . Nil ( t , teamErr )
}
// Create separate teams for each user
2025-11-12 07:00:51 -05:00
team1 := th . CreateTeam ( t )
team2 := th . CreateTeam ( t )
th . LinkUserToTeam ( t , th . BasicUser , team1 )
th . LinkUserToTeam ( t , th . BasicUser2 , team2 )
2025-09-30 11:41:14 -04:00
post := & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : dmChannel . Id ,
Message : "test post" ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr := th . App . CreatePostAsUser ( th . Context , post , "" , true )
2025-09-30 11:41:14 -04:00
require . NotNil ( t , appErr )
require . Equal ( t , "api.post.create_post.can_not_post_in_restricted_dm.error" , appErr . Id )
require . Equal ( t , http . StatusBadRequest , appErr . StatusCode )
// Reset config
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . TeamSettings . RestrictDirectMessage = model . DirectMessageAny
} )
} )
2020-04-22 11:28:31 -04:00
}
2019-03-11 04:26:31 -04:00
func TestPatchPostInArchivedChannel ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-03-11 04:26:31 -04:00
2025-11-12 07:00:51 -05:00
archivedChannel := th . CreateChannel ( t , th . BasicTeam )
post := th . CreatePost ( t , archivedChannel )
2025-04-29 02:50:47 -04:00
appErr := th . App . DeleteChannel ( th . Context , archivedChannel , "" )
require . Nil ( t , appErr )
2019-03-11 04:26:31 -04:00
2026-01-20 04:38:27 -05:00
_ , _ , err := th . App . PatchPost ( th . Context , post . Id , & model . PostPatch { IsPinned : model . NewPointer ( true ) } , nil )
2019-03-11 04:26:31 -04:00
require . NotNil ( t , err )
require . Equal ( t , "api.post.patch_post.can_not_update_post_in_deleted.error" , err . Id )
}
2023-12-14 03:32:41 -05:00
func TestUpdateEphemeralPost ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2023-12-14 03:32:41 -05:00
t . Run ( "Post contains preview if the user has permissions" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel )
2023-12-14 03:32:41 -05:00
referencedPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "hello world" ,
UserId : th . BasicUser . Id ,
}
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . SiteURL = "http://mymattermost.com"
} )
th . Context . Session ( ) . UserId = th . BasicUser . Id
2026-01-20 04:38:27 -05:00
referencedPost , _ , err := th . App . CreatePost ( th . Context , referencedPost , th . BasicChannel , model . CreatePostFlags { } )
2023-12-14 03:32:41 -05:00
require . Nil ( t , err )
permalink := fmt . Sprintf ( "%s/%s/pl/%s" , * th . App . Config ( ) . ServiceSettings . SiteURL , th . BasicTeam . Name , referencedPost . Id )
testPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : permalink ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
testPost , _ = th . App . UpdateEphemeralPost ( th . Context , th . BasicUser . Id , testPost )
2023-12-14 03:32:41 -05:00
require . NotNil ( t , testPost . Metadata )
require . Len ( t , testPost . Metadata . Embeds , 1 )
require . Equal ( t , model . PostEmbedPermalink , testPost . Metadata . Embeds [ 0 ] . Type )
} )
t . Run ( "Post does not contain preview if the user has no permissions" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
privateChannel := th . CreatePrivateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , th . BasicUser , privateChannel )
th . AddUserToChannel ( t , th . BasicUser2 , th . BasicChannel )
2023-12-14 03:32:41 -05:00
referencedPost := & model . Post {
ChannelId : privateChannel . Id ,
Message : "hello world" ,
UserId : th . BasicUser . Id ,
}
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . SiteURL = "http://mymattermost.com"
} )
th . Context . Session ( ) . UserId = th . BasicUser . Id
2026-01-20 04:38:27 -05:00
referencedPost , _ , err := th . App . CreatePost ( th . Context , referencedPost , th . BasicChannel , model . CreatePostFlags { } )
2023-12-14 03:32:41 -05:00
require . Nil ( t , err )
permalink := fmt . Sprintf ( "%s/%s/pl/%s" , * th . App . Config ( ) . ServiceSettings . SiteURL , th . BasicTeam . Name , referencedPost . Id )
testPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : permalink ,
UserId : th . BasicUser2 . Id ,
}
2026-01-20 04:38:27 -05:00
testPost , _ = th . App . UpdateEphemeralPost ( th . Context , th . BasicUser2 . Id , testPost )
2023-12-14 03:32:41 -05:00
require . Nil ( t , testPost . Metadata . Embeds )
} )
}
2018-12-13 09:47:30 -05:00
func TestUpdatePost ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2018-12-13 09:47:30 -05:00
t . Run ( "call PreparePostForClient before returning" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2018-12-13 09:47:30 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2019-05-06 09:22:37 -04:00
* cfg . ServiceSettings . SiteURL = "http://mymattermost.com"
2019-01-24 15:11:32 -05:00
* cfg . ImageProxySettings . Enable = true
* cfg . ImageProxySettings . ImageProxyType = "atmos/camo"
* cfg . ImageProxySettings . RemoteImageProxyURL = "https://127.0.0.1"
2026-04-08 15:49:43 -04:00
* cfg . ImageProxySettings . RemoteImageProxyOptions = model . NewTestPassword ( )
2018-12-13 09:47:30 -05:00
} )
2022-08-24 03:10:56 -04:00
th . App . ch . imageProxy = imageproxy . MakeImageProxy ( th . Server . platform , th . Server . HTTPService ( ) , th . Server . Log ( ) )
2020-11-10 02:36:59 -05:00
2018-12-13 09:47:30 -05:00
imageURL := "http://mydomain.com/myimage"
2019-05-06 09:22:37 -04:00
proxiedImageURL := "http://mymattermost.com/api/v4/image?url=http%3A%2F%2Fmydomain.com%2Fmyimage"
2018-12-13 09:47:30 -05:00
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
rpost , _ , err := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2018-12-13 09:47:30 -05:00
require . Nil ( t , err )
assert . NotEqual ( t , "" , rpost . Message )
post . Id = rpost . Id
post . Message = ""
2026-01-20 04:38:27 -05:00
rpost , isMemberForPreviews , err := th . App . UpdatePost ( th . Context , post , nil )
require . True ( t , isMemberForPreviews )
2018-12-13 09:47:30 -05:00
require . Nil ( t , err )
assert . Equal ( t , "" , rpost . Message )
} )
2021-08-09 11:33:21 -04:00
t . Run ( "Sets PostPropsPreviewedPost when a post is updated to have a permalink as the first link" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel )
2021-08-09 11:33:21 -04:00
referencedPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "hello world" ,
UserId : th . BasicUser . Id ,
}
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . SiteURL = "http://mymattermost.com"
} )
th . Context . Session ( ) . UserId = th . BasicUser . Id
2026-01-20 04:38:27 -05:00
referencedPost , _ , err := th . App . CreatePost ( th . Context , referencedPost , th . BasicChannel , model . CreatePostFlags { } )
2021-08-09 11:33:21 -04:00
require . Nil ( t , err )
permalink := fmt . Sprintf ( "%s/%s/pl/%s" , * th . App . Config ( ) . ServiceSettings . SiteURL , th . BasicTeam . Name , referencedPost . Id )
2025-11-12 07:00:51 -05:00
channelForTestPost := th . CreateChannel ( t , th . BasicTeam )
2021-08-09 11:33:21 -04:00
testPost := & model . Post {
ChannelId : channelForTestPost . Id ,
Message : "hello world" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
testPost , _ , err = th . App . CreatePost ( th . Context , testPost , channelForTestPost , model . CreatePostFlags { } )
2021-08-09 11:33:21 -04:00
require . Nil ( t , err )
2024-10-21 00:05:32 -04:00
assert . Equal ( t , model . StringInterface { } , testPost . GetProps ( ) )
2021-08-09 11:33:21 -04:00
testPost . Message = permalink
2026-01-20 04:38:27 -05:00
testPost , isMemberForPreviews , err := th . App . UpdatePost ( th . Context , testPost , nil )
require . True ( t , isMemberForPreviews )
2021-08-09 11:33:21 -04:00
require . Nil ( t , err )
2024-10-21 00:05:32 -04:00
assert . Equal ( t , model . StringInterface { model . PostPropsPreviewedPost : referencedPost . Id } , testPost . GetProps ( ) )
2021-08-09 11:33:21 -04:00
} )
2023-12-01 04:50:57 -05:00
t . Run ( "sanitizes post metadata appropriately" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2023-12-01 04:50:57 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . SiteURL = "http://mymattermost.com"
} )
2025-11-12 07:00:51 -05:00
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel )
2023-12-01 04:50:57 -05:00
2025-11-12 07:00:51 -05:00
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
2023-12-01 04:50:57 -05:00
directChannel , err := th . App . createDirectChannel ( th . Context , user1 . Id , user2 . Id )
require . Nil ( t , err )
th . Context . Session ( ) . UserId = th . BasicUser . Id
testCases := [ ] struct {
Description string
Channel * model . Channel
Author string
Length int
} {
{
Description : "removes metadata from post for members who cannot read channel" ,
Channel : directChannel ,
Author : user1 . Id ,
Length : 0 ,
} ,
{
Description : "does not remove metadata from post for members who can read channel" ,
Channel : th . BasicChannel ,
Author : th . BasicUser . Id ,
Length : 1 ,
} ,
}
for _ , testCase := range testCases {
t . Run ( testCase . Description , func ( t * testing . T ) {
referencedPost := & model . Post {
ChannelId : testCase . Channel . Id ,
Message : "hello world" ,
UserId : testCase . Author ,
}
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , referencedPost , testCase . Channel , model . CreatePostFlags { } )
2023-12-01 04:50:57 -05:00
require . Nil ( t , err )
previewPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
previewPost , _ , err = th . App . CreatePost ( th . Context , previewPost , th . BasicChannel , model . CreatePostFlags { } )
2023-12-01 04:50:57 -05:00
require . Nil ( t , err )
permalink := fmt . Sprintf ( "%s/%s/pl/%s" , * th . App . Config ( ) . ServiceSettings . SiteURL , th . BasicTeam . Name , referencedPost . Id )
previewPost . Message = permalink
2026-01-20 04:38:27 -05:00
previewPost , isMemberForPreviews , err := th . App . UpdatePost ( th . Context , previewPost , nil )
require . True ( t , isMemberForPreviews )
2023-12-01 04:50:57 -05:00
require . Nil ( t , err )
require . Len ( t , previewPost . Metadata . Embeds , testCase . Length )
} )
}
} )
2025-09-30 11:41:14 -04:00
2026-01-23 16:11:16 -05:00
t . Run ( "should strip client-supplied embeds" , func ( t * testing . T ) {
// MM-67055: Verify that client-supplied metadata.embeds are stripped.
// This prevents WebSocket message spoofing via permalink embeds.
//
// Note: Priority and Acknowledgements are stored in separate database tables,
// not in post metadata. Shared Channels handles them separately via
// syncRemotePriorityMetadata and syncRemoteAcknowledgementsMetadata after
// calling UpdatePost. See sync_recv.go::upsertSyncPost
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel )
th . Context . Session ( ) . UserId = th . BasicUser . Id
// Create a basic post
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "original message" ,
UserId : th . BasicUser . Id ,
}
2026-01-26 07:33:51 -05:00
createdPost , _ , err := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { } )
2026-01-23 16:11:16 -05:00
require . Nil ( t , err )
// Try to update with spoofed embeds (the attack vector)
updatePost := & model . Post {
Id : createdPost . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "updated message" ,
UserId : th . BasicUser . Id ,
Metadata : & model . PostMetadata {
Embeds : [ ] * model . PostEmbed {
{
Type : model . PostEmbedPermalink ,
Data : & model . PreviewPost {
PostID : "spoofed-post-id" ,
Post : & model . Post {
Id : "spoofed-post-id" ,
UserId : th . BasicUser2 . Id ,
Message : "Spoofed message from another user!" ,
} ,
} ,
} ,
} ,
} ,
}
2026-01-26 07:33:51 -05:00
updatedPost , _ , err := th . App . UpdatePost ( th . Context , updatePost , nil )
2026-01-23 16:11:16 -05:00
require . Nil ( t , err )
require . NotNil ( t , updatedPost . Metadata )
// Verify embeds were stripped
assert . Empty ( t , updatedPost . Metadata . Embeds , "spoofed embeds should be stripped" )
} )
2025-09-30 11:41:14 -04:00
t . Run ( "cannot update post in restricted DM" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-09-30 11:41:14 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . TeamSettings . RestrictDirectMessage = model . DirectMessageTeam
} )
// Create a DM channel between two users who don't share a team
2025-11-12 07:00:51 -05:00
dmChannel := th . CreateDmChannel ( t , th . BasicUser2 )
2025-09-30 11:41:14 -04:00
// Ensure the two users do not share a team
teams , err := th . App . GetTeamsForUser ( th . BasicUser . Id )
require . Nil ( t , err )
for _ , team := range teams {
teamErr := th . App . RemoveUserFromTeam ( th . Context , team . Id , th . BasicUser . Id , th . SystemAdminUser . Id )
require . Nil ( t , teamErr )
}
teams , err = th . App . GetTeamsForUser ( th . BasicUser2 . Id )
require . Nil ( t , err )
for _ , team := range teams {
teamErr := th . App . RemoveUserFromTeam ( th . Context , team . Id , th . BasicUser2 . Id , th . SystemAdminUser . Id )
require . Nil ( t , teamErr )
}
// Create separate teams for each user
2025-11-12 07:00:51 -05:00
team1 := th . CreateTeam ( t )
team2 := th . CreateTeam ( t )
th . LinkUserToTeam ( t , th . BasicUser , team1 )
th . LinkUserToTeam ( t , th . BasicUser2 , team2 )
2025-09-30 11:41:14 -04:00
// Create a post in the DM channel
post := & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : dmChannel . Id ,
Message : "test post" ,
}
2026-01-20 04:38:27 -05:00
post , _ , err = th . App . CreatePost ( th . Context , post , dmChannel , model . CreatePostFlags { } )
2025-09-30 11:41:14 -04:00
require . Nil ( t , err )
// Try to update the post
post . Message = "updated message"
2026-01-20 04:38:27 -05:00
_ , _ , appErr := th . App . UpdatePost ( th . Context , post , model . DefaultUpdatePostOptions ( ) )
2025-09-30 11:41:14 -04:00
require . NotNil ( t , appErr )
require . Equal ( t , "api.post.update_post.can_not_update_post_in_restricted_dm.error" , appErr . Id )
require . Equal ( t , http . StatusBadRequest , appErr . StatusCode )
// Reset config
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . TeamSettings . RestrictDirectMessage = model . DirectMessageAny
} )
} )
2018-12-13 09:47:30 -05:00
}
2019-07-08 11:32:29 -04:00
2021-09-23 08:43:09 -04:00
func TestSearchPostsForUser ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2019-07-08 11:32:29 -04:00
perPage := 5
searchTerm := "searchTerm"
setup := func ( t * testing . T , enableElasticsearch bool ) ( * TestHelper , [ ] * model . Post ) {
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-07-08 11:32:29 -04:00
posts := make ( [ ] * model . Post , 7 )
for i := 0 ; i < cap ( posts ) ; i ++ {
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-07-08 11:32:29 -04:00
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : searchTerm ,
2024-10-22 10:00:26 -04:00
} , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2019-07-08 11:32:29 -04:00
require . Nil ( t , err )
posts [ i ] = post
}
if enableElasticsearch {
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( "elastic_search" ) )
2019-07-08 11:32:29 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ElasticsearchSettings . EnableIndexing = true
* cfg . ElasticsearchSettings . EnableSearching = true
} )
} else {
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ElasticsearchSettings . EnableSearching = false
} )
}
return th , posts
}
t . Run ( "should return everything as first page of posts from database" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2019-07-08 11:32:29 -04:00
th , posts := setup ( t , false )
page := 0
2026-01-20 04:38:27 -05:00
results , allPostHaveMembership , err := th . App . SearchPostsForUser ( th . Context , searchTerm , th . BasicUser . Id , th . BasicTeam . Id , false , false , 0 , page , perPage )
2019-07-08 11:32:29 -04:00
assert . Nil ( t , err )
assert . Equal ( t , [ ] string {
posts [ 6 ] . Id ,
posts [ 5 ] . Id ,
posts [ 4 ] . Id ,
posts [ 3 ] . Id ,
posts [ 2 ] . Id ,
posts [ 1 ] . Id ,
posts [ 0 ] . Id ,
} , results . Order )
2026-01-20 04:38:27 -05:00
assert . True ( t , allPostHaveMembership )
2019-07-08 11:32:29 -04:00
} )
t . Run ( "should not return later pages of posts from database" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2019-07-08 11:32:29 -04:00
th , _ := setup ( t , false )
page := 1
2026-01-20 04:38:27 -05:00
results , allPostHaveMembership , err := th . App . SearchPostsForUser ( th . Context , searchTerm , th . BasicUser . Id , th . BasicTeam . Id , false , false , 0 , page , perPage )
2019-07-08 11:32:29 -04:00
assert . Nil ( t , err )
assert . Equal ( t , [ ] string { } , results . Order )
2026-01-20 04:38:27 -05:00
assert . True ( t , allPostHaveMembership )
2019-07-08 11:32:29 -04:00
} )
t . Run ( "should return first page of posts from ElasticSearch" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2019-07-08 11:32:29 -04:00
th , posts := setup ( t , true )
page := 0
resultsPage := [ ] string {
posts [ 6 ] . Id ,
posts [ 5 ] . Id ,
posts [ 4 ] . Id ,
posts [ 3 ] . Id ,
posts [ 2 ] . Id ,
}
2020-03-13 10:33:18 -04:00
es := & mocks . SearchEngineInterface { }
2019-07-08 11:32:29 -04:00
es . On ( "SearchPosts" , mock . Anything , mock . Anything , page , perPage ) . Return ( resultsPage , nil , nil )
2020-03-13 10:33:18 -04:00
es . On ( "Start" ) . Return ( nil ) . Maybe ( )
es . On ( "IsActive" ) . Return ( true )
es . On ( "IsSearchEnabled" ) . Return ( true )
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . Platform ( ) . SearchEngine . ElasticsearchEngine = es
2020-03-13 10:33:18 -04:00
defer func ( ) {
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . Platform ( ) . SearchEngine . ElasticsearchEngine = nil
2020-03-13 10:33:18 -04:00
} ( )
2019-07-08 11:32:29 -04:00
2026-01-20 04:38:27 -05:00
results , allPostHaveMembership , err := th . App . SearchPostsForUser ( th . Context , searchTerm , th . BasicUser . Id , th . BasicTeam . Id , false , false , 0 , page , perPage )
2019-07-08 11:32:29 -04:00
assert . Nil ( t , err )
assert . Equal ( t , resultsPage , results . Order )
2026-01-20 04:38:27 -05:00
assert . True ( t , allPostHaveMembership )
2019-07-08 11:32:29 -04:00
es . AssertExpectations ( t )
} )
t . Run ( "should return later pages of posts from ElasticSearch" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2019-07-08 11:32:29 -04:00
th , posts := setup ( t , true )
page := 1
resultsPage := [ ] string {
posts [ 1 ] . Id ,
posts [ 0 ] . Id ,
}
2020-03-13 10:33:18 -04:00
es := & mocks . SearchEngineInterface { }
2019-07-08 11:32:29 -04:00
es . On ( "SearchPosts" , mock . Anything , mock . Anything , page , perPage ) . Return ( resultsPage , nil , nil )
2020-03-13 10:33:18 -04:00
es . On ( "Start" ) . Return ( nil ) . Maybe ( )
es . On ( "IsActive" ) . Return ( true )
es . On ( "IsSearchEnabled" ) . Return ( true )
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . Platform ( ) . SearchEngine . ElasticsearchEngine = es
2020-03-13 10:33:18 -04:00
defer func ( ) {
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . Platform ( ) . SearchEngine . ElasticsearchEngine = nil
2020-03-13 10:33:18 -04:00
} ( )
2019-07-08 11:32:29 -04:00
2026-01-20 04:38:27 -05:00
results , allPostHaveMembership , err := th . App . SearchPostsForUser ( th . Context , searchTerm , th . BasicUser . Id , th . BasicTeam . Id , false , false , 0 , page , perPage )
2019-07-08 11:32:29 -04:00
assert . Nil ( t , err )
assert . Equal ( t , resultsPage , results . Order )
2026-01-20 04:38:27 -05:00
assert . True ( t , allPostHaveMembership )
2019-07-08 11:32:29 -04:00
es . AssertExpectations ( t )
} )
t . Run ( "should fall back to database if ElasticSearch fails on first page" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2019-07-08 11:32:29 -04:00
th , posts := setup ( t , true )
page := 0
2020-03-13 10:33:18 -04:00
es := & mocks . SearchEngineInterface { }
2019-07-08 11:32:29 -04:00
es . On ( "SearchPosts" , mock . Anything , mock . Anything , page , perPage ) . Return ( nil , nil , & model . AppError { } )
2020-03-13 10:33:18 -04:00
es . On ( "GetName" ) . Return ( "mock" )
es . On ( "Start" ) . Return ( nil ) . Maybe ( )
es . On ( "IsActive" ) . Return ( true )
es . On ( "IsSearchEnabled" ) . Return ( true )
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . Platform ( ) . SearchEngine . ElasticsearchEngine = es
2020-03-13 10:33:18 -04:00
defer func ( ) {
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . Platform ( ) . SearchEngine . ElasticsearchEngine = nil
2020-03-13 10:33:18 -04:00
} ( )
2019-07-08 11:32:29 -04:00
2026-01-20 04:38:27 -05:00
results , allPostHaveMembership , err := th . App . SearchPostsForUser ( th . Context , searchTerm , th . BasicUser . Id , th . BasicTeam . Id , false , false , 0 , page , perPage )
2019-07-08 11:32:29 -04:00
assert . Nil ( t , err )
assert . Equal ( t , [ ] string {
posts [ 6 ] . Id ,
posts [ 5 ] . Id ,
posts [ 4 ] . Id ,
posts [ 3 ] . Id ,
posts [ 2 ] . Id ,
posts [ 1 ] . Id ,
posts [ 0 ] . Id ,
} , results . Order )
2026-01-20 04:38:27 -05:00
assert . True ( t , allPostHaveMembership )
2019-07-08 11:32:29 -04:00
es . AssertExpectations ( t )
} )
t . Run ( "should return nothing if ElasticSearch fails on later pages" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2019-07-08 11:32:29 -04:00
th , _ := setup ( t , true )
page := 1
2020-03-13 10:33:18 -04:00
es := & mocks . SearchEngineInterface { }
2019-07-08 11:32:29 -04:00
es . On ( "SearchPosts" , mock . Anything , mock . Anything , page , perPage ) . Return ( nil , nil , & model . AppError { } )
2020-03-13 10:33:18 -04:00
es . On ( "GetName" ) . Return ( "mock" )
es . On ( "Start" ) . Return ( nil ) . Maybe ( )
es . On ( "IsActive" ) . Return ( true )
es . On ( "IsSearchEnabled" ) . Return ( true )
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . Platform ( ) . SearchEngine . ElasticsearchEngine = es
2020-03-13 10:33:18 -04:00
defer func ( ) {
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . Platform ( ) . SearchEngine . ElasticsearchEngine = nil
2020-03-13 10:33:18 -04:00
} ( )
2019-07-08 11:32:29 -04:00
2026-01-20 04:38:27 -05:00
results , allPostHaveMembership , err := th . App . SearchPostsForUser ( th . Context , searchTerm , th . BasicUser . Id , th . BasicTeam . Id , false , false , 0 , page , perPage )
2019-07-08 11:32:29 -04:00
assert . Nil ( t , err )
assert . Equal ( t , [ ] string { } , results . Order )
2026-01-20 04:38:27 -05:00
assert . True ( t , allPostHaveMembership )
2019-07-08 11:32:29 -04:00
es . AssertExpectations ( t )
} )
2024-09-12 06:19:51 -04:00
t . Run ( "should return the same results if there is a tilde in the channel name" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2024-09-12 06:19:51 -04:00
th , _ := setup ( t , false )
page := 0
searchQueryWithPrefix := fmt . Sprintf ( "in:~%s %s" , th . BasicChannel . Name , searchTerm )
2026-01-20 04:38:27 -05:00
resultsWithPrefix , _ , err := th . App . SearchPostsForUser ( th . Context , searchQueryWithPrefix , th . BasicUser . Id , th . BasicTeam . Id , false , false , 0 , page , perPage )
2024-09-12 06:19:51 -04:00
assert . Nil ( t , err )
assert . Greater ( t , len ( resultsWithPrefix . PostList . Posts ) , 0 , "searching using a tilde in front of a channel should return results" )
searchQueryWithoutPrefix := fmt . Sprintf ( "in:%s %s" , th . BasicChannel . Name , searchTerm )
2026-01-20 04:38:27 -05:00
resultsWithoutPrefix , _ , err := th . App . SearchPostsForUser ( th . Context , searchQueryWithoutPrefix , th . BasicUser . Id , th . BasicTeam . Id , false , false , 0 , page , perPage )
2024-09-12 06:19:51 -04:00
assert . Nil ( t , err )
assert . Equal ( t , len ( resultsWithPrefix . Posts ) , len ( resultsWithoutPrefix . Posts ) , "searching using a tilde in front of a channel should return the same number of results" )
for k , v := range resultsWithPrefix . Posts {
assert . Equal ( t , v , resultsWithoutPrefix . Posts [ k ] , "post at %s was different" , k )
}
} )
t . Run ( "should return the same results if there is an 'at' in the user" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2024-09-12 06:19:51 -04:00
th , _ := setup ( t , false )
page := 0
searchQueryWithPrefix := fmt . Sprintf ( "from:@%s %s" , th . BasicUser . Username , searchTerm )
2026-01-20 04:38:27 -05:00
resultsWithPrefix , _ , err := th . App . SearchPostsForUser ( th . Context , searchQueryWithPrefix , th . BasicUser . Id , th . BasicTeam . Id , false , false , 0 , page , perPage )
2024-09-12 06:19:51 -04:00
assert . Nil ( t , err )
assert . Greater ( t , len ( resultsWithPrefix . PostList . Posts ) , 0 , "searching using a 'at' symbol in front of a channel should return results" )
searchQueryWithoutPrefix := fmt . Sprintf ( "from:@%s %s" , th . BasicUser . Username , searchTerm )
2026-01-20 04:38:27 -05:00
resultsWithoutPrefix , _ , err := th . App . SearchPostsForUser ( th . Context , searchQueryWithoutPrefix , th . BasicUser . Id , th . BasicTeam . Id , false , false , 0 , page , perPage )
2024-09-12 06:19:51 -04:00
assert . Nil ( t , err )
assert . Equal ( t , len ( resultsWithPrefix . Posts ) , len ( resultsWithoutPrefix . Posts ) , "searching using an 'at' symbol in front of a channel should return the same number of results" )
for k , v := range resultsWithPrefix . Posts {
assert . Equal ( t , v , resultsWithoutPrefix . Posts [ k ] , "post at %s was different" , k )
}
} )
2019-07-08 11:32:29 -04:00
}
2019-09-19 10:10:10 -04:00
func TestCountMentionsFromPost ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2019-09-19 10:10:10 -04:00
t . Run ( "should not count posts without mentions" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test2" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test3" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 0 , count )
} )
t . Run ( "should count keyword mentions" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2021-07-12 14:05:36 -04:00
user2 . NotifyProps [ model . MentionKeysNotifyProp ] = "apple"
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test2" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "apple" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
// post1 and post3 should mention the user
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 2 , count )
} )
t . Run ( "should count channel-wide mentions when enabled" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2021-07-12 14:05:36 -04:00
user2 . NotifyProps [ model . ChannelMentionsNotifyProp ] = "true"
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "@channel" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "@all" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
// post2 and post3 should mention the user
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 2 , count )
} )
t . Run ( "should not count channel-wide mentions when disabled for user" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2021-07-12 14:05:36 -04:00
user2 . NotifyProps [ model . ChannelMentionsNotifyProp ] = "false"
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "@channel" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "@all" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 0 , count )
} )
t . Run ( "should not count channel-wide mentions when disabled for channel" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2021-07-12 14:05:36 -04:00
user2 . NotifyProps [ model . ChannelMentionsNotifyProp ] = "true"
2019-09-19 10:10:10 -04:00
2022-07-14 05:01:29 -04:00
_ , err := th . App . UpdateChannelMemberNotifyProps ( th . Context , map [ string ] string {
2021-07-12 14:05:36 -04:00
model . IgnoreChannelMentionsNotifyProp : model . IgnoreChannelMentionsOn ,
2019-09-19 10:10:10 -04:00
} , channel . Id , user2 . Id )
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "@channel" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "@all" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 0 , count )
} )
t . Run ( "should count comment mentions when using COMMENTS_NOTIFY_ROOT" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2021-07-12 14:05:36 -04:00
user2 . NotifyProps [ model . CommentsNotifyProp ] = model . CommentsNotifyRoot
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
Message : "test" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
RootId : post1 . Id ,
Message : "test2" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
post3 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test3" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
RootId : post3 . Id ,
Message : "test4" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
RootId : post3 . Id ,
Message : "test5" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
// post2 should mention the user
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 1 , count )
} )
t . Run ( "should count comment mentions when using COMMENTS_NOTIFY_ANY" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2021-07-12 14:05:36 -04:00
user2 . NotifyProps [ model . CommentsNotifyProp ] = model . CommentsNotifyAny
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
Message : "test" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
RootId : post1 . Id ,
Message : "test2" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
post3 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test3" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
RootId : post3 . Id ,
Message : "test4" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
RootId : post3 . Id ,
Message : "test5" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
// post2 and post5 should mention the user
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 2 , count )
} )
t . Run ( "should count mentions caused by being added to the channel" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test" ,
2021-07-12 14:05:36 -04:00
Type : model . PostTypeAddToChannel ,
2022-07-05 02:46:50 -04:00
Props : map [ string ] any {
2021-07-12 14:05:36 -04:00
model . PostPropsAddedUserId : model . NewId ( ) ,
2019-09-19 10:10:10 -04:00
} ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test2" ,
2021-07-12 14:05:36 -04:00
Type : model . PostTypeAddToChannel ,
2022-07-05 02:46:50 -04:00
Props : map [ string ] any {
2021-07-12 14:05:36 -04:00
model . PostPropsAddedUserId : user2 . Id ,
2019-09-19 10:10:10 -04:00
} ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test3" ,
2021-07-12 14:05:36 -04:00
Type : model . PostTypeAddToChannel ,
2022-07-05 02:46:50 -04:00
Props : map [ string ] any {
2021-07-12 14:05:36 -04:00
model . PostPropsAddedUserId : user2 . Id ,
2019-09-19 10:10:10 -04:00
} ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
// should be mentioned by post2 and post3
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 2 , count )
} )
2019-12-02 09:30:08 -05:00
t . Run ( "should return the number of posts made by the other user for a direct channel" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2022-07-14 05:01:29 -04:00
channel , err := th . App . createDirectChannel ( th . Context , user1 . Id , user2 . Id )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-11-19 10:05:04 -05:00
require . Nil ( t , err )
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-12-02 09:30:08 -05:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test2" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-12-02 09:30:08 -05:00
require . Nil ( t , err )
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
2019-12-02 09:30:08 -05:00
assert . Equal ( t , 2 , count )
2022-11-23 14:08:21 -05:00
count , _ , _ , err = th . App . countMentionsFromPost ( th . Context , user1 , post1 )
2019-12-02 09:30:08 -05:00
assert . Nil ( t , err )
assert . Equal ( t , 0 , count )
2019-09-19 10:10:10 -04:00
} )
2023-10-24 09:27:30 -04:00
t . Run ( "should return the number of posts made by the other user for a group message" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2023-10-24 09:27:30 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
user3 := th . SystemAdminUser
2025-06-13 06:43:30 -04:00
channel , err := th . App . createGroupChannel ( th . Context , [ ] string { user1 . Id , user2 . Id , user3 . Id } , user1 . Id )
2023-10-24 09:27:30 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2023-10-24 09:27:30 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2023-10-24 09:27:30 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2023-10-24 09:27:30 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test2" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2023-10-24 09:27:30 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2023-10-24 09:27:30 -04:00
UserId : user3 . Id ,
ChannelId : channel . Id ,
Message : "test3" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2023-10-24 09:27:30 -04:00
require . Nil ( t , err )
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
assert . Nil ( t , err )
assert . Equal ( t , 3 , count )
count , _ , _ , err = th . App . countMentionsFromPost ( th . Context , user1 , post1 )
assert . Nil ( t , err )
assert . Equal ( t , 1 , count )
} )
2019-09-19 10:10:10 -04:00
t . Run ( "should not count mentions from the before the given post" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
_ , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
post2 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test2" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
// post1 and post3 should mention the user, but we only count post3
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post2 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 1 , count )
} )
t . Run ( "should not count mentions from the user's own posts" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
// post2 should mention the user
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 1 , count )
} )
t . Run ( "should include comments made before the given post when counting comment mentions" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2021-07-12 14:05:36 -04:00
user2 . NotifyProps [ model . CommentsNotifyProp ] = model . CommentsNotifyAny
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test1" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
RootId : post1 . Id ,
Message : "test2" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
post3 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test3" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
RootId : post1 . Id ,
Message : "test4" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
// post4 should mention the user
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post3 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 1 , count )
} )
2023-05-03 06:42:57 -04:00
t . Run ( "should not include comments made before the given post when rootPost is inaccessible" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2023-05-03 06:42:57 -04:00
2025-09-10 22:52:19 -04:00
// Create an Entry license with post history limits
license := model . NewTestLicenseSKU ( model . LicenseShortSkuMattermostEntry )
license . Limits = & model . LicenseLimits {
PostHistory : 10000 , // Set some post history limit to enable filtering
}
th . App . Srv ( ) . SetLicense ( license )
2023-05-03 06:42:57 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2023-05-03 06:42:57 -04:00
user2 . NotifyProps [ model . CommentsNotifyProp ] = model . CommentsNotifyAny
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2023-05-03 06:42:57 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test1" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2023-05-03 06:42:57 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2023-05-03 06:42:57 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
RootId : post1 . Id ,
Message : "test2" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2023-05-03 06:42:57 -04:00
require . Nil ( t , err )
time . Sleep ( time . Millisecond * 2 )
2026-01-20 04:38:27 -05:00
post3 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2023-05-03 06:42:57 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test3" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2023-05-03 06:42:57 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2023-05-03 06:42:57 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
RootId : post1 . Id ,
Message : "test4" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2023-05-03 06:42:57 -04:00
require . Nil ( t , err )
// Make posts created before post3 inaccessible
e := th . App . Srv ( ) . Store ( ) . System ( ) . SaveOrUpdate ( & model . System {
Name : model . SystemLastAccessiblePostTime ,
Value : strconv . FormatInt ( post3 . CreateAt , 10 ) ,
} )
require . NoError ( t , e )
// post4 should mention the user, but since post2 is inaccessible due to the cloud plan's limit,
// post4 does not notify the user.
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post3 )
assert . Nil ( t , err )
assert . Zero ( t , count )
} )
2019-09-19 10:10:10 -04:00
t . Run ( "should count mentions from the user's webhook posts" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test1" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2022-07-05 02:46:50 -04:00
Props : map [ string ] any {
2025-03-20 07:53:50 -04:00
model . PostPropsFromWebhook : "true" ,
2019-09-19 10:10:10 -04:00
} ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
// post3 should mention the user
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , 1 , count )
} )
t . Run ( "should count multiple pages of mentions" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2019-09-19 10:10:10 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2019-09-19 10:10:10 -04:00
numPosts := 215
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
for i := 0 ; i < numPosts - 1 ; i ++ {
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2019-09-19 10:10:10 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2019-09-19 10:10:10 -04:00
require . Nil ( t , err )
}
// Every post should mention the user
2022-11-23 14:08:21 -05:00
count , _ , _ , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
2019-09-19 10:10:10 -04:00
assert . Nil ( t , err )
assert . Equal ( t , numPosts , count )
} )
2022-11-23 14:08:21 -05:00
t . Run ( "should count urgent mentions" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2022-11-23 14:08:21 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . PostPriority = true
} )
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2022-11-23 14:08:21 -05:00
user2 . NotifyProps [ model . MentionKeysNotifyProp ] = "apple"
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2022-11-23 14:08:21 -05:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
Metadata : & model . PostMetadata {
Priority : & model . PostPriority {
2024-08-05 23:45:00 -04:00
Priority : model . NewPointer ( model . PostPriorityUrgent ) ,
2022-11-23 14:08:21 -05:00
} ,
} ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2022-11-23 14:08:21 -05:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2022-11-23 14:08:21 -05:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2022-11-23 14:08:21 -05:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2022-11-23 14:08:21 -05:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "apple" ,
Metadata : & model . PostMetadata {
Priority : & model . PostPriority {
2024-08-05 23:45:00 -04:00
Priority : model . NewPointer ( model . PostPriorityUrgent ) ,
2022-11-23 14:08:21 -05:00
} ,
} ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2022-11-23 14:08:21 -05:00
require . Nil ( t , err )
// all posts mention the user but only post1, post3 are urgent
_ , _ , count , err := th . App . countMentionsFromPost ( th . Context , user2 , post1 )
assert . Nil ( t , err )
assert . Equal ( t , 2 , count )
} )
2019-09-19 10:10:10 -04:00
}
2020-05-05 06:00:59 -04:00
func TestFillInPostProps ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2020-05-05 06:00:59 -04:00
t . Run ( "should not add disable group highlight to post props for user with group mention permissions" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( "ldap" ) )
2020-05-05 06:00:59 -04:00
user1 := th . BasicUser
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
2020-05-05 06:00:59 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2020-05-05 06:00:59 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test123123 @group1 @group2 blah blah blah" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2020-05-05 06:00:59 -04:00
require . Nil ( t , err )
2022-07-14 05:01:29 -04:00
err = th . App . FillInPostProps ( th . Context , post1 , channel )
2020-05-05 06:00:59 -04:00
assert . Nil ( t , err )
assert . Equal ( t , post1 . Props , model . StringInterface { } )
} )
t . Run ( "should not add disable group highlight to post props for app without license" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2020-05-05 06:00:59 -04:00
id := model . NewId ( )
guest := & model . User {
Email : "success+" + id + "@simulator.amazonses.com" ,
Username : "un_" + id ,
Nickname : "nn_" + id ,
2026-04-08 15:49:43 -04:00
Password : model . NewTestPassword ( ) ,
2020-05-05 06:00:59 -04:00
EmailVerified : true ,
}
2021-05-11 06:00:44 -04:00
guest , err := th . App . CreateGuest ( th . Context , guest )
2020-05-05 06:00:59 -04:00
require . Nil ( t , err )
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , guest , th . BasicTeam )
2020-05-05 06:00:59 -04:00
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , guest , channel )
2020-05-05 06:00:59 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2020-05-05 06:00:59 -04:00
UserId : guest . Id ,
ChannelId : channel . Id ,
Message : "test123123 @group1 @group2 blah blah blah" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2020-05-05 06:00:59 -04:00
require . Nil ( t , err )
2022-07-14 05:01:29 -04:00
err = th . App . FillInPostProps ( th . Context , post1 , channel )
2020-05-05 06:00:59 -04:00
assert . Nil ( t , err )
assert . Equal ( t , post1 . Props , model . StringInterface { } )
} )
t . Run ( "should add disable group highlight to post props for guest user" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( "ldap" ) )
2020-05-05 06:00:59 -04:00
id := model . NewId ( )
guest := & model . User {
Email : "success+" + id + "@simulator.amazonses.com" ,
Username : "un_" + id ,
Nickname : "nn_" + id ,
2026-04-08 15:49:43 -04:00
Password : model . NewTestPassword ( ) ,
2020-05-05 06:00:59 -04:00
EmailVerified : true ,
}
2021-05-11 06:00:44 -04:00
guest , err := th . App . CreateGuest ( th . Context , guest )
2020-05-05 06:00:59 -04:00
require . Nil ( t , err )
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , guest , th . BasicTeam )
2020-05-05 06:00:59 -04:00
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , guest , channel )
2020-05-05 06:00:59 -04:00
2026-01-20 04:38:27 -05:00
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2020-05-05 06:00:59 -04:00
UserId : guest . Id ,
ChannelId : channel . Id ,
Message : "test123123 @group1 @group2 blah blah blah" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2020-05-05 06:00:59 -04:00
require . Nil ( t , err )
2022-07-14 05:01:29 -04:00
err = th . App . FillInPostProps ( th . Context , post1 , channel )
2020-05-05 06:00:59 -04:00
assert . Nil ( t , err )
assert . Equal ( t , post1 . Props , model . StringInterface { "disable_group_highlight" : true } )
} )
2025-11-10 16:32:18 -05:00
t . Run ( "should set AI-generated username when user ID is post creator" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-11-10 16:32:18 -05:00
user1 := th . BasicUser
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
2025-11-10 16:32:18 -05:00
post1 := & model . Post {
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test post" ,
}
post1 . AddProp ( model . PostPropsAIGeneratedByUserID , user1 . Id )
err := th . App . FillInPostProps ( th . Context , post1 , channel )
assert . Nil ( t , err )
assert . Equal ( t , user1 . Id , post1 . GetProp ( model . PostPropsAIGeneratedByUserID ) )
assert . Equal ( t , user1 . Username , post1 . GetProp ( model . PostPropsAIGeneratedByUsername ) )
} )
t . Run ( "should set AI-generated username when user ID is a bot" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-11-10 16:32:18 -05:00
user1 := th . BasicUser
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
2025-11-10 16:32:18 -05:00
// Create a bot
bot , appErr := th . App . CreateBot ( th . Context , & model . Bot {
Username : "testbot" ,
Description : "test bot" ,
OwnerId : user1 . Id ,
} )
require . Nil ( t , appErr )
post1 := & model . Post {
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test post generated by bot" ,
}
post1 . AddProp ( model . PostPropsAIGeneratedByUserID , bot . UserId )
err := th . App . FillInPostProps ( th . Context , post1 , channel )
assert . Nil ( t , err )
assert . Equal ( t , bot . UserId , post1 . GetProp ( model . PostPropsAIGeneratedByUserID ) )
assert . Equal ( t , bot . Username , post1 . GetProp ( model . PostPropsAIGeneratedByUsername ) )
} )
t . Run ( "should return error when user ID is a different non-bot user" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-11-10 16:32:18 -05:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
2025-11-10 16:32:18 -05:00
post1 := & model . Post {
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test post" ,
}
// Try to set AI-generated user ID to a different user (not post creator, not bot)
post1 . AddProp ( model . PostPropsAIGeneratedByUserID , user2 . Id )
err := th . App . FillInPostProps ( th . Context , post1 , channel )
// Should return an error since user2 is neither the post creator nor a bot
assert . NotNil ( t , err )
assert . Equal ( t , "FillInPostProps" , err . Where )
assert . Equal ( t , http . StatusBadRequest , err . StatusCode )
} )
t . Run ( "should remove AI-generated prop when user ID does not exist" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-11-10 16:32:18 -05:00
user1 := th . BasicUser
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
2025-11-10 16:32:18 -05:00
post1 := & model . Post {
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "test post" ,
}
// Set AI-generated user ID to a non-existent user
post1 . AddProp ( model . PostPropsAIGeneratedByUserID , model . NewId ( ) )
err := th . App . FillInPostProps ( th . Context , post1 , channel )
assert . Nil ( t , err )
// The property should be removed since the user doesn't exist
assert . Nil ( t , post1 . GetProp ( model . PostPropsAIGeneratedByUserID ) )
assert . Nil ( t , post1 . GetProp ( model . PostPropsAIGeneratedByUsername ) )
} )
2025-12-09 08:46:45 -05:00
t . Run ( "should not populate channel mentions for channels in teams where the user is not a member" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
user1 := th . BasicUser
user2 := th . BasicUser2
team2 := th . CreateTeam ( t )
th . LinkUserToTeam ( t , user2 , team2 )
// Create a channel in team2 which user1 is not a member of
channel2 , err := th . App . CreateChannel ( th . Context , & model . Channel {
DisplayName : "Channel in Team 2" ,
Name : "channel-in-team-2" ,
Type : model . ChannelTypeOpen ,
TeamId : team2 . Id ,
CreatorId : user2 . Id ,
} , false )
require . Nil ( t , err )
dmChannelBetweenUser1AndUser2 := th . CreateDmChannel ( t , user2 )
2026-02-16 15:31:32 -05:00
th . Context . Session ( ) . UserId = user1 . Id
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2025-12-09 08:46:45 -05:00
UserId : user1 . Id ,
ChannelId : dmChannelBetweenUser1AndUser2 . Id ,
Message : "Testing out i should not be able to mention channel2 from team2? ~" + channel2 . Name ,
} , dmChannelBetweenUser1AndUser2 , model . CreatePostFlags { SetOnline : true } )
require . Nil ( t , err )
err = th . App . FillInPostProps ( th . Context , post , dmChannelBetweenUser1AndUser2 )
require . Nil ( t , err )
mentions := post . GetProp ( model . PostPropsChannelMentions )
require . Nil ( t , mentions )
} )
t . Run ( "should populate channel mentions for channels in teams where the user is a member" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
user1 := th . BasicUser
user2 := th . BasicUser2
channel := th . CreateChannel ( t , th . BasicTeam )
dmChannel := th . CreateDmChannel ( t , user2 )
2026-02-16 15:31:32 -05:00
th . Context . Session ( ) . UserId = user1 . Id
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2025-12-09 08:46:45 -05:00
UserId : user1 . Id ,
ChannelId : dmChannel . Id ,
Message : "Check out ~" + channel . Name ,
} , dmChannel , model . CreatePostFlags { SetOnline : true } )
require . Nil ( t , err )
mentions := post . GetProp ( model . PostPropsChannelMentions )
require . NotNil ( t , mentions )
mentionsMap , ok := mentions . ( map [ string ] any )
require . True ( t , ok )
require . Contains ( t , mentionsMap , channel . Name )
} )
2020-05-05 06:00:59 -04:00
}
2020-10-15 11:01:16 -04:00
func TestThreadMembership ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2020-10-15 11:01:16 -04:00
t . Run ( "should update memberships for conversation participants" , func ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2022-02-01 05:51:04 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . ThreadAutoFollow = true
* cfg . ServiceSettings . CollapsedThreads = model . CollapsedThreadsDefaultOn
} )
2020-10-15 11:01:16 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2020-10-15 11:01:16 -04:00
2026-01-20 04:38:27 -05:00
postRoot , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2020-10-15 11:01:16 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "root post" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2020-10-15 11:01:16 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2020-10-15 11:01:16 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
RootId : postRoot . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2020-10-15 11:01:16 -04:00
require . Nil ( t , err )
// first user should now be part of the thread since they replied to a post
2020-12-06 03:02:53 -05:00
memberships , err2 := th . App . GetThreadMembershipsForUser ( user1 . Id , th . BasicTeam . Id )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err2 )
2020-10-15 11:01:16 -04:00
require . Len ( t , memberships , 1 )
// second user should also be part of a thread since they were mentioned
2020-12-06 03:02:53 -05:00
memberships , err2 = th . App . GetThreadMembershipsForUser ( user2 . Id , th . BasicTeam . Id )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err2 )
2020-10-15 11:01:16 -04:00
require . Len ( t , memberships , 1 )
2026-01-20 04:38:27 -05:00
post2 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2020-10-15 11:01:16 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
Message : "second post" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2020-10-15 11:01:16 -04:00
require . Nil ( t , err )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
2020-10-15 11:01:16 -04:00
UserId : user2 . Id ,
ChannelId : channel . Id ,
RootId : post2 . Id ,
Message : fmt . Sprintf ( "@%s" , user1 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2020-10-15 11:01:16 -04:00
require . Nil ( t , err )
// first user should now be part of two threads
2020-12-06 03:02:53 -05:00
memberships , err2 = th . App . GetThreadMembershipsForUser ( user1 . Id , th . BasicTeam . Id )
2021-02-16 06:00:01 -05:00
require . NoError ( t , err2 )
2020-10-15 11:01:16 -04:00
require . Len ( t , memberships , 2 )
} )
}
2021-01-14 06:46:27 -05:00
2021-04-21 02:48:30 -04:00
func TestFollowThreadSkipsParticipants ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-04-21 02:48:30 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . ThreadAutoFollow = true
2021-07-12 14:05:36 -04:00
* cfg . ServiceSettings . CollapsedThreads = model . CollapsedThreadsDefaultOn
2021-04-21 02:48:30 -04:00
} )
channel := th . BasicChannel
user := th . BasicUser
user2 := th . BasicUser2
sysadmin := th . SystemAdminUser
2021-05-11 06:00:44 -04:00
appErr := th . App . JoinChannel ( th . Context , channel , user . Id )
2021-04-21 02:48:30 -04:00
require . Nil ( t , appErr )
2021-05-11 06:00:44 -04:00
appErr = th . App . JoinChannel ( th . Context , channel , user2 . Id )
2021-04-21 02:48:30 -04:00
require . Nil ( t , appErr )
2021-05-11 06:00:44 -04:00
_ , appErr = th . App . JoinUserToTeam ( th . Context , th . BasicTeam , sysadmin , sysadmin . Id )
2021-04-21 02:48:30 -04:00
require . Nil ( t , appErr )
2021-05-11 06:00:44 -04:00
appErr = th . App . JoinChannel ( th . Context , channel , sysadmin . Id )
2021-04-21 02:48:30 -04:00
require . Nil ( t , appErr )
2026-01-20 04:38:27 -05:00
p1 , _ , appErr := th . App . CreatePost ( th . Context , & model . Post { UserId : user . Id , ChannelId : channel . Id , Message : "Hi @" + sysadmin . Username } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post { RootId : p1 . Id , UserId : user . Id , ChannelId : channel . Id , Message : "Hola" } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-04-21 02:48:30 -04:00
2025-04-29 02:50:47 -04:00
threadMembership , appErr := th . App . GetThreadMembershipForUser ( user . Id , p1 . Id )
require . Nil ( t , appErr )
2025-09-18 10:14:24 -04:00
thread , appErr := th . App . GetThreadForUser ( th . Context , threadMembership , false )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-04-21 02:48:30 -04:00
require . Len ( t , thread . Participants , 1 ) // length should be 1, the original poster, since sysadmin was just mentioned but didn't post
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post { RootId : p1 . Id , UserId : sysadmin . Id , ChannelId : channel . Id , Message : "sysadmin reply" } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-04-21 02:48:30 -04:00
2025-04-29 02:50:47 -04:00
threadMembership , appErr = th . App . GetThreadMembershipForUser ( user . Id , p1 . Id )
require . Nil ( t , appErr )
2025-09-18 10:14:24 -04:00
thread , appErr = th . App . GetThreadForUser ( th . Context , threadMembership , false )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-04-21 02:48:30 -04:00
require . Len ( t , thread . Participants , 2 ) // length should be 2, the original poster and sysadmin, since sysadmin participated now
// another user follows the thread
2025-04-29 02:50:47 -04:00
appErr = th . App . UpdateThreadFollowForUser ( user2 . Id , th . BasicTeam . Id , p1 . Id , true )
require . Nil ( t , appErr )
2021-04-21 02:48:30 -04:00
2025-04-29 02:50:47 -04:00
threadMembership , appErr = th . App . GetThreadMembershipForUser ( user2 . Id , p1 . Id )
require . Nil ( t , appErr )
2025-09-18 10:14:24 -04:00
thread , appErr = th . App . GetThreadForUser ( th . Context , threadMembership , false )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-04-21 02:48:30 -04:00
require . Len ( t , thread . Participants , 2 ) // length should be 2, since follow shouldn't update participant list, only user1 and sysadmin are participants
for _ , p := range thread . Participants {
require . True ( t , p . Id == sysadmin . Id || p . Id == user . Id )
}
2023-01-03 11:00:51 -05:00
2023-01-09 11:55:29 -05:00
oldID := threadMembership . PostId
2023-01-03 11:00:51 -05:00
threadMembership . PostId = "notfound"
2025-09-18 10:14:24 -04:00
_ , appErr = th . App . GetThreadForUser ( th . Context , threadMembership , false )
2025-04-29 02:50:47 -04:00
require . NotNil ( t , appErr )
assert . Equal ( t , http . StatusNotFound , appErr . StatusCode )
2023-01-09 11:55:29 -05:00
threadMembership . Following = false
threadMembership . PostId = oldID
2025-09-18 10:14:24 -04:00
_ , appErr = th . App . GetThreadForUser ( th . Context , threadMembership , false )
2025-04-29 02:50:47 -04:00
require . NotNil ( t , appErr )
assert . Equal ( t , http . StatusNotFound , appErr . StatusCode )
2021-04-21 02:48:30 -04:00
}
2021-04-15 10:32:01 -04:00
func TestAutofollowBasedOnRootPost ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-04-15 10:32:01 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . ThreadAutoFollow = true
2021-07-12 14:05:36 -04:00
* cfg . ServiceSettings . CollapsedThreads = model . CollapsedThreadsDefaultOn
2021-04-15 10:32:01 -04:00
} )
channel := th . BasicChannel
user := th . BasicUser
user2 := th . BasicUser2
2021-05-11 06:00:44 -04:00
appErr := th . App . JoinChannel ( th . Context , channel , user . Id )
2021-04-15 10:32:01 -04:00
require . Nil ( t , appErr )
2021-05-11 06:00:44 -04:00
appErr = th . App . JoinChannel ( th . Context , channel , user2 . Id )
2021-04-15 10:32:01 -04:00
require . Nil ( t , appErr )
2026-01-20 04:38:27 -05:00
p1 , _ , appErr := th . App . CreatePost ( th . Context , & model . Post { UserId : user . Id , ChannelId : channel . Id , Message : "Hi @" + user2 . Username } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
m , err := th . App . GetThreadMembershipsForUser ( user2 . Id , th . BasicTeam . Id )
require . NoError ( t , err )
2021-04-15 10:32:01 -04:00
require . Len ( t , m , 0 )
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post { RootId : p1 . Id , UserId : user . Id , ChannelId : channel . Id , Message : "Hola" } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
m , err = th . App . GetThreadMembershipsForUser ( user2 . Id , th . BasicTeam . Id )
require . NoError ( t , err )
2021-04-15 10:32:01 -04:00
require . Len ( t , m , 1 )
}
2021-04-21 02:48:59 -04:00
func TestViewChannelShouldNotUpdateThreads ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-04-21 02:48:59 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . ThreadAutoFollow = true
2021-07-12 14:05:36 -04:00
* cfg . ServiceSettings . CollapsedThreads = model . CollapsedThreadsDefaultOn
2021-04-21 02:48:59 -04:00
} )
channel := th . BasicChannel
user := th . BasicUser
user2 := th . BasicUser2
2021-05-11 06:00:44 -04:00
appErr := th . App . JoinChannel ( th . Context , channel , user . Id )
2021-04-21 02:48:59 -04:00
require . Nil ( t , appErr )
2021-05-11 06:00:44 -04:00
appErr = th . App . JoinChannel ( th . Context , channel , user2 . Id )
2021-04-21 02:48:59 -04:00
require . Nil ( t , appErr )
2026-01-20 04:38:27 -05:00
p1 , _ , appErr := th . App . CreatePost ( th . Context , & model . Post { UserId : user . Id , ChannelId : channel . Id , Message : "Hi @" + user2 . Username } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post { RootId : p1 . Id , UserId : user . Id , ChannelId : channel . Id , Message : "Hola" } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
m , err := th . App . GetThreadMembershipsForUser ( user2 . Id , th . BasicTeam . Id )
require . NoError ( t , err )
2021-04-21 02:48:59 -04:00
2025-04-29 02:50:47 -04:00
_ , appErr = th . App . ViewChannel ( th . Context , & model . ChannelView {
2021-04-21 02:48:59 -04:00
ChannelId : channel . Id ,
PrevChannelId : "" ,
2021-05-26 11:10:25 -04:00
} , user2 . Id , "" , true )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-04-21 02:48:59 -04:00
2025-04-29 02:50:47 -04:00
m1 , err := th . App . GetThreadMembershipsForUser ( user2 . Id , th . BasicTeam . Id )
require . NoError ( t , err )
2021-04-21 02:48:59 -04:00
require . Equal ( t , m [ 0 ] . LastViewed , m1 [ 0 ] . LastViewed ) // opening the channel shouldn't update threads
}
2021-01-14 06:46:27 -05:00
func TestCollapsedThreadFetch ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-03-16 09:30:52 -04:00
2021-01-14 06:46:27 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . ThreadAutoFollow = true
2021-07-12 14:05:36 -04:00
* cfg . ServiceSettings . CollapsedThreads = model . CollapsedThreadsDefaultOn
2021-01-14 06:46:27 -05:00
} )
user1 := th . BasicUser
user2 := th . BasicUser2
t . Run ( "should only return root posts, enriched" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2025-04-29 02:50:47 -04:00
defer func ( ) {
appErr := th . App . DeleteChannel ( th . Context , channel , user1 . Id )
require . Nil ( t , appErr )
} ( )
2021-01-14 06:46:27 -05:00
2026-01-20 04:38:27 -05:00
postRoot , _ , appErr := th . App . CreatePost ( th . Context , & model . Post {
2021-01-14 06:46:27 -05:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "root post" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-01-14 06:46:27 -05:00
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post {
2021-01-14 06:46:27 -05:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
RootId : postRoot . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
thread , err := th . App . Srv ( ) . Store ( ) . Thread ( ) . Get ( postRoot . Id )
require . NoError ( t , err )
2021-04-21 02:48:30 -04:00
require . Len ( t , thread . Participants , 1 )
2025-04-29 02:50:47 -04:00
_ , appErr = th . App . MarkChannelAsUnreadFromPost ( th . Context , postRoot . Id , user1 . Id , true )
require . Nil ( t , appErr )
l , appErr := th . App . GetPostsForChannelAroundLastUnread ( th . Context , channel . Id , user1 . Id , 10 , 10 , true , true , false )
require . Nil ( t , appErr )
2021-01-14 06:46:27 -05:00
require . Len ( t , l . Order , 1 )
require . EqualValues ( t , 1 , l . Posts [ postRoot . Id ] . ReplyCount )
2021-04-21 02:48:30 -04:00
require . EqualValues ( t , [ ] string { user1 . Id } , [ ] string { l . Posts [ postRoot . Id ] . Participants [ 0 ] . Id } )
2021-01-14 06:46:27 -05:00
require . Empty ( t , l . Posts [ postRoot . Id ] . Participants [ 0 ] . Email )
require . NotZero ( t , l . Posts [ postRoot . Id ] . LastReplyAt )
2021-06-14 08:25:44 -04:00
require . True ( t , * l . Posts [ postRoot . Id ] . IsFollowing )
2021-01-14 06:46:27 -05:00
// try extended fetch
2025-04-29 02:50:47 -04:00
l , appErr = th . App . GetPostsForChannelAroundLastUnread ( th . Context , channel . Id , user1 . Id , 10 , 10 , true , true , true )
require . Nil ( t , appErr )
2021-01-14 06:46:27 -05:00
require . Len ( t , l . Order , 1 )
require . NotEmpty ( t , l . Posts [ postRoot . Id ] . Participants [ 0 ] . Email )
} )
2021-03-16 09:30:52 -04:00
t . Run ( "Should not panic on unexpected db error" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
th . AddUserToChannel ( t , user2 , channel )
2025-04-29 02:50:47 -04:00
defer func ( ) {
appErr := th . App . DeleteChannel ( th . Context , channel , user1 . Id )
require . Nil ( t , appErr )
} ( )
2021-03-16 09:30:52 -04:00
2026-01-20 04:38:27 -05:00
postRoot , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2021-03-16 09:30:52 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "root post" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2021-03-16 09:30:52 -04:00
require . Nil ( t , err )
// we introduce a race to trigger an unexpected error from the db side.
var wg sync . WaitGroup
2026-03-27 16:11:52 -04:00
wg . Go ( func ( ) {
2025-04-29 02:50:47 -04:00
err := th . Server . Store ( ) . Post ( ) . PermanentDeleteByUser ( th . Context , user1 . Id )
require . NoError ( t , err )
2026-03-27 16:11:52 -04:00
} )
2021-03-16 09:30:52 -04:00
require . NotPanics ( t , func ( ) {
2025-04-29 02:50:47 -04:00
// We're only testing that this doesn't panic, not checking the error
// #nosec G104 - purposely not checking error as we're in a NotPanics block
2026-01-20 04:38:27 -05:00
_ , _ , _ = th . App . CreatePost ( th . Context , & model . Post {
2021-03-16 09:30:52 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
RootId : postRoot . Id ,
Message : fmt . Sprintf ( "@%s" , user2 . Username ) ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2021-03-16 09:30:52 -04:00
} )
wg . Wait ( )
} )
2022-05-26 08:41:06 -04:00
t . Run ( "should sanitize participant data" , func ( t * testing . T ) {
id := model . NewId ( )
2025-04-29 02:50:47 -04:00
user3 , appErr := th . App . CreateUser ( th . Context , & model . User {
2022-05-26 08:41:06 -04:00
Email : "success+" + id + "@simulator.amazonses.com" ,
Username : "un_" + id ,
Nickname : "nn_" + id ,
2024-10-29 08:29:53 -04:00
AuthData : model . NewPointer ( "bobbytables" ) ,
2022-05-26 08:41:06 -04:00
AuthService : "saml" ,
EmailVerified : true ,
} )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
defer func ( ) {
appErr = th . App . PermanentDeleteUser ( th . Context , user3 )
require . Nil ( t , appErr )
} ( )
2022-05-26 08:41:06 -04:00
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam )
2025-04-29 02:50:47 -04:00
defer func ( ) {
appErr = th . App . DeleteChannel ( th . Context , channel , user1 . Id )
require . Nil ( t , appErr )
} ( )
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , user3 , th . BasicTeam )
th . AddUserToChannel ( t , user3 , channel )
2022-05-26 08:41:06 -04:00
2026-01-20 04:38:27 -05:00
postRoot , _ , appErr := th . App . CreatePost ( th . Context , & model . Post {
2022-05-26 08:41:06 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
Message : "root post" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2022-05-26 08:41:06 -04:00
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post {
2022-05-26 08:41:06 -04:00
UserId : user3 . Id ,
ChannelId : channel . Id ,
RootId : postRoot . Id ,
Message : "reply" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
thread , err := th . App . Srv ( ) . Store ( ) . Thread ( ) . Get ( postRoot . Id )
require . NoError ( t , err )
2022-05-26 08:41:06 -04:00
require . Len ( t , thread . Participants , 1 )
// extended fetch posts page
2025-09-18 10:14:24 -04:00
l , appErr := th . App . GetPostsPage ( th . Context , model . GetPostsOptions {
2022-05-26 08:41:06 -04:00
UserId : user1 . Id ,
ChannelId : channel . Id ,
PerPage : int ( 10 ) ,
SkipFetchThreads : false ,
CollapsedThreads : true ,
CollapsedThreadsExtended : true ,
} )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2022-05-26 08:41:06 -04:00
require . Len ( t , l . Order , 1 )
require . NotEmpty ( t , l . Posts [ postRoot . Id ] . Participants [ 0 ] . Email )
require . Empty ( t , l . Posts [ postRoot . Id ] . Participants [ 0 ] . AuthData )
2025-04-29 02:50:47 -04:00
_ , appErr = th . App . MarkChannelAsUnreadFromPost ( th . Context , postRoot . Id , user1 . Id , true )
require . Nil ( t , appErr )
2022-05-26 08:41:06 -04:00
// extended fetch posts around
2025-04-29 02:50:47 -04:00
l , appErr = th . App . GetPostsForChannelAroundLastUnread ( th . Context , channel . Id , user1 . Id , 10 , 10 , true , true , true )
require . Nil ( t , appErr )
2022-05-26 08:41:06 -04:00
require . Len ( t , l . Order , 1 )
require . NotEmpty ( t , l . Posts [ postRoot . Id ] . Participants [ 0 ] . Email )
require . Empty ( t , l . Posts [ postRoot . Id ] . Participants [ 0 ] . AuthData )
// extended fetch post thread
opts := model . GetPostsOptions {
SkipFetchThreads : false ,
CollapsedThreads : true ,
CollapsedThreadsExtended : true ,
}
2025-09-18 10:14:24 -04:00
l , appErr = th . App . GetPostThread ( th . Context , postRoot . Id , opts , user1 . Id )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2022-05-26 08:41:06 -04:00
require . Len ( t , l . Order , 2 )
require . NotEmpty ( t , l . Posts [ postRoot . Id ] . Participants [ 0 ] . Email )
require . Empty ( t , l . Posts [ postRoot . Id ] . Participants [ 0 ] . AuthData )
} )
2021-01-14 06:46:27 -05:00
}
2021-03-12 12:37:30 -05:00
2021-04-01 13:44:56 -04:00
func TestSharedChannelSyncForPostActions ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2021-04-01 13:44:56 -04:00
t . Run ( "creating a post in a shared channel performs a content sync when sync service is running on that node" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th := setupSharedChannels ( t ) . InitBasic ( t )
2021-04-01 13:44:56 -04:00
2024-02-09 10:47:12 -05:00
sharedChannelService := NewMockSharedChannelService ( th . Server . GetSharedChannelSyncService ( ) )
th . Server . SetSharedChannelSyncService ( sharedChannelService )
2021-04-01 13:44:56 -04:00
testCluster := & testlib . FakeClusterInterface { }
2022-10-06 04:04:21 -04:00
th . Server . Platform ( ) . SetCluster ( testCluster )
2021-04-01 13:44:56 -04:00
user := th . BasicUser
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam , WithShared ( true ) )
2021-04-01 13:44:56 -04:00
2026-01-20 04:38:27 -05:00
_ , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2021-04-01 13:44:56 -04:00
UserId : user . Id ,
ChannelId : channel . Id ,
Message : "Hello folks" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2021-04-01 13:44:56 -04:00
require . Nil ( t , err , "Creating a post should not error" )
2024-02-09 10:47:12 -05:00
require . Len ( t , sharedChannelService . channelNotifications , 1 )
assert . Equal ( t , channel . Id , sharedChannelService . channelNotifications [ 0 ] )
2021-04-01 13:44:56 -04:00
} )
t . Run ( "updating a post in a shared channel performs a content sync when sync service is running on that node" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th := setupSharedChannels ( t ) . InitBasic ( t )
2021-04-01 13:44:56 -04:00
2024-02-09 10:47:12 -05:00
sharedChannelService := NewMockSharedChannelService ( th . Server . GetSharedChannelSyncService ( ) )
th . Server . SetSharedChannelSyncService ( sharedChannelService )
2021-04-01 13:44:56 -04:00
testCluster := & testlib . FakeClusterInterface { }
2022-10-06 04:04:21 -04:00
th . Server . Platform ( ) . SetCluster ( testCluster )
2021-04-01 13:44:56 -04:00
user := th . BasicUser
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam , WithShared ( true ) )
2021-04-01 13:44:56 -04:00
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2021-04-01 13:44:56 -04:00
UserId : user . Id ,
ChannelId : channel . Id ,
Message : "Hello folks" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2021-04-01 13:44:56 -04:00
require . Nil ( t , err , "Creating a post should not error" )
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . UpdatePost ( th . Context , post , & model . UpdatePostOptions { SafeUpdate : true } )
2021-04-01 13:44:56 -04:00
require . Nil ( t , err , "Updating a post should not error" )
2024-02-09 10:47:12 -05:00
require . Len ( t , sharedChannelService . channelNotifications , 2 )
assert . Equal ( t , channel . Id , sharedChannelService . channelNotifications [ 0 ] )
assert . Equal ( t , channel . Id , sharedChannelService . channelNotifications [ 1 ] )
2021-04-01 13:44:56 -04:00
} )
t . Run ( "deleting a post in a shared channel performs a content sync when sync service is running on that node" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th := setupSharedChannels ( t ) . InitBasic ( t )
2021-04-01 13:44:56 -04:00
2024-02-09 10:47:12 -05:00
sharedChannelService := NewMockSharedChannelService ( th . Server . GetSharedChannelSyncService ( ) )
th . Server . SetSharedChannelSyncService ( sharedChannelService )
2021-04-01 13:44:56 -04:00
testCluster := & testlib . FakeClusterInterface { }
2022-10-06 04:04:21 -04:00
th . Server . Platform ( ) . SetCluster ( testCluster )
2021-04-01 13:44:56 -04:00
user := th . BasicUser
2025-11-12 07:00:51 -05:00
channel := th . CreateChannel ( t , th . BasicTeam , WithShared ( true ) )
2021-04-01 13:44:56 -04:00
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePost ( th . Context , & model . Post {
2021-04-01 13:44:56 -04:00
UserId : user . Id ,
ChannelId : channel . Id ,
Message : "Hello folks" ,
2024-10-22 10:00:26 -04:00
} , channel , model . CreatePostFlags { SetOnline : true } )
2021-04-01 13:44:56 -04:00
require . Nil ( t , err , "Creating a post should not error" )
2022-07-14 05:01:29 -04:00
_ , err = th . App . DeletePost ( th . Context , post . Id , user . Id )
2021-04-01 13:44:56 -04:00
require . Nil ( t , err , "Deleting a post should not error" )
// one creation and two deletes
2024-02-09 10:47:12 -05:00
require . Len ( t , sharedChannelService . channelNotifications , 3 )
assert . Equal ( t , channel . Id , sharedChannelService . channelNotifications [ 0 ] )
assert . Equal ( t , channel . Id , sharedChannelService . channelNotifications [ 1 ] )
assert . Equal ( t , channel . Id , sharedChannelService . channelNotifications [ 2 ] )
2021-04-01 13:44:56 -04:00
} )
}
2021-07-16 10:04:22 -04:00
func TestAutofollowOnPostingAfterUnfollow ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-07-16 10:04:22 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . ThreadAutoFollow = true
2021-07-12 14:05:36 -04:00
* cfg . ServiceSettings . CollapsedThreads = model . CollapsedThreadsDefaultOn
2021-07-16 10:04:22 -04:00
} )
channel := th . BasicChannel
user := th . BasicUser
user2 := th . BasicUser2
appErr := th . App . JoinChannel ( th . Context , channel , user . Id )
require . Nil ( t , appErr )
appErr = th . App . JoinChannel ( th . Context , channel , user2 . Id )
require . Nil ( t , appErr )
2026-01-20 04:38:27 -05:00
p1 , _ , appErr := th . App . CreatePost ( th . Context , & model . Post { UserId : user . Id , ChannelId : channel . Id , Message : "Hi @" + user2 . Username } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post { RootId : p1 . Id , UserId : user2 . Id , ChannelId : channel . Id , Message : "Hola" } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post { RootId : p1 . Id , UserId : user . Id , ChannelId : channel . Id , Message : "reply" } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-07-16 10:04:22 -04:00
// unfollow thread
2025-04-29 02:50:47 -04:00
m , err := th . App . Srv ( ) . Store ( ) . Thread ( ) . MaintainMembership ( user . Id , p1 . Id , store . ThreadMembershipOpts {
2021-07-30 09:17:41 -04:00
Following : false ,
UpdateFollowing : true ,
2021-07-16 10:04:22 -04:00
} )
2025-04-29 02:50:47 -04:00
require . NoError ( t , err )
2021-07-16 10:04:22 -04:00
require . False ( t , m . Following )
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post { RootId : p1 . Id , UserId : user . Id , ChannelId : channel . Id , Message : "another reply" } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-07-16 10:04:22 -04:00
// User should be following thread after posting in it, even after previously
// unfollowing it, if ThreadAutoFollow is true
2025-04-29 02:50:47 -04:00
m , appErr = th . App . GetThreadMembershipForUser ( user . Id , p1 . Id )
require . Nil ( t , appErr )
2021-07-16 10:04:22 -04:00
require . True ( t , m . Following )
}
2021-07-28 13:47:45 -04:00
func TestGetPostIfAuthorized ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-07-28 13:47:45 -04:00
2023-11-30 05:43:51 -05:00
t . Run ( "Private channel" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
privateChannel := th . CreatePrivateChannel ( t , th . BasicTeam )
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePost ( th . Context , & model . Post { UserId : th . BasicUser . Id , ChannelId : privateChannel . Id , Message : "Hello" } , privateChannel , model . CreatePostFlags { } )
2023-11-30 05:43:51 -05:00
require . Nil ( t , err )
require . NotNil ( t , post )
2021-07-28 13:47:45 -04:00
2023-11-30 05:43:51 -05:00
session1 , err := th . App . CreateSession ( th . Context , & model . Session { UserId : th . BasicUser . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
require . NotNil ( t , session1 )
2021-07-28 13:47:45 -04:00
2023-11-30 05:43:51 -05:00
session2 , err := th . App . CreateSession ( th . Context , & model . Session { UserId : th . BasicUser2 . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
require . NotNil ( t , session2 )
2021-07-28 13:47:45 -04:00
2023-11-30 05:43:51 -05:00
// User is not authorized to get post
2026-01-20 04:38:27 -05:00
_ , err , _ = th . App . GetPostIfAuthorized ( th . Context , post . Id , session2 , false )
2023-11-30 05:43:51 -05:00
require . NotNil ( t , err )
2021-07-28 13:47:45 -04:00
2023-11-30 05:43:51 -05:00
// User is authorized to get post
2026-01-20 04:38:27 -05:00
_ , err , _ = th . App . GetPostIfAuthorized ( th . Context , post . Id , session1 , false )
2023-11-30 05:43:51 -05:00
require . Nil ( t , err )
} )
t . Run ( "Public channel" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
publicChannel := th . CreateChannel ( t , th . BasicTeam )
2026-01-20 04:38:27 -05:00
post , _ , err := th . App . CreatePost ( th . Context , & model . Post { UserId : th . BasicUser . Id , ChannelId : publicChannel . Id , Message : "Hello" } , publicChannel , model . CreatePostFlags { } )
2023-11-30 05:43:51 -05:00
require . Nil ( t , err )
require . NotNil ( t , post )
session1 , err := th . App . CreateSession ( th . Context , & model . Session { UserId : th . BasicUser . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
require . NotNil ( t , session1 )
session2 , err := th . App . CreateSession ( th . Context , & model . Session { UserId : th . BasicUser2 . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
require . NotNil ( t , session2 )
// User is authorized to get post
2026-01-20 04:38:27 -05:00
_ , err , _ = th . App . GetPostIfAuthorized ( th . Context , post . Id , session2 , false )
2023-11-30 05:43:51 -05:00
require . Nil ( t , err )
// User is authorized to get post
2026-01-20 04:38:27 -05:00
_ , err , _ = th . App . GetPostIfAuthorized ( th . Context , post . Id , session1 , false )
2023-11-30 05:43:51 -05:00
require . Nil ( t , err )
th . App . UpdateConfig ( func ( c * model . Config ) {
b := true
c . ComplianceSettings . Enable = & b
} )
// User is not authorized to get post
2026-01-20 04:38:27 -05:00
_ , err , _ = th . App . GetPostIfAuthorized ( th . Context , post . Id , session2 , false )
2023-11-30 05:43:51 -05:00
require . NotNil ( t , err )
// User is authorized to get post
2026-01-20 04:38:27 -05:00
_ , err , _ = th . App . GetPostIfAuthorized ( th . Context , post . Id , session1 , false )
2023-11-30 05:43:51 -05:00
require . Nil ( t , err )
} )
2021-07-28 13:47:45 -04:00
}
2021-07-30 09:17:41 -04:00
2026-04-01 05:07:45 -04:00
// MM-68140: thread context for rewrite must not be built from posts in channels the session cannot read.
func TestBuildThreadContextForRewriteRequiresChannelReadAccess ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
t . Run ( "direct message between other users" , func ( t * testing . T ) {
secretToken := "MM68140_SECRET_DM_THREAD_" + model . NewId ( )
dm := th . CreateDmChannel ( t , th . BasicUser2 )
root , _ , err := th . App . CreatePost ( th . Context , & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : dm . Id ,
Message : secretToken ,
} , dm , model . CreatePostFlags { } )
require . Nil ( t , err )
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
RootId : root . Id ,
UserId : th . BasicUser2 . Id ,
ChannelId : dm . Id ,
Message : "reply only visible to DM participants" ,
} , dm , model . CreatePostFlags { } )
require . Nil ( t , err )
attacker := th . CreateUser ( t )
session , err := th . App . CreateSession ( th . Context , & model . Session { UserId : attacker . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
ctx := th . Context . WithSession ( session )
contextStr , appErr := th . App . buildThreadContextForRewrite ( ctx , root . Id )
require . NotNil ( t , appErr , "expected permission error when root_id is in a channel the user cannot read, got nil" )
assert . Equal ( t , http . StatusForbidden , appErr . StatusCode )
assert . NotContains ( t , contextStr , secretToken )
} )
t . Run ( "private channel the user is not a member of" , func ( t * testing . T ) {
secretToken := "MM68140_SECRET_PRIVATE_THREAD_" + model . NewId ( )
privateCh := th . CreatePrivateChannel ( t , th . BasicTeam )
root , _ , err := th . App . CreatePost ( th . Context , & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : privateCh . Id ,
Message : secretToken ,
} , privateCh , model . CreatePostFlags { } )
require . Nil ( t , err )
session , err := th . App . CreateSession ( th . Context , & model . Session { UserId : th . BasicUser2 . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
ctx := th . Context . WithSession ( session )
contextStr , appErr := th . App . buildThreadContextForRewrite ( ctx , root . Id )
require . NotNil ( t , appErr )
assert . Equal ( t , http . StatusForbidden , appErr . StatusCode )
assert . NotContains ( t , contextStr , secretToken )
} )
}
// MM-68140: additional edge cases for thread context authorization and anchor resolution.
func TestBuildThreadContextForRewriteEdgeCasesMM68140 ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
t . Run ( "reply post id as root_id resolves thread and includes root message" , func ( t * testing . T ) {
_ , appErr := th . App . AddUserToChannel ( th . Context , th . BasicUser2 , th . BasicChannel , false )
require . Nil ( t , appErr )
rootSecret := "MM68140_ROOT_VIA_REPLY_ANCHOR_" + model . NewId ( )
root , _ , err := th . App . CreatePost ( th . Context , & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : rootSecret ,
} , th . BasicChannel , model . CreatePostFlags { } )
require . Nil ( t , err )
reply , _ , err := th . App . CreatePost ( th . Context , & model . Post {
RootId : root . Id ,
UserId : th . BasicUser2 . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "reply anchor" ,
} , th . BasicChannel , model . CreatePostFlags { } )
require . Nil ( t , err )
session , err := th . App . CreateSession ( th . Context , & model . Session { UserId : th . BasicUser2 . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
ctx := th . Context . WithSession ( session )
contextStr , appErr := th . App . buildThreadContextForRewrite ( ctx , reply . Id )
require . Nil ( t , appErr )
assert . Contains ( t , contextStr , rootSecret )
assert . Contains ( t , contextStr , "reply anchor" )
} )
t . Run ( "nonexistent post id returns not found" , func ( t * testing . T ) {
session , err := th . App . CreateSession ( th . Context , & model . Session { UserId : th . BasicUser . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
ctx := th . Context . WithSession ( session )
_ , appErr := th . App . buildThreadContextForRewrite ( ctx , model . NewId ( ) )
require . NotNil ( t , appErr )
assert . Equal ( t , http . StatusNotFound , appErr . StatusCode )
} )
t . Run ( "soft-deleted anchor post returns not found" , func ( t * testing . T ) {
root , _ , err := th . App . CreatePost ( th . Context , & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : th . BasicChannel . Id ,
Message : "to be deleted" ,
} , th . BasicChannel , model . CreatePostFlags { } )
require . Nil ( t , err )
_ , err = th . App . DeletePost ( th . Context , root . Id , th . BasicUser . Id )
require . Nil ( t , err )
session , err := th . App . CreateSession ( th . Context , & model . Session { UserId : th . BasicUser . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
ctx := th . Context . WithSession ( session )
_ , appErr := th . App . buildThreadContextForRewrite ( ctx , root . Id )
require . NotNil ( t , appErr )
assert . Equal ( t , http . StatusNotFound , appErr . StatusCode )
} )
t . Run ( "guest on team cannot use root_id for private channel they are not in" , func ( t * testing . T ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . GuestAccountsSettings . Enable = true
} )
guest := th . CreateGuest ( t )
_ , _ , appErr := th . App . AddUserToTeam ( th . Context , th . BasicTeam . Id , guest . Id , "" )
require . Nil ( t , appErr )
privateCh := th . CreatePrivateChannel ( t , th . BasicTeam )
secretToken := "MM68140_GUEST_PRIVATE_" + model . NewId ( )
root , _ , err := th . App . CreatePost ( th . Context , & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : privateCh . Id ,
Message : secretToken ,
} , privateCh , model . CreatePostFlags { } )
require . Nil ( t , err )
session , err := th . App . CreateSession ( th . Context , & model . Session { UserId : guest . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
ctx := th . Context . WithSession ( session )
contextStr , appErr := th . App . buildThreadContextForRewrite ( ctx , root . Id )
require . NotNil ( t , appErr )
assert . Equal ( t , http . StatusForbidden , appErr . StatusCode )
assert . NotContains ( t , contextStr , secretToken )
} )
t . Run ( "system admin may read thread context for DM they do not participate in" , func ( t * testing . T ) {
dm := th . CreateDmChannel ( t , th . BasicUser2 )
secretToken := "MM68140_ADMIN_DM_THREAD_" + model . NewId ( )
root , _ , err := th . App . CreatePost ( th . Context , & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : dm . Id ,
Message : secretToken ,
} , dm , model . CreatePostFlags { } )
require . Nil ( t , err )
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
RootId : root . Id ,
UserId : th . BasicUser2 . Id ,
ChannelId : dm . Id ,
Message : "dm reply" ,
} , dm , model . CreatePostFlags { } )
require . Nil ( t , err )
session , err := th . App . CreateSession ( th . Context , & model . Session { UserId : th . SystemAdminUser . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
ctx := th . Context . WithSession ( session )
contextStr , appErr := th . App . buildThreadContextForRewrite ( ctx , root . Id )
require . Nil ( t , appErr )
assert . Contains ( t , contextStr , secretToken )
} )
t . Run ( "member can build context after channel is archived" , func ( t * testing . T ) {
ch := th . CreateChannel ( t , th . BasicTeam )
root , _ , err := th . App . CreatePost ( th . Context , & model . Post {
UserId : th . BasicUser . Id ,
ChannelId : ch . Id ,
Message : "MM68140_ARCHIVED_ROOT" ,
} , ch , model . CreatePostFlags { } )
require . Nil ( t , err )
_ , _ , err = th . App . CreatePost ( th . Context , & model . Post {
RootId : root . Id ,
UserId : th . BasicUser . Id ,
ChannelId : ch . Id ,
Message : "reply in archived" ,
} , ch , model . CreatePostFlags { } )
require . Nil ( t , err )
appErr := th . App . DeleteChannel ( th . Context , ch , th . SystemAdminUser . Id )
require . Nil ( t , appErr )
session , err := th . App . CreateSession ( th . Context , & model . Session { UserId : th . BasicUser . Id , Props : model . StringMap { } } )
require . Nil ( t , err )
ctx := th . Context . WithSession ( session )
contextStr , appErr := th . App . buildThreadContextForRewrite ( ctx , root . Id )
require . Nil ( t , appErr )
assert . Contains ( t , contextStr , "MM68140_ARCHIVED_ROOT" )
} )
}
2021-07-30 09:17:41 -04:00
func TestShouldNotRefollowOnOthersReply ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-07-30 09:17:41 -04:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . ThreadAutoFollow = true
* cfg . ServiceSettings . CollapsedThreads = model . CollapsedThreadsDefaultOn
} )
channel := th . BasicChannel
user := th . BasicUser
user2 := th . BasicUser2
appErr := th . App . JoinChannel ( th . Context , channel , user . Id )
require . Nil ( t , appErr )
appErr = th . App . JoinChannel ( th . Context , channel , user2 . Id )
require . Nil ( t , appErr )
2026-01-20 04:38:27 -05:00
p1 , _ , appErr := th . App . CreatePost ( th . Context , & model . Post { UserId : user . Id , ChannelId : channel . Id , Message : "Hi @" + user2 . Username } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post { RootId : p1 . Id , UserId : user2 . Id , ChannelId : channel . Id , Message : "Hola" } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-07-30 09:17:41 -04:00
// User2 unfollows thread
2025-04-29 02:50:47 -04:00
m , err := th . App . Srv ( ) . Store ( ) . Thread ( ) . MaintainMembership ( user2 . Id , p1 . Id , store . ThreadMembershipOpts {
2021-07-30 09:17:41 -04:00
Following : false ,
UpdateFollowing : true ,
} )
2025-04-29 02:50:47 -04:00
require . NoError ( t , err )
2021-07-30 09:17:41 -04:00
require . False ( t , m . Following )
// user posts in the thread
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post { RootId : p1 . Id , UserId : user . Id , ChannelId : channel . Id , Message : "another reply" } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-07-30 09:17:41 -04:00
// User2 should still not be following the thread because they manually
// unfollowed the thread
2025-04-29 02:50:47 -04:00
m , appErr = th . App . GetThreadMembershipForUser ( user2 . Id , p1 . Id )
require . Nil ( t , appErr )
2021-07-30 09:17:41 -04:00
require . False ( t , m . Following )
// user posts in the thread mentioning user2
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , & model . Post { RootId : p1 . Id , UserId : user . Id , ChannelId : channel . Id , Message : "reply with mention @" + user2 . Username } , channel , model . CreatePostFlags { } )
2025-04-29 02:50:47 -04:00
require . Nil ( t , appErr )
2021-07-30 09:17:41 -04:00
// User2 should now be following the thread because they were explicitly mentioned
2025-04-29 02:50:47 -04:00
m , appErr = th . App . GetThreadMembershipForUser ( user2 . Id , p1 . Id )
require . Nil ( t , appErr )
2021-07-30 09:17:41 -04:00
require . True ( t , m . Following )
}
[MM-44084] Feature: Top threads insights (#20195)
* Add route endpoints, model, store functions, and tests for top threads
* Run make store-layers
* Make the following changes
- Fix top user threads query
- Fix passing parameters in api4/insights.go to handler in app
- Add top user threads test
* Add post-message, user_id, participants information to insights results
* model.TopThread.UserID -> model.TopThread.UserId, for compatibility with MySQL
* Rename name -> channel_name
* Add user information to response
* Link post in response, filter out deleted root posts from top threads
* Handle thread delete cases, add app tests for threads insights
* lint: fix typo
* lint: rename asserts
* lint: require.nil -> require.NoError
* Add integration tests for thread insights
* Add embeds and images to top posts
* Add license checks for top threads endpoints
* Query users in batch to populate post-creator
* Make the following changes
- Add license to test server in api4/
- Add tests for threads insights
- top team threads shouldn't include threads from other teams, DMs
- Test duration constraint
- Pagination testing for top threads in model/insights_test.go
* Add i18n-extract
* i18n fixes
* Add username, nickname to user_information
* Hide message, user_id, post_id, reply_count in depth=1 of top threads response
* Fix tests using response.reply_count to use response.post.reply_count
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
2022-06-20 10:27:17 -04:00
2022-07-06 02:26:39 -04:00
func TestGetLastAccessiblePostTime ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2022-07-06 02:26:39 -04:00
th := SetupWithStoreMock ( t )
2025-09-10 22:52:19 -04:00
// Setup store mocks needed for GetServerLimits
mockStore := th . App . Srv ( ) . Store ( ) . ( * storemocks . Store )
mockUserStore := storemocks . UserStore { }
mockUserStore . On ( "Count" , mock . Anything ) . Return ( int64 ( 10 ) , nil )
mockStore . On ( "User" ) . Return ( & mockUserStore )
// Test with no license - should return 0
2022-07-06 02:26:39 -04:00
r , err := th . App . GetLastAccessiblePostTime ( )
assert . Nil ( t , err )
assert . Equal ( t , int64 ( 0 ) , r )
2025-09-10 22:52:19 -04:00
// Test with Entry license but no limits configured - should return 0
entryLicenseNoLimits := model . NewTestLicenseSKU ( model . LicenseShortSkuMattermostEntry )
entryLicenseNoLimits . Limits = nil // No limits configured
th . App . Srv ( ) . SetLicense ( entryLicenseNoLimits )
r , err = th . App . GetLastAccessiblePostTime ( )
assert . Nil ( t , err )
assert . Equal ( t , int64 ( 0 ) , r , "Entry license with no limits should return 0" )
2022-07-06 02:26:39 -04:00
2025-09-10 22:52:19 -04:00
// Test with Entry license with zero post history limit - should return 0
entryLicenseZeroLimit := model . NewTestLicenseSKU ( model . LicenseShortSkuMattermostEntry )
entryLicenseZeroLimit . Limits = & model . LicenseLimits { PostHistory : 0 } // Zero limit
th . App . Srv ( ) . SetLicense ( entryLicenseZeroLimit )
r , err = th . App . GetLastAccessiblePostTime ( )
assert . Nil ( t , err )
assert . Equal ( t , int64 ( 0 ) , r , "Entry license with zero post history limit should return 0" )
// Test with Entry license that has post history limits
entryLicenseWithLimits := model . NewTestLicenseSKU ( model . LicenseShortSkuMattermostEntry )
entryLicenseWithLimits . Limits = & model . LicenseLimits { PostHistory : 1000 } // Actual limit
th . App . Srv ( ) . SetLicense ( entryLicenseWithLimits )
2022-07-06 02:26:39 -04:00
2025-09-10 22:52:19 -04:00
// Test case 1: No system value found (ErrNotFound) - should return 0
2022-07-06 02:26:39 -04:00
mockSystemStore := storemocks . SystemStore { }
mockStore . On ( "System" ) . Return ( & mockSystemStore )
mockSystemStore . On ( "GetByName" , mock . Anything ) . Return ( nil , store . NewErrNotFound ( "" , "" ) )
r , err = th . App . GetLastAccessiblePostTime ( )
assert . Nil ( t , err )
assert . Equal ( t , int64 ( 0 ) , r )
2025-09-10 22:52:19 -04:00
// Test case 2: Database error - should return error
2022-07-06 02:26:39 -04:00
mockSystemStore = storemocks . SystemStore { }
mockStore . On ( "System" ) . Return ( & mockSystemStore )
2025-09-10 22:52:19 -04:00
mockSystemStore . On ( "GetByName" , mock . Anything ) . Return ( nil , errors . New ( "database error" ) )
2022-07-06 02:26:39 -04:00
_ , err = th . App . GetLastAccessiblePostTime ( )
assert . NotNil ( t , err )
2025-09-10 22:52:19 -04:00
// Test case 3: Valid system value found - should return parsed timestamp
2022-07-06 02:26:39 -04:00
mockSystemStore = storemocks . SystemStore { }
mockStore . On ( "System" ) . Return ( & mockSystemStore )
2025-09-10 22:52:19 -04:00
mockSystemStore . On ( "GetByName" , mock . Anything ) . Return ( & model . System { Name : model . SystemLastAccessiblePostTime , Value : "1234567890" } , nil )
2022-07-06 02:26:39 -04:00
r , err = th . App . GetLastAccessiblePostTime ( )
assert . Nil ( t , err )
2025-09-10 22:52:19 -04:00
assert . Equal ( t , int64 ( 1234567890 ) , r )
2022-07-06 02:26:39 -04:00
}
func TestComputeLastAccessiblePostTime ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-09-10 22:52:19 -04:00
t . Run ( "Updates the time, if Entry license limit is applicable" , func ( t * testing . T ) {
2022-10-31 09:56:04 -04:00
th := SetupWithStoreMock ( t )
2022-07-06 02:26:39 -04:00
2025-09-10 22:52:19 -04:00
// Set Entry license with post history limit of 100 messages
entryLicensePostsLimit := model . NewTestLicenseSKU ( model . LicenseShortSkuMattermostEntry )
entryLicensePostsLimit . Limits = & model . LicenseLimits { PostHistory : 100 }
th . App . Srv ( ) . SetLicense ( entryLicensePostsLimit )
2022-07-06 02:26:39 -04:00
2022-10-31 09:56:04 -04:00
mockStore := th . App . Srv ( ) . Store ( ) . ( * storemocks . Store )
mockPostStore := storemocks . PostStore { }
2025-09-10 22:52:19 -04:00
mockPostStore . On ( "GetNthRecentPostTime" , int64 ( 100 ) ) . Return ( int64 ( 1234567890 ) , nil )
2022-10-31 09:56:04 -04:00
mockSystemStore := storemocks . SystemStore { }
mockSystemStore . On ( "SaveOrUpdate" , mock . Anything ) . Return ( nil )
mockStore . On ( "Post" ) . Return ( & mockPostStore )
mockStore . On ( "System" ) . Return ( & mockSystemStore )
2022-07-06 02:26:39 -04:00
2022-10-31 09:56:04 -04:00
err := th . App . ComputeLastAccessiblePostTime ( )
assert . NoError ( t , err )
2022-07-06 02:26:39 -04:00
2025-09-10 22:52:19 -04:00
// Verify that the system value was saved with the calculated timestamp
mockSystemStore . AssertCalled ( t , "SaveOrUpdate" , & model . System {
Name : model . SystemLastAccessiblePostTime ,
Value : "1234567890" ,
} )
2022-10-31 09:56:04 -04:00
} )
2025-09-10 22:52:19 -04:00
t . Run ( "Remove the time if license limit is NOT applicable" , func ( t * testing . T ) {
2022-10-31 09:56:04 -04:00
th := SetupWithStoreMock ( t )
2025-09-10 22:52:19 -04:00
// Set license without post history limits (using test license without limits)
license := model . NewTestLicense ( )
license . Limits = nil // No limits
th . App . Srv ( ) . SetLicense ( license )
2022-10-31 09:56:04 -04:00
mockStore := th . App . Srv ( ) . Store ( ) . ( * storemocks . Store )
mockSystemStore := storemocks . SystemStore { }
2025-09-10 22:52:19 -04:00
mockSystemStore . On ( "GetByName" , model . SystemLastAccessiblePostTime ) . Return ( & model . System { Name : model . SystemLastAccessiblePostTime , Value : "1234567890" } , nil )
mockSystemStore . On ( "PermanentDeleteByName" , model . SystemLastAccessiblePostTime ) . Return ( nil , nil )
2022-10-31 09:56:04 -04:00
mockStore . On ( "System" ) . Return ( & mockSystemStore )
err := th . App . ComputeLastAccessiblePostTime ( )
assert . NoError ( t , err )
2025-09-10 22:52:19 -04:00
// Verify that SaveOrUpdate was not called (no new timestamp calculated)
2022-10-31 09:56:04 -04:00
mockSystemStore . AssertNotCalled ( t , "SaveOrUpdate" , mock . Anything )
2025-09-10 22:52:19 -04:00
// Verify that the previous value was deleted
mockSystemStore . AssertCalled ( t , "PermanentDeleteByName" , model . SystemLastAccessiblePostTime )
2022-10-31 09:56:04 -04:00
} )
2022-07-06 02:26:39 -04:00
}
2023-02-07 09:30:37 -05:00
func TestGetEditHistoryForPost ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2023-02-07 09:30:37 -05:00
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "new message" ,
UserId : th . BasicUser . Id ,
}
2026-01-20 04:38:27 -05:00
rpost , _ , err := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2023-02-07 09:30:37 -05:00
require . Nil ( t , err )
// update the post message
patch := & model . PostPatch {
2024-08-05 23:45:00 -04:00
Message : model . NewPointer ( "new message edited" ) ,
2023-02-07 09:30:37 -05:00
}
2026-01-20 04:38:27 -05:00
_ , _ , err1 := th . App . PatchPost ( th . Context , rpost . Id , patch , nil )
2023-02-07 09:30:37 -05:00
require . Nil ( t , err1 )
// update the post message again
patch = & model . PostPatch {
2024-08-05 23:45:00 -04:00
Message : model . NewPointer ( "new message edited again" ) ,
2023-02-07 09:30:37 -05:00
}
2026-01-20 04:38:27 -05:00
_ , _ , err2 := th . App . PatchPost ( th . Context , rpost . Id , patch , nil )
2023-02-07 09:30:37 -05:00
require . Nil ( t , err2 )
t . Run ( "should return the edit history" , func ( t * testing . T ) {
2025-01-13 07:46:56 -05:00
edits , err := th . App . GetEditHistoryForPost ( post . Id )
require . Nil ( t , err )
2023-02-07 09:30:37 -05:00
require . Len ( t , edits , 2 )
require . Equal ( t , "new message edited" , edits [ 0 ] . Message )
require . Equal ( t , "new message" , edits [ 1 ] . Message )
} )
t . Run ( "should return an error if the post is not found" , func ( t * testing . T ) {
edits , err := th . App . GetEditHistoryForPost ( "invalid-post-id" )
require . NotNil ( t , err )
require . Empty ( t , edits )
} )
2025-01-13 07:46:56 -05:00
t . Run ( "edit history should contain file metadata" , func ( t * testing . T ) {
fileBytes := [ ] byte ( "file contents" )
fileInfo , err := th . App . UploadFile ( th . Context , fileBytes , th . BasicChannel . Id , "file.txt" )
require . Nil ( t , err )
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "new message" ,
UserId : th . BasicUser . Id ,
FileIds : model . StringArray { fileInfo . Id } ,
}
2026-01-20 04:38:27 -05:00
_ , _ , err = th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2025-01-13 07:46:56 -05:00
require . Nil ( t , err )
patch := & model . PostPatch {
Message : model . NewPointer ( "new message edited" ) ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr := th . App . PatchPost ( th . Context , post . Id , patch , nil )
2025-01-13 07:46:56 -05:00
require . Nil ( t , appErr )
patch = & model . PostPatch {
Message : model . NewPointer ( "new message edited 2" ) ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . PatchPost ( th . Context , post . Id , patch , nil )
2025-01-13 07:46:56 -05:00
require . Nil ( t , appErr )
patch = & model . PostPatch {
Message : model . NewPointer ( "new message edited 3" ) ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . PatchPost ( th . Context , post . Id , patch , nil )
2025-01-13 07:46:56 -05:00
require . Nil ( t , appErr )
edits , err := th . App . GetEditHistoryForPost ( post . Id )
require . Nil ( t , err )
require . Len ( t , edits , 3 )
for _ , edit := range edits {
require . Len ( t , edit . FileIds , 1 )
require . Equal ( t , fileInfo . Id , edit . FileIds [ 0 ] )
require . Len ( t , edit . Metadata . Files , 1 )
require . Equal ( t , fileInfo . Id , edit . Metadata . Files [ 0 ] . Id )
}
} )
t . Run ( "edit history should contain file metadata even if the file info is deleted" , func ( t * testing . T ) {
fileBytes := [ ] byte ( "file contents" )
fileInfo , appErr := th . App . UploadFile ( th . Context , fileBytes , th . BasicChannel . Id , "file.txt" )
require . Nil ( t , appErr )
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "new message" ,
UserId : th . BasicUser . Id ,
FileIds : model . StringArray { fileInfo . Id } ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2025-01-13 07:46:56 -05:00
require . Nil ( t , appErr )
patch := & model . PostPatch {
Message : model . NewPointer ( "new message edited" ) ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . PatchPost ( th . Context , post . Id , patch , nil )
2025-01-13 07:46:56 -05:00
require . Nil ( t , appErr )
patch = & model . PostPatch {
Message : model . NewPointer ( "new message edited 2" ) ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . PatchPost ( th . Context , post . Id , patch , nil )
2025-01-13 07:46:56 -05:00
require . Nil ( t , appErr )
patch = & model . PostPatch {
Message : model . NewPointer ( "new message edited 3" ) ,
}
2026-01-20 04:38:27 -05:00
_ , _ , appErr = th . App . PatchPost ( th . Context , post . Id , patch , nil )
2025-01-13 07:46:56 -05:00
require . Nil ( t , appErr )
// now delete the file info, and it should still be include in edit history metadata
_ , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . DeleteForPost ( th . Context , post . Id )
require . NoError ( t , err )
edits , appErr := th . App . GetEditHistoryForPost ( post . Id )
require . Nil ( t , appErr )
require . Len ( t , edits , 3 )
for _ , edit := range edits {
require . Len ( t , edit . FileIds , 1 )
require . Equal ( t , fileInfo . Id , edit . FileIds [ 0 ] )
require . Len ( t , edit . Metadata . Files , 1 )
require . Equal ( t , fileInfo . Id , edit . Metadata . Files [ 0 ] . Id )
require . Greater ( t , edit . Metadata . Files [ 0 ] . DeleteAt , int64 ( 0 ) )
}
} )
2023-02-07 09:30:37 -05:00
}
2023-12-11 15:27:34 -05:00
func TestCopyWranglerPostlist ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2023-12-11 15:27:34 -05:00
// Create a post with a file attachment
fileBytes := [ ] byte ( "file contents" )
fileInfo , err := th . App . UploadFile ( th . Context , fileBytes , th . BasicChannel . Id , "file.txt" )
require . Nil ( t , err )
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "test message" ,
UserId : th . BasicUser . Id ,
FileIds : [ ] string { fileInfo . Id } ,
}
2026-01-20 04:38:27 -05:00
rootPost , _ , err := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2023-12-11 15:27:34 -05:00
require . Nil ( t , err )
// Add a reaction to the post
reaction := & model . Reaction {
UserId : th . BasicUser . Id ,
PostId : rootPost . Id ,
EmojiName : "smile" ,
}
_ , err = th . App . SaveReactionForPost ( th . Context , reaction )
require . Nil ( t , err )
// Copy the post to a new channel
targetChannel := & model . Channel {
TeamId : th . BasicTeam . Id ,
Name : "test-channel" ,
Type : model . ChannelTypeOpen ,
}
targetChannel , err = th . App . CreateChannel ( th . Context , targetChannel , false )
require . Nil ( t , err )
wpl := & model . WranglerPostList {
Posts : [ ] * model . Post { rootPost } ,
FileAttachmentCount : 1 ,
}
2026-01-20 04:38:27 -05:00
newRootPost , _ , err := th . App . CopyWranglerPostlist ( th . Context , wpl , targetChannel )
2023-12-11 15:27:34 -05:00
require . Nil ( t , err )
// Check that the new post has the same message and file attachment
require . Equal ( t , rootPost . Message , newRootPost . Message )
require . Len ( t , newRootPost . FileIds , 1 )
// Check that the new post has the same reaction
reactions , err := th . App . GetReactionsForPost ( newRootPost . Id )
require . Nil ( t , err )
require . Len ( t , reactions , 1 )
require . Equal ( t , reaction . EmojiName , reactions [ 0 ] . EmojiName )
}
func TestValidateMoveOrCopy ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2023-12-11 15:27:34 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2024-08-05 23:45:00 -04:00
cfg . WranglerSettings . MoveThreadFromPrivateChannelEnable = model . NewPointer ( true )
cfg . WranglerSettings . MoveThreadFromDirectMessageChannelEnable = model . NewPointer ( true )
cfg . WranglerSettings . MoveThreadFromGroupMessageChannelEnable = model . NewPointer ( true )
cfg . WranglerSettings . MoveThreadToAnotherTeamEnable = model . NewPointer ( true )
cfg . WranglerSettings . MoveThreadMaxCount = model . NewPointer ( int64 ( 100 ) )
2023-12-11 15:27:34 -05:00
} )
t . Run ( "empty post list" , func ( t * testing . T ) {
err := th . App . ValidateMoveOrCopy ( th . Context , & model . WranglerPostList { } , th . BasicChannel , th . BasicChannel , th . BasicUser )
require . Error ( t , err )
require . Equal ( t , "The wrangler post list contains no posts" , err . Error ( ) )
} )
t . Run ( "moving from private channel with MoveThreadFromPrivateChannelEnable disabled" , func ( t * testing . T ) {
privateChannel := & model . Channel {
TeamId : th . BasicTeam . Id ,
Name : "private-channel" ,
Type : model . ChannelTypePrivate ,
}
privateChannel , err := th . App . CreateChannel ( th . Context , privateChannel , false )
require . Nil ( t , err )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2024-08-05 23:45:00 -04:00
cfg . WranglerSettings . MoveThreadFromPrivateChannelEnable = model . NewPointer ( false )
2023-12-11 15:27:34 -05:00
} )
e := th . App . ValidateMoveOrCopy ( th . Context , & model . WranglerPostList { Posts : [ ] * model . Post { { ChannelId : privateChannel . Id } } } , privateChannel , th . BasicChannel , th . BasicUser )
require . Error ( t , e )
require . Equal ( t , "Wrangler is currently configured to not allow moving posts from private channels" , e . Error ( ) )
} )
t . Run ( "moving from direct channel with MoveThreadFromDirectMessageChannelEnable disabled" , func ( t * testing . T ) {
directChannel , err := th . App . createDirectChannel ( th . Context , th . BasicUser . Id , th . BasicUser2 . Id )
require . Nil ( t , err )
require . NotNil ( t , directChannel )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2024-08-05 23:45:00 -04:00
cfg . WranglerSettings . MoveThreadFromDirectMessageChannelEnable = model . NewPointer ( false )
2023-12-11 15:27:34 -05:00
} )
e := th . App . ValidateMoveOrCopy ( th . Context , & model . WranglerPostList { Posts : [ ] * model . Post { { ChannelId : directChannel . Id } } } , directChannel , th . BasicChannel , th . BasicUser )
require . Error ( t , e )
require . Equal ( t , "Wrangler is currently configured to not allow moving posts from direct message channels" , e . Error ( ) )
} )
t . Run ( "moving from group channel with MoveThreadFromGroupMessageChannelEnable disabled" , func ( t * testing . T ) {
groupChannel := & model . Channel {
TeamId : th . BasicTeam . Id ,
Name : "group-channel" ,
Type : model . ChannelTypeGroup ,
}
groupChannel , err := th . App . CreateChannel ( th . Context , groupChannel , false )
require . Nil ( t , err )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2024-08-05 23:45:00 -04:00
cfg . WranglerSettings . MoveThreadFromGroupMessageChannelEnable = model . NewPointer ( false )
2023-12-11 15:27:34 -05:00
} )
e := th . App . ValidateMoveOrCopy ( th . Context , & model . WranglerPostList { Posts : [ ] * model . Post { { ChannelId : groupChannel . Id } } } , groupChannel , th . BasicChannel , th . BasicUser )
require . Error ( t , e )
require . Equal ( t , "Wrangler is currently configured to not allow moving posts from group message channels" , e . Error ( ) )
} )
t . Run ( "moving to different team with MoveThreadToAnotherTeamEnable disabled" , func ( t * testing . T ) {
team := & model . Team {
Name : "testteam" ,
DisplayName : "testteam" ,
Type : model . TeamOpen ,
}
targetTeam , err := th . App . CreateTeam ( th . Context , team )
require . Nil ( t , err )
require . NotNil ( t , targetTeam )
targetChannel := & model . Channel {
TeamId : targetTeam . Id ,
Name : "test-channel" ,
Type : model . ChannelTypeOpen ,
}
targetChannel , err = th . App . CreateChannel ( th . Context , targetChannel , false )
require . Nil ( t , err )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2024-08-05 23:45:00 -04:00
cfg . WranglerSettings . MoveThreadToAnotherTeamEnable = model . NewPointer ( false )
2023-12-11 15:27:34 -05:00
} )
e := th . App . ValidateMoveOrCopy ( th . Context , & model . WranglerPostList { Posts : [ ] * model . Post { { ChannelId : th . BasicChannel . Id } } } , th . BasicChannel , targetChannel , th . BasicUser )
require . Error ( t , e )
require . Equal ( t , "Wrangler is currently configured to not allow moving messages to different teams" , e . Error ( ) )
} )
t . Run ( "moving to channel user is not a member of" , func ( t * testing . T ) {
targetChannel := & model . Channel {
TeamId : th . BasicTeam . Id ,
Name : "test-channel" ,
Type : model . ChannelTypePrivate ,
}
targetChannel , err := th . App . CreateChannel ( th . Context , targetChannel , false )
require . Nil ( t , err )
err = th . App . RemoveUserFromChannel ( th . Context , th . BasicUser . Id , th . SystemAdminUser . Id , th . BasicChannel )
require . Nil ( t , err )
e := th . App . ValidateMoveOrCopy ( th . Context , & model . WranglerPostList { Posts : [ ] * model . Post { { ChannelId : th . BasicChannel . Id } } } , th . BasicChannel , targetChannel , th . BasicUser )
require . Error ( t , e )
require . Equal ( t , fmt . Sprintf ( "channel with ID %s doesn't exist or you are not a member" , targetChannel . Id ) , e . Error ( ) )
} )
t . Run ( "moving thread longer than MoveThreadMaxCount" , func ( t * testing . T ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . WranglerSettings . MoveThreadMaxCount = 1
} )
e := th . App . ValidateMoveOrCopy ( th . Context , & model . WranglerPostList { Posts : [ ] * model . Post { { ChannelId : th . BasicChannel . Id } , { ChannelId : th . BasicChannel . Id } } } , th . BasicChannel , th . BasicChannel , th . BasicUser )
require . Error ( t , e )
require . Equal ( t , "the thread is 2 posts long, but this command is configured to only move threads of up to 1 posts" , e . Error ( ) )
} )
}
2024-10-08 10:45:31 -04:00
func TestPermanentDeletePost ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2024-10-08 10:45:31 -04:00
ci: enable fullyparallel mode for server tests (#35816)
* ci: enable fullyparallel mode for server tests
Replace os.Setenv, os.Chdir, and global state mutations with
parallel-safe alternatives (t.Setenv, t.Chdir, test hooks) across
37 files. Refactor GetLogRootPath and MM_INSTALL_TYPE to use
package-level test hooks instead of environment variables.
This enables gotestsum --fullparallel, allowing all test packages
to run with maximum parallelism within each shard.
Co-authored-by: Claude <claude@anthropic.com>
* ci: split fullyparallel from continue-on-error in workflow template
- Add new boolean input 'allow-failure' separate from 'fullyparallel'
- Change continue-on-error to use allow-failure instead of fullyparallel
- Update server-ci.yml to pass allow-failure: true for test coverage job
- Allows independent control of parallel execution and failure tolerance
Co-authored-by: Claude <claude@anthropic.com>
* fix: protect TestOverrideLogRootPath with sync.Mutex for parallel tests
- Replace global var TestOverrideLogRootPath with mutex-protected functions
- Add SetTestOverrideLogRootPath() and getTestOverrideLogRootPath() functions
- Update GetLogRootPath() to use thread-safe getter
- Update all test files to use SetTestOverrideLogRootPath() with t.Cleanup()
- Fixes race condition when running tests with t.Parallel()
Co-authored-by: Claude <claude@anthropic.com>
* fix: configure audit settings before server setup in tests
- Move ExperimentalAuditSettings from UpdateConfig() to config defaults
- Pass audit config via app.Config() option in SetupWithServerOptions()
- Fixes audit test setup ordering to configure BEFORE server initialization
- Resolves CodeRabbit's audit config timing issue in api4 tests
Co-authored-by: Claude <claude@anthropic.com>
* fix: implement SetTestOverrideLogRootPath mutex in logger.go
The previous commit updated test callers to use SetTestOverrideLogRootPath()
but didn't actually create the function in config/logger.go, causing build
failures across all CI shards. This commit:
- Replaces the exported var TestOverrideLogRootPath with mutex-protected
unexported state (testOverrideLogRootPath + testOverrideLogRootMu)
- Adds exported SetTestOverrideLogRootPath() setter
- Adds unexported getTestOverrideLogRootPath() getter
- Updates GetLogRootPath() to use the thread-safe getter
- Fixes log_test.go callers that were missed in the previous commit
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): use SetupConfig for access_control feature flag registration
InitAccessControlPolicy() checks FeatureFlags.AttributeBasedAccessControl
at route registration time during server startup. Setting the flag via
UpdateConfig after Setup() is too late — routes are never registered
and API calls return 404.
Use SetupConfig() to pass the feature flag in the initial config before
server startup, ensuring routes are properly registered.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore BurnOnRead flag state in TestRevealPost subtest
The 'feature not enabled' subtest disables BurnOnRead without restoring
it via t.Cleanup. Subsequent subtests inherit the disabled state, which
can cause 501 errors when they expect the feature to be available.
Add t.Cleanup to restore FeatureFlags.BurnOnRead = true after the
subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore EnableSharedChannelsMemberSync flag via t.Cleanup
The test disables EnableSharedChannelsMemberSync without restoring it.
If the subtest exits early (e.g., require failure), later sibling
subtests inherit a disabled flag and become flaky.
Add t.Cleanup to restore the flag after the subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* Fix test parallelism: use instance-scoped overrides and init-time audit config
Replace package-level test globals (TestOverrideInstallType,
SetTestOverrideLogRootPath) with fields on PlatformService so each test
gets its own instance without process-wide mutation. Fix three audit
tests (TestUserLoginAudit, TestLogoutAuditAuthStatus,
TestUpdatePasswordAudit) that configured the audit logger after server
init — the audit logger only reads config at startup, so pass audit
settings via app.Config() at init time instead.
Also revert the Go 1.24.13 downgrade and bump mattermost-govet to
v2.0.2 for Go 1.25.8 compatibility.
* Fix audit unit tests
* Fix MMCLOUDURL unit tests
* Fixed unit tests using MM_NOTIFY_ADMIN_COOL_OFF_DAYS
* Make app migrations idempotent for parallel test safety
Change System().Save() to System().SaveOrUpdate() in all migration
completion markers. When two parallel tests share a database pool entry,
both may race through the check-then-insert migration pattern. Save()
causes a duplicate key fatal crash; SaveOrUpdate() makes the second
write a harmless no-op.
* test: address review feedback on fullyparallel PR
- Use SetLogRootPathOverride() setter instead of direct field access
in platform/support_packet_test.go and platform/log_test.go (pvev)
- Restore TestGetLogRootPath in config/logger_test.go to keep
MM_LOG_PATH env var coverage; test uses t.Setenv so it runs
serially which is fine (pvev)
- Fix misleading comment in config_test.go: code uses t.Setenv,
not os.Setenv (jgheithcock)
Co-authored-by: Claude <claude@anthropic.com>
* fix: add missing os import in post_test.go
The os import was dropped during a merge conflict resolution while
burn-on-read shared channel tests from master still use os.Setenv.
Co-authored-by: Claude <claude@anthropic.com>
---------
Co-authored-by: Claude <claude@anthropic.com>
Co-authored-by: wiggin77 <wiggin77@warpmail.net>
Co-authored-by: Mattermost Build <build@mattermost.com>
2026-04-08 20:48:36 -04:00
// Enable BurnOnRead feature flag
th . App . UpdateConfig ( func ( cfg * model . Config ) { cfg . FeatureFlags . BurnOnRead = true } )
2024-10-08 10:45:31 -04:00
t . Run ( "should permanently delete a post and its file attachment" , func ( t * testing . T ) {
// Create a post with a file attachment.
teamID := th . BasicTeam . Id
channelID := th . BasicChannel . Id
userID := th . BasicUser . Id
filename := "test"
data := [ ] byte ( "abcd" )
info1 , err := th . App . DoUploadFile ( th . Context , time . Date ( 2007 , 2 , 4 , 1 , 2 , 3 , 4 , time . Local ) , teamID , channelID , userID , filename , data , true )
assert . Nil ( t , err )
post := & model . Post {
Message : "asd" ,
ChannelId : channelID ,
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
UserId : userID ,
CreateAt : 0 ,
FileIds : [ ] string { info1 . Id } ,
}
2026-01-20 04:38:27 -05:00
post , _ , err = th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2024-10-08 10:45:31 -04:00
assert . Nil ( t , err )
// Delete the post.
err = th . App . PermanentDeletePost ( th . Context , post . Id , userID )
assert . Nil ( t , err )
// Wait for the cleanup routine to finish.
time . Sleep ( time . Millisecond * 100 )
// Check that the post can no longer be reached.
_ , err = th . App . GetSinglePost ( th . Context , post . Id , true )
assert . NotNil ( t , err )
// Check that the file can no longer be reached.
_ , err = th . App . GetFileInfo ( th . Context , info1 . Id )
assert . NotNil ( t , err )
} )
t . Run ( "should permanently delete a post that is soft deleted" , func ( t * testing . T ) {
// Create a post with a file attachment.
teamID := th . BasicTeam . Id
channelID := th . BasicChannel . Id
userID := th . BasicUser . Id
filename := "test"
data := [ ] byte ( "abcd" )
2025-04-29 02:50:47 -04:00
info1 , appErr := th . App . DoUploadFile ( th . Context , time . Date ( 2007 , 2 , 4 , 1 , 2 , 3 , 4 , time . Local ) , teamID , channelID , userID , filename , data , true )
require . Nil ( t , appErr )
2024-10-08 10:45:31 -04:00
post := & model . Post {
Message : "asd" ,
ChannelId : channelID ,
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
UserId : userID ,
CreateAt : 0 ,
FileIds : [ ] string { info1 . Id } ,
}
2026-01-20 04:38:27 -05:00
post , _ , appErr = th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2025-04-29 02:50:47 -04:00
assert . Nil ( t , appErr )
2024-10-08 10:45:31 -04:00
2025-04-29 02:50:47 -04:00
infos , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . GetForPost ( post . Id , true , true , false )
require . NoError ( t , err )
2024-10-08 10:45:31 -04:00
assert . Len ( t , infos , 1 )
// Soft delete the post.
2025-04-29 02:50:47 -04:00
_ , appErr = th . App . DeletePost ( th . Context , post . Id , userID )
assert . Nil ( t , appErr )
2024-10-08 10:45:31 -04:00
// Wait for the cleanup routine to finish.
time . Sleep ( time . Millisecond * 100 )
// Delete the post.
2025-04-29 02:50:47 -04:00
appErr = th . App . PermanentDeletePost ( th . Context , post . Id , userID )
assert . Nil ( t , appErr )
2024-10-08 10:45:31 -04:00
// Check that the post can no longer be reached.
2025-04-29 02:50:47 -04:00
_ , appErr = th . App . GetSinglePost ( th . Context , post . Id , true )
assert . NotNil ( t , appErr )
2024-10-08 10:45:31 -04:00
2025-04-29 02:50:47 -04:00
infos , err = th . App . Srv ( ) . Store ( ) . FileInfo ( ) . GetForPost ( post . Id , true , true , false )
require . NoError ( t , err )
2024-10-08 10:45:31 -04:00
assert . Len ( t , infos , 0 )
} )
2025-12-17 04:19:04 -05:00
t . Run ( "should permanently delete a burn-on-read post and its file attachments" , func ( t * testing . T ) {
// Enable feature with license
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
cfg . ServiceSettings . EnableBurnOnRead = model . NewPointer ( true )
} )
// Create a burn-on-read post with a file attachment
teamID := th . BasicTeam . Id
channelID := th . BasicChannel . Id
userID := th . BasicUser . Id
filename := "burn_on_read_file"
data := [ ] byte ( "burn on read file content" )
info1 , err := th . App . DoUploadFile ( th . Context , time . Date ( 2007 , 2 , 4 , 1 , 2 , 3 , 4 , time . Local ) , teamID , channelID , userID , filename , data , true )
require . Nil ( t , err )
post := & model . Post {
Message : "burn on read message with file" ,
ChannelId : channelID ,
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
UserId : userID ,
CreateAt : 0 ,
FileIds : [ ] string { info1 . Id } ,
Type : model . PostTypeBurnOnRead ,
}
post . AddProp ( model . PostPropsExpireAt , model . GetMillis ( ) + int64 ( model . DefaultExpirySeconds * 1000 ) )
2026-01-20 04:38:27 -05:00
post , _ , appErr := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
2025-12-17 04:19:04 -05:00
require . Nil ( t , appErr )
require . Equal ( t , model . PostTypeBurnOnRead , post . Type )
// Verify that the post has empty message and file IDs (stored in TemporaryPosts)
assert . Empty ( t , post . Message )
assert . Empty ( t , post . FileIds )
// Verify that TemporaryPost exists with original content
2026-02-09 15:47:27 -05:00
tmpPost , tmpErr := th . App . Srv ( ) . Store ( ) . TemporaryPost ( ) . Get ( th . Context , post . Id , true )
2025-12-17 04:19:04 -05:00
require . NoError ( t , tmpErr )
require . NotNil ( t , tmpPost )
assert . Equal ( t , "burn on read message with file" , tmpPost . Message )
assert . Equal ( t , model . StringArray { info1 . Id } , tmpPost . FileIDs )
// Verify file info exists before deletion
_ , err = th . App . GetFileInfo ( th . Context , info1 . Id )
require . Nil ( t , err )
// Permanently delete the post
appErr = th . App . PermanentDeletePost ( th . Context , post . Id , userID )
require . Nil ( t , appErr )
// Check that the post can no longer be reached
_ , err = th . App . GetSinglePost ( th . Context , post . Id , true )
assert . NotNil ( t , err )
// Check that the file also deleted
_ , err = th . App . GetFileInfo ( th . Context , info1 . Id )
assert . NotNil ( t , err )
// Verify TemporaryPost is also deleted
2026-02-09 15:47:27 -05:00
_ , tmpErr = th . App . Srv ( ) . Store ( ) . TemporaryPost ( ) . Get ( th . Context , post . Id , true )
2025-12-17 04:19:04 -05:00
assert . Error ( t , tmpErr )
assert . True ( t , store . IsErrNotFound ( tmpErr ) )
} )
2026-01-27 01:27:06 -05:00
t . Run ( "should send unrevealed post in websocket broadcast" , func ( t * testing . T ) {
// Enable feature with license
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
cfg . ServiceSettings . EnableBurnOnRead = model . NewPointer ( true )
} )
// Create a burn-on-read post
//teamID := th.BasicTeam.Id
channelID := th . BasicChannel . Id
userID := th . BasicUser . Id
wsMessages , closeWS := connectFakeWebSocket ( t , th , userID , "" , [ ] model . WebsocketEventType { model . WebsocketEventPostDeleted } )
defer closeWS ( )
post := & model . Post {
Message : "burn on read message with file" ,
ChannelId : channelID ,
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
UserId : userID ,
CreateAt : 0 ,
Type : model . PostTypeBurnOnRead ,
}
post . AddProp ( model . PostPropsExpireAt , model . GetMillis ( ) + int64 ( model . DefaultExpirySeconds * 1000 ) )
post , _ , appErr := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
require . Nil ( t , appErr )
require . Equal ( t , model . PostTypeBurnOnRead , post . Type )
appErr = th . App . PermanentDeletePost ( th . Context , post . Id , userID )
require . Nil ( t , appErr )
var received * model . WebSocketEvent
select {
case received = <- wsMessages :
// the post sent in websocket payload shouldn't contain message or file IDs
data := received . GetData ( )
postJSON , ok := data [ "post" ] . ( string )
require . True ( t , ok )
var receivedPost model . Post
err := json . Unmarshal ( [ ] byte ( postJSON ) , & receivedPost )
require . NoError ( t , err )
require . Equal ( t , post . Id , receivedPost . Id )
require . Equal ( t , "" , receivedPost . Message )
require . Equal ( t , 0 , len ( receivedPost . FileIds ) )
case <- time . After ( 10 * time . Second ) :
require . Fail ( t , "Did not receive websocket message in time" )
}
} )
2024-10-08 10:45:31 -04:00
}
2024-11-08 07:57:06 -05:00
func TestSendTestMessage ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2024-11-08 07:57:06 -05:00
t . Run ( "Should create the post with the correct prop" , func ( t * testing . T ) {
post , result := th . App . SendTestMessage ( th . Context , th . BasicUser . Id )
assert . Nil ( t , result )
assert . NotEmpty ( t , post . GetProp ( model . PostPropsForceNotification ) )
} )
}
2025-01-13 07:46:56 -05:00
func TestPopulateEditHistoryFileMetadata ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-01-13 07:46:56 -05:00
t . Run ( "should populate file metadata for all posts" , func ( t * testing . T ) {
fileInfo1 , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . Save ( th . Context ,
& model . FileInfo {
CreatorId : th . BasicUser . Id ,
Path : "path.txt" ,
} )
require . NoError ( t , err )
fileInfo2 , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . Save ( th . Context ,
& model . FileInfo {
CreatorId : th . BasicUser . Id ,
Path : "path.txt" ,
} )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
post1 := th . CreatePost ( t , th . BasicChannel , func ( post * model . Post ) {
2025-01-13 07:46:56 -05:00
post . FileIds = model . StringArray { fileInfo1 . Id }
} )
2025-11-12 07:00:51 -05:00
post2 := th . CreatePost ( t , th . BasicChannel , func ( post * model . Post ) {
2025-01-13 07:46:56 -05:00
post . FileIds = model . StringArray { fileInfo2 . Id }
} )
appErr := th . App . populateEditHistoryFileMetadata ( [ ] * model . Post { post1 , post2 } )
require . Nil ( t , appErr )
require . Len ( t , post1 . Metadata . Files , 1 )
require . Equal ( t , fileInfo1 . Id , post1 . Metadata . Files [ 0 ] . Id )
require . Len ( t , post2 . Metadata . Files , 1 )
require . Equal ( t , fileInfo2 . Id , post2 . Metadata . Files [ 0 ] . Id )
} )
t . Run ( "should populate file metadata even for deleted posts" , func ( t * testing . T ) {
fileInfo1 , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . Save ( th . Context ,
& model . FileInfo {
CreatorId : th . BasicUser . Id ,
Path : "path.txt" ,
} )
require . NoError ( t , err )
fileInfo2 , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . Save ( th . Context ,
& model . FileInfo {
CreatorId : th . BasicUser . Id ,
Path : "path.txt" ,
} )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
post1 := th . CreatePost ( t , th . BasicChannel , func ( post * model . Post ) {
2025-01-13 07:46:56 -05:00
post . FileIds = model . StringArray { fileInfo1 . Id }
} )
2025-11-12 07:00:51 -05:00
post2 := th . CreatePost ( t , th . BasicChannel , func ( post * model . Post ) {
2025-01-13 07:46:56 -05:00
post . FileIds = model . StringArray { fileInfo2 . Id }
} )
_ , appErr := th . App . DeletePost ( th . Context , post1 . Id , th . BasicUser . Id )
require . Nil ( t , appErr )
_ , appErr = th . App . DeletePost ( th . Context , post2 . Id , th . BasicUser . Id )
require . Nil ( t , appErr )
appErr = th . App . populateEditHistoryFileMetadata ( [ ] * model . Post { post1 , post2 } )
require . Nil ( t , appErr )
require . Len ( t , post1 . Metadata . Files , 1 )
require . Equal ( t , fileInfo1 . Id , post1 . Metadata . Files [ 0 ] . Id )
require . Len ( t , post2 . Metadata . Files , 1 )
require . Equal ( t , fileInfo2 . Id , post2 . Metadata . Files [ 0 ] . Id )
} )
t . Run ( "should populate file metadata even for deleted fileInfos" , func ( t * testing . T ) {
fileInfo1 , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . Save ( th . Context ,
& model . FileInfo {
CreatorId : th . BasicUser . Id ,
Path : "path.txt" ,
} )
require . NoError ( t , err )
fileInfo2 , err := th . App . Srv ( ) . Store ( ) . FileInfo ( ) . Save ( th . Context ,
& model . FileInfo {
CreatorId : th . BasicUser . Id ,
Path : "path.txt" ,
} )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
post1 := th . CreatePost ( t , th . BasicChannel , func ( post * model . Post ) {
2025-01-13 07:46:56 -05:00
post . FileIds = model . StringArray { fileInfo1 . Id }
} )
2025-11-12 07:00:51 -05:00
post2 := th . CreatePost ( t , th . BasicChannel , func ( post * model . Post ) {
2025-01-13 07:46:56 -05:00
post . FileIds = model . StringArray { fileInfo2 . Id }
} )
_ , err = th . App . Srv ( ) . Store ( ) . FileInfo ( ) . DeleteForPost ( th . Context , post1 . Id )
require . NoError ( t , err )
_ , err = th . App . Srv ( ) . Store ( ) . FileInfo ( ) . DeleteForPost ( th . Context , post2 . Id )
require . NoError ( t , err )
appErr := th . App . populateEditHistoryFileMetadata ( [ ] * model . Post { post1 , post2 } )
require . Nil ( t , appErr )
require . Len ( t , post1 . Metadata . Files , 1 )
require . Equal ( t , fileInfo1 . Id , post1 . Metadata . Files [ 0 ] . Id )
require . Greater ( t , post1 . Metadata . Files [ 0 ] . DeleteAt , int64 ( 0 ) )
require . Len ( t , post2 . Metadata . Files , 1 )
require . Equal ( t , fileInfo2 . Id , post2 . Metadata . Files [ 0 ] . Id )
require . Greater ( t , post2 . Metadata . Files [ 0 ] . DeleteAt , int64 ( 0 ) )
} )
}
2025-12-11 01:59:50 -05:00
2026-01-07 09:21:55 -05:00
func TestFilterPostsByChannelPermissions ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . GuestAccountsSettings . Enable = true
} )
guestUser := th . CreateGuest ( t )
_ , _ , appErr := th . App . AddUserToTeam ( th . Context , th . BasicTeam . Id , guestUser . Id , "" )
require . Nil ( t , appErr )
privateChannel := th . CreatePrivateChannel ( t , th . BasicTeam )
_ , appErr = th . App . AddUserToChannel ( th . Context , guestUser , privateChannel , false )
require . Nil ( t , appErr )
_ , appErr = th . App . AddUserToChannel ( th . Context , guestUser , th . BasicChannel , false )
require . Nil ( t , appErr )
post1 := th . CreatePost ( t , th . BasicChannel )
post2 := th . CreatePost ( t , privateChannel )
post3 := th . CreatePost ( t , th . BasicChannel )
t . Run ( "should filter posts when user has read_channel_content permission" , func ( t * testing . T ) {
postList := model . NewPostList ( )
postList . Posts [ post1 . Id ] = post1
postList . Posts [ post2 . Id ] = post2
postList . Posts [ post3 . Id ] = post3
postList . Order = [ ] string { post1 . Id , post2 . Id , post3 . Id }
2026-01-20 04:38:27 -05:00
allPostHaveMembership , appErr := th . App . FilterPostsByChannelPermissions ( th . Context , postList , th . BasicUser . Id )
2026-01-07 09:21:55 -05:00
require . Nil ( t , appErr )
require . Len ( t , postList . Posts , 3 )
require . Len ( t , postList . Order , 3 )
2026-01-20 04:38:27 -05:00
require . True ( t , allPostHaveMembership )
2026-01-07 09:21:55 -05:00
} )
t . Run ( "should filter posts when guest has read_channel_content permission" , func ( t * testing . T ) {
postList := model . NewPostList ( )
postList . Posts [ post1 . Id ] = post1
postList . Posts [ post2 . Id ] = post2
postList . Posts [ post3 . Id ] = post3
postList . Order = [ ] string { post1 . Id , post2 . Id , post3 . Id }
2026-01-20 04:38:27 -05:00
allPostHaveMembership , appErr := th . App . FilterPostsByChannelPermissions ( th . Context , postList , guestUser . Id )
2026-01-07 09:21:55 -05:00
require . Nil ( t , appErr )
require . Len ( t , postList . Posts , 3 )
require . Len ( t , postList . Order , 3 )
2026-01-20 04:38:27 -05:00
require . True ( t , allPostHaveMembership )
2026-01-07 09:21:55 -05:00
} )
t . Run ( "should filter posts when guest does not have read_channel_content permission" , func ( t * testing . T ) {
channelGuestRole , appErr := th . App . GetRoleByName ( th . Context , model . ChannelGuestRoleId )
require . Nil ( t , appErr )
originalPermissions := make ( [ ] string , len ( channelGuestRole . Permissions ) )
copy ( originalPermissions , channelGuestRole . Permissions )
newPermissions := [ ] string { }
for _ , perm := range channelGuestRole . Permissions {
if perm != model . PermissionReadChannelContent . Id && perm != model . PermissionReadChannel . Id {
newPermissions = append ( newPermissions , perm )
}
}
_ , appErr = th . App . PatchRole ( channelGuestRole , & model . RolePatch {
Permissions : & newPermissions ,
} )
require . Nil ( t , appErr )
defer func ( ) {
_ , err := th . App . PatchRole ( channelGuestRole , & model . RolePatch {
Permissions : & originalPermissions ,
} )
require . Nil ( t , err )
} ( )
postList := model . NewPostList ( )
postList . Posts [ post1 . Id ] = post1
postList . Posts [ post2 . Id ] = post2
postList . Posts [ post3 . Id ] = post3
postList . Order = [ ] string { post1 . Id , post2 . Id , post3 . Id }
2026-01-20 04:38:27 -05:00
allPostHaveMembership , appErr := th . App . FilterPostsByChannelPermissions ( th . Context , postList , guestUser . Id )
2026-01-07 09:21:55 -05:00
require . Nil ( t , appErr )
require . Len ( t , postList . Posts , 0 )
require . Len ( t , postList . Order , 0 )
2026-01-20 04:38:27 -05:00
require . True ( t , allPostHaveMembership )
2026-01-07 09:21:55 -05:00
} )
t . Run ( "should handle empty post list" , func ( t * testing . T ) {
postList := model . NewPostList ( )
2026-01-20 04:38:27 -05:00
allPostHaveMembership , appErr := th . App . FilterPostsByChannelPermissions ( th . Context , postList , th . BasicUser . Id )
2026-01-07 09:21:55 -05:00
require . Nil ( t , appErr )
require . Len ( t , postList . Posts , 0 )
require . Len ( t , postList . Order , 0 )
2026-01-20 04:38:27 -05:00
require . True ( t , allPostHaveMembership )
2026-01-07 09:21:55 -05:00
} )
t . Run ( "should handle nil post list" , func ( t * testing . T ) {
2026-01-20 04:38:27 -05:00
_ , appErr := th . App . FilterPostsByChannelPermissions ( th . Context , nil , th . BasicUser . Id )
2026-01-07 09:21:55 -05:00
require . Nil ( t , appErr )
} )
t . Run ( "should handle posts with empty channel IDs" , func ( t * testing . T ) {
postList := model . NewPostList ( )
postWithoutChannel := & model . Post {
Id : model . NewId ( ) ,
ChannelId : "" ,
Message : "test" ,
}
postList . Posts [ postWithoutChannel . Id ] = postWithoutChannel
postList . Order = [ ] string { postWithoutChannel . Id }
2026-01-20 04:38:27 -05:00
allPostHaveMembership , appErr := th . App . FilterPostsByChannelPermissions ( th . Context , postList , th . BasicUser . Id )
2026-01-07 09:21:55 -05:00
require . Nil ( t , appErr )
require . Len ( t , postList . Posts , 0 )
require . Len ( t , postList . Order , 0 )
2026-01-20 04:38:27 -05:00
require . True ( t , allPostHaveMembership )
2026-01-07 09:21:55 -05:00
} )
t . Run ( "should handle posts from non-existent channels" , func ( t * testing . T ) {
postList := model . NewPostList ( )
postWithInvalidChannel := & model . Post {
Id : model . NewId ( ) ,
ChannelId : model . NewId ( ) ,
Message : "test" ,
}
postList . Posts [ postWithInvalidChannel . Id ] = postWithInvalidChannel
postList . Order = [ ] string { postWithInvalidChannel . Id }
2026-01-20 04:38:27 -05:00
allPostHaveMembership , appErr := th . App . FilterPostsByChannelPermissions ( th . Context , postList , th . BasicUser . Id )
2026-01-07 09:21:55 -05:00
require . Nil ( t , appErr )
require . Len ( t , postList . Posts , 0 )
require . Len ( t , postList . Order , 0 )
2026-01-20 04:38:27 -05:00
require . True ( t , allPostHaveMembership )
2026-01-07 09:21:55 -05:00
} )
}
2025-12-11 01:59:50 -05:00
func TestRevealPost ( t * testing . T ) {
th := Setup ( t ) . InitBasic ( t )
ci: enable fullyparallel mode for server tests (#35816)
* ci: enable fullyparallel mode for server tests
Replace os.Setenv, os.Chdir, and global state mutations with
parallel-safe alternatives (t.Setenv, t.Chdir, test hooks) across
37 files. Refactor GetLogRootPath and MM_INSTALL_TYPE to use
package-level test hooks instead of environment variables.
This enables gotestsum --fullparallel, allowing all test packages
to run with maximum parallelism within each shard.
Co-authored-by: Claude <claude@anthropic.com>
* ci: split fullyparallel from continue-on-error in workflow template
- Add new boolean input 'allow-failure' separate from 'fullyparallel'
- Change continue-on-error to use allow-failure instead of fullyparallel
- Update server-ci.yml to pass allow-failure: true for test coverage job
- Allows independent control of parallel execution and failure tolerance
Co-authored-by: Claude <claude@anthropic.com>
* fix: protect TestOverrideLogRootPath with sync.Mutex for parallel tests
- Replace global var TestOverrideLogRootPath with mutex-protected functions
- Add SetTestOverrideLogRootPath() and getTestOverrideLogRootPath() functions
- Update GetLogRootPath() to use thread-safe getter
- Update all test files to use SetTestOverrideLogRootPath() with t.Cleanup()
- Fixes race condition when running tests with t.Parallel()
Co-authored-by: Claude <claude@anthropic.com>
* fix: configure audit settings before server setup in tests
- Move ExperimentalAuditSettings from UpdateConfig() to config defaults
- Pass audit config via app.Config() option in SetupWithServerOptions()
- Fixes audit test setup ordering to configure BEFORE server initialization
- Resolves CodeRabbit's audit config timing issue in api4 tests
Co-authored-by: Claude <claude@anthropic.com>
* fix: implement SetTestOverrideLogRootPath mutex in logger.go
The previous commit updated test callers to use SetTestOverrideLogRootPath()
but didn't actually create the function in config/logger.go, causing build
failures across all CI shards. This commit:
- Replaces the exported var TestOverrideLogRootPath with mutex-protected
unexported state (testOverrideLogRootPath + testOverrideLogRootMu)
- Adds exported SetTestOverrideLogRootPath() setter
- Adds unexported getTestOverrideLogRootPath() getter
- Updates GetLogRootPath() to use the thread-safe getter
- Fixes log_test.go callers that were missed in the previous commit
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): use SetupConfig for access_control feature flag registration
InitAccessControlPolicy() checks FeatureFlags.AttributeBasedAccessControl
at route registration time during server startup. Setting the flag via
UpdateConfig after Setup() is too late — routes are never registered
and API calls return 404.
Use SetupConfig() to pass the feature flag in the initial config before
server startup, ensuring routes are properly registered.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore BurnOnRead flag state in TestRevealPost subtest
The 'feature not enabled' subtest disables BurnOnRead without restoring
it via t.Cleanup. Subsequent subtests inherit the disabled state, which
can cause 501 errors when they expect the feature to be available.
Add t.Cleanup to restore FeatureFlags.BurnOnRead = true after the
subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore EnableSharedChannelsMemberSync flag via t.Cleanup
The test disables EnableSharedChannelsMemberSync without restoring it.
If the subtest exits early (e.g., require failure), later sibling
subtests inherit a disabled flag and become flaky.
Add t.Cleanup to restore the flag after the subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* Fix test parallelism: use instance-scoped overrides and init-time audit config
Replace package-level test globals (TestOverrideInstallType,
SetTestOverrideLogRootPath) with fields on PlatformService so each test
gets its own instance without process-wide mutation. Fix three audit
tests (TestUserLoginAudit, TestLogoutAuditAuthStatus,
TestUpdatePasswordAudit) that configured the audit logger after server
init — the audit logger only reads config at startup, so pass audit
settings via app.Config() at init time instead.
Also revert the Go 1.24.13 downgrade and bump mattermost-govet to
v2.0.2 for Go 1.25.8 compatibility.
* Fix audit unit tests
* Fix MMCLOUDURL unit tests
* Fixed unit tests using MM_NOTIFY_ADMIN_COOL_OFF_DAYS
* Make app migrations idempotent for parallel test safety
Change System().Save() to System().SaveOrUpdate() in all migration
completion markers. When two parallel tests share a database pool entry,
both may race through the check-then-insert migration pattern. Save()
causes a duplicate key fatal crash; SaveOrUpdate() makes the second
write a harmless no-op.
* test: address review feedback on fullyparallel PR
- Use SetLogRootPathOverride() setter instead of direct field access
in platform/support_packet_test.go and platform/log_test.go (pvev)
- Restore TestGetLogRootPath in config/logger_test.go to keep
MM_LOG_PATH env var coverage; test uses t.Setenv so it runs
serially which is fine (pvev)
- Fix misleading comment in config_test.go: code uses t.Setenv,
not os.Setenv (jgheithcock)
Co-authored-by: Claude <claude@anthropic.com>
* fix: add missing os import in post_test.go
The os import was dropped during a merge conflict resolution while
burn-on-read shared channel tests from master still use os.Setenv.
Co-authored-by: Claude <claude@anthropic.com>
---------
Co-authored-by: Claude <claude@anthropic.com>
Co-authored-by: wiggin77 <wiggin77@warpmail.net>
Co-authored-by: Mattermost Build <build@mattermost.com>
2026-04-08 20:48:36 -04:00
// Enable BurnOnRead feature flag
th . App . UpdateConfig ( func ( cfg * model . Config ) { cfg . FeatureFlags . BurnOnRead = true } )
2025-12-11 01:59:50 -05:00
// Helper to create a burn-on-read post
createBurnOnReadPost := func ( ) * model . Post {
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "burn on read message" ,
Type : model . PostTypeBurnOnRead ,
}
post . AddProp ( model . PostPropsExpireAt , model . GetMillis ( ) + int64 ( model . DefaultExpirySeconds * 1000 ) )
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { } )
2025-12-11 01:59:50 -05:00
require . Nil ( t , appErr )
require . NotNil ( t , createdPost )
return createdPost
}
// Helper to create a regular post
createRegularPost := func ( ) * model . Post {
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "regular message" ,
}
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { } )
2025-12-11 01:59:50 -05:00
require . Nil ( t , appErr )
require . NotNil ( t , createdPost )
return createdPost
}
// Create a second user for testing
user2 := th . CreateUser ( t )
th . LinkUserToTeam ( t , user2 , th . BasicTeam )
th . AddUserToChannel ( t , user2 , th . BasicChannel )
t . Run ( "post type non burn on read" , func ( t * testing . T ) {
regularPost := createRegularPost ( )
revealedPost , appErr := th . App . RevealPost ( th . Context , regularPost , user2 . Id , "" )
require . NotNil ( t , appErr )
require . Nil ( t , revealedPost )
require . Equal ( t , "app.reveal_post.not_burn_on_read.app_error" , appErr . Id )
require . Equal ( t , http . StatusBadRequest , appErr . StatusCode )
} )
t . Run ( "post doesn't have required prop" , func ( t * testing . T ) {
enableBoRFeature ( th )
// Create a burn-on-read post without expire_at prop
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "burn on read message" ,
Type : model . PostTypeBurnOnRead ,
}
// First save the post normally (which will add expire_at automatically)
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { } )
2025-12-11 01:59:50 -05:00
require . Nil ( t , appErr )
// Now manually remove the expire_at prop to test missing prop scenario
createdPost . SetProps ( make ( model . StringInterface ) )
revealedPost , appErr := th . App . RevealPost ( th . Context , createdPost , user2 . Id , "" )
require . NotNil ( t , appErr )
require . Nil ( t , revealedPost )
require . Equal ( t , "app.reveal_post.missing_expire_at.app_error" , appErr . Id )
require . Equal ( t , http . StatusBadRequest , appErr . StatusCode )
} )
t . Run ( "post with invalid expire_at prop type" , func ( t * testing . T ) {
enableBoRFeature ( th )
post := createBurnOnReadPost ( )
// Manually set invalid expire_at type
post . SetProps ( make ( model . StringInterface ) )
post . AddProp ( model . PostPropsExpireAt , "invalid_string" )
revealedPost , appErr := th . App . RevealPost ( th . Context , post , user2 . Id , "" )
require . NotNil ( t , appErr )
require . Nil ( t , revealedPost )
require . Equal ( t , "app.reveal_post.missing_expire_at.app_error" , appErr . Id )
} )
t . Run ( "post with zero expire_at" , func ( t * testing . T ) {
enableBoRFeature ( th )
post := createBurnOnReadPost ( )
// Manually set zero expire_at
post . SetProps ( make ( model . StringInterface ) )
post . AddProp ( model . PostPropsExpireAt , float64 ( 0 ) )
revealedPost , appErr := th . App . RevealPost ( th . Context , post , user2 . Id , "" )
require . NotNil ( t , appErr )
require . Nil ( t , revealedPost )
require . Equal ( t , "app.reveal_post.missing_expire_at.app_error" , appErr . Id )
} )
t . Run ( "read receipt does not exist" , func ( t * testing . T ) {
enableBoRFeature ( th )
post := createBurnOnReadPost ( )
revealedPost , appErr := th . App . RevealPost ( th . Context , post , user2 . Id , "" )
require . Nil ( t , appErr )
require . NotNil ( t , revealedPost )
require . Equal ( t , "burn on read message" , revealedPost . Message )
require . NotNil ( t , revealedPost . Metadata )
require . NotZero ( t , revealedPost . Metadata . ExpireAt )
// Verify read receipt was created
receipt , err := th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Get ( th . Context , post . Id , user2 . Id )
require . NoError ( t , err )
require . NotNil ( t , receipt )
require . Equal ( t , post . Id , receipt . PostID )
require . Equal ( t , user2 . Id , receipt . UserID )
require . Equal ( t , revealedPost . Metadata . ExpireAt , receipt . ExpireAt )
} )
t . Run ( "read receipt exists and not expired" , func ( t * testing . T ) {
enableBoRFeature ( th )
post := createBurnOnReadPost ( )
// First reveal to create receipt
revealedPost1 , appErr := th . App . RevealPost ( th . Context , post , user2 . Id , "" )
require . Nil ( t , appErr )
require . NotNil ( t , revealedPost1 )
require . NotZero ( t , revealedPost1 . Metadata . ExpireAt )
// Reveal again - should succeed and return the same post
revealedPost2 , appErr := th . App . RevealPost ( th . Context , post , user2 . Id , "" )
require . Nil ( t , appErr )
require . NotNil ( t , revealedPost2 )
require . Equal ( t , "burn on read message" , revealedPost2 . Message )
require . NotNil ( t , revealedPost2 . Metadata )
require . Equal ( t , revealedPost1 . Metadata . ExpireAt , revealedPost2 . Metadata . ExpireAt )
} )
t . Run ( "read receipt exists but expired" , func ( t * testing . T ) {
enableBoRFeature ( th )
post := createBurnOnReadPost ( )
// Create an expired read receipt
expiredReceipt := & model . ReadReceipt {
UserID : user2 . Id ,
PostID : post . Id ,
ExpireAt : model . GetMillis ( ) - 1000 , // Expired 1 second ago
}
_ , err := th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Save ( th . Context , expiredReceipt )
require . NoError ( t , err )
revealedPost , appErr := th . App . RevealPost ( th . Context , post , user2 . Id , "" )
require . NotNil ( t , appErr )
require . Nil ( t , revealedPost )
require . Equal ( t , "app.reveal_post.read_receipt_expired.error" , appErr . Id )
require . Equal ( t , http . StatusForbidden , appErr . StatusCode )
} )
t . Run ( "post expired" , func ( t * testing . T ) {
post := createBurnOnReadPost ( )
// Manually set expired expire_at
post . SetProps ( make ( model . StringInterface ) )
post . AddProp ( model . PostPropsExpireAt , model . GetMillis ( ) - 1000 )
revealedPost , appErr := th . App . RevealPost ( th . Context , post , user2 . Id , "" )
require . NotNil ( t , appErr )
require . Nil ( t , revealedPost )
require . Equal ( t , "app.reveal_post.post_expired.app_error" , appErr . Id )
require . Equal ( t , http . StatusBadRequest , appErr . StatusCode )
} )
t . Run ( "revealed post preserves existing metadata" , func ( t * testing . T ) {
enableBoRFeature ( th )
fileBytes := [ ] byte ( "test" )
fileInfo , err := th . App . UploadFile ( th . Context , fileBytes , th . BasicChannel . Id , "file.txt" )
require . Nil ( t , err )
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "burn on read message" ,
Type : model . PostTypeBurnOnRead ,
FileIds : model . StringArray { fileInfo . Id } ,
}
post . AddProp ( model . PostPropsExpireAt , model . GetMillis ( ) + int64 ( model . DefaultExpirySeconds * 1000 ) )
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { } )
2025-12-11 01:59:50 -05:00
require . Nil ( t , appErr )
require . NotNil ( t , createdPost )
revealedPost , appErr := th . App . RevealPost ( th . Context , createdPost , user2 . Id , "" )
require . Nil ( t , appErr )
require . NotNil ( t , revealedPost )
require . NotNil ( t , revealedPost . Metadata )
require . NotZero ( t , revealedPost . Metadata . ExpireAt )
require . Len ( t , revealedPost . Metadata . Files , 1 )
} )
2026-02-09 15:47:27 -05:00
t . Run ( "updateTemporaryPostIfAllRead bypasses cache to get fresh data" , func ( t * testing . T ) {
enableBoRFeature ( th )
// Create a third user to have multiple recipients
user3 := th . CreateUser ( t )
th . LinkUserToTeam ( t , user3 , th . BasicTeam )
th . AddUserToChannel ( t , user3 , th . BasicChannel )
post := createBurnOnReadPost ( )
// Get initial TemporaryPost ExpireAt (should be max TTL - 24 hours)
tmpPostInitial , err := th . App . Srv ( ) . Store ( ) . TemporaryPost ( ) . Get ( th . Context , post . Id , true )
require . NoError ( t , err )
initialExpireAt := tmpPostInitial . ExpireAt
// First reveal by user2 creates a read receipt
_ , appErr := th . App . RevealPost ( th . Context , post , user2 . Id , "" )
require . Nil ( t , appErr )
// Get the read receipt's ExpireAt (should be 5 minutes)
receipt , err := th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Get ( th . Context , post . Id , user2 . Id )
require . NoError ( t , err )
// Verify receipt ExpireAt is less than initial tmpPost ExpireAt (5 minutes vs 24 hours)
require . Less ( t , receipt . ExpireAt , initialExpireAt )
// At this point, user3 hasn't revealed yet, so TemporaryPost shouldn't be updated
tmpPostAfterFirstReveal , err := th . App . Srv ( ) . Store ( ) . TemporaryPost ( ) . Get ( th . Context , post . Id , false )
require . NoError ( t , err )
require . Equal ( t , initialExpireAt , tmpPostAfterFirstReveal . ExpireAt )
// Now user3 reveals - this should trigger updateTemporaryPostIfAllRead
_ , appErr = th . App . RevealPost ( th . Context , post , user3 . Id , "" )
require . Nil ( t , appErr )
// Fetch the TemporaryPost with cache bypass to verify it was updated
tmpPostAfterAllReveal , err := th . App . Srv ( ) . Store ( ) . TemporaryPost ( ) . Get ( th . Context , post . Id , false )
require . NoError ( t , err )
// Verify the ExpireAt was updated to match the shortest receipt ExpireAt
require . Less ( t , tmpPostAfterAllReveal . ExpireAt , initialExpireAt )
// Should be close to the first recipient's ExpireAt (within a few ms due to timing)
require . InDelta ( t , receipt . ExpireAt , tmpPostAfterAllReveal . ExpireAt , 10000 ) // 10 second tolerance
} )
2025-12-11 01:59:50 -05:00
}
func TestBurnPost ( t * testing . T ) {
th := Setup ( t ) . InitBasic ( t )
ci: enable fullyparallel mode for server tests (#35816)
* ci: enable fullyparallel mode for server tests
Replace os.Setenv, os.Chdir, and global state mutations with
parallel-safe alternatives (t.Setenv, t.Chdir, test hooks) across
37 files. Refactor GetLogRootPath and MM_INSTALL_TYPE to use
package-level test hooks instead of environment variables.
This enables gotestsum --fullparallel, allowing all test packages
to run with maximum parallelism within each shard.
Co-authored-by: Claude <claude@anthropic.com>
* ci: split fullyparallel from continue-on-error in workflow template
- Add new boolean input 'allow-failure' separate from 'fullyparallel'
- Change continue-on-error to use allow-failure instead of fullyparallel
- Update server-ci.yml to pass allow-failure: true for test coverage job
- Allows independent control of parallel execution and failure tolerance
Co-authored-by: Claude <claude@anthropic.com>
* fix: protect TestOverrideLogRootPath with sync.Mutex for parallel tests
- Replace global var TestOverrideLogRootPath with mutex-protected functions
- Add SetTestOverrideLogRootPath() and getTestOverrideLogRootPath() functions
- Update GetLogRootPath() to use thread-safe getter
- Update all test files to use SetTestOverrideLogRootPath() with t.Cleanup()
- Fixes race condition when running tests with t.Parallel()
Co-authored-by: Claude <claude@anthropic.com>
* fix: configure audit settings before server setup in tests
- Move ExperimentalAuditSettings from UpdateConfig() to config defaults
- Pass audit config via app.Config() option in SetupWithServerOptions()
- Fixes audit test setup ordering to configure BEFORE server initialization
- Resolves CodeRabbit's audit config timing issue in api4 tests
Co-authored-by: Claude <claude@anthropic.com>
* fix: implement SetTestOverrideLogRootPath mutex in logger.go
The previous commit updated test callers to use SetTestOverrideLogRootPath()
but didn't actually create the function in config/logger.go, causing build
failures across all CI shards. This commit:
- Replaces the exported var TestOverrideLogRootPath with mutex-protected
unexported state (testOverrideLogRootPath + testOverrideLogRootMu)
- Adds exported SetTestOverrideLogRootPath() setter
- Adds unexported getTestOverrideLogRootPath() getter
- Updates GetLogRootPath() to use the thread-safe getter
- Fixes log_test.go callers that were missed in the previous commit
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): use SetupConfig for access_control feature flag registration
InitAccessControlPolicy() checks FeatureFlags.AttributeBasedAccessControl
at route registration time during server startup. Setting the flag via
UpdateConfig after Setup() is too late — routes are never registered
and API calls return 404.
Use SetupConfig() to pass the feature flag in the initial config before
server startup, ensuring routes are properly registered.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore BurnOnRead flag state in TestRevealPost subtest
The 'feature not enabled' subtest disables BurnOnRead without restoring
it via t.Cleanup. Subsequent subtests inherit the disabled state, which
can cause 501 errors when they expect the feature to be available.
Add t.Cleanup to restore FeatureFlags.BurnOnRead = true after the
subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore EnableSharedChannelsMemberSync flag via t.Cleanup
The test disables EnableSharedChannelsMemberSync without restoring it.
If the subtest exits early (e.g., require failure), later sibling
subtests inherit a disabled flag and become flaky.
Add t.Cleanup to restore the flag after the subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* Fix test parallelism: use instance-scoped overrides and init-time audit config
Replace package-level test globals (TestOverrideInstallType,
SetTestOverrideLogRootPath) with fields on PlatformService so each test
gets its own instance without process-wide mutation. Fix three audit
tests (TestUserLoginAudit, TestLogoutAuditAuthStatus,
TestUpdatePasswordAudit) that configured the audit logger after server
init — the audit logger only reads config at startup, so pass audit
settings via app.Config() at init time instead.
Also revert the Go 1.24.13 downgrade and bump mattermost-govet to
v2.0.2 for Go 1.25.8 compatibility.
* Fix audit unit tests
* Fix MMCLOUDURL unit tests
* Fixed unit tests using MM_NOTIFY_ADMIN_COOL_OFF_DAYS
* Make app migrations idempotent for parallel test safety
Change System().Save() to System().SaveOrUpdate() in all migration
completion markers. When two parallel tests share a database pool entry,
both may race through the check-then-insert migration pattern. Save()
causes a duplicate key fatal crash; SaveOrUpdate() makes the second
write a harmless no-op.
* test: address review feedback on fullyparallel PR
- Use SetLogRootPathOverride() setter instead of direct field access
in platform/support_packet_test.go and platform/log_test.go (pvev)
- Restore TestGetLogRootPath in config/logger_test.go to keep
MM_LOG_PATH env var coverage; test uses t.Setenv so it runs
serially which is fine (pvev)
- Fix misleading comment in config_test.go: code uses t.Setenv,
not os.Setenv (jgheithcock)
Co-authored-by: Claude <claude@anthropic.com>
* fix: add missing os import in post_test.go
The os import was dropped during a merge conflict resolution while
burn-on-read shared channel tests from master still use os.Setenv.
Co-authored-by: Claude <claude@anthropic.com>
---------
Co-authored-by: Claude <claude@anthropic.com>
Co-authored-by: wiggin77 <wiggin77@warpmail.net>
Co-authored-by: Mattermost Build <build@mattermost.com>
2026-04-08 20:48:36 -04:00
// Enable BurnOnRead feature flag
th . App . UpdateConfig ( func ( cfg * model . Config ) { cfg . FeatureFlags . BurnOnRead = true } )
2025-12-11 01:59:50 -05:00
// feature flag, configuration and license is not checked for this feature
// so we set these to enable the feature to create a burn on read post
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
cfg . ServiceSettings . EnableBurnOnRead = model . NewPointer ( true )
} )
th . AddUserToChannel ( t , th . BasicUser , th . BasicChannel ) // author of the post
th . AddUserToChannel ( t , th . BasicUser2 , th . BasicChannel ) // recipient of the post
// Helper to create a burn-on-read post
createBurnOnReadPost := func ( ) * model . Post {
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "burn on read message" ,
Type : model . PostTypeBurnOnRead ,
}
post . AddProp ( model . PostPropsExpireAt , model . GetMillis ( ) + int64 ( model . DefaultExpirySeconds * 1000 ) )
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { } )
2025-12-11 01:59:50 -05:00
require . Nil ( t , appErr )
require . NotNil ( t , createdPost )
return createdPost
}
// Helper to create a regular post
createRegularPost := func ( ) * model . Post {
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "regular message" ,
}
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { } )
2025-12-11 01:59:50 -05:00
require . Nil ( t , appErr )
require . NotNil ( t , createdPost )
return createdPost
}
t . Run ( "burn on read post" , func ( t * testing . T ) {
post := createBurnOnReadPost ( )
appErr := th . App . BurnPost ( th . Context , post , th . BasicUser . Id , "" )
require . Nil ( t , appErr )
// Verify post is deleted
post , err := th . App . Srv ( ) . Store ( ) . Post ( ) . GetSingle ( th . Context , post . Id , false )
require . Error ( t , err )
require . Nil ( t , post )
require . True ( t , store . IsErrNotFound ( err ) )
} )
t . Run ( "regular post" , func ( t * testing . T ) {
post := createRegularPost ( )
appErr := th . App . BurnPost ( th . Context , post , th . BasicUser . Id , "" )
require . NotNil ( t , appErr )
require . Equal ( t , "app.burn_post.not_burn_on_read.app_error" , appErr . Id )
} )
t . Run ( "read receipt does not exist" , func ( t * testing . T ) {
post := createBurnOnReadPost ( )
appErr := th . App . BurnPost ( th . Context , post , th . BasicUser2 . Id , "" )
require . NotNil ( t , appErr )
require . Equal ( t , "app.burn_post.not_revealed.app_error" , appErr . Id )
} )
t . Run ( "read receipt exists but expired" , func ( t * testing . T ) {
post := createBurnOnReadPost ( )
// Create an expired read receipt
expiredTime := model . GetMillis ( ) - 1000 // Expired 1 second ago
expiredReceipt := & model . ReadReceipt {
UserID : th . BasicUser2 . Id ,
PostID : post . Id ,
ExpireAt : expiredTime ,
}
_ , err := th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Save ( th . Context , expiredReceipt )
require . NoError ( t , err )
appErr := th . App . BurnPost ( th . Context , post , th . BasicUser2 . Id , "" )
require . Nil ( t , appErr ) // this is a no-op
// Verify receipt ExpireAt is unchanged (no-op)
receipt , err := th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Get ( th . Context , post . Id , th . BasicUser2 . Id )
require . NoError ( t , err )
require . Equal ( t , expiredTime , receipt . ExpireAt )
} )
t . Run ( "read receipt exists and not expired" , func ( t * testing . T ) {
post := createBurnOnReadPost ( )
// Create a read receipt that is not expired
notExpiredReceipt := & model . ReadReceipt {
UserID : th . BasicUser2 . Id ,
PostID : post . Id ,
ExpireAt : model . GetMillis ( ) + 10000 , // Not expired 10 seconds from now
}
_ , err := th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Save ( th . Context , notExpiredReceipt )
require . NoError ( t , err )
appErr := th . App . BurnPost ( th . Context , post , th . BasicUser2 . Id , "" )
require . Nil ( t , appErr )
// Verify receipt ExpireAt is updated to current time
receipt , err := th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Get ( th . Context , post . Id , th . BasicUser2 . Id )
require . NoError ( t , err )
require . LessOrEqual ( t , receipt . ExpireAt , model . GetMillis ( ) )
} )
}
2025-12-17 05:45:44 -05:00
func TestGetFlaggedPostsWithExpiredBurnOnRead ( t * testing . T ) {
th := Setup ( t ) . InitBasic ( t )
ci: enable fullyparallel mode for server tests (#35816)
* ci: enable fullyparallel mode for server tests
Replace os.Setenv, os.Chdir, and global state mutations with
parallel-safe alternatives (t.Setenv, t.Chdir, test hooks) across
37 files. Refactor GetLogRootPath and MM_INSTALL_TYPE to use
package-level test hooks instead of environment variables.
This enables gotestsum --fullparallel, allowing all test packages
to run with maximum parallelism within each shard.
Co-authored-by: Claude <claude@anthropic.com>
* ci: split fullyparallel from continue-on-error in workflow template
- Add new boolean input 'allow-failure' separate from 'fullyparallel'
- Change continue-on-error to use allow-failure instead of fullyparallel
- Update server-ci.yml to pass allow-failure: true for test coverage job
- Allows independent control of parallel execution and failure tolerance
Co-authored-by: Claude <claude@anthropic.com>
* fix: protect TestOverrideLogRootPath with sync.Mutex for parallel tests
- Replace global var TestOverrideLogRootPath with mutex-protected functions
- Add SetTestOverrideLogRootPath() and getTestOverrideLogRootPath() functions
- Update GetLogRootPath() to use thread-safe getter
- Update all test files to use SetTestOverrideLogRootPath() with t.Cleanup()
- Fixes race condition when running tests with t.Parallel()
Co-authored-by: Claude <claude@anthropic.com>
* fix: configure audit settings before server setup in tests
- Move ExperimentalAuditSettings from UpdateConfig() to config defaults
- Pass audit config via app.Config() option in SetupWithServerOptions()
- Fixes audit test setup ordering to configure BEFORE server initialization
- Resolves CodeRabbit's audit config timing issue in api4 tests
Co-authored-by: Claude <claude@anthropic.com>
* fix: implement SetTestOverrideLogRootPath mutex in logger.go
The previous commit updated test callers to use SetTestOverrideLogRootPath()
but didn't actually create the function in config/logger.go, causing build
failures across all CI shards. This commit:
- Replaces the exported var TestOverrideLogRootPath with mutex-protected
unexported state (testOverrideLogRootPath + testOverrideLogRootMu)
- Adds exported SetTestOverrideLogRootPath() setter
- Adds unexported getTestOverrideLogRootPath() getter
- Updates GetLogRootPath() to use the thread-safe getter
- Fixes log_test.go callers that were missed in the previous commit
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): use SetupConfig for access_control feature flag registration
InitAccessControlPolicy() checks FeatureFlags.AttributeBasedAccessControl
at route registration time during server startup. Setting the flag via
UpdateConfig after Setup() is too late — routes are never registered
and API calls return 404.
Use SetupConfig() to pass the feature flag in the initial config before
server startup, ensuring routes are properly registered.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore BurnOnRead flag state in TestRevealPost subtest
The 'feature not enabled' subtest disables BurnOnRead without restoring
it via t.Cleanup. Subsequent subtests inherit the disabled state, which
can cause 501 errors when they expect the feature to be available.
Add t.Cleanup to restore FeatureFlags.BurnOnRead = true after the
subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore EnableSharedChannelsMemberSync flag via t.Cleanup
The test disables EnableSharedChannelsMemberSync without restoring it.
If the subtest exits early (e.g., require failure), later sibling
subtests inherit a disabled flag and become flaky.
Add t.Cleanup to restore the flag after the subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* Fix test parallelism: use instance-scoped overrides and init-time audit config
Replace package-level test globals (TestOverrideInstallType,
SetTestOverrideLogRootPath) with fields on PlatformService so each test
gets its own instance without process-wide mutation. Fix three audit
tests (TestUserLoginAudit, TestLogoutAuditAuthStatus,
TestUpdatePasswordAudit) that configured the audit logger after server
init — the audit logger only reads config at startup, so pass audit
settings via app.Config() at init time instead.
Also revert the Go 1.24.13 downgrade and bump mattermost-govet to
v2.0.2 for Go 1.25.8 compatibility.
* Fix audit unit tests
* Fix MMCLOUDURL unit tests
* Fixed unit tests using MM_NOTIFY_ADMIN_COOL_OFF_DAYS
* Make app migrations idempotent for parallel test safety
Change System().Save() to System().SaveOrUpdate() in all migration
completion markers. When two parallel tests share a database pool entry,
both may race through the check-then-insert migration pattern. Save()
causes a duplicate key fatal crash; SaveOrUpdate() makes the second
write a harmless no-op.
* test: address review feedback on fullyparallel PR
- Use SetLogRootPathOverride() setter instead of direct field access
in platform/support_packet_test.go and platform/log_test.go (pvev)
- Restore TestGetLogRootPath in config/logger_test.go to keep
MM_LOG_PATH env var coverage; test uses t.Setenv so it runs
serially which is fine (pvev)
- Fix misleading comment in config_test.go: code uses t.Setenv,
not os.Setenv (jgheithcock)
Co-authored-by: Claude <claude@anthropic.com>
* fix: add missing os import in post_test.go
The os import was dropped during a merge conflict resolution while
burn-on-read shared channel tests from master still use os.Setenv.
Co-authored-by: Claude <claude@anthropic.com>
---------
Co-authored-by: Claude <claude@anthropic.com>
Co-authored-by: wiggin77 <wiggin77@warpmail.net>
Co-authored-by: Mattermost Build <build@mattermost.com>
2026-04-08 20:48:36 -04:00
// Enable BurnOnRead feature flag
th . App . UpdateConfig ( func ( cfg * model . Config ) { cfg . FeatureFlags . BurnOnRead = true } )
2025-12-17 05:45:44 -05:00
// Create a second user for testing
user2 := th . CreateUser ( t )
th . LinkUserToTeam ( t , user2 , th . BasicTeam )
th . AddUserToChannel ( t , user2 , th . BasicChannel )
t . Run ( "expired burn-on-read post should not be returned in flagged posts" , func ( t * testing . T ) {
enableBoRFeature ( th )
// Create a burn-on-read post
borPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "burn on read message" ,
Type : model . PostTypeBurnOnRead ,
}
borPost . AddProp ( model . PostPropsExpireAt , model . GetMillis ( ) + int64 ( 10 * 1000 ) ) // 10 seconds
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , borPost , th . BasicChannel , model . CreatePostFlags { } )
2025-12-17 05:45:44 -05:00
require . Nil ( t , appErr )
require . NotNil ( t , createdPost )
// User2 reveals the post
revealedPost , appErr := th . App . RevealPost ( th . Context , createdPost , user2 . Id , "" )
require . Nil ( t , appErr )
require . NotNil ( t , revealedPost )
// User2 saves/flags the post
preference := model . Preference {
UserId : user2 . Id ,
Category : model . PreferenceCategoryFlaggedPost ,
Name : createdPost . Id ,
Value : "true" ,
}
err := th . App . Srv ( ) . Store ( ) . Preference ( ) . Save ( model . Preferences { preference } )
require . NoError ( t , err )
// Verify post appears in flagged posts before expiration
flaggedPosts , appErr := th . App . GetFlaggedPosts ( th . Context , user2 . Id , 0 , 10 )
require . Nil ( t , appErr )
require . NotNil ( t , flaggedPosts )
require . Contains ( t , flaggedPosts . Order , createdPost . Id )
require . NotNil ( t , flaggedPosts . Posts [ createdPost . Id ] )
// Simulate expiration by updating the receipt's ExpireAt to the past
receipt , err := th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Get ( th . Context , createdPost . Id , user2 . Id )
require . NoError ( t , err )
require . NotNil ( t , receipt )
receipt . ExpireAt = model . GetMillis ( ) - 1000 // 1 second in the past
_ , err = th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Update ( th . Context , receipt )
require . NoError ( t , err )
// Get flagged posts again - expired post should be filtered out
flaggedPosts , appErr = th . App . GetFlaggedPosts ( th . Context , user2 . Id , 0 , 10 )
require . Nil ( t , appErr )
require . NotNil ( t , flaggedPosts )
require . NotContains ( t , flaggedPosts . Order , createdPost . Id , "Expired burn-on-read post should not be in flagged posts" )
require . Nil ( t , flaggedPosts . Posts [ createdPost . Id ] , "Expired burn-on-read post should not be in posts map" )
} )
t . Run ( "expired burn-on-read post should not be returned in flagged posts for team" , func ( t * testing . T ) {
enableBoRFeature ( th )
// Create a burn-on-read post
borPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "burn on read team message" ,
Type : model . PostTypeBurnOnRead ,
}
borPost . AddProp ( model . PostPropsExpireAt , model . GetMillis ( ) + int64 ( 10 * 1000 ) )
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , borPost , th . BasicChannel , model . CreatePostFlags { } )
2025-12-17 05:45:44 -05:00
require . Nil ( t , appErr )
// User2 reveals and flags the post
revealedPost , appErr := th . App . RevealPost ( th . Context , createdPost , user2 . Id , "" )
require . Nil ( t , appErr )
require . NotNil ( t , revealedPost )
preference := model . Preference {
UserId : user2 . Id ,
Category : model . PreferenceCategoryFlaggedPost ,
Name : createdPost . Id ,
Value : "true" ,
}
err := th . App . Srv ( ) . Store ( ) . Preference ( ) . Save ( model . Preferences { preference } )
require . NoError ( t , err )
// Expire the receipt
receipt , err := th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Get ( th . Context , createdPost . Id , user2 . Id )
require . NoError ( t , err )
receipt . ExpireAt = model . GetMillis ( ) - 1000
_ , err = th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Update ( th . Context , receipt )
require . NoError ( t , err )
// Get flagged posts for team
flaggedPosts , appErr := th . App . GetFlaggedPostsForTeam ( th . Context , user2 . Id , th . BasicTeam . Id , 0 , 10 )
require . Nil ( t , appErr )
require . NotContains ( t , flaggedPosts . Order , createdPost . Id )
} )
t . Run ( "expired burn-on-read post should not be returned in flagged posts for channel" , func ( t * testing . T ) {
enableBoRFeature ( th )
// Create a burn-on-read post
borPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "burn on read channel message" ,
Type : model . PostTypeBurnOnRead ,
}
borPost . AddProp ( model . PostPropsExpireAt , model . GetMillis ( ) + int64 ( 10 * 1000 ) )
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , borPost , th . BasicChannel , model . CreatePostFlags { } )
2025-12-17 05:45:44 -05:00
require . Nil ( t , appErr )
// User2 reveals and flags the post
revealedPost , appErr := th . App . RevealPost ( th . Context , createdPost , user2 . Id , "" )
require . Nil ( t , appErr )
require . NotNil ( t , revealedPost )
preference := model . Preference {
UserId : user2 . Id ,
Category : model . PreferenceCategoryFlaggedPost ,
Name : createdPost . Id ,
Value : "true" ,
}
err := th . App . Srv ( ) . Store ( ) . Preference ( ) . Save ( model . Preferences { preference } )
require . NoError ( t , err )
// Expire the receipt
receipt , err := th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Get ( th . Context , createdPost . Id , user2 . Id )
require . NoError ( t , err )
receipt . ExpireAt = model . GetMillis ( ) - 1000
_ , err = th . App . Srv ( ) . Store ( ) . ReadReceipt ( ) . Update ( th . Context , receipt )
require . NoError ( t , err )
// Get flagged posts for channel
flaggedPosts , appErr := th . App . GetFlaggedPostsForChannel ( th . Context , user2 . Id , th . BasicChannel . Id , 0 , 10 )
require . Nil ( t , appErr )
require . NotContains ( t , flaggedPosts . Order , createdPost . Id )
} )
t . Run ( "non-expired burn-on-read post should appear in flagged posts" , func ( t * testing . T ) {
enableBoRFeature ( th )
// Create a burn-on-read post with long expiration
borPost := & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "burn on read message still valid" ,
Type : model . PostTypeBurnOnRead ,
}
borPost . AddProp ( model . PostPropsExpireAt , model . GetMillis ( ) + int64 ( 3600 * 1000 ) ) // 1 hour
2026-01-20 04:38:27 -05:00
createdPost , _ , appErr := th . App . CreatePost ( th . Context , borPost , th . BasicChannel , model . CreatePostFlags { } )
2025-12-17 05:45:44 -05:00
require . Nil ( t , appErr )
// User2 reveals and flags the post
revealedPost , appErr := th . App . RevealPost ( th . Context , createdPost , user2 . Id , "" )
require . Nil ( t , appErr )
require . NotNil ( t , revealedPost )
preference := model . Preference {
UserId : user2 . Id ,
Category : model . PreferenceCategoryFlaggedPost ,
Name : createdPost . Id ,
Value : "true" ,
}
err := th . App . Srv ( ) . Store ( ) . Preference ( ) . Save ( model . Preferences { preference } )
require . NoError ( t , err )
// Get flagged posts - post should be present
flaggedPosts , appErr := th . App . GetFlaggedPosts ( th . Context , user2 . Id , 0 , 10 )
require . Nil ( t , appErr )
require . Contains ( t , flaggedPosts . Order , createdPost . Id , "Non-expired burn-on-read post should be in flagged posts" )
require . NotNil ( t , flaggedPosts . Posts [ createdPost . Id ] )
// Verify metadata is populated correctly
post := flaggedPosts . Posts [ createdPost . Id ]
require . NotNil ( t , post . Metadata )
require . NotZero ( t , post . Metadata . ExpireAt )
require . Greater ( t , post . Metadata . ExpireAt , model . GetMillis ( ) )
} )
}
2026-02-12 05:25:49 -05:00
2026-02-12 14:10:05 -05:00
func TestBurnOnReadRestrictionsForDMsAndBots ( t * testing . T ) {
th := Setup ( t ) . InitBasic ( t )
ci: enable fullyparallel mode for server tests (#35816)
* ci: enable fullyparallel mode for server tests
Replace os.Setenv, os.Chdir, and global state mutations with
parallel-safe alternatives (t.Setenv, t.Chdir, test hooks) across
37 files. Refactor GetLogRootPath and MM_INSTALL_TYPE to use
package-level test hooks instead of environment variables.
This enables gotestsum --fullparallel, allowing all test packages
to run with maximum parallelism within each shard.
Co-authored-by: Claude <claude@anthropic.com>
* ci: split fullyparallel from continue-on-error in workflow template
- Add new boolean input 'allow-failure' separate from 'fullyparallel'
- Change continue-on-error to use allow-failure instead of fullyparallel
- Update server-ci.yml to pass allow-failure: true for test coverage job
- Allows independent control of parallel execution and failure tolerance
Co-authored-by: Claude <claude@anthropic.com>
* fix: protect TestOverrideLogRootPath with sync.Mutex for parallel tests
- Replace global var TestOverrideLogRootPath with mutex-protected functions
- Add SetTestOverrideLogRootPath() and getTestOverrideLogRootPath() functions
- Update GetLogRootPath() to use thread-safe getter
- Update all test files to use SetTestOverrideLogRootPath() with t.Cleanup()
- Fixes race condition when running tests with t.Parallel()
Co-authored-by: Claude <claude@anthropic.com>
* fix: configure audit settings before server setup in tests
- Move ExperimentalAuditSettings from UpdateConfig() to config defaults
- Pass audit config via app.Config() option in SetupWithServerOptions()
- Fixes audit test setup ordering to configure BEFORE server initialization
- Resolves CodeRabbit's audit config timing issue in api4 tests
Co-authored-by: Claude <claude@anthropic.com>
* fix: implement SetTestOverrideLogRootPath mutex in logger.go
The previous commit updated test callers to use SetTestOverrideLogRootPath()
but didn't actually create the function in config/logger.go, causing build
failures across all CI shards. This commit:
- Replaces the exported var TestOverrideLogRootPath with mutex-protected
unexported state (testOverrideLogRootPath + testOverrideLogRootMu)
- Adds exported SetTestOverrideLogRootPath() setter
- Adds unexported getTestOverrideLogRootPath() getter
- Updates GetLogRootPath() to use the thread-safe getter
- Fixes log_test.go callers that were missed in the previous commit
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): use SetupConfig for access_control feature flag registration
InitAccessControlPolicy() checks FeatureFlags.AttributeBasedAccessControl
at route registration time during server startup. Setting the flag via
UpdateConfig after Setup() is too late — routes are never registered
and API calls return 404.
Use SetupConfig() to pass the feature flag in the initial config before
server startup, ensuring routes are properly registered.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore BurnOnRead flag state in TestRevealPost subtest
The 'feature not enabled' subtest disables BurnOnRead without restoring
it via t.Cleanup. Subsequent subtests inherit the disabled state, which
can cause 501 errors when they expect the feature to be available.
Add t.Cleanup to restore FeatureFlags.BurnOnRead = true after the
subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* fix(test): restore EnableSharedChannelsMemberSync flag via t.Cleanup
The test disables EnableSharedChannelsMemberSync without restoring it.
If the subtest exits early (e.g., require failure), later sibling
subtests inherit a disabled flag and become flaky.
Add t.Cleanup to restore the flag after the subtest completes.
Co-authored-by: Claude <claude@anthropic.com>
* Fix test parallelism: use instance-scoped overrides and init-time audit config
Replace package-level test globals (TestOverrideInstallType,
SetTestOverrideLogRootPath) with fields on PlatformService so each test
gets its own instance without process-wide mutation. Fix three audit
tests (TestUserLoginAudit, TestLogoutAuditAuthStatus,
TestUpdatePasswordAudit) that configured the audit logger after server
init — the audit logger only reads config at startup, so pass audit
settings via app.Config() at init time instead.
Also revert the Go 1.24.13 downgrade and bump mattermost-govet to
v2.0.2 for Go 1.25.8 compatibility.
* Fix audit unit tests
* Fix MMCLOUDURL unit tests
* Fixed unit tests using MM_NOTIFY_ADMIN_COOL_OFF_DAYS
* Make app migrations idempotent for parallel test safety
Change System().Save() to System().SaveOrUpdate() in all migration
completion markers. When two parallel tests share a database pool entry,
both may race through the check-then-insert migration pattern. Save()
causes a duplicate key fatal crash; SaveOrUpdate() makes the second
write a harmless no-op.
* test: address review feedback on fullyparallel PR
- Use SetLogRootPathOverride() setter instead of direct field access
in platform/support_packet_test.go and platform/log_test.go (pvev)
- Restore TestGetLogRootPath in config/logger_test.go to keep
MM_LOG_PATH env var coverage; test uses t.Setenv so it runs
serially which is fine (pvev)
- Fix misleading comment in config_test.go: code uses t.Setenv,
not os.Setenv (jgheithcock)
Co-authored-by: Claude <claude@anthropic.com>
* fix: add missing os import in post_test.go
The os import was dropped during a merge conflict resolution while
burn-on-read shared channel tests from master still use os.Setenv.
Co-authored-by: Claude <claude@anthropic.com>
---------
Co-authored-by: Claude <claude@anthropic.com>
Co-authored-by: wiggin77 <wiggin77@warpmail.net>
Co-authored-by: Mattermost Build <build@mattermost.com>
2026-04-08 20:48:36 -04:00
// Enable BurnOnRead feature flag
th . App . UpdateConfig ( func ( cfg * model . Config ) { cfg . FeatureFlags . BurnOnRead = true } )
2026-02-12 14:10:05 -05:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
cfg . ServiceSettings . EnableBurnOnRead = model . NewPointer ( true )
cfg . ServiceSettings . BurnOnReadMaximumTimeToLiveSeconds = model . NewPointer ( 600 )
cfg . ServiceSettings . BurnOnReadDurationSeconds = model . NewPointer ( 600 )
} )
t . Run ( "should allow burn-on-read posts in direct messages with another user" , func ( t * testing . T ) {
// Create a direct message channel between two different users
dmChannel , appErr := th . App . GetOrCreateDirectChannel ( th . Context , th . BasicUser . Id , th . BasicUser2 . Id )
require . Nil ( t , appErr )
require . Equal ( t , model . ChannelTypeDirect , dmChannel . Type )
post := & model . Post {
ChannelId : dmChannel . Id ,
Message : "This is a burn-on-read message in DM" ,
UserId : th . BasicUser . Id ,
Type : model . PostTypeBurnOnRead ,
}
createdPost , _ , err := th . App . CreatePost ( th . Context , post , dmChannel , model . CreatePostFlags { SetOnline : true } )
require . Nil ( t , err )
require . NotNil ( t , createdPost )
require . Equal ( t , model . PostTypeBurnOnRead , createdPost . Type )
} )
t . Run ( "should allow burn-on-read posts in group messages" , func ( t * testing . T ) {
// Create a group message channel with at least 3 users
user3 := th . CreateUser ( t )
th . LinkUserToTeam ( t , user3 , th . BasicTeam )
gmChannel := th . CreateGroupChannel ( t , th . BasicUser2 , user3 )
require . Equal ( t , model . ChannelTypeGroup , gmChannel . Type )
// This should succeed - group messages allow BoR
post := & model . Post {
ChannelId : gmChannel . Id ,
Message : "This is a burn-on-read message in GM" ,
UserId : th . BasicUser . Id ,
Type : model . PostTypeBurnOnRead ,
}
createdPost , _ , err := th . App . CreatePost ( th . Context , post , gmChannel , model . CreatePostFlags { SetOnline : true } )
require . Nil ( t , err )
require . NotNil ( t , createdPost )
require . Equal ( t , model . PostTypeBurnOnRead , createdPost . Type )
} )
t . Run ( "should allow burn-on-read posts from bot users" , func ( t * testing . T ) {
// Create a bot user
bot := & model . Bot {
Username : "testbot" ,
DisplayName : "Test Bot" ,
Description : "Test Bot for burn-on-read (bots can send BoR for OTP, integrations, etc.)" ,
OwnerId : th . BasicUser . Id ,
}
createdBot , appErr := th . App . CreateBot ( th . Context , bot )
require . Nil ( t , appErr )
// Get the bot user
botUser , appErr := th . App . GetUser ( createdBot . UserId )
require . Nil ( t , appErr )
require . True ( t , botUser . IsBot )
// Create a burn-on-read post as bot (should succeed - bots can send BoR)
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "This is a burn-on-read message from bot" ,
UserId : botUser . Id ,
Type : model . PostTypeBurnOnRead ,
}
createdPost , _ , err := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
require . Nil ( t , err )
require . NotNil ( t , createdPost )
require . Equal ( t , model . PostTypeBurnOnRead , createdPost . Type )
} )
t . Run ( "should reject burn-on-read posts in self DMs" , func ( t * testing . T ) {
// Create a self DM channel (user messaging themselves)
selfDMChannel , appErr := th . App . GetOrCreateDirectChannel ( th . Context , th . BasicUser . Id , th . BasicUser . Id )
require . Nil ( t , appErr )
require . Equal ( t , model . ChannelTypeDirect , selfDMChannel . Type )
// Try to create a burn-on-read post in self DM
post := & model . Post {
ChannelId : selfDMChannel . Id ,
Message : "This is a burn-on-read message to myself" ,
UserId : th . BasicUser . Id ,
Type : model . PostTypeBurnOnRead ,
}
_ , _ , err := th . App . CreatePost ( th . Context , post , selfDMChannel , model . CreatePostFlags { SetOnline : true } )
require . NotNil ( t , err )
require . Equal ( t , "api.post.fill_in_post_props.burn_on_read.self_dm.app_error" , err . Id )
} )
t . Run ( "should reject burn-on-read posts in DMs with bots/AI agents" , func ( t * testing . T ) {
// Create a bot user
bot := & model . Bot {
Username : "aiagent" ,
DisplayName : "AI Agent" ,
Description : "Test AI Agent for burn-on-read restrictions" ,
OwnerId : th . BasicUser . Id ,
}
createdBot , appErr := th . App . CreateBot ( th . Context , bot )
require . Nil ( t , appErr )
// Get the bot user
botUser , appErr := th . App . GetUser ( createdBot . UserId )
require . Nil ( t , appErr )
require . True ( t , botUser . IsBot )
// Create a DM channel between the regular user and the bot
dmWithBotChannel , appErr := th . App . GetOrCreateDirectChannel ( th . Context , th . BasicUser . Id , botUser . Id )
require . Nil ( t , appErr )
require . Equal ( t , model . ChannelTypeDirect , dmWithBotChannel . Type )
// Try to create a burn-on-read post in DM with bot (regular user sending)
post := & model . Post {
ChannelId : dmWithBotChannel . Id ,
Message : "This is a burn-on-read message to AI agent" ,
UserId : th . BasicUser . Id ,
Type : model . PostTypeBurnOnRead ,
}
_ , _ , err := th . App . CreatePost ( th . Context , post , dmWithBotChannel , model . CreatePostFlags { SetOnline : true } )
require . NotNil ( t , err )
require . Equal ( t , "api.post.fill_in_post_props.burn_on_read.bot_dm.app_error" , err . Id )
} )
t . Run ( "should reject burn-on-read posts in DMs with deleted users" , func ( t * testing . T ) {
// Create a user that we'll delete
userToDelete := th . CreateUser ( t )
th . LinkUserToTeam ( t , userToDelete , th . BasicTeam )
// Create a DM channel between the regular user and the user we'll delete
dmChannel , appErr := th . App . GetOrCreateDirectChannel ( th . Context , th . BasicUser . Id , userToDelete . Id )
require . Nil ( t , appErr )
require . Equal ( t , model . ChannelTypeDirect , dmChannel . Type )
// Delete the user
appErr = th . App . PermanentDeleteUser ( th . Context , userToDelete )
require . Nil ( t , appErr )
// Try to create a burn-on-read post in DM with deleted user
post := & model . Post {
ChannelId : dmChannel . Id ,
Message : "This is a burn-on-read message to deleted user" ,
UserId : th . BasicUser . Id ,
Type : model . PostTypeBurnOnRead ,
}
// This should fail because we can't validate the other user (deleted)
_ , _ , err := th . App . CreatePost ( th . Context , post , dmChannel , model . CreatePostFlags { SetOnline : true } )
require . NotNil ( t , err )
require . Equal ( t , "api.post.fill_in_post_props.burn_on_read.user.app_error" , err . Id )
} )
t . Run ( "should allow burn-on-read posts in public channels" , func ( t * testing . T ) {
// This should succeed - public channel, regular user
require . Equal ( t , model . ChannelTypeOpen , th . BasicChannel . Type )
post := & model . Post {
ChannelId : th . BasicChannel . Id ,
Message : "This is a burn-on-read message in public channel" ,
UserId : th . BasicUser . Id ,
Type : model . PostTypeBurnOnRead ,
}
createdPost , _ , err := th . App . CreatePost ( th . Context , post , th . BasicChannel , model . CreatePostFlags { SetOnline : true } )
require . Nil ( t , err )
require . NotNil ( t , createdPost )
require . Equal ( t , model . PostTypeBurnOnRead , createdPost . Type )
} )
t . Run ( "should allow burn-on-read posts in private channels" , func ( t * testing . T ) {
// Create a private channel using helper
createdPrivateChannel := th . CreatePrivateChannel ( t , th . BasicTeam )
require . Equal ( t , model . ChannelTypePrivate , createdPrivateChannel . Type )
// This should succeed - private channel, regular user
post := & model . Post {
ChannelId : createdPrivateChannel . Id ,
Message : "This is a burn-on-read message in private channel" ,
UserId : th . BasicUser . Id ,
Type : model . PostTypeBurnOnRead ,
}
createdPost , _ , err := th . App . CreatePost ( th . Context , post , createdPrivateChannel , model . CreatePostFlags { SetOnline : true } )
require . Nil ( t , err )
require . NotNil ( t , createdPost )
require . Equal ( t , model . PostTypeBurnOnRead , createdPost . Type )
} )
}
2026-02-12 05:25:49 -05:00
func TestGetBurnOnReadPost ( t * testing . T ) {
t . Run ( "success - temporary post found" , func ( t * testing . T ) {
th := Setup ( t ) . InitBasic ( t )
post := & model . Post {
Id : model . NewId ( ) ,
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "placeholder message" ,
FileIds : model . StringArray { "file1" } ,
Type : model . PostTypeBurnOnRead ,
}
temporaryPost := & model . TemporaryPost {
ID : post . Id ,
Type : model . PostTypeBurnOnRead ,
ExpireAt : model . GetMillis ( ) + 3600000 ,
Message : "actual secret message" ,
FileIDs : model . StringArray { "file2" , "file3" } ,
}
_ , err := th . App . Srv ( ) . Store ( ) . TemporaryPost ( ) . Save ( th . Context , temporaryPost )
require . NoError ( t , err )
resultPost , appErr := th . App . getBurnOnReadPost ( th . Context , post )
require . Nil ( t , appErr )
require . NotNil ( t , resultPost )
assert . Equal ( t , temporaryPost . Message , resultPost . Message )
assert . Equal ( t , temporaryPost . FileIDs , resultPost . FileIds )
// Ensure original post is not modified
assert . Equal ( t , "placeholder message" , post . Message )
assert . Equal ( t , model . StringArray { "file1" } , post . FileIds )
} )
t . Run ( "temporary post not found - returns app error" , func ( t * testing . T ) {
th := Setup ( t ) . InitBasic ( t )
post := & model . Post {
Id : model . NewId ( ) ,
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "placeholder message" ,
Type : model . PostTypeBurnOnRead ,
}
resultPost , appErr := th . App . getBurnOnReadPost ( th . Context , post )
require . NotNil ( t , appErr )
require . Nil ( t , resultPost )
assert . Equal ( t , "app.post.get_post.app_error" , appErr . Id )
assert . Equal ( t , http . StatusInternalServerError , appErr . StatusCode )
} )
}
2026-02-16 15:31:32 -05:00
func TestPostChannelMentionsWithPrivateChannels ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
channel := th . BasicChannel
user := th . BasicUser
// Create context with session for the user to properly test sanitization
ctx := th . Context . WithSession ( & model . Session { UserId : user . Id } )
// Create a private channel where user IS a member
privateChannelMember , err := th . App . CreateChannel ( th . Context , & model . Channel {
DisplayName : "Private Member" ,
Name : "private-member" ,
Type : model . ChannelTypePrivate ,
TeamId : th . BasicTeam . Id ,
} , false )
require . Nil ( t , err )
th . AddUserToChannel ( t , user , privateChannelMember )
// Create a private channel where user is NOT a member
privateChannelNonMember , err := th . App . CreateChannel ( th . Context , & model . Channel {
DisplayName : "Private Non-Member" ,
Name : "private-non-member" ,
Type : model . ChannelTypePrivate ,
TeamId : th . BasicTeam . Id ,
} , false )
require . Nil ( t , err )
// Create a public channel where user is NOT a member
publicChannel , err := th . App . CreateChannel ( th . Context , & model . Channel {
DisplayName : "Public Channel" ,
Name : "public-channel" ,
Type : model . ChannelTypeOpen ,
TeamId : th . BasicTeam . Id ,
} , false )
require . Nil ( t , err )
post := & model . Post {
Message : fmt . Sprintf ( "~%v and ~%v and ~%v" , privateChannelMember . Name , privateChannelNonMember . Name , publicChannel . Name ) ,
ChannelId : channel . Id ,
PendingPostId : model . NewId ( ) + ":" + fmt . Sprint ( model . GetMillis ( ) ) ,
UserId : user . Id ,
CreateAt : 0 ,
}
post , _ , err = th . App . CreatePostAsUser ( ctx , post , "" , true )
require . Nil ( t , err )
mentions := post . GetProp ( model . PostPropsChannelMentions )
require . NotNil ( t , mentions )
mentionsMap , ok := mentions . ( map [ string ] any )
require . True ( t , ok )
// Should include private channel where user IS a member
assert . Contains ( t , mentionsMap , privateChannelMember . Name )
// Should NOT include private channel where user is NOT a member
assert . NotContains ( t , mentionsMap , privateChannelNonMember . Name )
// Should include public channel (user has team access)
assert . Contains ( t , mentionsMap , publicChannel . Name )
}
Merge the Integrated Boards MVP feature branch (#35796)
* Add CreatedBy and UpdatedBy to the properties fields and values (#34485)
* Add CreatedBy and UpdatedBy to the properties fields and values
* Fix types
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Adds ObjectType to the property fields table (#34908)
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Update ObjectType migration setting an empty value and marking the column as not null (#34915)
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Adds uniqueness mechanisms to the property fields (#35058)
* Adds uniqueness mechanisms to the property fields
After adding ObjectType, this commit ensures that both the PSAv1 and
PSAv2 schemas are supported, and enforces property uniqueness through
both database indexes and a logical check when creating new property
fields.
* Adds uniqueness check to property updates
Updates are covered on this commit and we refactor as well the SQL
code to use the squirrel builder and work better with the conditional
addition of the `existingID` piece of the query.
* Add translations to error messages
* Fixing retrylayer mocks
* Remove retrylayer duplication
* Address review comments
* Fix comment to avoid linter issues
* Address PR comments
* Update server/channels/db/migrations/postgres/000157_add_object_type_to_property_fields.down.sql
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
* Update server/channels/db/migrations/postgres/000157_add_object_type_to_property_fields.up.sql
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
* Update server/channels/db/migrations/postgres/000157_add_object_type_to_property_fields.up.sql
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
* Update field validation to check only for valid target types
* Update migrations to avoid concurrent index creation within a transaction
* Update migrations to make all index ops concurrent
* Update tests to use valid PSAv2 property fields
* Adds a helper for valid PSAv2 TargetTypes
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
* Fix property tests (#35388)
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Adds Integrated Boards feature flag (#35378)
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Adds Integrated Boards MVP API changes (#34822)
This PR includes the necessary changes for channels and posts
endpoints and adds a set of generic endpoints to retrieve and manage
property fields and values following the new Property System approach.
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
Co-authored-by: Mattermost Build <build@mattermost.com>
* Property System Architecture permissions for v2 (#35113)
* Adds uniqueness mechanisms to the property fields
After adding ObjectType, this commit ensures that both the PSAv1 and
PSAv2 schemas are supported, and enforces property uniqueness through
both database indexes and a logical check when creating new property
fields.
* Adds uniqueness check to property updates
Updates are covered on this commit and we refactor as well the SQL
code to use the squirrel builder and work better with the conditional
addition of the `existingID` piece of the query.
* Add translations to error messages
* Add the permissions to the migrations, model and update the store calls
* Adds the property field and property group app layer
* Adds authorization helpers for property fields and values
* Make sure that users cannot lock themselves out of property fields
* Migrate permissions from a JSON column to three normalized columns
* Remove the audit comment
* Use target level constants in authorization
* Log authorization membership failures
* Rename admin to sysadmin
* Fix i18n sorting
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Add Views store and app layer (#35361)
* Add Views store and app layer for Integrated Boards
Implements the View entity (model, SQL store, service, app) as described
in the Integrated Boards tech spec. Views are channel-scoped board
configurations with typed props (board, kanban subviews) and soft-delete.
- public/model: View, ViewBoardProps, Subview, ViewPatch types with
PreSave/PreUpdate/IsValid/Patch/Clone/Auditable
- Migration 158: Views table with jsonb Props column and indexes
- SqlViewStore: CRUD with nil-safe Props marshaling (AppendBinaryFlag)
- ViewService: CreateView seeds default kanban subview and links the
boards property field; caches boardPropertyFieldID at startup
- App layer: CreateView/GetView/GetViewsForChannel/UpdateView/DeleteView
with channel-membership permission checks and WebSocket events
(view_created, view_updated, view_deleted)
- doSetupBoardsPropertyField: registers the Boards property group and
board field in NewServer() before ViewService construction
- GetFieldByName now returns store.ErrNotFound instead of raw sql.ErrNoRows
* Move permission checks out of App layer for views
- Remove HasPermissionToChannel calls from all App view methods
- Drop userID params from GetView, GetViewsForChannel, UpdateView, DeleteView
- Fix doSetupBoardsPropertyField to include required TargetType for PSAv2 field
* Make View service generic and enforce board validation in model
- Remove board-specific auto-setup from service and server startup
- Enforce that board views require Props, at least one subview, and at least one linked property in IsValid()
- Move default subview seeding out of app layer; callers must provide valid props
- Call PreSave on subviews during PreUpdate to assign IDs to new subviews
- Update all tests to reflect the new validation requirements
* Restore migrations files to match base branch
* Distinguish ErrNotFound from other errors in view store Get
* Use CONCURRENTLY and nontransactional for index operations in views migration
* Split views index creation into separate nontransactional migrations
* Update migrations.list
* Update i18n translations for views
* Fix makeView helper to include required Props for board view validation
* Rename ctx parameter from c to rctx in OAuthProvider mock
* Remove views service layer, call store directly from app
* Return 500 for unexpected DB errors in GetView, 404 only for not-found
* Harden View model: deep-copy Props, validate linked property IDs
- Add ViewBoardProps.Clone() to deep-copy LinkedProperties and Subviews
- Use it in View.Clone() and View.Patch() to prevent shared-slice aliasing
- Iterate over LinkedProperties in View.IsValid() and reject invalid IDs
with a dedicated i18n key
- Register ViewStore in storetest AssertExpectations so mock expectations
are enforced
- Add tests covering all new behaviours
* Restore autotranslation worker_stopped i18n translation
* Fix view store test IDs and improve error handling in app layer
- Use model.NewId() for linked property IDs in testUpdateView to fix
validation failure (IsValid rejects non-UUID strings)
- Fix import grouping in app/view.go (stdlib imports in one block)
- Return 404 instead of 500 when Update/Delete store calls return
ErrNotFound (e.g. concurrent deletion TOCTOU race)
* Add View store mock to retrylayer test genStore helper
The View store was added to the store interface but the genStore()
helper in retrylayer_test.go was not updated, causing TestRetry to panic.
Also removes the duplicate Recap mock registration.
* Refactor view deletion and websocket event handling; update SQL store methods to use query builder
* revert property field store
* Remove useless migrations
* Add cursor-based pagination to View store GetForChannel
- Add ViewQueryCursor and ViewQueryOpts types with validation
- Return (views, cursor, error) for caller-driven pagination
- PerPage clamping: <=0 defaults to 20, >200 clamps to 200
- Support IncludeDeleted filter
- Add comprehensive store tests for pagination, cursor edge cases,
PerPage clamping, and invalid input rejection
- Add app layer test for empty channelID → 400
- Update interface, retrylayer, timerlayer, and mock signatures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Refactor test loops in ViewStore tests for improved readability
* change pagination to limit/offset
* Add upper-bound limits on View Subviews and LinkedProperties
Defense-in-depth validation: cap Subviews at 50 and LinkedProperties
at 500 to prevent abuse below the 300KB payload limit.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* MM-67388, MM-66528, MM-67750: Add View REST API endpoints, websocket events, and sort order (#35442)
* Add Views store and app layer for Integrated Boards
Implements the View entity (model, SQL store, service, app) as described
in the Integrated Boards tech spec. Views are channel-scoped board
configurations with typed props (board, kanban subviews) and soft-delete.
- public/model: View, ViewBoardProps, Subview, ViewPatch types with
PreSave/PreUpdate/IsValid/Patch/Clone/Auditable
- Migration 158: Views table with jsonb Props column and indexes
- SqlViewStore: CRUD with nil-safe Props marshaling (AppendBinaryFlag)
- ViewService: CreateView seeds default kanban subview and links the
boards property field; caches boardPropertyFieldID at startup
- App layer: CreateView/GetView/GetViewsForChannel/UpdateView/DeleteView
with channel-membership permission checks and WebSocket events
(view_created, view_updated, view_deleted)
- doSetupBoardsPropertyField: registers the Boards property group and
board field in NewServer() before ViewService construction
- GetFieldByName now returns store.ErrNotFound instead of raw sql.ErrNoRows
* Move permission checks out of App layer for views
- Remove HasPermissionToChannel calls from all App view methods
- Drop userID params from GetView, GetViewsForChannel, UpdateView, DeleteView
- Fix doSetupBoardsPropertyField to include required TargetType for PSAv2 field
* Make View service generic and enforce board validation in model
- Remove board-specific auto-setup from service and server startup
- Enforce that board views require Props, at least one subview, and at least one linked property in IsValid()
- Move default subview seeding out of app layer; callers must provide valid props
- Call PreSave on subviews during PreUpdate to assign IDs to new subviews
- Update all tests to reflect the new validation requirements
* Restore migrations files to match base branch
* Distinguish ErrNotFound from other errors in view store Get
* Use CONCURRENTLY and nontransactional for index operations in views migration
* Split views index creation into separate nontransactional migrations
* Update migrations.list
* Update i18n translations for views
* Fix makeView helper to include required Props for board view validation
* Rename ctx parameter from c to rctx in OAuthProvider mock
* Remove views service layer, call store directly from app
* Return 500 for unexpected DB errors in GetView, 404 only for not-found
* Harden View model: deep-copy Props, validate linked property IDs
- Add ViewBoardProps.Clone() to deep-copy LinkedProperties and Subviews
- Use it in View.Clone() and View.Patch() to prevent shared-slice aliasing
- Iterate over LinkedProperties in View.IsValid() and reject invalid IDs
with a dedicated i18n key
- Register ViewStore in storetest AssertExpectations so mock expectations
are enforced
- Add tests covering all new behaviours
* Restore autotranslation worker_stopped i18n translation
* Fix view store test IDs and improve error handling in app layer
- Use model.NewId() for linked property IDs in testUpdateView to fix
validation failure (IsValid rejects non-UUID strings)
- Fix import grouping in app/view.go (stdlib imports in one block)
- Return 404 instead of 500 when Update/Delete store calls return
ErrNotFound (e.g. concurrent deletion TOCTOU race)
* Add View store mock to retrylayer test genStore helper
The View store was added to the store interface but the genStore()
helper in retrylayer_test.go was not updated, causing TestRetry to panic.
Also removes the duplicate Recap mock registration.
* Refactor view deletion and websocket event handling; update SQL store methods to use query builder
* revert property field store
* Add View API endpoints with OpenAPI spec, client methods, and i18n
Implement REST API for channel views (board-type) behind the
IntegratedBoards feature flag. Adds CRUD endpoints under
/api/v4/channels/{channel_id}/views with permission checks
matching the channel bookmark pattern.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Remove useless migrations
* Add cursor-based pagination to View store GetForChannel
- Add ViewQueryCursor and ViewQueryOpts types with validation
- Return (views, cursor, error) for caller-driven pagination
- PerPage clamping: <=0 defaults to 20, >200 clamps to 200
- Support IncludeDeleted filter
- Add comprehensive store tests for pagination, cursor edge cases,
PerPage clamping, and invalid input rejection
- Add app layer test for empty channelID → 400
- Update interface, retrylayer, timerlayer, and mock signatures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add cursor-based pagination to View API for channel views
* Enhance cursor handling in getViewsForChannel and update tests for pagination
* Refactor test loops in ViewStore tests for improved readability
* Refactor loop in TestGetViewsForChannel for improved readability
* change pagination to limit/offset
* switch to limit/offset pagination
* Add upper-bound limits on View Subviews and LinkedProperties
Defense-in-depth validation: cap Subviews at 50 and LinkedProperties
at 500 to prevent abuse below the 300KB payload limit.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add view sort order API endpoint
Add POST /api/v4/channels/{channel_id}/views/{view_id}/sort_order
endpoint following the channel bookmarks reorder pattern. Includes
store, app, and API layers with full test coverage at each layer.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add connectionId to view WebSocket events and sort_order API spec
Thread connectionId from request header through all view handlers
(create, update, delete, sort_order) to WebSocket events, matching
the channel bookmarks pattern. Add sort_order endpoint to OpenAPI
spec. Update minimum server version to 11.6.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Remove duplicate View/ViewPatch definitions from definitions.yaml
The merge from integrated-boards-mvp introduced duplicate View and
ViewPatch schema definitions that were already defined earlier in
the file with more detail (including ViewBoardProps ref and enums).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Update minimum server version to 11.6 in views API spec
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add missing translations for view sort order error messages
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Merge integrated-boards-mvp into ibmvp_api-views; remove spec files
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Fix flaky TestViewStore timestamp test on CI
Add sleep before UpdateSortOrder to ensure timestamps differ,
preventing same-millisecond comparisons on fast CI machines.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* remove duplicate views.yaml imclude
* Use c.boolString() for include_deleted query param in GetViewsForChannel
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Fix views.yaml sort order schema: use integer type and require body
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Refactor view sort order tests to use named IDs instead of array indices
Extract idA/idB/idC from views slice and add BEFORE/AFTER comments
to make stateful subtest ordering easier to follow.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Return 404 instead of 403 for view operations on deleted channels
Deleted channels should appear non-existent to callers rather than
revealing their existence via a 403. Detailed error text explains
the context for debugging.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* add missing channel deleteat checks
* Use c.Params.Page instead of manual page query param parsing in getViewsForChannel
c.Params already validates and defaults page/per_page, so the manual
parsing was redundant.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add support for total count in views retrieval
* Add tests for handling deleted views in GetViewsForChannel and GetView
* Short-circuit negative newIndex in UpdateSortOrder before opening transaction
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add per-channel limit on views to bound UpdateSortOrder cost
Without a cap, unbounded view creation makes sort-order updates
increasingly expensive (CASE WHEN per view, row locks). Adds
MaxViewsPerChannel=50 constant and enforces it in the app layer
before saving. Includes API and app layer tests.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Remove include_deleted support from views API
Soft-deleted views are structural metadata with low risk, but no other
similar endpoint (e.g. channel bookmarks) exposes deleted records without
an admin gate. Rather than adding an admin-only permission check for
consistency, remove the feature entirely since there is no current use case.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Update view permissions to require `create_post` instead of channel management permissions
* Remove obsolete view management error messages for direct and group messages
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* feat(migrations): add user tracking and object type to property fields
- Introduced user tracking columns (CreatedBy, UpdatedBy) to PropertyFields and PropertyValues.
- Added ObjectType column to PropertyFields with associated unique indexes for legacy and typed properties.
- Created new migration scripts for adding and dropping these features, including necessary indexes for data integrity.
- Established views for managing property fields with new attributes.
This update enhances the schema to support better tracking and categorization of property fields.
* Add Property System Architecture v2 API endpoints (#35583)
* Adds uniqueness mechanisms to the property fields
After adding ObjectType, this commit ensures that both the PSAv1 and
PSAv2 schemas are supported, and enforces property uniqueness through
both database indexes and a logical check when creating new property
fields.
* Adds uniqueness check to property updates
Updates are covered on this commit and we refactor as well the SQL
code to use the squirrel builder and work better with the conditional
addition of the `existingID` piece of the query.
* Add translations to error messages
* Add the permissions to the migrations, model and update the store calls
* Adds the property field and property group app layer
* Adds authorization helpers for property fields and values
* Make sure that users cannot lock themselves out of property fields
* Migrate permissions from a JSON column to three normalized columns
* Remove the audit comment
* Use target level constants in authorization
* Log authorization membership failures
* Rename admin to sysadmin
* Adds the Property System Architecture v2 API endpoints
* Adds permission checks to the create field endpoint
* Add target access checks to value endpoints
* Add default branches for object_type and target_type and extra guards for cursor client4 methods
* Fix vet API mismatch
* Fix error checks
* Fix linter
* Add merge semantics for property patch logic and API endpoint
* Fix i18n
* Fix duplicated patch elements and early return on bad cursor
* Update docs to use enums
* Fix i18n sorting
* Update app layer to return model.AppError
* Adds a limit to the number of property values that can be patched in the same request
* Require target_type filter when searching property fields
* Add objectType validation as part of field.IsValid()
* Fix linter
* Fix test with bad objecttpye
* Fix test grouping
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* MM-67968: Flatten view model — remove icon, subviews, typed board props (#35726)
* feat(views): flatten view model by removing icon, subview, and board props
Simplifies the View data model as part of MM-67968: removes Icon, Subview,
and ViewBoardProps types; renames ViewTypeBoard to ViewTypeKanban; replaces
typed Props with StringInterface (map[string]any); adds migration 000167
to drop the Icon column from the Views table.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
* feat(api): update views OpenAPI spec to reflect flattened model
Removes ViewBoardProps, Subview, and icon from the View and ViewPatch
schemas. Changes type enum from board to kanban. Replaces typed props
with a free-form StringInterface object. Aligns with MM-67968.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
* refactor(views): simplify store by dropping dbView and marshalViewProps
StringInterface already implements driver.Valuer and sql.Scanner, so the
manual JSON marshal/unmarshal and the dbView intermediate struct were
redundant. model.View now scans directly from the database. Also removes
the dead ViewMaxLinkedProperties constant and wraps the Commit() error in
UpdateSortOrder.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
* fix(api): allow arbitrary JSON in view props OpenAPI schema
The props field was restricted to string values via
additionalProperties: { type: string }, conflicting with the Go model's
StringInterface (map[string]any). Changed to additionalProperties: true
in View, ViewPatch, and inline POST schemas.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
* Adds basic implementation of the generic redux store for PSAv2 (#35512)
* Adds basic implementation of the generic redux store for PSAv2
* Add created_by and updated_by to the test fixtures
* Make target_id, target_type and object_type mandatory
* Wrap getPropertyFieldsByIds and getPropertyValuesForTargetByFieldIds with createSelector
* Address PR comments
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* Adds websocket messages for the PSAv2 API events (#35696)
* Adds uniqueness mechanisms to the property fields
After adding ObjectType, this commit ensures that both the PSAv1 and
PSAv2 schemas are supported, and enforces property uniqueness through
both database indexes and a logical check when creating new property
fields.
* Adds uniqueness check to property updates
Updates are covered on this commit and we refactor as well the SQL
code to use the squirrel builder and work better with the conditional
addition of the `existingID` piece of the query.
* Add translations to error messages
* Add the permissions to the migrations, model and update the store calls
* Adds the property field and property group app layer
* Adds authorization helpers for property fields and values
* Make sure that users cannot lock themselves out of property fields
* Migrate permissions from a JSON column to three normalized columns
* Remove the audit comment
* Use target level constants in authorization
* Log authorization membership failures
* Rename admin to sysadmin
* Adds the Property System Architecture v2 API endpoints
* Adds permission checks to the create field endpoint
* Add target access checks to value endpoints
* Add default branches for object_type and target_type and extra guards for cursor client4 methods
* Fix vet API mismatch
* Fix error checks
* Fix linter
* Add merge semantics for property patch logic and API endpoint
* Fix i18n
* Fix duplicated patch elements and early return on bad cursor
* Update docs to use enums
* Fix i18n sorting
* Update app layer to return model.AppError
* Adds a limit to the number of property values that can be patched in the same request
* Adds websocket messages for the PSAv2 API events
* Add IsPSAv2 helper to the property field for clarity
* Add guard against nil returns on field deletion
* Add docs to the websocket endpoints
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
* migrations: consolidate views migrations and reorder after master
- Merged 000165 (create Views) with 000167 (drop Icon) since Icon was never needed
- Renumbered branch migrations 159-166 → 160-167 so master's 000159 (deduplicate_policy_names) runs first
- Regenerated migrations.list
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Add API endpoint to retrieve posts for a specific view (#35604)
Automatic Merge
* Apply fixes after merge
* Return a more specific error from getting multiple fields
* Prevent getting broadcast params on field deletion if not needed
* Remove duplicated migration code
* Update property conflict code to always use master
* Adds nil guard when iterating on property fields
* Check that permission level is valid before getting rejected by the database
* Validate correctness on TargetID for PSAv2 fields
* Avoid PSAv1 using permissions or protected
* Fix test data after validation change
* Fix flaky search test
* Adds more posts for filter use cases to properly test exclusions
---------
Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es>
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
Co-authored-by: Julien Tant <julien@craftyx.fr>
Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: Julien Tant <785518+JulienTant@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-27 05:36:35 -04:00
func TestGetPostsForView ( t * testing . T ) {
mainHelper . Parallel ( t )
t . Run ( "returns posts for channel" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
// Create a few posts
post1 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "post 1" ,
} , th . BasicChannel , model . CreatePostFlags { } )
require . Nil ( t , err )
post2 , _ , err := th . App . CreatePost ( th . Context , & model . Post {
ChannelId : th . BasicChannel . Id ,
UserId : th . BasicUser . Id ,
Message : "post 2" ,
} , th . BasicChannel , model . CreatePostFlags { } )
require . Nil ( t , err )
options := model . GetPostsOptions {
ChannelId : th . BasicChannel . Id ,
Page : 0 ,
PerPage : 10 ,
UserId : th . BasicUser . Id ,
}
postList , appErr := th . App . GetPostsForView ( th . Context , options )
require . Nil ( t , appErr )
require . NotNil ( t , postList )
assert . Contains ( t , postList . Posts , post1 . Id )
assert . Contains ( t , postList . Posts , post2 . Id )
} )
t . Run ( "returns empty list for channel with no posts" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
channel := th . CreateChannel ( t , th . BasicTeam )
options := model . GetPostsOptions {
ChannelId : channel . Id ,
Page : 0 ,
PerPage : 10 ,
UserId : th . BasicUser . Id ,
}
postList , appErr := th . App . GetPostsForView ( th . Context , options )
require . Nil ( t , appErr )
require . NotNil ( t , postList )
assert . Empty ( t , postList . Posts )
} )
t . Run ( "respects pagination" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
channel := th . CreateChannel ( t , th . BasicTeam )
for i := range 5 {
_ , _ , err := th . App . CreatePost ( th . Context , & model . Post {
ChannelId : channel . Id ,
UserId : th . BasicUser . Id ,
Message : fmt . Sprintf ( "post %d" , i ) ,
} , channel , model . CreatePostFlags { } )
require . Nil ( t , err )
}
options := model . GetPostsOptions {
ChannelId : channel . Id ,
Page : 0 ,
PerPage : 2 ,
UserId : th . BasicUser . Id ,
}
postList , appErr := th . App . GetPostsForView ( th . Context , options )
require . Nil ( t , appErr )
require . NotNil ( t , postList )
assert . Len ( t , postList . Posts , 2 )
} )
t . Run ( "invalid channel id returns error" , func ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
options := model . GetPostsOptions {
ChannelId : model . NewId ( ) ,
Page : 0 ,
PerPage : 10 ,
UserId : th . BasicUser . Id ,
}
postList , appErr := th . App . GetPostsForView ( th . Context , options )
require . Nil ( t , appErr )
require . NotNil ( t , postList )
assert . Empty ( t , postList . Posts )
} )
}