2019-11-29 06:59:40 -05:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2017-02-02 09:04:36 -05:00
package api4
import (
2021-03-31 03:40:35 -04:00
"context"
2021-09-01 08:43:12 -04:00
"encoding/json"
2026-02-06 12:19:06 -05:00
"errors"
2017-03-03 12:25:32 -05:00
"fmt"
2017-02-02 09:04:36 -05:00
"net/http"
2018-01-12 09:01:45 -05:00
"strings"
2017-02-02 09:04:36 -05:00
"testing"
2018-05-28 10:31:08 -04:00
"time"
2017-02-02 09:04:36 -05:00
2025-05-06 04:33:35 -04:00
"github.com/mattermost/mattermost/server/v8/channels/web"
2021-01-07 12:12:43 -05: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/v8/channels/app"
"github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
"github.com/mattermost/mattermost/server/v8/channels/utils/testutils"
2026-02-05 07:43:50 -05:00
einterfacesmocks "github.com/mattermost/mattermost/server/v8/einterfaces/mocks"
2017-02-02 09:04:36 -05:00
)
func TestCreateChannel ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-02-02 09:04:36 -05:00
team := th . BasicTeam
2021-07-12 14:05:36 -04:00
channel := & model . Channel { DisplayName : "Test API Name" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypeOpen , TeamId : team . Id }
private := & model . Channel { DisplayName : "Test API Name" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypePrivate , TeamId : team . Id }
2017-02-02 09:04:36 -05:00
2023-06-06 17:29:29 -04:00
rchannel , resp , err := client . CreateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-31 09:56:20 -04:00
CheckCreatedStatus ( t , resp )
2017-02-02 09:04:36 -05:00
2019-11-13 13:53:57 -05:00
require . Equal ( t , channel . Name , rchannel . Name , "names did not match" )
require . Equal ( t , channel . DisplayName , rchannel . DisplayName , "display names did not match" )
require . Equal ( t , channel . TeamId , rchannel . TeamId , "team ids did not match" )
2017-02-02 09:04:36 -05:00
2023-06-06 17:29:29 -04:00
rprivate , _ , err := client . CreateChannel ( context . Background ( ) , private )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-02 09:04:36 -05:00
2019-11-13 13:53:57 -05:00
require . Equal ( t , private . Name , rprivate . Name , "names did not match" )
2021-07-12 14:05:36 -04:00
require . Equal ( t , model . ChannelTypePrivate , rprivate . Type , "wrong channel type" )
2019-11-13 13:53:57 -05:00
require . Equal ( t , th . BasicUser . Id , rprivate . CreatorId , "wrong creator id" )
2017-02-02 09:04:36 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "store.sql_channel.save_channel.exists.app_error" )
2017-02-02 09:04:36 -05:00
CheckBadRequestStatus ( t , resp )
2021-07-12 14:05:36 -04:00
direct := & model . Channel { DisplayName : "Test API Name" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypeDirect , TeamId : team . Id }
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateChannel ( context . Background ( ) , direct )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.channel.create_channel.direct_channel.app_error" )
2017-02-02 09:04:36 -05:00
CheckBadRequestStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-02 09:04:36 -05:00
CheckUnauthorizedStatus ( t , resp )
2025-11-12 07:00:51 -05:00
userNotOnTeam := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , userNotOnTeam . Email , userNotOnTeam . Password )
require . NoError ( t , err )
2017-02-02 09:04:36 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-02 09:04:36 -05:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateChannel ( context . Background ( ) , private )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-02 09:04:36 -05:00
CheckForbiddenStatus ( t , resp )
2018-02-06 10:34:08 -05:00
// Check the appropriate permissions are enforced.
2025-11-12 07:00:51 -05:00
defaultRolePermissions := th . SaveDefaultRolePermissions ( t )
2017-02-02 09:04:36 -05:00
defer func ( ) {
2025-11-12 07:00:51 -05:00
th . RestoreDefaultRolePermissions ( t , defaultRolePermissions )
2017-02-02 09:04:36 -05:00
} ( )
2018-02-06 10:34:08 -05:00
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionCreatePublicChannel . Id , model . TeamUserRoleId )
th . AddPermissionToRole ( t , model . PermissionCreatePrivateChannel . Id , model . TeamUserRoleId )
2018-02-06 10:34:08 -05:00
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2017-02-02 09:04:36 -05:00
channel . Name = GenerateTestChannelName ( )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . CreateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-02 09:04:36 -05:00
private . Name = GenerateTestChannelName ( )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . CreateChannel ( context . Background ( ) , private )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-02 09:04:36 -05:00
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionCreatePublicChannel . Id , model . TeamAdminRoleId )
th . AddPermissionToRole ( t , model . PermissionCreatePrivateChannel . Id , model . TeamAdminRoleId )
th . RemovePermissionFromRole ( t , model . PermissionCreatePublicChannel . Id , model . TeamUserRoleId )
th . RemovePermissionFromRole ( t , model . PermissionCreatePrivateChannel . Id , model . TeamUserRoleId )
2017-02-02 09:04:36 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-02 09:04:36 -05:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateChannel ( context . Background ( ) , private )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-02 09:04:36 -05:00
CheckForbiddenStatus ( t , resp )
2025-11-12 07:00:51 -05:00
th . LoginTeamAdmin ( t )
2017-02-02 09:04:36 -05:00
channel . Name = GenerateTestChannelName ( )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . CreateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-02 09:04:36 -05:00
private . Name = GenerateTestChannelName ( )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . CreateChannel ( context . Background ( ) , private )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-02 09:04:36 -05:00
2020-05-19 12:20:41 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
channel . Name = GenerateTestChannelName ( )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . CreateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-02 09:04:36 -05:00
2020-05-19 12:20:41 -04:00
private . Name = GenerateTestChannelName ( )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . CreateChannel ( context . Background ( ) , private )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-05-19 12:20:41 -04:00
} )
2017-02-02 09:04:36 -05:00
2023-10-05 09:55:59 -04:00
t . Run ( "null value" , func ( t * testing . T ) {
var channel * model . Channel
_ , resp , err = client . CreateChannel ( context . Background ( ) , channel )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
2018-02-06 10:34:08 -05:00
// Test posting Garbage
2023-06-06 17:29:29 -04:00
r , err := client . DoAPIPost ( context . Background ( ) , "/channels" , "garbage" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err , "expected error" )
2019-11-13 13:53:57 -05:00
require . Equal ( t , http . StatusBadRequest , r . StatusCode , "Expected 400 Bad Request" )
2019-04-15 11:13:11 -04:00
// Test GroupConstrained flag
2024-08-05 23:45:00 -04:00
groupConstrainedChannel := & model . Channel { DisplayName : "Test API Name" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypeOpen , TeamId : team . Id , GroupConstrained : model . NewPointer ( true ) }
2023-06-06 17:29:29 -04:00
rchannel , _ , err = client . CreateChannel ( context . Background ( ) , groupConstrainedChannel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-04-15 11:13:11 -04:00
2019-11-13 13:53:57 -05:00
require . Equal ( t , * groupConstrainedChannel . GroupConstrained , * rchannel . GroupConstrained , "GroupConstrained flags do not match" )
2024-04-10 05:04:41 -04:00
t . Run ( "Test create channel with missing team id" , func ( t * testing . T ) {
channel := & model . Channel { DisplayName : "Test API Name" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypeOpen , TeamId : "" }
2025-08-20 07:50:38 -04:00
_ , resp , err = client . CreateChannel ( context . Background ( ) , channel )
2024-04-10 05:04:41 -04:00
CheckErrorID ( t , err , "api.context.invalid_body_param.app_error" )
CheckBadRequestStatus ( t , resp )
} )
t . Run ( "Test create channel with missing display name" , func ( t * testing . T ) {
channel := & model . Channel { DisplayName : "" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypeOpen , TeamId : team . Id }
2025-08-20 07:50:38 -04:00
_ , resp , err = client . CreateChannel ( context . Background ( ) , channel )
2024-04-10 05:04:41 -04:00
CheckErrorID ( t , err , "api.context.invalid_body_param.app_error" )
CheckBadRequestStatus ( t , resp )
} )
2025-02-25 04:22:15 -05:00
t . Run ( "Can create channel with banner info" , func ( t * testing . T ) {
channel := & model . Channel {
DisplayName : GenerateTestChannelName ( ) ,
Name : GenerateTestChannelName ( ) ,
Type : model . ChannelTypeOpen ,
TeamId : team . Id ,
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
Text : model . NewPointer ( "banner text" ) ,
2025-05-13 03:59:31 -04:00
BackgroundColor : model . NewPointer ( "#dddddd" ) ,
2025-02-25 04:22:15 -05:00
} ,
}
2025-08-20 07:50:38 -04:00
var createdChannel * model . Channel
createdChannel , resp , err = client . CreateChannel ( context . Background ( ) , channel )
2025-02-25 04:22:15 -05:00
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
require . True ( t , * createdChannel . BannerInfo . Enabled )
require . Equal ( t , "banner text" , * createdChannel . BannerInfo . Text )
2025-05-13 03:59:31 -04:00
require . Equal ( t , "#dddddd" , * createdChannel . BannerInfo . BackgroundColor )
2025-02-25 04:22:15 -05:00
} )
t . Run ( "Cannot create channel with banner enabled but not configured" , func ( t * testing . T ) {
channel := & model . Channel {
DisplayName : "" ,
Name : GenerateTestChannelName ( ) ,
Type : model . ChannelTypeOpen ,
TeamId : team . Id ,
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
} ,
}
2025-08-20 07:50:38 -04:00
_ , resp , err = client . CreateChannel ( context . Background ( ) , channel )
2025-02-25 04:22:15 -05:00
CheckErrorID ( t , err , "api.context.invalid_body_param.app_error" )
CheckBadRequestStatus ( t , resp )
} )
2025-08-20 07:50:38 -04:00
2026-03-12 10:32:29 -04:00
t . Run ( "should override channel name with server-generated ID when UseAnonymousURLs is enabled and not otherwise" , func ( t * testing . T ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . PrivacySettings . UseAnonymousURLs = true } )
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
defer func ( ) {
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
} ( )
originalName := GenerateTestChannelName ( )
ch := & model . Channel { DisplayName : "Anonymous URL Channel" , Name : originalName , Type : model . ChannelTypeOpen , TeamId : team . Id }
createdChannel , response , err := th . SystemAdminClient . CreateChannel ( context . Background ( ) , ch )
require . NoError ( t , err )
CheckCreatedStatus ( t , response )
require . NotEqual ( t , originalName , createdChannel . Name , "channel name should be overridden by server" )
require . True ( t , model . IsValidId ( createdChannel . Name ) )
require . Equal ( t , "Anonymous URL Channel" , createdChannel . DisplayName , "display name should remain unchanged" )
// setting UseAnonymousURLs to false should preserve names
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . PrivacySettings . UseAnonymousURLs = false } )
ch = & model . Channel { DisplayName : "Regular Channel" , Name : originalName , Type : model . ChannelTypeOpen , TeamId : team . Id }
createdChannel , response , err = th . SystemAdminClient . CreateChannel ( context . Background ( ) , ch )
require . NoError ( t , err )
CheckCreatedStatus ( t , response )
require . Equal ( t , originalName , createdChannel . Name )
// setting license to something other than Enterprise Advanced should also preserve team name
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterprise ) )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . PrivacySettings . UseAnonymousURLs = true } )
originalName = GenerateTestChannelName ( )
ch = & model . Channel { DisplayName : "Regular Channel" , Name : originalName , Type : model . ChannelTypeOpen , TeamId : team . Id }
createdChannel , response , err = th . SystemAdminClient . CreateChannel ( context . Background ( ) , ch )
require . NoError ( t , err )
CheckCreatedStatus ( t , response )
require . Equal ( t , originalName , createdChannel . Name )
} )
2025-08-20 07:50:38 -04:00
t . Run ( "Guest users" , func ( t * testing . T ) {
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterprise ) )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = true } )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . AllowEmailAccounts = true } )
2025-11-12 07:00:51 -05:00
guestUser := th . CreateUser ( t )
2025-08-20 07:50:38 -04:00
appErr := th . App . VerifyUserEmail ( guestUser . Id , guestUser . Email )
require . Nil ( t , appErr )
appErr = th . App . DemoteUserToGuest ( th . Context , guestUser )
require . Nil ( t , appErr )
_ , _ , appErr = th . App . AddUserToTeam ( th . Context , th . BasicTeam . Id , guestUser . Id , "" )
require . Nil ( t , appErr )
guestClient := th . CreateClient ( )
_ , _ , err := guestClient . Login ( context . Background ( ) , guestUser . Username , guestUser . Password )
require . NoError ( t , err )
t . Cleanup ( func ( ) {
_ , lErr := guestClient . Logout ( context . Background ( ) )
require . NoError ( t , lErr )
} )
2025-11-12 07:00:51 -05:00
userOutsideOfChannels := th . CreateUser ( t )
2025-08-20 07:50:38 -04:00
_ , _ , err = th . Client . AddTeamMember ( context . Background ( ) , team . Id , userOutsideOfChannels . Id )
require . NoError ( t , err )
public := & model . Channel { DisplayName : "Test API Name" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypeOpen , TeamId : team . Id }
private := & model . Channel { DisplayName : "Test API Name" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypePrivate , TeamId : team . Id }
t . Run ( "Guest user should not be able to create channels" , func ( t * testing . T ) {
_ , resp , err = guestClient . CreateChannel ( context . Background ( ) , public )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
private . Name = GenerateTestChannelName ( )
_ , resp , err = guestClient . CreateChannel ( context . Background ( ) , private )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
} )
t . Run ( "Guest user should not be able to add channel members if they have no common channels" , func ( t * testing . T ) {
// Now actually create the channels with the main client
public , _ , err = th . Client . CreateChannel ( context . Background ( ) , public )
require . NoError ( t , err )
private , _ , err = th . Client . CreateChannel ( context . Background ( ) , private )
require . NoError ( t , err )
// Add the guest user to the private channel
_ , _ , err = th . Client . AddChannelMember ( context . Background ( ) , private . Id , guestUser . Id )
require . NoError ( t , err )
// Verify that the guest user can access the private channel they were added to
2026-01-20 11:46:17 -05:00
_ , _ , err = guestClient . GetChannel ( context . Background ( ) , private . Id )
2025-08-20 07:50:38 -04:00
require . NoError ( t , err )
// Verify that the guest user cannot add members to the private channel
_ , resp , err = guestClient . AddChannelMember ( context . Background ( ) , private . Id , userOutsideOfChannels . Id )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
// Add the guest user to the public channel
_ , _ , err = th . Client . AddChannelMember ( context . Background ( ) , public . Id , guestUser . Id )
require . NoError ( t , err )
// Verify that the guest user can access the public channel they were added to
2026-01-20 11:46:17 -05:00
_ , _ , err = guestClient . GetChannel ( context . Background ( ) , public . Id )
2025-08-20 07:50:38 -04:00
require . NoError ( t , err )
// Verify that the guest user cannot add members to the public channel
_ , resp , err = guestClient . AddChannelMember ( context . Background ( ) , public . Id , userOutsideOfChannels . Id )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
// Update team guest permissions to allow creating private channels
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionCreatePrivateChannel . Id , model . TeamGuestRoleId )
2025-08-20 07:50:38 -04:00
privateGuest := & model . Channel { DisplayName : "Test API Name" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypePrivate , TeamId : team . Id }
privateGuest , resp , err = guestClient . CreateChannel ( context . Background ( ) , privateGuest )
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
// Verify that the guest user can't add users they have no visibility to
_ , resp , err = guestClient . AddChannelMember ( context . Background ( ) , privateGuest . Id , userOutsideOfChannels . Id )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
} )
} )
2017-02-02 09:04:36 -05:00
}
2017-02-03 10:27:12 -05:00
2017-03-13 09:24:30 -04:00
func TestUpdateChannel ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-13 09:24:30 -04:00
team := th . BasicTeam
2021-07-12 14:05:36 -04:00
channel := & model . Channel { DisplayName : "Test API Name" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypeOpen , TeamId : team . Id }
private := & model . Channel { DisplayName : "Test API Name" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypePrivate , TeamId : team . Id }
2017-03-13 09:24:30 -04:00
2025-06-19 08:13:45 -04:00
channel , _ , err := client . CreateChannel ( context . Background ( ) , channel )
require . NoError ( t , err )
private , _ , err = client . CreateChannel ( context . Background ( ) , private )
require . NoError ( t , err )
2017-03-13 09:24:30 -04:00
2025-01-29 08:58:43 -05:00
// Update a open channel
2017-03-13 09:24:30 -04:00
channel . DisplayName = "My new display name"
channel . Header = "My fancy header"
channel . Purpose = "Mattermost ftw!"
2023-06-06 17:29:29 -04:00
newChannel , _ , err := client . UpdateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-13 09:24:30 -04:00
2019-11-13 13:53:57 -05:00
require . Equal ( t , channel . DisplayName , newChannel . DisplayName , "Update failed for DisplayName" )
require . Equal ( t , channel . Header , newChannel . Header , "Update failed for Header" )
require . Equal ( t , channel . Purpose , newChannel . Purpose , "Update failed for Purpose" )
2017-03-13 09:24:30 -04:00
2019-04-15 11:13:11 -04:00
// Test GroupConstrained flag
2024-08-05 23:45:00 -04:00
channel . GroupConstrained = model . NewPointer ( true )
2023-06-06 17:29:29 -04:00
rchannel , resp , err := client . UpdateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-04-15 11:13:11 -04:00
CheckOKStatus ( t , resp )
2019-11-13 13:53:57 -05:00
require . Equal ( t , * channel . GroupConstrained , * rchannel . GroupConstrained , "GroupConstrained flags do not match" )
2019-04-15 11:13:11 -04:00
2025-01-29 08:58:43 -05:00
// Update a private channel
2017-03-13 09:24:30 -04:00
private . DisplayName = "My new display name for private channel"
private . Header = "My fancy private header"
private . Purpose = "Mattermost ftw! in private mode"
2023-06-06 17:29:29 -04:00
newPrivateChannel , _ , err := client . UpdateChannel ( context . Background ( ) , private )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-13 09:24:30 -04:00
2019-11-13 13:53:57 -05:00
require . Equal ( t , private . DisplayName , newPrivateChannel . DisplayName , "Update failed for DisplayName in private channel" )
require . Equal ( t , private . Header , newPrivateChannel . Header , "Update failed for Header in private channel" )
require . Equal ( t , private . Purpose , newPrivateChannel . Purpose , "Update failed for Purpose in private channel" )
2017-03-13 09:24:30 -04:00
2025-01-29 08:58:43 -05:00
// Test updating default channel's name and returns error
2025-06-19 08:13:45 -04:00
defaultChannel , appErr := th . App . GetChannelByName ( th . Context , model . DefaultChannelName , team . Id , false )
require . Nil ( t , appErr )
2021-10-29 13:46:50 -04:00
defaultChannel . Name = "testing"
2023-06-06 17:29:29 -04:00
_ , resp , err = client . UpdateChannel ( context . Background ( ) , defaultChannel )
2021-10-29 13:46:50 -04:00
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
2019-12-13 12:12:47 -05:00
2021-10-29 13:46:50 -04:00
// Test that changing the type fails and returns error
2021-07-12 14:05:36 -04:00
private . Type = model . ChannelTypeOpen
2023-06-06 17:29:29 -04:00
_ , resp , err = client . UpdateChannel ( context . Background ( ) , private )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-12-13 12:12:47 -05:00
CheckBadRequestStatus ( t , resp )
// Test that keeping the same type succeeds
2021-07-12 14:05:36 -04:00
private . Type = model . ChannelTypePrivate
2023-06-06 17:29:29 -04:00
_ , _ , err = client . UpdateChannel ( context . Background ( ) , private )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-12-13 12:12:47 -05:00
2025-01-29 08:58:43 -05:00
// Non existing channel
2021-07-12 14:05:36 -04:00
channel1 := & model . Channel { DisplayName : "Test API Name for apiv4" , Name : GenerateTestChannelName ( ) , Type : model . ChannelTypeOpen , TeamId : team . Id }
2023-06-06 17:29:29 -04:00
_ , resp , err = client . UpdateChannel ( context . Background ( ) , channel1 )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-13 09:24:30 -04:00
CheckNotFoundStatus ( t , resp )
2025-01-29 08:58:43 -05:00
// Try to update with not logged user
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . UpdateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-13 09:24:30 -04:00
CheckUnauthorizedStatus ( t , resp )
2025-01-29 08:58:43 -05:00
// Try to update using another user
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2017-03-13 09:24:30 -04:00
channel . DisplayName = "Should not update"
2023-06-06 17:29:29 -04:00
_ , resp , err = client . UpdateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-07-10 04:55:46 -04:00
CheckForbiddenStatus ( t , resp )
// Test updating the header of someone else's GM channel.
2025-11-12 07:00:51 -05:00
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
user3 := th . CreateUser ( t )
2018-07-10 04:55:46 -04:00
2023-06-06 17:29:29 -04:00
groupChannel , _ , err := client . CreateGroupChannel ( context . Background ( ) , [ ] string { user1 . Id , user2 . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-07-10 04:55:46 -04:00
groupChannel . Header = "lolololol"
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user3 . Email , user3 . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . UpdateChannel ( context . Background ( ) , groupChannel )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-07-10 04:55:46 -04:00
CheckForbiddenStatus ( t , resp )
2017-03-13 09:24:30 -04:00
2018-07-10 04:55:46 -04:00
// Test updating the header of someone else's GM channel.
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2018-07-10 04:55:46 -04:00
2023-06-06 17:29:29 -04:00
directChannel , _ , err := client . CreateDirectChannel ( context . Background ( ) , user . Id , user1 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-07-10 04:55:46 -04:00
directChannel . Header = "lolololol"
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user3 . Email , user3 . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . UpdateChannel ( context . Background ( ) , directChannel )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-07-10 04:55:46 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-05 13:16:47 -04:00
t . Run ( "null value" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
r , err := client . DoAPIPut ( context . Background ( ) , fmt . Sprintf ( "/channels" + "/%v" , channel . Id ) , "null" )
2023-06-05 13:16:47 -04:00
resp := model . BuildResponse ( r )
defer closeBody ( r )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
2023-09-05 05:30:32 -04:00
t . Run ( "Should block changes to name, display name or purpose for group messages" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
user3 := th . CreateUser ( t )
2023-09-05 05:30:32 -04:00
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user1 . Email , user1 . Password )
require . NoError ( t , err )
2023-09-05 05:30:32 -04:00
groupChannel , _ , err := client . CreateGroupChannel ( context . Background ( ) , [ ] string { user1 . Id , user2 . Id , user3 . Id } )
require . NoError ( t , err )
updatedChannel := & model . Channel { Id : groupChannel . Id , Name : "test name" }
_ , resp , err := client . UpdateChannel ( context . Background ( ) , updatedChannel )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
updatedChannel2 := & model . Channel { Id : groupChannel . Id , DisplayName : "test display name" }
_ , resp , err = client . UpdateChannel ( context . Background ( ) , updatedChannel2 )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
updatedChannel3 := & model . Channel { Id : groupChannel . Id , Purpose : "test purpose" }
_ , resp , err = client . UpdateChannel ( context . Background ( ) , updatedChannel3 )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
t . Run ( "Should block changes to name, display name or purpose for direct messages" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
2023-09-05 05:30:32 -04:00
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user1 . Email , user1 . Password )
require . NoError ( t , err )
2023-09-05 05:30:32 -04:00
directChannel , _ , err := client . CreateDirectChannel ( context . Background ( ) , user1 . Id , user2 . Id )
require . NoError ( t , err )
updatedChannel := & model . Channel { Id : directChannel . Id , Name : "test name" }
_ , resp , err := client . UpdateChannel ( context . Background ( ) , updatedChannel )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
updatedChannel2 := & model . Channel { Id : directChannel . Id , DisplayName : "test display name" }
_ , resp , err = client . UpdateChannel ( context . Background ( ) , updatedChannel2 )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
updatedChannel3 := & model . Channel { Id : directChannel . Id , Purpose : "test purpose" }
_ , resp , err = client . UpdateChannel ( context . Background ( ) , updatedChannel3 )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
2017-03-13 09:24:30 -04:00
}
2026-02-06 12:19:06 -05:00
func TestPatchChannelGroupConstrained ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-24 16:45:34 -04:00
2025-02-25 04:22:15 -05:00
t . Run ( "Test GroupConstrained flag" , func ( t * testing . T ) {
// Test GroupConstrained flag
patch := & model . ChannelPatch { }
patch . GroupConstrained = model . NewPointer ( true )
rchannel , resp , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2025-02-25 04:22:15 -05:00
CheckOKStatus ( t , resp )
2017-03-24 16:45:34 -04:00
2025-02-25 04:22:15 -05:00
require . Equal ( t , * rchannel . GroupConstrained , * patch . GroupConstrained , "GroupConstrained flags do not match" )
patch . GroupConstrained = nil
_ , resp , err = client . PatchChannel ( context . Background ( ) , "junk" , patch )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
_ , resp , err = client . PatchChannel ( context . Background ( ) , model . NewId ( ) , patch )
require . Error ( t , err )
CheckNotFoundStatus ( t , resp )
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2026-02-06 12:19:06 -05:00
patch . GroupConstrained = model . NewPointer ( false )
2025-02-25 04:22:15 -05:00
_ , resp , err = client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
_ , _ , err = client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . NoError ( t , err )
_ , _ , err = client . PatchChannel ( context . Background ( ) , th . BasicPrivateChannel . Id , patch )
require . NoError ( t , err )
} )
2020-07-16 04:25:22 -04:00
} )
2019-04-02 16:09:48 -04:00
2025-04-01 12:20:00 -04:00
t . Run ( "Test GroupConstrained flag set to true and non group members are removed" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2025-04-01 12:20:00 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterprise ) )
defer func ( ) {
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2025-04-01 12:20:00 -04:00
} ( )
// Create a test group
2025-11-12 07:00:51 -05:00
group := th . CreateGroup ( t )
2025-04-01 12:20:00 -04:00
// Create a channel and set it as group-constrained
2025-11-12 07:00:51 -05:00
channel := th . CreatePrivateChannel ( t )
2025-04-01 12:20:00 -04:00
// Add user to the channel
2025-11-12 07:00:51 -05:00
th . AddUserToChannel ( t , th . BasicUser2 , channel )
2025-04-01 12:20:00 -04:00
// Create a group user
2025-11-12 07:00:51 -05:00
groupUser := th . CreateUser ( t )
th . LinkUserToTeam ( t , groupUser , th . BasicTeam )
2025-04-01 12:20:00 -04:00
// Create a group member
_ , appErr := th . App . UpsertGroupMember ( group . Id , groupUser . Id )
require . Nil ( t , appErr )
// Associate the group with the channel
autoAdd := true
schemeAdmin := true
_ , r , err := th . SystemAdminClient . LinkGroupSyncable ( context . Background ( ) , group . Id , channel . Id , model . GroupSyncableTypeChannel , & model . GroupSyncablePatch { AutoAdd : & autoAdd , SchemeAdmin : & schemeAdmin } )
require . NoError ( t , err )
CheckCreatedStatus ( t , r )
patch := & model . ChannelPatch { }
patch . GroupConstrained = model . NewPointer ( true )
_ , r , err = th . SystemAdminClient . PatchChannel ( context . Background ( ) , channel . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , r )
// Wait for the user to be removed from the channel by polling until they're gone
// or until we hit the timeout
timeout := time . After ( 3 * time . Second )
ticker := time . NewTicker ( 100 * time . Millisecond )
defer ticker . Stop ( )
userRemoved := false
for ! userRemoved {
select {
case <- timeout :
require . Fail ( t , "Timed out waiting for user to be removed from channel" )
return
case <- ticker . C :
// Check if the user is still a member
_ , r , err = th . SystemAdminClient . GetChannelMember ( context . Background ( ) , channel . Id , th . BasicUser2 . Id , "" )
if err != nil && r . StatusCode == http . StatusNotFound {
// User has been removed, we can continue the test
userRemoved = true
}
}
}
// Verify the user is no longer a member of the channel
_ , r , err = th . SystemAdminClient . GetChannelMember ( context . Background ( ) , channel . Id , th . BasicUser2 . Id , "" )
require . Error ( t , err )
CheckNotFoundStatus ( t , r )
} )
t . Run ( "Test GroupConstrained flag changed from true to false and non group members are not removed" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2025-04-01 12:20:00 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterprise ) )
defer func ( ) {
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2025-04-01 12:20:00 -04:00
} ( )
// Create a test group
2025-11-12 07:00:51 -05:00
group := th . CreateGroup ( t )
2025-04-01 12:20:00 -04:00
// Create a channel and set it as group-constrained
2025-11-12 07:00:51 -05:00
channel := th . CreatePrivateChannel ( t )
2025-04-01 12:20:00 -04:00
// Create a group user
2025-11-12 07:00:51 -05:00
groupUser := th . CreateUser ( t )
th . LinkUserToTeam ( t , groupUser , th . BasicTeam )
2025-04-01 12:20:00 -04:00
// Create a group member
_ , appErr := th . App . UpsertGroupMember ( group . Id , groupUser . Id )
require . Nil ( t , appErr )
// Associate the group with the channel
autoAdd := true
schemeAdmin := true
_ , r , err := th . SystemAdminClient . LinkGroupSyncable ( context . Background ( ) , group . Id , channel . Id , model . GroupSyncableTypeChannel , & model . GroupSyncablePatch { AutoAdd : & autoAdd , SchemeAdmin : & schemeAdmin } )
require . NoError ( t , err )
CheckCreatedStatus ( t , r )
// Wait for the user to be added to the channel by polling until you see them
// or until we hit the timeout
timeout := time . After ( 3 * time . Second )
ticker := time . NewTicker ( 100 * time . Millisecond )
defer ticker . Stop ( )
var cm * model . ChannelMember
userFound := false
for ! userFound {
select {
case <- timeout :
require . Fail ( t , "Timed out waiting for user to be added to the channel" )
return
case <- ticker . C :
// Check if the user is now a member
cm , _ , err = th . SystemAdminClient . GetChannelMember ( context . Background ( ) , channel . Id , groupUser . Id , "" )
if err == nil && cm . UserId == groupUser . Id {
// User has been added, we can continue the test
userFound = true
}
}
}
patch := & model . ChannelPatch { }
patch . GroupConstrained = model . NewPointer ( true )
_ , r , err = th . SystemAdminClient . PatchChannel ( context . Background ( ) , channel . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , r )
// Change the GroupConstrained flag to false
patch . GroupConstrained = model . NewPointer ( false )
_ , r , err = th . SystemAdminClient . PatchChannel ( context . Background ( ) , channel . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , r )
// Unlink the group
r , err = th . SystemAdminClient . UnlinkGroupSyncable ( context . Background ( ) , group . Id , channel . Id , model . GroupSyncableTypeChannel )
require . NoError ( t , err )
CheckOKStatus ( t , r )
// Wait for a reasonable amount of time to ensure the user is not removed because the channel is no longer group constrained
timeout = time . After ( 2 * time . Second )
ticker = time . NewTicker ( 100 * time . Millisecond )
defer ticker . Stop ( )
userStillPresent := true
for userStillPresent {
select {
case <- timeout :
// If we reach the timeout, the user is still present, which is what we want
// Verify the user is still a member of the channel
cm , r , err = th . SystemAdminClient . GetChannelMember ( context . Background ( ) , channel . Id , groupUser . Id , "" )
require . NoError ( t , err )
CheckOKStatus ( t , r )
require . Equal ( t , groupUser . Id , cm . UserId )
return
case <- ticker . C :
// Check if the user is still a member
_ , r , err = th . SystemAdminClient . GetChannelMember ( context . Background ( ) , channel . Id , groupUser . Id , "" )
if err != nil && r . StatusCode == http . StatusNotFound {
// User has been removed, which is not what we want
require . Fail ( t , "User was incorrectly removed from the channel" )
userStillPresent = false
}
}
}
} )
2026-02-06 12:19:06 -05:00
}
func TestPatchChannel ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
client := th . Client
team := th . BasicTeam
t . Run ( "should be unable to apply a null patch" , func ( t * testing . T ) {
var nullPatch * model . ChannelPatch
_ , nullResp , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , nullPatch )
require . Error ( t , err )
CheckBadRequestStatus ( t , nullResp )
} )
t . Run ( "should be able to patch values" , func ( t * testing . T ) {
patch := & model . ChannelPatch {
Name : new ( string ) ,
DisplayName : new ( string ) ,
Header : new ( string ) ,
Purpose : new ( string ) ,
}
* patch . Name = model . NewId ( )
* patch . DisplayName = model . NewId ( )
* patch . Header = model . NewId ( )
* patch . Purpose = model . NewId ( )
channel , _ , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . NoError ( t , err )
require . Equal ( t , * patch . Name , channel . Name , "do not match" )
require . Equal ( t , * patch . DisplayName , channel . DisplayName , "do not match" )
require . Equal ( t , * patch . Header , channel . Header , "do not match" )
require . Equal ( t , * patch . Purpose , channel . Purpose , "do not match" )
} )
t . Run ( "should be able to patch with no name" , func ( t * testing . T ) {
channel := & model . Channel {
DisplayName : GenerateTestChannelName ( ) ,
Name : GenerateTestChannelName ( ) ,
Type : model . ChannelTypeOpen ,
TeamId : team . Id ,
}
var err error
channel , _ , err = client . CreateChannel ( context . Background ( ) , channel )
require . NoError ( t , err )
patch := & model . ChannelPatch {
Header : new ( string ) ,
Purpose : new ( string ) ,
}
oldName := channel . Name
patchedChannel , _ , err := client . PatchChannel ( context . Background ( ) , channel . Id , patch )
require . NoError ( t , err )
require . Equal ( t , oldName , patchedChannel . Name , "should not have updated" )
} )
t . Run ( "Test updating default channel's name and returns error" , func ( t * testing . T ) {
// Test updating default channel's name and returns error
defaultChannel , appErr := th . App . GetChannelByName ( th . Context , model . DefaultChannelName , team . Id , false )
require . Nil ( t , appErr )
defaultChannelPatch := & model . ChannelPatch {
Name : new ( string ) ,
}
* defaultChannelPatch . Name = "testing"
_ , resp , err := client . PatchChannel ( context . Background ( ) , defaultChannel . Id , defaultChannelPatch )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
2025-04-01 12:20:00 -04:00
2025-02-25 04:22:15 -05:00
t . Run ( "Test updating the header of someone else's GM channel" , func ( t * testing . T ) {
// Test updating the header of someone else's GM channel.
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
user3 := th . CreateUser ( t )
2018-07-10 04:55:46 -04:00
2025-02-25 04:22:15 -05:00
groupChannel , _ , err := client . CreateGroupChannel ( context . Background ( ) , [ ] string { user1 . Id , user2 . Id } )
require . NoError ( t , err )
2018-07-10 04:55:46 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user3 . Email , user3 . Password )
require . NoError ( t , err )
2018-07-10 04:55:46 -04:00
2025-02-25 04:22:15 -05:00
channelPatch := & model . ChannelPatch { }
channelPatch . Header = new ( string )
* channelPatch . Header = "lolololol"
2018-07-10 04:55:46 -04:00
2025-02-25 04:22:15 -05:00
_ , resp , err := client . PatchChannel ( context . Background ( ) , groupChannel . Id , channelPatch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
2018-07-10 04:55:46 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2018-07-10 04:55:46 -04:00
2025-02-25 04:22:15 -05:00
directChannel , _ , err := client . CreateDirectChannel ( context . Background ( ) , user . Id , user1 . Id )
require . NoError ( t , err )
2018-07-10 04:55:46 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user3 . Email , user3 . Password )
require . NoError ( t , err )
2025-02-25 04:22:15 -05:00
_ , resp , err = client . PatchChannel ( context . Background ( ) , directChannel . Id , channelPatch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
} )
2023-09-05 05:30:32 -04:00
t . Run ( "Should block changes to name, display name or purpose for group messages" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
user3 := th . CreateUser ( t )
2023-09-05 05:30:32 -04:00
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user1 . Email , user1 . Password )
require . NoError ( t , err )
2023-09-05 05:30:32 -04:00
groupChannel , _ , err := client . CreateGroupChannel ( context . Background ( ) , [ ] string { user1 . Id , user2 . Id , user3 . Id } )
require . NoError ( t , err )
groupChannelPatch := & model . ChannelPatch {
Name : new ( string ) ,
}
* groupChannelPatch . Name = "testing"
_ , resp , err := client . PatchChannel ( context . Background ( ) , groupChannel . Id , groupChannelPatch )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
groupChannelPatch2 := & model . ChannelPatch {
DisplayName : new ( string ) ,
}
* groupChannelPatch2 . DisplayName = "test display name"
_ , resp , err = client . PatchChannel ( context . Background ( ) , groupChannel . Id , groupChannelPatch2 )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
groupChannelPatch3 := & model . ChannelPatch {
Purpose : new ( string ) ,
}
* groupChannelPatch3 . Purpose = "test purpose"
_ , resp , err = client . PatchChannel ( context . Background ( ) , groupChannel . Id , groupChannelPatch3 )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
t . Run ( "Should block changes to name, display name or purpose for direct messages" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
2023-09-05 05:30:32 -04:00
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user1 . Email , user1 . Password )
require . NoError ( t , err )
2023-09-05 05:30:32 -04:00
directChannel , _ , err := client . CreateDirectChannel ( context . Background ( ) , user1 . Id , user2 . Id )
require . NoError ( t , err )
directChannelPatch := & model . ChannelPatch {
Name : new ( string ) ,
}
* directChannelPatch . Name = "test"
_ , resp , err := client . PatchChannel ( context . Background ( ) , directChannel . Id , directChannelPatch )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
directChannelPatch2 := & model . ChannelPatch {
DisplayName : new ( string ) ,
}
* directChannelPatch2 . DisplayName = "test display name"
_ , resp , err = client . PatchChannel ( context . Background ( ) , directChannel . Id , directChannelPatch2 )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
directChannelPatch3 := & model . ChannelPatch {
Purpose : new ( string ) ,
}
* directChannelPatch3 . Purpose = "test purpose"
_ , resp , err = client . PatchChannel ( context . Background ( ) , directChannel . Id , directChannelPatch3 )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
2025-02-25 04:22:15 -05:00
2025-03-13 03:08:29 -04:00
t . Run ( "Should not be able to configure channel banner without a license" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2025-03-13 03:08:29 -04:00
channel := & model . Channel {
DisplayName : GenerateTestChannelName ( ) ,
Name : GenerateTestChannelName ( ) ,
Type : model . ChannelTypeOpen ,
TeamId : team . Id ,
}
channel , _ , err = client . CreateChannel ( context . Background ( ) , channel )
require . NoError ( t , err )
patch := & model . ChannelPatch {
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
Text : model . NewPointer ( "banner text" ) ,
2025-05-13 03:59:31 -04:00
BackgroundColor : model . NewPointer ( "#dddddd" ) ,
2025-03-13 03:08:29 -04:00
} ,
}
patchedChannel , resp , err := client . PatchChannel ( context . Background ( ) , channel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
require . Nil ( t , patchedChannel )
} )
t . Run ( "Should not be able to configure channel banner with a professional license" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2025-03-13 03:08:29 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuProfessional ) )
defer func ( ) {
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2025-03-13 03:08:29 -04:00
} ( )
channel := & model . Channel {
DisplayName : GenerateTestChannelName ( ) ,
Name : GenerateTestChannelName ( ) ,
Type : model . ChannelTypeOpen ,
TeamId : team . Id ,
}
channel , _ , err = client . CreateChannel ( context . Background ( ) , channel )
require . NoError ( t , err )
patch := & model . ChannelPatch {
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
Text : model . NewPointer ( "banner text" ) ,
2025-05-13 03:59:31 -04:00
BackgroundColor : model . NewPointer ( "#dddddd" ) ,
2025-03-13 03:08:29 -04:00
} ,
}
patchedChannel , resp , err := client . PatchChannel ( context . Background ( ) , channel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
require . Nil ( t , patchedChannel )
} )
2025-02-25 04:22:15 -05:00
t . Run ( "Should be able to configure channel banner on a channel" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2025-05-02 02:04:46 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
2025-03-13 03:08:29 -04:00
defer func ( ) {
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2025-03-13 03:08:29 -04:00
} ( )
2025-02-25 04:22:15 -05:00
channel := & model . Channel {
DisplayName : GenerateTestChannelName ( ) ,
Name : GenerateTestChannelName ( ) ,
Type : model . ChannelTypeOpen ,
TeamId : team . Id ,
}
channel , _ , err = client . CreateChannel ( context . Background ( ) , channel )
require . NoError ( t , err )
patch := & model . ChannelPatch {
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
Text : model . NewPointer ( "banner text" ) ,
2025-05-13 03:59:31 -04:00
BackgroundColor : model . NewPointer ( "#dddddd" ) ,
2025-02-25 04:22:15 -05:00
} ,
}
patchedChannel , resp , err := client . PatchChannel ( context . Background ( ) , channel . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
require . NotNil ( t , patchedChannel . BannerInfo )
require . True ( t , * patchedChannel . BannerInfo . Enabled )
require . Equal ( t , "banner text" , * patchedChannel . BannerInfo . Text )
2025-05-13 03:59:31 -04:00
require . Equal ( t , "#dddddd" , * patchedChannel . BannerInfo . BackgroundColor )
2025-02-25 04:22:15 -05:00
} )
2025-05-06 04:33:35 -04:00
t . Run ( "Should not be able to configure channel banner on a channel as a non-admin channel member" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2025-05-06 04:33:35 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
defer func ( ) {
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2025-05-06 04:33:35 -04:00
} ( )
patch := & model . ChannelPatch {
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
Text : model . NewPointer ( "banner text" ) ,
2025-05-13 03:59:31 -04:00
BackgroundColor : model . NewPointer ( "#dddddd" ) ,
2025-05-06 04:33:35 -04:00
} ,
}
_ , resp , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
} )
t . Run ( "Should be able to configure channel banner as a team admin" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LoginTeamAdmin ( t )
2025-05-06 04:33:35 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
defer func ( ) {
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2025-05-06 04:33:35 -04:00
} ( )
patch := & model . ChannelPatch {
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
Text : model . NewPointer ( "banner text" ) ,
2025-05-13 03:59:31 -04:00
BackgroundColor : model . NewPointer ( "#dddddd" ) ,
2025-05-06 04:33:35 -04:00
} ,
}
patchedChannel , resp , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel2 . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
require . NotNil ( t , patchedChannel . BannerInfo )
require . True ( t , * patchedChannel . BannerInfo . Enabled )
require . Equal ( t , "banner text" , * patchedChannel . BannerInfo . Text )
2025-05-13 03:59:31 -04:00
require . Equal ( t , "#dddddd" , * patchedChannel . BannerInfo . BackgroundColor )
2025-05-06 04:33:35 -04:00
} )
2025-02-25 04:22:15 -05:00
t . Run ( "Cannot enable channel banner without configuring it" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2025-05-02 02:04:46 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
2025-03-13 03:08:29 -04:00
defer func ( ) {
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2025-03-13 03:08:29 -04:00
} ( )
2025-02-25 04:22:15 -05:00
channel := & model . Channel {
DisplayName : GenerateTestChannelName ( ) ,
Name : GenerateTestChannelName ( ) ,
Type : model . ChannelTypeOpen ,
TeamId : team . Id ,
}
channel , _ , err = client . CreateChannel ( context . Background ( ) , channel )
require . NoError ( t , err )
patch := & model . ChannelPatch {
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
} ,
}
_ , resp , err := client . PatchChannel ( context . Background ( ) , channel . Id , patch )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
// now we will configure it first, then enable it
patch = & model . ChannelPatch {
BannerInfo : & model . ChannelBannerInfo {
Enabled : nil ,
Text : model . NewPointer ( "banner text" ) ,
2025-05-13 03:59:31 -04:00
BackgroundColor : model . NewPointer ( "#dddddd" ) ,
2025-02-25 04:22:15 -05:00
} ,
}
patchedChannel , resp , err := client . PatchChannel ( context . Background ( ) , channel . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
require . NotNil ( t , patchedChannel . BannerInfo )
require . Nil ( t , patchedChannel . BannerInfo . Enabled )
require . Equal ( t , "banner text" , * patchedChannel . BannerInfo . Text )
2025-05-13 03:59:31 -04:00
require . Equal ( t , "#dddddd" , * patchedChannel . BannerInfo . BackgroundColor )
2025-02-25 04:22:15 -05:00
patch = & model . ChannelPatch {
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
} ,
}
patchedChannel , resp , err = client . PatchChannel ( context . Background ( ) , channel . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
require . NotNil ( t , patchedChannel . BannerInfo )
require . True ( t , * patchedChannel . BannerInfo . Enabled )
require . Equal ( t , "banner text" , * patchedChannel . BannerInfo . Text )
2025-05-13 03:59:31 -04:00
require . Equal ( t , "#dddddd" , * patchedChannel . BannerInfo . BackgroundColor )
2025-02-25 04:22:15 -05:00
} )
t . Run ( "Cannot configure channel banner on a DM channel" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2025-05-02 02:04:46 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
2025-03-13 03:08:29 -04:00
defer func ( ) {
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2025-03-13 03:08:29 -04:00
} ( )
2025-02-25 04:22:15 -05:00
dmChannel , resp , err := client . CreateDirectChannel ( context . Background ( ) , th . BasicUser . Id , th . BasicUser2 . Id )
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
patch := & model . ChannelPatch {
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
Text : model . NewPointer ( "banner text" ) ,
2025-05-13 03:59:31 -04:00
BackgroundColor : model . NewPointer ( "#dddddd" ) ,
2025-02-25 04:22:15 -05:00
} ,
}
patchedChannel , resp , err := client . PatchChannel ( context . Background ( ) , dmChannel . Id , patch )
require . Error ( t , err )
require . Equal ( t , "Channel banner can only be configured on Public and Private channels." , err . Error ( ) )
CheckBadRequestStatus ( t , resp )
require . Nil ( t , patchedChannel )
} )
t . Run ( "Cannot configure channel banner on a GM channel" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2025-05-02 02:04:46 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
2025-03-13 03:08:29 -04:00
defer func ( ) {
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2025-03-13 03:08:29 -04:00
} ( )
2025-02-25 04:22:15 -05:00
2025-11-12 07:00:51 -05:00
user3 := th . CreateUser ( t )
2025-02-25 04:22:15 -05:00
gmChannel , resp , err := client . CreateGroupChannel ( context . Background ( ) , [ ] string { th . BasicUser . Id , th . BasicUser2 . Id , user3 . Id } )
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
patch := & model . ChannelPatch {
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( true ) ,
Text : model . NewPointer ( "banner text" ) ,
2025-05-13 03:59:31 -04:00
BackgroundColor : model . NewPointer ( "#dddddd" ) ,
2025-02-25 04:22:15 -05:00
} ,
}
patchedChannel , resp , err := client . PatchChannel ( context . Background ( ) , gmChannel . Id , patch )
require . Error ( t , err )
require . Equal ( t , "Channel banner can only be configured on Public and Private channels." , err . Error ( ) )
CheckBadRequestStatus ( t , resp )
require . Nil ( t , patchedChannel )
} )
2026-02-06 12:19:06 -05:00
t . Run ( "Patch channel with no changes returns 400" , func ( t * testing . T ) {
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
th . LoginBasic ( t )
patch := & model . ChannelPatch { }
_ , resp , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
t . Run ( "Patch channel with autotranslation when feature is available properly updates the channel for admins" , func ( t * testing . T ) {
mockAutoTranslation := & einterfacesmocks . AutoTranslationInterface { }
mockAutoTranslation . On ( "IsFeatureAvailable" ) . Return ( true )
mockAutoTranslation . On ( "IsChannelEnabled" , mock . Anything ) . Return ( true , nil )
mockAutoTranslation . On ( "Translate" , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil , nil )
originalAutoTranslation := th . Server . AutoTranslation
th . Server . AutoTranslation = mockAutoTranslation
defer func ( ) {
th . Server . AutoTranslation = originalAutoTranslation
} ( )
_ , err := th . SystemAdminClient . Logout ( context . Background ( ) )
require . NoError ( t , err )
th . LoginSystemAdmin ( t )
patch := & model . ChannelPatch {
AutoTranslation : model . NewPointer ( true ) ,
}
_ , resp , err := th . SystemAdminClient . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
patchedChannel , appErr := th . App . GetChannel ( th . Context , th . BasicChannel . Id )
require . Nil ( t , appErr )
require . True ( t , patchedChannel . AutoTranslation )
patch = & model . ChannelPatch {
AutoTranslation : model . NewPointer ( false ) ,
}
_ , resp , err = th . SystemAdminClient . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
patchedChannel , appErr = th . App . GetChannel ( th . Context , th . BasicChannel . Id )
require . Nil ( t , appErr )
require . False ( t , patchedChannel . AutoTranslation )
} )
t . Run ( "Patch channel with autotranslation when feature is available properly updates the channel for users only with the proper permissions" , func ( t * testing . T ) {
mockAutoTranslation := & einterfacesmocks . AutoTranslationInterface { }
mockAutoTranslation . On ( "IsFeatureAvailable" ) . Return ( true )
mockAutoTranslation . On ( "IsChannelEnabled" , mock . Anything ) . Return ( true , nil )
mockAutoTranslation . On ( "Translate" , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil , nil )
originalAutoTranslation := th . Server . AutoTranslation
th . Server . AutoTranslation = mockAutoTranslation
defer func ( ) {
th . Server . AutoTranslation = originalAutoTranslation
} ( )
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
th . LoginBasic ( t )
privateChannel := th . CreateChannelWithClient ( t , th . SystemAdminClient , model . ChannelTypePrivate )
th . AddUserToChannel ( t , th . BasicUser , privateChannel )
patch := & model . ChannelPatch {
AutoTranslation : model . NewPointer ( true ) ,
}
_ , resp , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
_ , resp , err = client . PatchChannel ( context . Background ( ) , privateChannel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
th . AddPermissionToRole ( t , model . PermissionManagePrivateChannelAutoTranslation . Id , model . SystemUserRoleId )
defer th . RemovePermissionFromRole ( t , model . PermissionManagePrivateChannelAutoTranslation . Id , model . SystemUserRoleId )
_ , resp , err = client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
_ , _ , err = client . PatchChannel ( context . Background ( ) , privateChannel . Id , patch )
require . NoError ( t , err )
patchedChannel , appErr := th . App . GetChannel ( th . Context , privateChannel . Id )
require . Nil ( t , appErr )
require . True ( t , patchedChannel . AutoTranslation )
th . AddPermissionToRole ( t , model . PermissionManagePublicChannelAutoTranslation . Id , model . SystemUserRoleId )
defer th . RemovePermissionFromRole ( t , model . PermissionManagePublicChannelAutoTranslation . Id , model . SystemUserRoleId )
_ , _ , err = client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . NoError ( t , err )
patchedChannel , appErr = th . App . GetChannel ( th . Context , privateChannel . Id )
require . Nil ( t , appErr )
require . True ( t , patchedChannel . AutoTranslation )
} )
t . Run ( "Patch channel with AutoTranslation when feature not available returns 403" , func ( t * testing . T ) {
mockAutoTranslation := & einterfacesmocks . AutoTranslationInterface { }
mockAutoTranslation . On ( "IsFeatureAvailable" ) . Return ( false )
mockAutoTranslation . On ( "IsChannelEnabled" , mock . Anything ) . Return ( true , nil )
mockAutoTranslation . On ( "Translate" , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil , nil )
originalAutoTranslation := th . Server . AutoTranslation
th . Server . AutoTranslation = mockAutoTranslation
defer func ( ) {
th . Server . AutoTranslation = originalAutoTranslation
} ( )
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
th . LoginBasic ( t )
patch := & model . ChannelPatch {
AutoTranslation : model . NewPointer ( true ) ,
}
_ , resp , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
var appErr * model . AppError
require . True ( t , errors . As ( err , & appErr ) )
require . Contains ( t , [ ] string { "api.channel.patch_update_channel.feature_not_available.app_error" , "api.channel.patch_update_channel.auto_translation_restricted.app_error" } , appErr . Id )
} )
t . Run ( "Patch channel with autotranslation on DM is only available for members" , func ( t * testing . T ) {
mockAutoTranslation := & einterfacesmocks . AutoTranslationInterface { }
mockAutoTranslation . On ( "IsFeatureAvailable" ) . Return ( true )
mockAutoTranslation . On ( "IsChannelEnabled" , mock . Anything ) . Return ( true , nil )
mockAutoTranslation . On ( "Translate" , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil , nil )
originalAutoTranslation := th . Server . AutoTranslation
th . Server . AutoTranslation = mockAutoTranslation
defer func ( ) {
th . Server . AutoTranslation = originalAutoTranslation
} ( )
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
th . LoginBasic ( t )
dmChannel , resp , err := client . CreateDirectChannel ( context . Background ( ) , th . BasicUser . Id , th . BasicUser2 . Id )
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
nonMemberDmChannel , resp , err := th . SystemAdminClient . CreateDirectChannel ( context . Background ( ) , th . BasicUser2 . Id , th . SystemAdminUser . Id )
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
patch := & model . ChannelPatch {
AutoTranslation : model . NewPointer ( true ) ,
}
_ , resp , err = client . PatchChannel ( context . Background ( ) , dmChannel . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
patchedChannel , appErr := th . App . GetChannel ( th . Context , dmChannel . Id )
require . Nil ( t , appErr )
require . True ( t , patchedChannel . AutoTranslation )
_ , resp , err = client . PatchChannel ( context . Background ( ) , nonMemberDmChannel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
} )
t . Run ( "Patch channel with autotranslation on GM is only available for members" , func ( t * testing . T ) {
mockAutoTranslation := & einterfacesmocks . AutoTranslationInterface { }
mockAutoTranslation . On ( "IsFeatureAvailable" ) . Return ( true )
mockAutoTranslation . On ( "IsChannelEnabled" , mock . Anything ) . Return ( true , nil )
mockAutoTranslation . On ( "Translate" , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil , nil )
originalAutoTranslation := th . Server . AutoTranslation
th . Server . AutoTranslation = mockAutoTranslation
defer func ( ) {
th . Server . AutoTranslation = originalAutoTranslation
} ( )
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
th . LoginBasic ( t )
user3 := th . CreateUser ( t )
gmChannel , resp , err := client . CreateGroupChannel ( context . Background ( ) , [ ] string { th . BasicUser . Id , th . BasicUser2 . Id , user3 . Id } )
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
nonMemberGmChannel , resp , err := th . SystemAdminClient . CreateGroupChannel ( context . Background ( ) , [ ] string { th . BasicUser2 . Id , th . SystemAdminUser . Id , user3 . Id } )
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
patch := & model . ChannelPatch {
AutoTranslation : model . NewPointer ( true ) ,
}
_ , resp , err = client . PatchChannel ( context . Background ( ) , gmChannel . Id , patch )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
patchedChannel , appErr := th . App . GetChannel ( th . Context , gmChannel . Id )
require . Nil ( t , appErr )
require . True ( t , patchedChannel . AutoTranslation )
_ , resp , err = client . PatchChannel ( context . Background ( ) , nonMemberGmChannel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
} )
t . Run ( "Patch DM with AutoTranslation when RestrictDMAndGM is true returns 403" , func ( t * testing . T ) {
mockAutoTranslation := & einterfacesmocks . AutoTranslationInterface { }
mockAutoTranslation . On ( "IsFeatureAvailable" ) . Return ( true )
mockAutoTranslation . On ( "IsChannelEnabled" , mock . Anything ) . Return ( true , nil )
mockAutoTranslation . On ( "Translate" , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil , nil )
originalAutoTranslation := th . Server . AutoTranslation
th . Server . AutoTranslation = mockAutoTranslation
defer func ( ) {
th . Server . AutoTranslation = originalAutoTranslation
} ( )
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
th . LoginBasic ( t )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . AutoTranslationSettings . RestrictDMAndGM = true
} )
defer th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . AutoTranslationSettings . RestrictDMAndGM = false
} )
dmChannel , resp , err := client . CreateDirectChannel ( context . Background ( ) , th . BasicUser . Id , th . BasicUser2 . Id )
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
patch := & model . ChannelPatch {
AutoTranslation : model . NewPointer ( true ) ,
}
_ , resp , err = client . PatchChannel ( context . Background ( ) , dmChannel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
// May be feature_not_available when AutoTranslation is nil, or auto_translation_restricted when RestrictDMAndGM applies
var appErr * model . AppError
require . True ( t , errors . As ( err , & appErr ) )
require . Contains ( t , [ ] string { "api.channel.patch_update_channel.feature_not_available.app_error" , "api.channel.patch_update_channel.auto_translation_restricted.app_error" } , appErr . Id )
} )
t . Run ( "Patch GM with AutoTranslation when RestrictDMAndGM is true returns 403" , func ( t * testing . T ) {
mockAutoTranslation := & einterfacesmocks . AutoTranslationInterface { }
mockAutoTranslation . On ( "IsFeatureAvailable" ) . Return ( true )
mockAutoTranslation . On ( "IsChannelEnabled" , mock . Anything ) . Return ( true , nil )
mockAutoTranslation . On ( "Translate" , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil , nil )
originalAutoTranslation := th . Server . AutoTranslation
th . Server . AutoTranslation = mockAutoTranslation
defer func ( ) {
th . Server . AutoTranslation = originalAutoTranslation
} ( )
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
th . LoginBasic ( t )
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . AutoTranslationSettings . RestrictDMAndGM = true
} )
defer th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . AutoTranslationSettings . RestrictDMAndGM = false
} )
user3 := th . CreateUser ( t )
gmChannel , resp , err := client . CreateGroupChannel ( context . Background ( ) , [ ] string { th . BasicUser . Id , th . BasicUser2 . Id , user3 . Id } )
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
patch := & model . ChannelPatch {
AutoTranslation : model . NewPointer ( true ) ,
}
_ , resp , err = client . PatchChannel ( context . Background ( ) , gmChannel . Id , patch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
// May be feature_not_available when AutoTranslation is nil, or auto_translation_restricted when RestrictDMAndGM applies
var appErr * model . AppError
require . True ( t , errors . As ( err , & appErr ) )
require . Contains ( t , [ ] string { "api.channel.patch_update_channel.feature_not_available.app_error" , "api.channel.patch_update_channel.auto_translation_restricted.app_error" } , appErr . Id )
} )
t . Run ( "Mixed patch only gets through if all permissions are met" , func ( t * testing . T ) {
mockAutoTranslation := & einterfacesmocks . AutoTranslationInterface { }
mockAutoTranslation . On ( "IsFeatureAvailable" ) . Return ( true )
mockAutoTranslation . On ( "IsChannelEnabled" , mock . Anything ) . Return ( true , nil )
mockAutoTranslation . On ( "Translate" , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil , nil )
originalAutoTranslation := th . Server . AutoTranslation
th . Server . AutoTranslation = mockAutoTranslation
defer func ( ) {
th . Server . AutoTranslation = originalAutoTranslation
} ( )
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
th . LoginBasic ( t )
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
defer func ( ) {
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
} ( )
// Mixed patch (channel property + AutoTranslation) fails when user lacks AutoTranslation permission
newHeader := "mixed patch header"
mixedPatch := & model . ChannelPatch {
Header : & newHeader ,
AutoTranslation : model . NewPointer ( true ) ,
BannerInfo : & model . ChannelBannerInfo {
Enabled : model . NewPointer ( false ) ,
Text : model . NewPointer ( "mixed patch banner" ) ,
} ,
}
// Permissions missing: AutoTranslation, BannerInfo
_ , resp , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , mixedPatch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
// Permissions missing: Channel properties
th . AddPermissionToRole ( t , model . PermissionManagePublicChannelAutoTranslation . Id , model . SystemUserRoleId )
defer th . RemovePermissionFromRole ( t , model . PermissionManagePublicChannelAutoTranslation . Id , model . SystemUserRoleId )
th . AddPermissionToRole ( t , model . PermissionManagePublicChannelBanner . Id , model . SystemUserRoleId )
defer th . RemovePermissionFromRole ( t , model . PermissionManagePublicChannelBanner . Id , model . SystemUserRoleId )
th . RemovePermissionFromRole ( t , model . PermissionManagePublicChannelProperties . Id , model . ChannelUserRoleId )
defer th . AddPermissionToRole ( t , model . PermissionManagePublicChannelProperties . Id , model . ChannelUserRoleId )
_ , resp , err = client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , mixedPatch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
// Permissions missing: AutoTranslation
th . AddPermissionToRole ( t , model . PermissionManagePublicChannelProperties . Id , model . ChannelUserRoleId )
th . RemovePermissionFromRole ( t , model . PermissionManagePublicChannelAutoTranslation . Id , model . SystemUserRoleId )
_ , resp , err = client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , mixedPatch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
// Permission missing: BannerInfo
th . AddPermissionToRole ( t , model . PermissionManagePublicChannelAutoTranslation . Id , model . SystemUserRoleId )
th . RemovePermissionFromRole ( t , model . PermissionManagePublicChannelBanner . Id , model . SystemUserRoleId )
_ , resp , err = client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , mixedPatch )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
// No missing permissions
th . AddPermissionToRole ( t , model . PermissionManagePublicChannelBanner . Id , model . SystemUserRoleId )
patchedChannel , resp , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , mixedPatch )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
require . Equal ( t , newHeader , patchedChannel . Header )
require . True ( t , patchedChannel . AutoTranslation )
} )
2017-03-24 16:45:34 -04:00
}
2025-05-06 04:33:35 -04:00
func TestCanEditChannelBanner ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-05-06 04:33:35 -04:00
t . Run ( "when license is nil" , func ( t * testing . T ) {
channel := & model . Channel {
Type : model . ChannelTypeOpen ,
}
th . App . Srv ( ) . SetLicense ( nil )
webContext := & Context {
App : th . App ,
AppContext : th . Context ,
Params : & web . Params {
ChannelId : "channel_id" ,
} ,
}
canEditChannelBanner ( webContext , channel )
require . NotNil ( t , webContext . Err )
assert . Equal ( t , "api.context.permissions.app_error" , webContext . Err . Id )
assert . Equal ( t , http . StatusForbidden , webContext . Err . StatusCode )
} )
t . Run ( "when license is not E20 or Enterprise" , func ( t * testing . T ) {
license := model . NewTestLicenseSKU ( model . LicenseShortSkuProfessional )
th . App . Srv ( ) . SetLicense ( license )
webContext := & Context {
App : th . App ,
AppContext : th . Context ,
Params : & web . Params {
ChannelId : "channel_id" ,
} ,
}
channel := & model . Channel {
Type : model . ChannelTypeOpen ,
}
canEditChannelBanner ( webContext , channel )
require . NotNil ( t , webContext . Err )
assert . Equal ( t , "api.context.permissions.app_error" , webContext . Err . Id )
assert . Equal ( t , http . StatusForbidden , webContext . Err . StatusCode )
} )
t . Run ( "when channel type is direct message" , func ( t * testing . T ) {
license := model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced )
th . App . Srv ( ) . SetLicense ( license )
webContext := & Context {
App : th . App ,
AppContext : th . Context ,
Params : & web . Params {
ChannelId : "channel_id" ,
} ,
}
channel := & model . Channel {
Type : model . ChannelTypeDirect ,
}
canEditChannelBanner ( webContext , channel )
require . NotNil ( t , webContext . Err )
assert . Equal ( t , "api.channel.update_channel.banner_info.channel_type.not_allowed" , webContext . Err . Id )
assert . Equal ( t , http . StatusBadRequest , webContext . Err . StatusCode )
} )
t . Run ( "when channel type is group message" , func ( t * testing . T ) {
license := model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced )
th . App . Srv ( ) . SetLicense ( license )
webContext := & Context {
App : th . App ,
AppContext : th . Context ,
Params : & web . Params {
ChannelId : "channel_id" ,
} ,
}
channel := & model . Channel {
Type : model . ChannelTypeGroup ,
}
canEditChannelBanner ( webContext , channel )
require . NotNil ( t , webContext . Err )
assert . Equal ( t , "api.channel.update_channel.banner_info.channel_type.not_allowed" , webContext . Err . Id )
assert . Equal ( t , http . StatusBadRequest , webContext . Err . StatusCode )
} )
t . Run ( "when channel type is open and license is valid" , func ( t * testing . T ) {
license := model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced )
th . App . Srv ( ) . SetLicense ( license )
2025-11-12 07:00:51 -05:00
channel := th . CreatePublicChannel ( t )
th . MakeUserChannelAdmin ( t , th . BasicUser , channel )
2025-05-06 04:33:35 -04:00
webContext := & Context {
App : th . App ,
AppContext : th . Context ,
Params : & web . Params {
ChannelId : channel . Id ,
} ,
}
webContext . AppContext = webContext . AppContext . WithSession ( & model . Session {
UserId : th . BasicUser . Id ,
} )
canEditChannelBanner ( webContext , channel )
assert . Nil ( t , webContext . Err )
} )
t . Run ( "when channel type is private and license is valid" , func ( t * testing . T ) {
license := model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced )
th . App . Srv ( ) . SetLicense ( license )
2025-11-12 07:00:51 -05:00
channel := th . CreatePrivateChannel ( t )
th . MakeUserChannelAdmin ( t , th . BasicUser , channel )
2025-05-06 04:33:35 -04:00
webContext := & Context {
App : th . App ,
AppContext : th . Context ,
Params : & web . Params {
ChannelId : channel . Id ,
} ,
}
webContext . AppContext = webContext . AppContext . WithSession ( & model . Session {
UserId : th . BasicUser . Id ,
} )
canEditChannelBanner ( webContext , channel )
assert . Nil ( t , webContext . Err )
} )
}
2020-04-07 16:56:07 -04:00
func TestChannelUnicodeNames ( 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-08-13 07:12:16 -04:00
client := th . Client
2020-04-07 16:56:07 -04:00
team := th . BasicTeam
t . Run ( "create channel unicode" , func ( t * testing . T ) {
channel := & model . Channel {
Name : "\u206cenglish\u206dchannel" ,
DisplayName : "The \u206cEnglish\u206d Channel" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypeOpen ,
2025-01-29 08:58:43 -05:00
TeamId : team . Id ,
}
2020-04-07 16:56:07 -04:00
2023-06-06 17:29:29 -04:00
rchannel , resp , err := client . CreateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-07 16:56:07 -04:00
CheckCreatedStatus ( t , resp )
require . Equal ( t , "englishchannel" , rchannel . Name , "bad unicode should be filtered from name" )
require . Equal ( t , "The English Channel" , rchannel . DisplayName , "bad unicode should be filtered from display name" )
} )
t . Run ( "update channel unicode" , func ( t * testing . T ) {
channel := & model . Channel {
DisplayName : "Test API Name" ,
Name : GenerateTestChannelName ( ) ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypeOpen ,
2020-04-07 16:56:07 -04:00
TeamId : team . Id ,
}
2025-06-19 08:13:45 -04:00
channel , _ , err := client . CreateChannel ( context . Background ( ) , channel )
require . NoError ( t , err )
2020-04-07 16:56:07 -04:00
channel . Name = "\u206ahistorychannel"
channel . DisplayName = "UFO's and \ufff9stuff\ufffb."
2023-06-06 17:29:29 -04:00
newChannel , _ , err := client . UpdateChannel ( context . Background ( ) , channel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-07 16:56:07 -04:00
require . Equal ( t , "historychannel" , newChannel . Name , "bad unicode should be filtered from name" )
require . Equal ( t , "UFO's and stuff." , newChannel . DisplayName , "bad unicode should be filtered from display name" )
} )
t . Run ( "patch channel unicode" , func ( t * testing . T ) {
patch := & model . ChannelPatch {
Name : new ( string ) ,
DisplayName : new ( string ) ,
Header : new ( string ) ,
Purpose : new ( string ) ,
}
* patch . Name = "\u206ecommunitychannel\u206f"
* patch . DisplayName = "Natalie Tran's \ufffcAwesome Channel"
2023-06-06 17:29:29 -04:00
channel , _ , err := client . PatchChannel ( context . Background ( ) , th . BasicChannel . Id , patch )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-07 16:56:07 -04:00
require . Equal ( t , "communitychannel" , channel . Name , "bad unicode should be filtered from name" )
require . Equal ( t , "Natalie Tran's Awesome Channel" , channel . DisplayName , "bad unicode should be filtered from display name" )
} )
}
2017-02-03 10:27:12 -05:00
func TestCreateDirectChannel ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-02-03 10:27:12 -05:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
user3 := th . CreateUser ( t )
2017-02-03 10:27:12 -05:00
2023-06-06 17:29:29 -04:00
dm , _ , err := client . CreateDirectChannel ( context . Background ( ) , user1 . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-03 10:27:12 -05:00
channelName := ""
if user2 . Id > user1 . Id {
channelName = user1 . Id + "__" + user2 . Id
} else {
channelName = user2 . Id + "__" + user1 . Id
}
2019-11-13 13:53:57 -05:00
require . Equal ( t , channelName , dm . Name , "dm name didn't match" )
2017-02-03 10:27:12 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . CreateDirectChannel ( context . Background ( ) , "junk" , user2 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-03 10:27:12 -05:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateDirectChannel ( context . Background ( ) , user1 . Id , model . NewId ( ) )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-03 10:27:12 -05:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateDirectChannel ( context . Background ( ) , model . NewId ( ) , user1 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-03 10:27:12 -05:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateDirectChannel ( context . Background ( ) , model . NewId ( ) , user2 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-03 10:27:12 -05:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
r , err := client . DoAPIPost ( context . Background ( ) , "/channels/direct" , "garbage" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , http . StatusBadRequest , r . StatusCode )
2017-02-03 10:27:12 -05:00
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . CreateDirectChannel ( context . Background ( ) , user3 . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-05-19 08:45:03 -04:00
// Normal client should not be allowed to create a direct channel if users are
// restricted to messaging members of their own team
th . App . UpdateConfig ( func ( cfg * model . Config ) {
2021-07-12 14:05:36 -04:00
* cfg . TeamSettings . RestrictDirectMessage = model . DirectMessageTeam
2021-05-19 08:45:03 -04:00
} )
2025-11-12 07:00:51 -05:00
user4 := th . CreateUser ( t )
2023-06-06 17:29:29 -04:00
_ , resp , err = th . Client . CreateDirectChannel ( context . Background ( ) , user1 . Id , user4 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2021-05-19 08:45:03 -04:00
CheckForbiddenStatus ( t , resp )
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , user4 , th . BasicTeam )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . Client . CreateDirectChannel ( context . Background ( ) , user1 . Id , user4 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-05-19 08:45:03 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateDirectChannel ( context . Background ( ) , model . NewId ( ) , user2 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-03 10:27:12 -05:00
CheckUnauthorizedStatus ( t , resp )
}
2017-02-07 17:58:27 -05:00
2019-07-31 12:37:28 -04:00
func TestCreateDirectChannelAsGuest ( 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-08-13 07:12:16 -04:00
client := th . Client
2019-07-31 12:37:28 -04:00
user1 := th . BasicUser
enableGuestAccounts := * th . App . Config ( ) . GuestAccountsSettings . Enable
defer func ( ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = enableGuestAccounts } )
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2019-07-31 12:37:28 -04:00
} ( )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = true } )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2019-07-31 12:37:28 -04:00
id := model . NewId ( )
2026-04-08 15:49:43 -04:00
guestPassword := model . NewTestPassword ( )
2019-07-31 12:37:28 -04:00
guest := & model . User {
Email : "success+" + id + "@simulator.amazonses.com" ,
Username : "un_" + id ,
Nickname : "nn_" + id ,
2026-04-08 15:49:43 -04:00
Password : guestPassword ,
2019-07-31 12:37:28 -04:00
EmailVerified : true ,
}
2021-08-13 07:12:16 -04:00
guest , appErr := th . App . CreateGuest ( th . Context , guest )
require . Nil ( t , appErr )
2019-07-31 12:37:28 -04:00
2026-04-08 15:49:43 -04:00
_ , _ , err := client . Login ( context . Background ( ) , guest . Username , guestPassword )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-07-31 12:37:28 -04:00
t . Run ( "Try to created DM with not visible user" , func ( t * testing . T ) {
2021-08-13 07:12:16 -04:00
var resp * model . Response
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateDirectChannel ( context . Background ( ) , guest . Id , user1 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-07-31 12:37:28 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateDirectChannel ( context . Background ( ) , user1 . Id , guest . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-07-31 12:37:28 -04:00
CheckForbiddenStatus ( t , resp )
} )
t . Run ( "Creating DM with visible user" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , guest , th . BasicTeam )
th . AddUserToChannel ( t , guest , th . BasicChannel )
2019-07-31 12:37:28 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err = client . CreateDirectChannel ( context . Background ( ) , guest . Id , user1 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-07-31 12:37:28 -04:00
} )
}
2018-07-05 03:17:43 -04:00
func TestDeleteDirectChannel ( 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-08-13 07:12:16 -04:00
client := th . Client
2018-07-05 03:17:43 -04:00
user := th . BasicUser
user2 := th . BasicUser2
2023-06-06 17:29:29 -04:00
rgc , resp , err := client . CreateDirectChannel ( context . Background ( ) , user . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-07-05 03:17:43 -04:00
CheckCreatedStatus ( t , resp )
require . NotNil ( t , rgc , "should have created a direct channel" )
2023-06-06 17:29:29 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , rgc . Id )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.channel.delete_channel.type.invalid" )
2018-07-05 03:17:43 -04:00
}
2017-04-20 09:55:43 -04:00
func TestCreateGroupChannel ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-04-20 09:55:43 -04:00
user := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
user3 := th . CreateUser ( t )
2017-04-20 09:55:43 -04:00
userIds := [ ] string { user . Id , user2 . Id , user3 . Id }
2023-06-06 17:29:29 -04:00
rgc , resp , err := client . CreateGroupChannel ( context . Background ( ) , userIds )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-04-20 09:55:43 -04:00
CheckCreatedStatus ( t , resp )
2019-11-13 13:53:57 -05:00
require . NotNil ( t , rgc , "should have created a group channel" )
2021-07-12 14:05:36 -04:00
require . Equal ( t , model . ChannelTypeGroup , rgc . Type , "should have created a channel of group type" )
2017-04-20 09:55:43 -04:00
2025-06-19 08:13:45 -04:00
m , appErr := th . App . GetChannelMembersPage ( th . Context , rgc . Id , 0 , 10 )
require . Nil ( t , appErr )
2021-08-17 05:18:33 -04:00
require . Len ( t , m , 3 , "should have 3 channel members" )
2017-04-20 09:55:43 -04:00
// saving duplicate group channel
2023-06-06 17:29:29 -04:00
rgc2 , _ , err := client . CreateGroupChannel ( context . Background ( ) , [ ] string { user3 . Id , user2 . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , rgc . Id , rgc2 . Id , "should have returned existing channel" )
2017-04-20 09:55:43 -04:00
2025-06-19 08:13:45 -04:00
m2 , appErr := th . App . GetChannelMembersPage ( th . Context , rgc2 . Id , 0 , 10 )
require . Nil ( t , appErr )
2022-08-08 04:44:18 -04:00
require . ElementsMatch ( t , m , m2 )
2017-04-20 09:55:43 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateGroupChannel ( context . Background ( ) , [ ] string { user2 . Id } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-04-20 09:55:43 -04:00
CheckBadRequestStatus ( t , resp )
2025-11-12 07:00:51 -05:00
user4 := th . CreateUser ( t )
user5 := th . CreateUser ( t )
user6 := th . CreateUser ( t )
user7 := th . CreateUser ( t )
user8 := th . CreateUser ( t )
user9 := th . CreateUser ( t )
2017-04-20 09:55:43 -04:00
2023-06-06 17:29:29 -04:00
rgc , resp , err = client . CreateGroupChannel ( context . Background ( ) , [ ] string { user . Id , user2 . Id , user3 . Id , user4 . Id , user5 . Id , user6 . Id , user7 . Id , user8 . Id , user9 . Id } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-04-20 09:55:43 -04:00
CheckBadRequestStatus ( t , resp )
2019-11-13 13:53:57 -05:00
require . Nil ( t , rgc )
2017-04-20 09:55:43 -04:00
2023-12-20 00:46:54 -05:00
_ , resp , err = client . CreateGroupChannel ( context . Background ( ) , [ ] string { user . Id , user2 . Id , user3 . Id , GenerateTestID ( ) } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-04-20 09:55:43 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateGroupChannel ( context . Background ( ) , [ ] string { user . Id , user2 . Id , user3 . Id , "junk" } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-04-20 09:55:43 -04:00
CheckBadRequestStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2017-04-20 09:55:43 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateGroupChannel ( context . Background ( ) , userIds )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-04-20 09:55:43 -04:00
CheckUnauthorizedStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . CreateGroupChannel ( context . Background ( ) , userIds )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-04-20 09:55:43 -04:00
}
2019-07-31 12:37:28 -04:00
func TestCreateGroupChannelAsGuest ( 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-08-13 07:12:16 -04:00
client := th . Client
2019-07-31 12:37:28 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
user3 := th . CreateUser ( t )
user4 := th . CreateUser ( t )
user5 := th . CreateUser ( t )
th . LinkUserToTeam ( t , user2 , th . BasicTeam )
th . AddUserToChannel ( t , user2 , th . BasicChannel )
th . LinkUserToTeam ( t , user3 , th . BasicTeam )
th . AddUserToChannel ( t , user3 , th . BasicChannel )
2019-07-31 12:37:28 -04:00
enableGuestAccounts := * th . App . Config ( ) . GuestAccountsSettings . Enable
defer func ( ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = enableGuestAccounts } )
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2019-07-31 12:37:28 -04:00
} ( )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = true } )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2019-07-31 12:37:28 -04:00
id := model . NewId ( )
2026-04-08 15:49:43 -04:00
guestPassword := model . NewTestPassword ( )
2019-07-31 12:37:28 -04:00
guest := & model . User {
Email : "success+" + id + "@simulator.amazonses.com" ,
Username : "un_" + id ,
Nickname : "nn_" + id ,
2026-04-08 15:49:43 -04:00
Password : guestPassword ,
2019-07-31 12:37:28 -04:00
EmailVerified : true ,
}
2021-08-13 07:12:16 -04:00
guest , appErr := th . App . CreateGuest ( th . Context , guest )
require . Nil ( t , appErr )
2026-04-08 15:49:43 -04:00
_ , _ , err := client . Login ( context . Background ( ) , guest . Username , guestPassword )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-07-31 12:37:28 -04:00
2021-08-13 07:12:16 -04:00
var resp * model . Response
2019-07-31 12:37:28 -04:00
t . Run ( "Try to created GM with not visible users" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateGroupChannel ( context . Background ( ) , [ ] string { guest . Id , user1 . Id , user2 . Id , user3 . Id } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-07-31 12:37:28 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateGroupChannel ( context . Background ( ) , [ ] string { user1 . Id , user2 . Id , guest . Id , user3 . Id } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-07-31 12:37:28 -04:00
CheckForbiddenStatus ( t , resp )
} )
t . Run ( "Try to created GM with visible and not visible users" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , guest , th . BasicTeam )
th . AddUserToChannel ( t , guest , th . BasicChannel )
2019-07-31 12:37:28 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateGroupChannel ( context . Background ( ) , [ ] string { guest . Id , user1 . Id , user3 . Id , user4 . Id , user5 . Id } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-07-31 12:37:28 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . CreateGroupChannel ( context . Background ( ) , [ ] string { user1 . Id , user2 . Id , guest . Id , user4 . Id , user5 . Id } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-07-31 12:37:28 -04:00
CheckForbiddenStatus ( t , resp )
} )
t . Run ( "Creating GM with visible users" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , _ , err = client . CreateGroupChannel ( context . Background ( ) , [ ] string { guest . Id , user1 . Id , user2 . Id , user3 . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-07-31 12:37:28 -04:00
} )
}
2018-07-05 03:17:43 -04:00
func TestDeleteGroupChannel ( 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-07-05 03:17:43 -04:00
user := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
user3 := th . CreateUser ( t )
2018-07-05 03:17:43 -04:00
userIds := [ ] string { user . Id , user2 . Id , user3 . Id }
2020-05-27 09:24:44 -04:00
th . TestForAllClients ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
rgc , resp , err := th . Client . CreateGroupChannel ( context . Background ( ) , userIds )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-05-27 09:24:44 -04:00
CheckCreatedStatus ( t , resp )
require . NotNil ( t , rgc , "should have created a group channel" )
2023-06-06 17:29:29 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , rgc . Id )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.channel.delete_channel.type.invalid" )
2020-05-27 09:24:44 -04:00
} )
2018-07-05 03:17:43 -04:00
}
2017-02-14 10:28:08 -05:00
func TestGetChannel ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-10-15 02:37:30 -04:00
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2021-08-13 07:12:16 -04:00
client := th . Client
2017-02-14 10:28:08 -05:00
2026-01-20 11:46:17 -05:00
channel , _ , err := client . GetChannel ( context . Background ( ) , th . BasicChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , th . BasicChannel . Id , channel . Id , "ids did not match" )
2017-02-20 11:31:52 -05:00
2025-06-19 08:13:45 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id )
require . NoError ( t , err )
2026-01-20 11:46:17 -05:00
_ , _ , err = client . GetChannel ( context . Background ( ) , th . BasicChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-22 11:13:44 -04:00
2026-01-20 11:46:17 -05:00
channel , _ , err = client . GetChannel ( context . Background ( ) , th . BasicPrivateChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , th . BasicPrivateChannel . Id , channel . Id , "ids did not match" )
2017-03-22 11:13:44 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , th . BasicPrivateChannel . Id , th . BasicUser . Id )
require . NoError ( t , err )
2026-01-20 11:46:17 -05:00
_ , resp , err := client . GetChannel ( context . Background ( ) , th . BasicPrivateChannel . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-14 10:28:08 -05:00
CheckForbiddenStatus ( t , resp )
2026-01-20 11:46:17 -05:00
_ , resp , err = client . GetChannel ( context . Background ( ) , model . NewId ( ) )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-22 11:13:44 -04:00
CheckNotFoundStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2026-01-20 11:46:17 -05:00
_ , resp , err = client . GetChannel ( context . Background ( ) , th . BasicChannel . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-14 10:28:08 -05:00
CheckUnauthorizedStatus ( t , resp )
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2026-01-20 11:46:17 -05:00
_ , resp , err = client . GetChannel ( context . Background ( ) , th . BasicChannel . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-14 10:28:08 -05:00
CheckForbiddenStatus ( t , resp )
2020-06-14 04:31:20 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2026-01-20 11:46:17 -05:00
_ , _ , err = client . GetChannel ( context . Background ( ) , th . BasicChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-14 10:28:08 -05:00
2026-01-20 11:46:17 -05:00
_ , _ , err = client . GetChannel ( context . Background ( ) , th . BasicPrivateChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-22 11:13:44 -04:00
2026-01-20 11:46:17 -05:00
_ , resp , err = client . GetChannel ( context . Background ( ) , th . BasicUser . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-06-14 04:31:20 -04:00
CheckNotFoundStatus ( t , resp )
} )
2025-10-15 02:37:30 -04:00
t . Run ( "Content reviewer should be able to get channel without membership with flagged post" , func ( t * testing . T ) {
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterpriseAdvanced ) )
appErr := setBasicCommonReviewerConfig ( th )
require . Nil ( t , appErr )
contentReviewClient := th . CreateClient ( )
_ , _ , err := contentReviewClient . Login ( context . Background ( ) , th . BasicUser . Email , th . BasicUser . Password )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
privateChannel := th . CreateChannelWithClient ( t , contentReviewClient , model . ChannelTypePrivate )
post := th . CreatePostWithClient ( t , contentReviewClient , privateChannel )
2025-10-15 02:37:30 -04:00
response , err := contentReviewClient . FlagPostForContentReview ( context . Background ( ) , post . Id , & model . FlagContentRequest {
Rename Content Flagging to Data Spillage Handling (#35407)
* Rename Content Flagging to Data Spillage Handling
Update all user-facing text to use "Data Spillage Handling" and
"Quarantine for Review" terminology. Rename i18n keys that referenced
content flagging. Auto-patch bot display name on pre-existing servers.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Fixed searchable stringgs
* Revert unintended package-lock.json changes
Co-authored-by: Cursor <cursoragent@cursor.com>
* Fix i18n extract check: correct typo and key ordering
Fix "posed" -> "posted" typo in keep/remove quarantine modal
defaultMessages. Move admin.contentFlagging.title to correct
alphabetical position in en.json.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Fix webapp tests for Data Spillage Handling rename
Update test assertions to match renamed i18n strings:
notification settings, content reviewers, and additional
settings tests now expect the new quarantine terminology.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Use translatable i18n strings for notification messages
Replace hardcoded "flagged for review" notification templates with
i18n.T() calls using "quarantined for review" terminology. Add six
new server i18n keys for author, reporter, and reviewer notifications.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Fix server i18n key mismatches and update test assertions
Rename remaining app.content_flagging.* keys to app.data_spillage.*
in server/i18n/en.json to match Go code references. Fix the
quarantine_post_confirmation key name. Update test assertions to
match new "quarantined for review" terminology.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Fix gofmt formatting in content_flagging.go
Co-authored-by: Cursor <cursoragent@cursor.com>
* Prevent nil bot on PatchBot failure in getContentReviewBot
Use a separate variable for PatchBot result so the original bot
is preserved if the display name update fails.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Reorder server i18n keys after extract
Run mmgotool i18n extract to sort entries into correct
alphabetical order.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Replace i18n.T() with fmt.Sprintf for notification messages and fix test assertions
Use direct string formatting for bot notification messages instead of
i18n translation keys, which were being removed by mmgotool i18n extract
due to indirect key references. Also update test expectations for renamed
error keys (content_flagging -> data_spillage).
Co-authored-by: Cursor <cursoragent@cursor.com>
* Update default quarantine reasons to DISC-aligned terminology
Replace generic content moderation reasons with defense/intelligence
sector terminology: Classification mismatch, Need-to-know violation,
PII exposure, OPSEC concern, CUI violation, Unauthorized disclosure,
and Other. Updated across model, API tests, webapp tests, and e2e tests.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Adding a string missing from bad merge
* Update remaining flagged terminology and icon for data spillage rename
- Change post menu icon from flag-outline to alert-outline
- Update reviewer notification: "quarantined" -> "submitted" a message
- Update action notifications: "flagged message" -> "quarantined message"
- Update modal errors: "flagging" -> "quarantining" this message
- Update report title: "flagged" -> "submitted" a message for review
- Update e2e page object locator for renamed menu item
Made-with: Cursor
* Fix tests
* Fix quarantine icon alignment in post dot menu
Use AlertOutlineIcon React component with size={18} instead of raw
<i> tag to match the sizing of all other menu item icons.
Made-with: Cursor
* Fixed E2E tests
* Missing test fix
* Fix E2E tests
---------
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
2026-03-06 21:15:01 -05:00
Reason : "Classification mismatch" ,
2025-10-15 02:37:30 -04:00
Comment : "This is sensitive content" ,
} )
require . NoError ( t , err )
require . Equal ( t , http . StatusOK , response . StatusCode )
2025-11-12 07:00:51 -05:00
th . RemoveUserFromChannel ( t , th . BasicUser , privateChannel )
2025-10-15 02:37:30 -04:00
// We will fetch the channel providing the required params to indicate that we are fetching it for content review
fetchedChannel , _ , err := contentReviewClient . GetChannelAsContentReviewer ( context . Background ( ) , privateChannel . Id , "" , post . Id )
require . NoError ( t , err )
require . Equal ( t , privateChannel . Id , fetchedChannel . Id )
// This also doesn't work if user is not a content reviewer
contentFlaggingSettings , _ , err := th . SystemAdminClient . GetContentFlaggingSettings ( context . Background ( ) )
require . NoError ( t , err )
require . NotNil ( t , contentFlaggingSettings )
// Making system admin as a reviewer because there needs to be some reviewers
contentFlaggingSettings . ReviewerSettings . CommonReviewerIds = [ ] string { th . SystemAdminUser . Id }
resp , err = th . SystemAdminClient . SaveContentFlaggingSettings ( context . Background ( ) , contentFlaggingSettings )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
_ , resp , err = contentReviewClient . GetChannelAsContentReviewer ( context . Background ( ) , privateChannel . Id , "" , post . Id )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
} )
2017-02-14 10:28:08 -05:00
}
2017-05-09 08:52:46 -04:00
func TestGetDeletedChannelsForTeam ( 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-03 05:20:52 -04:00
2021-08-13 07:12:16 -04:00
client := th . Client
2017-05-09 08:52:46 -04:00
team := th . BasicTeam
2025-11-12 07:00:51 -05:00
th . LoginTeamAdmin ( t )
2017-05-09 08:52:46 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , err := client . GetDeletedChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-08-31 15:59:03 -04:00
numInitialChannelsForTeam := len ( channels )
2017-05-09 08:52:46 -04:00
// create and delete public channel
2025-11-12 07:00:51 -05:00
publicChannel1 := th . CreatePublicChannel ( t )
2025-06-19 08:13:45 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , publicChannel1 . Id )
require . NoError ( t , err )
2017-05-09 08:52:46 -04:00
2020-06-03 05:20:52 -04:00
th . TestForAllClients ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetDeletedChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-03 05:20:52 -04:00
require . Len ( t , channels , numInitialChannelsForTeam + 1 , "should be 1 deleted channel" )
} )
2017-05-09 08:52:46 -04:00
2025-11-12 07:00:51 -05:00
publicChannel2 := th . CreatePublicChannel ( t )
2025-06-19 08:13:45 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , publicChannel2 . Id )
require . NoError ( t , err )
2017-05-09 08:52:46 -04:00
2020-06-03 05:20:52 -04:00
th . TestForAllClients ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetDeletedChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-03 05:20:52 -04:00
require . Len ( t , channels , numInitialChannelsForTeam + 2 , "should be 2 deleted channels" )
} )
2017-05-09 08:52:46 -04:00
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2019-11-11 17:15:19 -05:00
2025-11-12 07:00:51 -05:00
privateChannel1 := th . CreatePrivateChannel ( t )
2025-06-19 08:13:45 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , privateChannel1 . Id )
require . NoError ( t , err )
2019-11-11 17:15:19 -05:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetDeletedChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-02-13 11:53:23 -05:00
require . Len ( t , channels , numInitialChannelsForTeam + 3 )
2019-11-11 17:15:19 -05:00
// Login as different user and create private channel
2025-11-12 07:00:51 -05:00
th . LoginBasic2 ( t )
privateChannel2 := th . CreatePrivateChannel ( t )
2025-06-19 08:13:45 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , privateChannel2 . Id )
require . NoError ( t , err )
2019-11-11 17:15:19 -05:00
// Log back in as first user
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2019-11-11 17:15:19 -05:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetDeletedChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-02-13 11:53:23 -05:00
require . Len ( t , channels , numInitialChannelsForTeam + 3 )
2019-11-11 17:15:19 -05:00
2020-06-03 05:20:52 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetDeletedChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2024-11-07 06:07:30 -05:00
// Local admin should see private archived channels
require . Len ( t , channels , numInitialChannelsForTeam + 4 )
2020-06-03 05:20:52 -04:00
} )
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetDeletedChannelsForTeam ( context . Background ( ) , team . Id , 0 , 1 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Len ( t , channels , 1 , "should be one channel per page" )
2017-05-09 08:52:46 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetDeletedChannelsForTeam ( context . Background ( ) , team . Id , 1 , 1 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Len ( t , channels , 1 , "should be one channel per page" )
2023-11-22 12:48:20 -05:00
// test non team member
2025-06-19 08:13:45 -04:00
_ , err = th . SystemAdminClient . RemoveTeamMember ( context . Background ( ) , team . Id , th . BasicUser . Id )
require . NoError ( t , err )
2023-11-22 12:48:20 -05:00
_ , resp , err := client . GetDeletedChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
2017-05-09 08:52:46 -04:00
}
2020-07-06 03:04:29 -04:00
func TestGetPrivateChannelsForTeam ( 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-07-06 03:04:29 -04:00
team := th . BasicTeam
// normal user
2023-06-06 17:29:29 -04:00
_ , resp , err := th . Client . GetPrivateChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-07-06 03:04:29 -04:00
CheckForbiddenStatus ( t , resp )
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , c * model . Client4 ) {
2023-06-06 17:29:29 -04:00
channels , _ , err := c . GetPrivateChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-07-06 03:04:29 -04:00
// th.BasicPrivateChannel and th.BasicPrivateChannel2
require . Len ( t , channels , 2 , "wrong number of private channels" )
for _ , c := range channels {
// check all channels included are private
2021-07-12 14:05:36 -04:00
require . Equal ( t , model . ChannelTypePrivate , c . Type , "should include private channels only" )
2020-07-06 03:04:29 -04:00
}
2023-06-06 17:29:29 -04:00
channels , _ , err = c . GetPrivateChannelsForTeam ( context . Background ( ) , team . Id , 0 , 1 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-07-06 03:04:29 -04:00
require . Len ( t , channels , 1 , "should be one channel per page" )
2023-06-06 17:29:29 -04:00
channels , _ , err = c . GetPrivateChannelsForTeam ( context . Background ( ) , team . Id , 1 , 1 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-07-06 03:04:29 -04:00
require . Len ( t , channels , 1 , "should be one channel per page" )
2023-06-06 17:29:29 -04:00
channels , _ , err = c . GetPrivateChannelsForTeam ( context . Background ( ) , team . Id , 10000 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-07-06 03:04:29 -04:00
require . Empty ( t , channels , "should be no channel" )
2023-06-06 17:29:29 -04:00
_ , resp , err = c . GetPrivateChannelsForTeam ( context . Background ( ) , "junk" , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-07-06 03:04:29 -04:00
CheckBadRequestStatus ( t , resp )
} )
}
2017-03-13 08:26:51 -04:00
func TestGetPublicChannelsForTeam ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-13 08:26:51 -04:00
team := th . BasicTeam
publicChannel1 := th . BasicChannel
publicChannel2 := th . BasicChannel2
2023-06-06 17:29:29 -04:00
channels , _ , err := client . GetPublicChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Len ( t , channels , 4 , "wrong path" )
2017-03-13 08:26:51 -04:00
2023-04-20 08:52:59 -04:00
var foundPublicChannel1 , foundPublicChannel2 bool
for _ , c := range channels {
2019-11-13 13:53:57 -05:00
// check all channels included are open
2021-07-12 14:05:36 -04:00
require . Equal ( t , model . ChannelTypeOpen , c . Type , "should include open channel only" )
2017-03-13 08:26:51 -04:00
// only check the created 2 public channels
2023-04-20 08:52:59 -04:00
switch c . DisplayName {
case publicChannel1 . DisplayName :
foundPublicChannel1 = true
case publicChannel2 . DisplayName :
foundPublicChannel2 = true
}
2017-03-13 08:26:51 -04:00
}
2023-04-20 08:52:59 -04:00
require . True ( t , foundPublicChannel1 , "failed to find publicChannel1" )
require . True ( t , foundPublicChannel2 , "failed to find publicChannel2" )
2025-11-12 07:00:51 -05:00
privateChannel := th . CreatePrivateChannel ( t )
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetPublicChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Len ( t , channels , 4 , "incorrect length of team public channels" )
2017-03-13 08:26:51 -04:00
2017-05-10 11:41:56 -04:00
for _ , c := range channels {
2021-07-12 14:05:36 -04:00
require . Equal ( t , model . ChannelTypeOpen , c . Type , "should not include private channel" )
2019-11-13 13:53:57 -05:00
require . NotEqual ( t , privateChannel . DisplayName , c . DisplayName , "should not match private channel display name" )
2017-03-13 08:26:51 -04:00
}
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetPublicChannelsForTeam ( context . Background ( ) , team . Id , 0 , 1 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Len ( t , channels , 1 , "should be one channel per page" )
2017-03-13 08:26:51 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetPublicChannelsForTeam ( context . Background ( ) , team . Id , 1 , 1 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Len ( t , channels , 1 , "should be one channel per page" )
2017-03-13 08:26:51 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetPublicChannelsForTeam ( context . Background ( ) , team . Id , 10000 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-12-22 06:35:31 -05:00
require . Empty ( t , channels , "should be no channel" )
2017-03-13 08:26:51 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . GetPublicChannelsForTeam ( context . Background ( ) , "junk" , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-13 08:26:51 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetPublicChannelsForTeam ( context . Background ( ) , model . NewId ( ) , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-13 08:26:51 -04:00
CheckForbiddenStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetPublicChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-13 08:26:51 -04:00
CheckUnauthorizedStatus ( t , resp )
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetPublicChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-13 08:26:51 -04:00
CheckForbiddenStatus ( t , resp )
2020-06-03 05:20:52 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
_ , _ , err = client . GetPublicChannelsForTeam ( context . Background ( ) , team . Id , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-03 05:20:52 -04:00
} )
2017-03-13 08:26:51 -04:00
}
2017-03-27 07:41:40 -04:00
func TestGetPublicChannelsByIdsForTeam ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-27 07:41:40 -04:00
teamId := th . BasicTeam . Id
2025-08-20 10:11:15 -04:00
t . Run ( "should return 1 channel" , func ( t * testing . T ) {
input := [ ] string { th . BasicChannel . Id }
output := [ ] string { th . BasicChannel . DisplayName }
2017-03-27 07:41:40 -04:00
2025-08-20 10:11:15 -04:00
channels , _ , err := client . GetPublicChannelsByIdsForTeam ( context . Background ( ) , teamId , input )
require . NoError ( t , err )
require . Len ( t , channels , 1 , "should return 1 channel" )
require . Equal ( t , output [ 0 ] , channels [ 0 ] . DisplayName , "missing channel" )
} )
2017-03-27 07:41:40 -04:00
2025-08-20 10:11:15 -04:00
t . Run ( "should return 2 channels" , func ( t * testing . T ) {
input := [ ] string { th . BasicChannel . Id }
2026-01-13 10:40:03 -05:00
expectedDisplayNames := [ ] string { th . BasicChannel . DisplayName }
2017-03-27 07:41:40 -04:00
2025-08-20 10:11:15 -04:00
input = append ( input , GenerateTestID ( ) )
input = append ( input , th . BasicChannel2 . Id )
input = append ( input , th . BasicPrivateChannel . Id )
2026-01-13 10:40:03 -05:00
expectedDisplayNames = append ( expectedDisplayNames , th . BasicChannel2 . DisplayName )
2017-03-27 07:41:40 -04:00
2025-08-20 10:11:15 -04:00
channels , _ , err := client . GetPublicChannelsByIdsForTeam ( context . Background ( ) , teamId , input )
require . NoError ( t , err )
require . Len ( t , channels , 2 , "should return 2 channels" )
2017-03-27 07:41:40 -04:00
2026-01-13 10:40:03 -05:00
actualDisplayNames := make ( [ ] string , len ( channels ) )
2025-08-20 10:11:15 -04:00
for i , c := range channels {
2026-01-13 10:40:03 -05:00
actualDisplayNames [ i ] = c . DisplayName
2025-08-20 10:11:15 -04:00
}
2026-01-13 10:40:03 -05:00
require . ElementsMatch ( t , expectedDisplayNames , actualDisplayNames , "missing channel" )
2025-08-20 10:11:15 -04:00
} )
2017-03-27 07:41:40 -04:00
2025-08-20 10:11:15 -04:00
t . Run ( "forbidden for invalid team" , func ( t * testing . T ) {
input := [ ] string { th . BasicChannel . Id , th . BasicChannel2 . Id }
_ , resp , err := client . GetPublicChannelsByIdsForTeam ( context . Background ( ) , GenerateTestID ( ) , input )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
} )
2017-03-27 07:41:40 -04:00
2025-08-20 10:11:15 -04:00
t . Run ( "bad request for empty input" , func ( t * testing . T ) {
_ , resp , err := client . GetPublicChannelsByIdsForTeam ( context . Background ( ) , teamId , [ ] string { } )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
2017-03-27 07:41:40 -04:00
2025-08-20 10:11:15 -04:00
t . Run ( "bad request for junk id" , func ( t * testing . T ) {
_ , resp , err := client . GetPublicChannelsByIdsForTeam ( context . Background ( ) , teamId , [ ] string { "junk" } )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
2017-03-27 07:41:40 -04:00
2025-08-20 10:11:15 -04:00
t . Run ( "not found for non-existent id" , func ( t * testing . T ) {
_ , resp , err := client . GetPublicChannelsByIdsForTeam ( context . Background ( ) , teamId , [ ] string { GenerateTestID ( ) } )
require . Error ( t , err )
CheckNotFoundStatus ( t , resp )
} )
2017-03-27 07:41:40 -04:00
2025-08-20 10:11:15 -04:00
t . Run ( "not found for private channel id" , func ( t * testing . T ) {
_ , resp , err := client . GetPublicChannelsByIdsForTeam ( context . Background ( ) , teamId , [ ] string { th . BasicPrivateChannel . Id } )
require . Error ( t , err )
CheckNotFoundStatus ( t , resp )
} )
2017-03-27 07:41:40 -04:00
2025-08-20 10:11:15 -04:00
t . Run ( "unauthorized when logged out" , func ( t * testing . T ) {
input := [ ] string { th . BasicChannel . Id , th . BasicChannel2 . Id }
_ , lErr := client . Logout ( context . Background ( ) )
require . NoError ( t , lErr )
_ , resp , err := client . GetPublicChannelsByIdsForTeam ( context . Background ( ) , teamId , input )
require . Error ( t , err )
CheckUnauthorizedStatus ( t , resp )
} )
t . Run ( "system admin can get channels" , func ( t * testing . T ) {
input := [ ] string { th . BasicChannel . Id , th . BasicChannel2 . Id }
_ , _ , err := th . SystemAdminClient . GetPublicChannelsByIdsForTeam ( context . Background ( ) , teamId , input )
require . NoError ( t , err )
} )
t . Run ( "guest users should not be able to get channels" , func ( t * testing . T ) {
th . App . Srv ( ) . SetLicense ( model . NewTestLicenseSKU ( model . LicenseShortSkuEnterprise ) )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = true } )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . AllowEmailAccounts = true } )
id := model . NewId ( )
2026-04-08 15:49:43 -04:00
guestPassword := model . NewTestPassword ( )
2025-08-20 10:11:15 -04:00
guest := & model . User {
Email : "success+" + id + "@simulator.amazonses.com" ,
Username : "un_" + id ,
Nickname : "nn_" + id ,
2026-04-08 15:49:43 -04:00
Password : guestPassword ,
2025-08-20 10:11:15 -04:00
EmailVerified : true ,
}
guest , appErr := th . App . CreateGuest ( th . Context , guest )
require . Nil ( t , appErr )
guestClient := th . CreateClient ( )
2026-04-08 15:49:43 -04:00
_ , _ , err := guestClient . Login ( context . Background ( ) , guest . Username , guestPassword )
2025-08-20 10:11:15 -04:00
require . NoError ( t , err )
t . Cleanup ( func ( ) {
_ , lErr := guestClient . Logout ( context . Background ( ) )
require . NoError ( t , lErr )
} )
input := [ ] string { th . BasicChannel . Id , th . BasicChannel2 . Id }
_ , resp , err := guestClient . GetPublicChannelsByIdsForTeam ( context . Background ( ) , teamId , input )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
} )
2017-03-27 07:41:40 -04:00
}
2017-03-24 16:45:34 -04:00
func TestGetChannelsForTeamForUser ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-24 16:45:34 -04:00
2020-03-02 16:15:15 -05:00
t . Run ( "get channels for the team for user" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
channels , resp , err := client . GetChannelsForTeamForUser ( context . Background ( ) , th . BasicTeam . Id , th . BasicUser . Id , false , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-24 16:45:34 -04:00
2020-03-02 16:15:15 -05:00
found := make ( [ ] bool , 3 )
for _ , c := range channels {
if c . Id == th . BasicChannel . Id {
found [ 0 ] = true
} else if c . Id == th . BasicChannel2 . Id {
found [ 1 ] = true
} else if c . Id == th . BasicPrivateChannel . Id {
found [ 2 ] = true
}
require . True ( t , c . TeamId == "" || c . TeamId == th . BasicTeam . Id )
2017-03-24 16:45:34 -04:00
}
2020-03-02 16:15:15 -05:00
for _ , f := range found {
require . True ( t , f , "missing a channel" )
}
2017-03-24 16:45:34 -04:00
2025-06-19 08:13:45 -04:00
channels , resp , err = client . GetChannelsForTeamForUser ( context . Background ( ) , th . BasicTeam . Id , th . BasicUser . Id , false , resp . Etag )
2025-10-07 06:19:21 -04:00
require . NoError ( t , err )
2020-03-02 16:15:15 -05:00
CheckEtag ( t , channels , resp )
2017-03-24 16:45:34 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelsForTeamForUser ( context . Background ( ) , th . BasicTeam . Id , "junk" , false , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-03-02 16:15:15 -05:00
CheckBadRequestStatus ( t , resp )
2017-03-24 16:45:34 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelsForTeamForUser ( context . Background ( ) , "junk" , th . BasicUser . Id , false , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-03-02 16:15:15 -05:00
CheckBadRequestStatus ( t , resp )
2017-03-24 16:45:34 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelsForTeamForUser ( context . Background ( ) , th . BasicTeam . Id , th . BasicUser2 . Id , false , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-03-02 16:15:15 -05:00
CheckForbiddenStatus ( t , resp )
2017-03-24 16:45:34 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelsForTeamForUser ( context . Background ( ) , model . NewId ( ) , th . BasicUser . Id , false , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-03-02 16:15:15 -05:00
CheckForbiddenStatus ( t , resp )
2017-03-24 16:45:34 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . GetChannelsForTeamForUser ( context . Background ( ) , th . BasicTeam . Id , th . BasicUser . Id , false , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-03-02 16:15:15 -05:00
} )
2017-03-24 16:45:34 -04:00
2020-03-02 16:15:15 -05:00
t . Run ( "deleted channel could be retrieved using the proper flag" , func ( t * testing . T ) {
testChannel := & model . Channel {
DisplayName : "dn_" + model . NewId ( ) ,
Name : GenerateTestChannelName ( ) ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypeOpen ,
2020-03-02 16:15:15 -05:00
TeamId : th . BasicTeam . Id ,
CreatorId : th . BasicUser . Id ,
}
2025-06-19 08:13:45 -04:00
testChannel , appErr := th . App . CreateChannel ( th . Context , testChannel , true )
require . Nil ( t , appErr )
defer func ( ) {
appErr = th . App . PermanentDeleteChannel ( th . Context , testChannel )
require . Nil ( t , appErr )
} ( )
2023-06-06 17:29:29 -04:00
channels , _ , err := client . GetChannelsForTeamForUser ( context . Background ( ) , th . BasicTeam . Id , th . BasicUser . Id , false , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-03-02 16:15:15 -05:00
assert . Equal ( t , 6 , len ( channels ) )
2025-06-19 08:13:45 -04:00
appErr = th . App . DeleteChannel ( th . Context , testChannel , th . BasicUser . Id )
require . Nil ( t , appErr )
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetChannelsForTeamForUser ( context . Background ( ) , th . BasicTeam . Id , th . BasicUser . Id , false , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-03-02 16:15:15 -05:00
assert . Equal ( t , 5 , len ( channels ) )
2020-07-27 07:31:39 -04:00
// Should return all channels including basicDeleted.
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetChannelsForTeamForUser ( context . Background ( ) , th . BasicTeam . Id , th . BasicUser . Id , true , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-07-27 07:31:39 -04:00
assert . Equal ( t , 7 , len ( channels ) )
// Should stil return all channels including basicDeleted.
now := time . Now ( ) . Add ( - time . Minute ) . Unix ( ) * 1000
2025-06-19 08:13:45 -04:00
channels , _ , err = client . GetChannelsForTeamAndUserWithLastDeleteAt ( context . Background ( ) , th . BasicTeam . Id , th . BasicUser . Id ,
2020-07-27 07:31:39 -04:00
true , int ( now ) , "" )
2025-06-19 08:13:45 -04:00
require . NoError ( t , err )
2020-07-27 07:31:39 -04:00
assert . Equal ( t , 7 , len ( channels ) )
2020-03-02 16:15:15 -05:00
} )
2017-03-24 16:45:34 -04:00
}
2021-10-26 02:00:59 -04:00
func TestGetChannelsForUser ( 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-10-26 02:00:59 -04:00
client := th . Client
// Adding another team with more channels (public and private)
2025-11-12 07:00:51 -05:00
myTeam := th . CreateTeam ( t )
ch1 := th . CreateChannelWithClientAndTeam ( t , client , model . ChannelTypeOpen , myTeam . Id )
ch2 := th . CreateChannelWithClientAndTeam ( t , client , model . ChannelTypePrivate , myTeam . Id )
th . LinkUserToTeam ( t , th . BasicUser , myTeam )
2025-06-19 08:13:45 -04:00
_ , appErr := th . App . AddUserToChannel ( th . Context , th . BasicUser , ch1 , false )
require . Nil ( t , appErr )
_ , appErr = th . App . AddUserToChannel ( th . Context , th . BasicUser , ch2 , false )
require . Nil ( t , appErr )
2021-10-26 02:00:59 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , err := client . GetChannelsForUserWithLastDeleteAt ( context . Background ( ) , th . BasicUser . Id , 0 )
2021-10-26 02:00:59 -04:00
require . NoError ( t , err )
numPrivate := 0
numPublic := 0
numOffTopic := 0
numTownSquare := 0
for _ , ch := range channels {
if ch . Type == model . ChannelTypeOpen {
numPublic ++
} else if ch . Type == model . ChannelTypePrivate {
numPrivate ++
}
if ch . DisplayName == "Off-Topic" {
numOffTopic ++
} else if ch . DisplayName == "Town Square" {
numTownSquare ++
}
}
assert . Len ( t , channels , 9 )
assert . Equal ( t , 2 , numPrivate )
assert . Equal ( t , 7 , numPublic )
assert . Equal ( t , 2 , numOffTopic )
assert . Equal ( t , 2 , numTownSquare )
// Creating some more channels to be exactly 100 to test page size boundaries.
2025-07-18 06:54:51 -04:00
for range 91 {
2025-11-12 07:00:51 -05:00
ch1 = th . CreateChannelWithClientAndTeam ( t , client , model . ChannelTypeOpen , myTeam . Id )
2025-06-19 08:13:45 -04:00
_ , appErr := th . App . AddUserToChannel ( th . Context , th . BasicUser , ch1 , false )
require . Nil ( t , appErr )
2021-10-26 02:00:59 -04:00
}
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetChannelsForUserWithLastDeleteAt ( context . Background ( ) , th . BasicUser . Id , 0 )
2021-10-26 02:00:59 -04:00
require . NoError ( t , err )
assert . Len ( t , channels , 100 )
}
2019-01-10 15:17:31 -05:00
func TestGetAllChannels ( 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 . LoginSystemManager ( t )
2021-08-13 07:12:16 -04:00
client := th . Client
2019-01-10 15:17:31 -05:00
2020-05-19 12:20:41 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
channels , _ , err := client . GetAllChannels ( context . Background ( ) , 0 , 20 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-01-10 15:17:31 -05:00
2020-05-19 12:20:41 -04:00
// At least, all the not-deleted channels created during the InitBasic
2021-08-17 05:18:33 -04:00
require . True ( t , len ( channels ) >= 3 )
for _ , c := range channels {
2020-05-19 12:20:41 -04:00
require . NotEqual ( t , c . TeamId , "" )
}
2019-01-10 15:17:31 -05:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetAllChannels ( context . Background ( ) , 0 , 10 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . True ( t , len ( channels ) >= 3 )
2019-01-10 15:17:31 -05:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetAllChannels ( context . Background ( ) , 1 , 1 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Len ( t , channels , 1 )
2019-01-10 15:17:31 -05:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetAllChannels ( context . Background ( ) , 10000 , 10000 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Empty ( t , channels )
2020-06-19 16:53:37 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetAllChannels ( context . Background ( ) , 0 , 10000 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
beforeCount := len ( channels )
2020-06-19 16:53:37 -04:00
2023-04-20 08:52:59 -04:00
deletedChannel := channels [ 0 ] . Channel
// Never try to delete the default channel
if deletedChannel . Name == "town-square" {
deletedChannel = channels [ 1 ] . Channel
}
2020-06-19 16:53:37 -04:00
2023-06-06 17:29:29 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , deletedChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-19 16:53:37 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetAllChannels ( context . Background ( ) , 0 , 10000 , "" )
2020-06-19 16:53:37 -04:00
var ids [ ] string
2021-08-17 05:18:33 -04:00
for _ , item := range channels {
2020-06-19 16:53:37 -04:00
ids = append ( ids , item . Channel . Id )
}
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Len ( t , channels , beforeCount - 1 )
2023-04-20 08:52:59 -04:00
require . NotContains ( t , ids , deletedChannel . Id )
2020-06-19 16:53:37 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , err = client . GetAllChannelsIncludeDeleted ( context . Background ( ) , 0 , 10000 , "" )
2020-06-19 16:53:37 -04:00
ids = [ ] string { }
2021-08-17 05:18:33 -04:00
for _ , item := range channels {
2020-06-19 16:53:37 -04:00
ids = append ( ids , item . Channel . Id )
}
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . True ( t , len ( channels ) > beforeCount )
2023-04-20 08:52:59 -04:00
require . Contains ( t , ids , deletedChannel . Id )
2020-05-19 12:20:41 -04:00
} )
2019-01-10 15:17:31 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . GetAllChannels ( context . Background ( ) , 0 , 20 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-01-10 15:17:31 -05:00
CheckForbiddenStatus ( t , resp )
2021-04-20 13:16:40 -04:00
2023-06-06 17:29:29 -04:00
sysManagerChannels , resp , err := th . SystemManagerClient . GetAllChannels ( context . Background ( ) , 0 , 10000 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-20 13:16:40 -04:00
CheckOKStatus ( t , resp )
2021-08-17 05:18:33 -04:00
policyChannel := ( sysManagerChannels ) [ 0 ]
2022-10-06 04:04:21 -04:00
policy , err := th . App . Srv ( ) . Store ( ) . RetentionPolicy ( ) . Save ( & model . RetentionPolicyWithTeamAndChannelIDs {
2021-04-20 13:16:40 -04:00
RetentionPolicy : model . RetentionPolicy {
2022-04-07 13:58:40 -04:00
DisplayName : "Policy 1" ,
2024-08-05 23:45:00 -04:00
PostDurationDays : model . NewPointer ( int64 ( 30 ) ) ,
2021-04-20 13:16:40 -04:00
} ,
ChannelIDs : [ ] string { policyChannel . Id } ,
} )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-20 13:16:40 -04:00
t . Run ( "exclude policy constrained" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , resp , err := th . SystemManagerClient . GetAllChannelsExcludePolicyConstrained ( context . Background ( ) , 0 , 10000 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2021-04-20 13:16:40 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
channels , resp , err := th . SystemAdminClient . GetAllChannelsExcludePolicyConstrained ( context . Background ( ) , 0 , 10000 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-20 13:16:40 -04:00
CheckOKStatus ( t , resp )
found := false
2021-08-17 05:18:33 -04:00
for _ , channel := range channels {
2021-04-20 13:16:40 -04:00
if channel . Id == policyChannel . Id {
found = true
break
}
}
require . False ( t , found )
} )
t . Run ( "does not return policy ID" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
channels , resp , err := th . SystemManagerClient . GetAllChannels ( context . Background ( ) , 0 , 10000 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-20 13:16:40 -04:00
CheckOKStatus ( t , resp )
found := false
2021-08-17 05:18:33 -04:00
for _ , channel := range channels {
2021-04-20 13:16:40 -04:00
if channel . Id == policyChannel . Id {
found = true
require . Nil ( t , channel . PolicyID )
break
}
}
require . True ( t , found )
} )
t . Run ( "returns policy ID" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
channels , resp , err := th . SystemAdminClient . GetAllChannels ( context . Background ( ) , 0 , 10000 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-20 13:16:40 -04:00
CheckOKStatus ( t , resp )
found := false
2021-08-17 05:18:33 -04:00
for _ , channel := range channels {
2021-04-20 13:16:40 -04:00
if channel . Id == policyChannel . Id {
found = true
require . Equal ( t , * channel . PolicyID , policy . ID )
break
}
}
require . True ( t , found )
} )
2024-09-17 17:19:03 -04:00
t . Run ( "verify correct sanitization" , func ( t * testing . T ) {
channels , resp , err := th . SystemAdminClient . GetAllChannels ( context . Background ( ) , 0 , 10000 , "" )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
require . True ( t , len ( channels ) > 0 )
for _ , channel := range channels {
if channel . DisplayName != "Off-Topic" && channel . DisplayName != "Town Square" {
require . NotEqual ( t , "" , channel . CreatorId )
require . NotEqual ( t , "" , channel . Name )
}
}
channels , resp , err = th . SystemManagerClient . GetAllChannels ( context . Background ( ) , 0 , 10000 , "" )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
require . True ( t , len ( channels ) > 0 )
for _ , channel := range channels {
if channel . DisplayName != "Off-Topic" && channel . DisplayName != "Town Square" {
require . NotEqual ( t , "" , channel . CreatorId )
require . NotEqual ( t , "" , channel . Name )
}
}
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionSysconsoleReadUserManagementChannels . Id , model . SystemManagerRoleId )
2024-09-17 17:19:03 -04:00
channels , resp , err = th . SystemManagerClient . GetAllChannels ( context . Background ( ) , 0 , 10000 , "" )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
require . True ( t , len ( channels ) > 0 )
for _ , channel := range channels {
require . Equal ( t , "" , channel . CreatorId )
require . Equal ( t , "" , channel . Name )
}
} )
2019-01-10 15:17:31 -05:00
}
2019-06-25 08:18:48 -04:00
func TestGetAllChannelsWithCount ( 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-08-13 07:12:16 -04:00
client := th . Client
2019-06-25 08:18:48 -04:00
2023-06-06 17:29:29 -04:00
channels , total , _ , err := th . SystemAdminClient . GetAllChannelsWithCount ( context . Background ( ) , 0 , 20 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-06-25 08:18:48 -04:00
// At least, all the not-deleted channels created during the InitBasic
2021-08-17 05:18:33 -04:00
require . True ( t , len ( channels ) >= 3 )
for _ , c := range channels {
2019-06-25 08:18:48 -04:00
require . NotEqual ( t , c . TeamId , "" )
}
2019-12-11 14:18:36 -05:00
require . Equal ( t , int64 ( 6 ) , total )
2019-06-25 08:18:48 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , _ , err = th . SystemAdminClient . GetAllChannelsWithCount ( context . Background ( ) , 0 , 10 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . True ( t , len ( channels ) >= 3 )
2019-06-25 08:18:48 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , _ , err = th . SystemAdminClient . GetAllChannelsWithCount ( context . Background ( ) , 1 , 1 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Len ( t , channels , 1 )
2019-06-25 08:18:48 -04:00
2023-06-06 17:29:29 -04:00
channels , _ , _ , err = th . SystemAdminClient . GetAllChannelsWithCount ( context . Background ( ) , 10000 , 10000 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Empty ( t , channels )
2019-06-25 08:18:48 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , resp , err := client . GetAllChannelsWithCount ( context . Background ( ) , 0 , 20 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-06-25 08:18:48 -04:00
CheckForbiddenStatus ( t , resp )
}
2017-03-24 16:45:34 -04:00
func TestSearchChannels ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-24 16:45:34 -04:00
2023-10-05 09:55:59 -04:00
t . Run ( "Search using null value" , func ( t * testing . T ) {
var nullSearch * model . ChannelSearch
_ , resp , err := client . SearchChannels ( context . Background ( ) , th . BasicTeam . Id , nullSearch )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
2017-03-24 16:45:34 -04:00
search := & model . ChannelSearch { Term : th . BasicChannel . Name }
2023-06-06 17:29:29 -04:00
channels , _ , err := client . SearchChannels ( context . Background ( ) , th . BasicTeam . Id , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-24 16:45:34 -04:00
found := false
2017-05-10 11:41:56 -04:00
for _ , c := range channels {
2021-07-12 14:05:36 -04:00
require . Equal ( t , model . ChannelTypeOpen , c . Type , "should only return public channels" )
2017-03-24 16:45:34 -04:00
if c . Id == th . BasicChannel . Id {
found = true
}
}
2019-11-13 13:53:57 -05:00
require . True ( t , found , "didn't find channel" )
2017-03-24 16:45:34 -04:00
search . Term = th . BasicPrivateChannel . Name
2023-06-06 17:29:29 -04:00
channels , _ , err = client . SearchChannels ( context . Background ( ) , th . BasicTeam . Id , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-24 16:45:34 -04:00
found = false
2017-05-10 11:41:56 -04:00
for _ , c := range channels {
2017-03-24 16:45:34 -04:00
if c . Id == th . BasicPrivateChannel . Id {
found = true
}
}
2019-11-13 13:53:57 -05:00
require . False ( t , found , "shouldn't find private channel" )
2017-03-24 16:45:34 -04:00
search . Term = ""
2023-06-06 17:29:29 -04:00
_ , _ , err = client . SearchChannels ( context . Background ( ) , th . BasicTeam . Id , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-24 16:45:34 -04:00
search . Term = th . BasicChannel . Name
2023-06-06 17:29:29 -04:00
_ , resp , err := client . SearchChannels ( context . Background ( ) , model . NewId ( ) , search )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-07-29 03:35:32 -04:00
CheckNotFoundStatus ( t , resp )
2017-03-24 16:45:34 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . SearchChannels ( context . Background ( ) , "junk" , search )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-24 16:45:34 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . SearchChannels ( context . Background ( ) , th . BasicTeam . Id , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-07-29 03:35:32 -04:00
// Check the appropriate permissions are enforced.
2025-11-12 07:00:51 -05:00
defaultRolePermissions := th . SaveDefaultRolePermissions ( t )
2019-07-29 03:35:32 -04:00
defer func ( ) {
2025-11-12 07:00:51 -05:00
th . RestoreDefaultRolePermissions ( t , defaultRolePermissions )
2019-07-29 03:35:32 -04:00
} ( )
// Remove list channels permission from the user
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionListTeamChannels . Id , model . TeamUserRoleId )
2019-07-29 03:35:32 -04:00
t . Run ( "Search for a BasicChannel, which the user is a member of" , func ( t * testing . T ) {
search . Term = th . BasicChannel . Name
2023-06-06 17:29:29 -04:00
channelList , _ , err := client . SearchChannels ( context . Background ( ) , th . BasicTeam . Id , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-07-29 03:35:32 -04:00
channelNames := [ ] string { }
for _ , c := range channelList {
channelNames = append ( channelNames , c . Name )
}
require . Contains ( t , channelNames , th . BasicChannel . Name )
} )
t . Run ( "Remove the user from BasicChannel and search again, should not be returned" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
appErr := th . App . RemoveUserFromChannel ( th . Context , th . BasicUser . Id , th . BasicUser . Id , th . BasicChannel )
require . Nil ( t , appErr )
2019-07-29 03:35:32 -04:00
search . Term = th . BasicChannel . Name
2023-06-06 17:29:29 -04:00
channelList , _ , err := client . SearchChannels ( context . Background ( ) , th . BasicTeam . Id , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-07-29 03:35:32 -04:00
channelNames := [ ] string { }
for _ , c := range channelList {
channelNames = append ( channelNames , c . Name )
}
require . NotContains ( t , channelNames , th . BasicChannel . Name )
} )
2022-06-01 02:18:59 -04:00
t . Run ( "Guests only receive autocompletion for which accounts they are a member of" , func ( t * testing . T ) {
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( "" ) )
defer th . App . Srv ( ) . SetLicense ( nil )
enableGuestAccounts := * th . App . Config ( ) . GuestAccountsSettings . Enable
defer func ( ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) { cfg . GuestAccountsSettings . Enable = & enableGuestAccounts } )
} ( )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = true } )
2025-11-12 07:00:51 -05:00
guest := th . CreateUser ( t )
2023-06-06 17:29:29 -04:00
_ , appErr := th . SystemAdminClient . DemoteUserToGuest ( context . Background ( ) , guest . Id )
2022-06-01 02:18:59 -04:00
require . NoError ( t , appErr )
2023-06-06 17:29:29 -04:00
_ , resp , err := th . SystemAdminClient . AddTeamMember ( context . Background ( ) , th . BasicTeam . Id , guest . Id )
2022-06-01 02:18:59 -04:00
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . Login ( context . Background ( ) , guest . Username , guest . Password )
2022-06-01 02:18:59 -04:00
require . NoError ( t , err )
CheckOKStatus ( t , resp )
search . Term = th . BasicChannel2 . Name
2023-06-06 17:29:29 -04:00
channelList , _ , err := client . SearchChannels ( context . Background ( ) , th . BasicTeam . Id , search )
2022-06-01 02:18:59 -04:00
require . NoError ( t , err )
require . Empty ( t , channelList )
2023-06-06 17:29:29 -04:00
_ , resp , err = th . SystemAdminClient . AddChannelMember ( context . Background ( ) , th . BasicChannel2 . Id , guest . Id )
2022-06-01 02:18:59 -04:00
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
search . Term = th . BasicChannel2 . Name
2023-06-06 17:29:29 -04:00
channelList , _ , err = client . SearchChannels ( context . Background ( ) , th . BasicTeam . Id , search )
2022-06-01 02:18:59 -04:00
require . NoError ( t , err )
require . NotEmpty ( t , channelList )
require . Equal ( t , th . BasicChannel2 . Id , channelList [ 0 ] . Id )
} )
2017-03-24 16:45:34 -04:00
}
2019-01-10 15:17:31 -05:00
func TestSearchAllChannels ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := setupForSharedChannels ( t ) . InitBasic ( t )
th . LoginSystemManager ( t )
2021-08-13 07:12:16 -04:00
client := th . Client
2019-01-10 15:17:31 -05:00
2023-06-06 17:29:29 -04:00
openChannel , _ , err := th . SystemAdminClient . CreateChannel ( context . Background ( ) , & model . Channel {
2020-07-23 10:46:33 -04:00
DisplayName : "SearchAllChannels-FOOBARDISPLAYNAME" ,
2020-06-15 11:21:42 -04:00
Name : "whatever" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypeOpen ,
2020-06-15 11:21:42 -04:00
TeamId : th . BasicTeam . Id ,
2020-07-23 10:46:33 -04:00
} )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-01-10 15:17:31 -05:00
2023-06-06 17:29:29 -04:00
privateChannel , _ , err := th . SystemAdminClient . CreateChannel ( context . Background ( ) , & model . Channel {
2020-07-23 10:46:33 -04:00
DisplayName : "SearchAllChannels-private1" ,
Name : "private1" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypePrivate ,
2020-07-23 10:46:33 -04:00
TeamId : th . BasicTeam . Id ,
} )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-07-23 10:46:33 -04:00
2025-11-12 07:00:51 -05:00
team := th . CreateTeam ( t )
2023-06-06 17:29:29 -04:00
privateChannel2 , _ , err := th . SystemAdminClient . CreateChannel ( context . Background ( ) , & model . Channel {
2021-10-26 02:00:59 -04:00
DisplayName : "dn_private2" ,
Name : "private2" ,
Type : model . ChannelTypePrivate ,
TeamId : team . Id ,
} )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , th . SystemAdminUser , team )
th . LinkUserToTeam ( t , th . SystemAdminUser , th . BasicTeam )
2021-10-26 02:00:59 -04:00
2023-06-06 17:29:29 -04:00
groupConstrainedChannel , _ , err := th . SystemAdminClient . CreateChannel ( context . Background ( ) , & model . Channel {
2020-07-23 10:46:33 -04:00
DisplayName : "SearchAllChannels-groupConstrained-1" ,
Name : "groupconstrained1" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypePrivate ,
2024-08-05 23:45:00 -04:00
GroupConstrained : model . NewPointer ( true ) ,
2020-07-23 10:46:33 -04:00
TeamId : team . Id ,
} )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-01-10 15:17:31 -05:00
2024-10-18 09:41:24 -04:00
// share the open and private channels, one homed locally and the
// other remotely
sco := & model . SharedChannel {
ChannelId : openChannel . Id ,
TeamId : openChannel . TeamId ,
Home : true ,
ShareName : "testsharelocal" ,
CreatorId : th . BasicChannel . CreatorId ,
}
_ , scoErr := th . App . ShareChannel ( th . Context , sco )
require . NoError ( t , scoErr )
scp := & model . SharedChannel {
ChannelId : privateChannel . Id ,
TeamId : privateChannel . TeamId ,
Home : false ,
RemoteId : model . NewId ( ) ,
ShareName : "testshareremote" ,
CreatorId : th . BasicChannel . CreatorId ,
}
_ , scpErr := th . App . ShareChannel ( th . Context , scp )
require . NoError ( t , scpErr )
2020-07-23 10:46:33 -04:00
testCases := [ ] struct {
Description string
Search * model . ChannelSearch
ExpectedChannelIds [ ] string
} {
{
"Middle of word search" ,
& model . ChannelSearch { Term : "bardisplay" } ,
[ ] string { openChannel . Id } ,
} ,
{
"Prefix search" ,
& model . ChannelSearch { Term : "SearchAllChannels-foobar" } ,
[ ] string { openChannel . Id } ,
} ,
{
"Suffix search" ,
& model . ChannelSearch { Term : "displayname" } ,
[ ] string { openChannel . Id } ,
} ,
{
"Name search" ,
& model . ChannelSearch { Term : "what" } ,
[ ] string { openChannel . Id } ,
} ,
{
"Name suffix search" ,
& model . ChannelSearch { Term : "ever" } ,
[ ] string { openChannel . Id } ,
} ,
{
"Basic channel name middle of word search" ,
& model . ChannelSearch { Term : th . BasicChannel . Name [ 2 : 14 ] } ,
[ ] string { th . BasicChannel . Id } ,
} ,
{
"Upper case search" ,
& model . ChannelSearch { Term : strings . ToUpper ( th . BasicChannel . Name ) } ,
[ ] string { th . BasicChannel . Id } ,
} ,
{
"Mixed case search" ,
& model . ChannelSearch { Term : th . BasicChannel . Name [ 0 : 2 ] + strings . ToUpper ( th . BasicChannel . Name [ 2 : 5 ] ) + th . BasicChannel . Name [ 5 : ] } ,
[ ] string { th . BasicChannel . Id } ,
} ,
{
"Non mixed case search" ,
& model . ChannelSearch { Term : th . BasicChannel . Name } ,
[ ] string { th . BasicChannel . Id } ,
} ,
{
"Search private channel name" ,
& model . ChannelSearch { Term : th . BasicPrivateChannel . Name } ,
[ ] string { th . BasicPrivateChannel . Id } ,
} ,
{
"Search with private channel filter" ,
& model . ChannelSearch { Private : true } ,
2021-10-26 02:00:59 -04:00
[ ] string { th . BasicPrivateChannel . Id , privateChannel2 . Id , th . BasicPrivateChannel2 . Id , privateChannel . Id , groupConstrainedChannel . Id } ,
2020-07-23 10:46:33 -04:00
} ,
{
"Search with public channel filter" ,
& model . ChannelSearch { Term : "SearchAllChannels" , Public : true } ,
[ ] string { openChannel . Id } ,
} ,
{
"Search with private channel filter" ,
& model . ChannelSearch { Term : "SearchAllChannels" , Private : true } ,
[ ] string { privateChannel . Id , groupConstrainedChannel . Id } ,
} ,
{
"Search with teamIds channel filter" ,
& model . ChannelSearch { Term : "SearchAllChannels" , TeamIds : [ ] string { th . BasicTeam . Id } } ,
[ ] string { openChannel . Id , privateChannel . Id } ,
} ,
{
"Search with deleted without IncludeDeleted filter" ,
& model . ChannelSearch { Term : th . BasicDeletedChannel . Name } ,
[ ] string { } ,
} ,
{
"Search with deleted IncludeDeleted filter" ,
& model . ChannelSearch { Term : th . BasicDeletedChannel . Name , IncludeDeleted : true } ,
[ ] string { th . BasicDeletedChannel . Id } ,
} ,
{
"Search with deleted IncludeDeleted filter" ,
& model . ChannelSearch { Term : th . BasicDeletedChannel . Name , IncludeDeleted : true } ,
[ ] string { th . BasicDeletedChannel . Id } ,
} ,
{
"Search with deleted Deleted filter and empty term" ,
& model . ChannelSearch { Term : "" , Deleted : true } ,
[ ] string { th . BasicDeletedChannel . Id } ,
} ,
{
"Search for group constrained" ,
& model . ChannelSearch { Term : "SearchAllChannels" , GroupConstrained : true } ,
[ ] string { groupConstrainedChannel . Id } ,
} ,
{
"Search for group constrained and public" ,
& model . ChannelSearch { Term : "SearchAllChannels" , GroupConstrained : true , Public : true } ,
[ ] string { } ,
} ,
{
"Search for exclude group constrained" ,
& model . ChannelSearch { Term : "SearchAllChannels" , ExcludeGroupConstrained : true } ,
[ ] string { openChannel . Id , privateChannel . Id } ,
} ,
2024-10-18 09:41:24 -04:00
{
"Search for local only channels" ,
& model . ChannelSearch { Term : "SearchAllChannels" , ExcludeRemote : true } ,
[ ] string { openChannel . Id , groupConstrainedChannel . Id } ,
} ,
2020-07-23 10:46:33 -04:00
}
for _ , testCase := range testCases {
t . Run ( testCase . Description , func ( t * testing . T ) {
2021-08-17 05:18:33 -04:00
var channels model . ChannelListWithTeamData
2023-06-06 17:29:29 -04:00
channels , _ , err = th . SystemAdminClient . SearchAllChannels ( context . Background ( ) , testCase . Search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
assert . Equal ( t , len ( testCase . ExpectedChannelIds ) , len ( channels ) )
2020-07-23 10:46:33 -04:00
actualChannelIds := [ ] string { }
2021-08-17 05:18:33 -04:00
for _ , channelWithTeamData := range channels {
2020-07-23 10:46:33 -04:00
actualChannelIds = append ( actualChannelIds , channelWithTeamData . Channel . Id )
}
assert . ElementsMatch ( t , testCase . ExpectedChannelIds , actualChannelIds )
} )
}
2019-01-10 15:17:31 -05:00
2023-06-06 17:29:29 -04:00
userChannels , _ , err := th . SystemAdminClient . SearchAllChannelsForUser ( context . Background ( ) , "private" )
2021-10-26 02:00:59 -04:00
require . NoError ( t , err )
assert . Len ( t , userChannels , 2 )
2023-06-06 17:29:29 -04:00
userChannels , _ , err = th . SystemAdminClient . SearchAllChannelsForUser ( context . Background ( ) , "FOOBARDISPLAYNAME" )
2021-10-26 02:00:59 -04:00
require . NoError ( t , err )
assert . Len ( t , userChannels , 1 )
2020-07-23 10:46:33 -04:00
// Searching with no terms returns all default channels
2023-06-06 17:29:29 -04:00
allChannels , _ , err := th . SystemAdminClient . SearchAllChannels ( context . Background ( ) , & model . ChannelSearch { Term : "" } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
assert . True ( t , len ( allChannels ) >= 3 )
2019-01-10 15:17:31 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . SearchAllChannels ( context . Background ( ) , & model . ChannelSearch { Term : "" } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-01-10 15:17:31 -05:00
CheckForbiddenStatus ( t , resp )
2021-04-20 13:16:40 -04:00
// Choose a policy which the system manager can read
2023-06-06 17:29:29 -04:00
sysManagerChannels , resp , err := th . SystemManagerClient . GetAllChannels ( context . Background ( ) , 0 , 10000 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-20 13:16:40 -04:00
CheckOKStatus ( t , resp )
2021-08-17 05:18:33 -04:00
policyChannel := sysManagerChannels [ 0 ]
2022-10-06 04:04:21 -04:00
policy , savePolicyErr := th . App . Srv ( ) . Store ( ) . RetentionPolicy ( ) . Save ( & model . RetentionPolicyWithTeamAndChannelIDs {
2021-04-20 13:16:40 -04:00
RetentionPolicy : model . RetentionPolicy {
2022-04-07 13:58:40 -04:00
DisplayName : "Policy 1" ,
2024-08-05 23:45:00 -04:00
PostDurationDays : model . NewPointer ( int64 ( 30 ) ) ,
2021-04-20 13:16:40 -04:00
} ,
ChannelIDs : [ ] string { policyChannel . Id } ,
} )
require . NoError ( t , savePolicyErr )
t . Run ( "does not return policy ID" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
channels , resp , err := th . SystemManagerClient . SearchAllChannels ( context . Background ( ) , & model . ChannelSearch { Term : policyChannel . Name } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-20 13:16:40 -04:00
CheckOKStatus ( t , resp )
found := false
2021-08-17 05:18:33 -04:00
for _ , channel := range channels {
2021-04-20 13:16:40 -04:00
if channel . Id == policyChannel . Id {
found = true
require . Nil ( t , channel . PolicyID )
break
}
}
require . True ( t , found )
} )
t . Run ( "returns policy ID" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
channels , resp , err := th . SystemAdminClient . SearchAllChannels ( context . Background ( ) , & model . ChannelSearch { Term : policyChannel . Name } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-20 13:16:40 -04:00
CheckOKStatus ( t , resp )
found := false
2021-08-17 05:18:33 -04:00
for _ , channel := range channels {
2021-04-20 13:16:40 -04:00
if channel . Id == policyChannel . Id {
found = true
require . Equal ( t , * channel . PolicyID , policy . ID )
break
}
}
require . True ( t , found )
} )
2024-09-17 17:19:03 -04:00
t . Run ( "verify correct sanitization" , func ( t * testing . T ) {
channels , resp , err := th . SystemAdminClient . SearchAllChannels ( context . Background ( ) , & model . ChannelSearch { Term : "" } )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
require . True ( t , len ( channels ) > 0 )
for _ , channel := range channels {
if channel . DisplayName != "Off-Topic" && channel . DisplayName != "Town Square" {
require . NotEqual ( t , "" , channel . CreatorId )
require . NotEqual ( t , "" , channel . Name )
}
}
channels , resp , err = th . SystemManagerClient . SearchAllChannels ( context . Background ( ) , & model . ChannelSearch { Term : "" } )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
require . True ( t , len ( channels ) > 0 )
for _ , channel := range channels {
if channel . DisplayName != "Off-Topic" && channel . DisplayName != "Town Square" {
require . NotEqual ( t , "" , channel . CreatorId )
require . NotEqual ( t , "" , channel . Name )
}
}
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionSysconsoleReadUserManagementChannels . Id , model . SystemManagerRoleId )
2024-09-17 17:19:03 -04:00
channels , resp , err = th . SystemManagerClient . SearchAllChannels ( context . Background ( ) , & model . ChannelSearch { Term : "" } )
require . NoError ( t , err )
require . True ( t , len ( channels ) > 0 )
CheckOKStatus ( t , resp )
for _ , channel := range channels {
require . Equal ( t , "" , channel . CreatorId )
require . Equal ( t , "" , channel . Name )
}
} )
2019-01-10 15:17:31 -05:00
}
2019-11-19 11:38:49 -05:00
func TestSearchAllChannelsPaged ( 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-08-13 07:12:16 -04:00
client := th . Client
2019-11-19 11:38:49 -05:00
search := & model . ChannelSearch { Term : th . BasicChannel . Name }
search . Term = ""
2024-08-05 23:45:00 -04:00
search . Page = model . NewPointer ( 0 )
search . PerPage = model . NewPointer ( 2 )
2023-06-06 17:29:29 -04:00
channelsWithCount , _ , err := th . SystemAdminClient . SearchAllChannelsPaged ( context . Background ( ) , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Len ( t , channelsWithCount . Channels , 2 )
2019-11-19 11:38:49 -05:00
search . Term = th . BasicChannel . Name
2023-06-06 17:29:29 -04:00
_ , resp , err := client . SearchAllChannels ( context . Background ( ) , search )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-11-19 11:38:49 -05:00
CheckForbiddenStatus ( t , resp )
}
2019-06-21 19:14:21 -04:00
func TestSearchGroupChannels ( 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-08-13 07:12:16 -04:00
client := th . Client
2019-06-21 19:14:21 -04:00
2025-11-12 07:00:51 -05:00
u1 := th . CreateUserWithClient ( t , th . SystemAdminClient )
2019-06-21 19:14:21 -04:00
// Create a group channel in which base user belongs but not sysadmin
2023-06-06 17:29:29 -04:00
gc1 , _ , err := th . Client . CreateGroupChannel ( context . Background ( ) , [ ] string { th . BasicUser . Id , th . BasicUser2 . Id , u1 . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-06-21 19:14:21 -04:00
2023-06-06 17:29:29 -04:00
gc2 , _ , err := th . Client . CreateGroupChannel ( context . Background ( ) , [ ] string { th . BasicUser . Id , th . BasicUser2 . Id , th . SystemAdminUser . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-06-21 19:14:21 -04:00
search := & model . ChannelSearch { Term : th . BasicUser2 . Username }
// sysadmin should only find gc2 as he doesn't belong to gc1
2023-06-06 17:29:29 -04:00
channels , _ , err := th . SystemAdminClient . SearchGroupChannels ( context . Background ( ) , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-06-21 19:14:21 -04:00
assert . Len ( t , channels , 1 )
assert . Equal ( t , channels [ 0 ] . Id , gc2 . Id )
// basic user should find both
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , th . BasicUser . Username , th . BasicUser . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
channels , _ , err = client . SearchGroupChannels ( context . Background ( ) , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-06-21 19:14:21 -04:00
assert . Len ( t , channels , 2 )
channelIds := [ ] string { }
for _ , c := range channels {
channelIds = append ( channelIds , c . Id )
}
assert . ElementsMatch ( t , channelIds , [ ] string { gc1 . Id , gc2 . Id } )
// searching for sysadmin, it should only find gc1
search = & model . ChannelSearch { Term : th . SystemAdminUser . Username }
2023-06-06 17:29:29 -04:00
channels , _ , err = client . SearchGroupChannels ( context . Background ( ) , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-06-21 19:14:21 -04:00
assert . Len ( t , channels , 1 )
assert . Equal ( t , channels [ 0 ] . Id , gc2 . Id )
// with an empty search, response should be empty
search = & model . ChannelSearch { Term : "" }
2023-06-06 17:29:29 -04:00
channels , _ , err = client . SearchGroupChannels ( context . Background ( ) , search )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-06-21 19:14:21 -04:00
2019-12-22 06:35:31 -05:00
assert . Empty ( t , channels )
2019-06-21 19:14:21 -04:00
// search unprivileged, forbidden
2025-06-19 08:13:45 -04:00
_ , err = th . Client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err := client . SearchAllChannels ( context . Background ( ) , search )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-06-21 19:14:21 -04:00
CheckUnauthorizedStatus ( t , resp )
2023-10-05 09:55:59 -04:00
t . Run ( "search with null value" , func ( t * testing . T ) {
var search * model . ChannelSearch
2025-06-19 08:13:45 -04:00
_ , _ , err := client . Login ( context . Background ( ) , th . BasicUser . Username , th . BasicUser . Password )
require . NoError ( t , err )
2023-10-05 09:55:59 -04:00
_ , resp , err := client . SearchGroupChannels ( context . Background ( ) , search )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
2019-06-21 19:14:21 -04:00
}
2017-03-14 08:08:58 -04:00
func TestDeleteChannel ( 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-27 09:24:44 -04:00
c := th . Client
2017-03-14 08:08:58 -04:00
team := th . BasicTeam
user := th . BasicUser
user2 := th . BasicUser2
// successful delete of public channel
2020-05-27 09:24:44 -04:00
th . TestForAllClients ( t , func ( t * testing . T , client * model . Client4 ) {
2025-11-12 07:00:51 -05:00
publicChannel1 := th . CreatePublicChannel ( t )
2023-06-06 17:29:29 -04:00
_ , err := client . DeleteChannel ( context . Background ( ) , publicChannel1 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-14 08:08:58 -04:00
2022-07-14 05:01:29 -04:00
ch , appErr := th . App . GetChannel ( th . Context , publicChannel1 . Id )
2025-06-19 08:13:45 -04:00
require . Nil ( t , appErr )
2022-03-31 01:13:41 -04:00
require . True ( t , ch . DeleteAt != 0 , "should have returned one with a populated DeleteAt." )
2017-03-14 08:08:58 -04:00
2023-12-20 00:46:54 -05:00
post1 := & model . Post { ChannelId : publicChannel1 . Id , Message : "a" + GenerateTestID ( ) + "a" }
2025-06-19 08:13:45 -04:00
_ , resp , err := client . CreatePost ( context . Background ( ) , post1 )
require . Error ( t , err )
2020-05-27 09:24:44 -04:00
require . NotNil ( t , resp , "expected response to not be nil" )
2017-03-14 08:08:58 -04:00
2020-05-27 09:24:44 -04:00
// successful delete of private channel
2025-11-12 07:00:51 -05:00
privateChannel2 := th . CreatePrivateChannel ( t )
2023-06-06 17:29:29 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , privateChannel2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-14 08:08:58 -04:00
2020-05-27 09:24:44 -04:00
// successful delete of channel with multiple members
2025-11-12 07:00:51 -05:00
publicChannel3 := th . CreatePublicChannel ( t )
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . AddUserToChannel ( th . Context , user , publicChannel3 , false )
require . Nil ( t , appErr )
_ , appErr = th . App . AddUserToChannel ( th . Context , user2 , publicChannel3 , false )
require . Nil ( t , appErr )
2023-06-06 17:29:29 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , publicChannel3 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-14 08:08:58 -04:00
2020-05-27 09:24:44 -04:00
// default channel cannot be deleted.
2025-06-19 08:13:45 -04:00
defaultChannel , appErr := th . App . GetChannelByName ( th . Context , model . DefaultChannelName , team . Id , false )
require . Nil ( t , appErr )
2023-06-06 17:29:29 -04:00
resp , err = client . DeleteChannel ( context . Background ( ) , defaultChannel . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-05-27 09:24:44 -04:00
CheckBadRequestStatus ( t , resp )
2017-04-05 11:41:33 -04:00
2020-05-27 09:24:44 -04:00
// check system admin can delete a channel without any appropriate team or channel membership.
2025-11-12 07:00:51 -05:00
sdTeam := th . CreateTeamWithClient ( t , c )
2020-05-27 09:24:44 -04:00
sdPublicChannel := & model . Channel {
DisplayName : "dn_" + model . NewId ( ) ,
Name : GenerateTestChannelName ( ) ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypeOpen ,
2020-05-27 09:24:44 -04:00
TeamId : sdTeam . Id ,
}
2023-06-06 17:29:29 -04:00
sdPublicChannel , _ , err = c . CreateChannel ( context . Background ( ) , sdPublicChannel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , sdPublicChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-04-05 11:41:33 -04:00
2020-05-27 09:24:44 -04:00
sdPrivateChannel := & model . Channel {
DisplayName : "dn_" + model . NewId ( ) ,
Name : GenerateTestChannelName ( ) ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypePrivate ,
2020-05-27 09:24:44 -04:00
TeamId : sdTeam . Id ,
}
2023-06-06 17:29:29 -04:00
sdPrivateChannel , _ , err = c . CreateChannel ( context . Background ( ) , sdPrivateChannel )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , sdPrivateChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-05-27 09:24:44 -04:00
} )
2017-03-14 08:08:58 -04:00
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
publicChannel5 := th . CreatePublicChannel ( t )
2025-06-19 08:13:45 -04:00
_ , err := c . Logout ( context . Background ( ) )
require . NoError ( t , err )
2017-03-14 08:08:58 -04:00
2025-06-19 08:13:45 -04:00
// Other users can't delete the channel
_ , _ , err = c . Login ( context . Background ( ) , user2 . Email , user2 . Password )
require . NoError ( t , err )
resp , err := c . DeleteChannel ( context . Background ( ) , publicChannel5 . Id )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
2020-05-27 09:24:44 -04:00
2025-06-19 08:13:45 -04:00
resp , err = c . DeleteChannel ( context . Background ( ) , "junk" )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
2020-05-27 09:24:44 -04:00
2025-06-19 08:13:45 -04:00
_ , err = c . Logout ( context . Background ( ) )
require . NoError ( t , err )
resp , err = c . DeleteChannel ( context . Background ( ) , GenerateTestID ( ) )
require . Error ( t , err )
CheckUnauthorizedStatus ( t , resp )
// The creator can delete the channel
_ , err = c . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = c . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
resp , err = c . DeleteChannel ( context . Background ( ) , publicChannel5 . Id )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
2018-11-20 20:16:25 -05:00
}
2017-03-14 08:08:58 -04:00
2018-11-20 20:16:25 -05:00
func TestDeleteChannel2 ( 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-08-13 07:12:16 -04:00
client := th . Client
2018-11-20 20:16:25 -05:00
user := th . BasicUser
2017-09-12 15:12:29 -04:00
2018-02-06 10:34:08 -05:00
// Check the appropriate permissions are enforced.
2025-11-12 07:00:51 -05:00
defaultRolePermissions := th . SaveDefaultRolePermissions ( t )
2017-03-14 08:08:58 -04:00
defer func ( ) {
2025-11-12 07:00:51 -05:00
th . RestoreDefaultRolePermissions ( t , defaultRolePermissions )
2017-03-14 08:08:58 -04:00
} ( )
2018-02-06 10:34:08 -05:00
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionDeletePublicChannel . Id , model . ChannelUserRoleId )
th . AddPermissionToRole ( t , model . PermissionDeletePrivateChannel . Id , model . ChannelUserRoleId )
2017-03-14 08:08:58 -04:00
// channels created by SystemAdmin
2025-11-12 07:00:51 -05:00
publicChannel6 := th . CreateChannelWithClient ( t , th . SystemAdminClient , model . ChannelTypeOpen )
privateChannel7 := th . CreateChannelWithClient ( t , th . SystemAdminClient , model . ChannelTypePrivate )
2025-06-19 08:13:45 -04:00
_ , appErr := th . App . AddUserToChannel ( th . Context , user , publicChannel6 , false )
require . Nil ( t , appErr )
_ , appErr = th . App . AddUserToChannel ( th . Context , user , privateChannel7 , false )
require . Nil ( t , appErr )
_ , appErr = th . App . AddUserToChannel ( th . Context , user , privateChannel7 , false )
require . Nil ( t , appErr )
2017-03-14 08:08:58 -04:00
// successful delete by user
2023-06-06 17:29:29 -04:00
_ , err := client . DeleteChannel ( context . Background ( ) , publicChannel6 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-14 08:08:58 -04:00
2023-06-06 17:29:29 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , privateChannel7 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-14 08:08:58 -04:00
2018-02-06 10:34:08 -05:00
// Restrict permissions to Channel Admins
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionDeletePublicChannel . Id , model . ChannelUserRoleId )
th . RemovePermissionFromRole ( t , model . PermissionDeletePrivateChannel . Id , model . ChannelUserRoleId )
th . AddPermissionToRole ( t , model . PermissionDeletePublicChannel . Id , model . ChannelAdminRoleId )
th . AddPermissionToRole ( t , model . PermissionDeletePrivateChannel . Id , model . ChannelAdminRoleId )
2017-03-14 08:08:58 -04:00
// channels created by SystemAdmin
2025-11-12 07:00:51 -05:00
publicChannel6 = th . CreateChannelWithClient ( t , th . SystemAdminClient , model . ChannelTypeOpen )
privateChannel7 = th . CreateChannelWithClient ( t , th . SystemAdminClient , model . ChannelTypePrivate )
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . AddUserToChannel ( th . Context , user , publicChannel6 , false )
require . Nil ( t , appErr )
_ , appErr = th . App . AddUserToChannel ( th . Context , user , privateChannel7 , false )
require . Nil ( t , appErr )
_ , appErr = th . App . AddUserToChannel ( th . Context , user , privateChannel7 , false )
require . Nil ( t , appErr )
2017-03-14 08:08:58 -04:00
// cannot delete by user
2023-06-06 17:29:29 -04:00
resp , err := client . DeleteChannel ( context . Background ( ) , publicChannel6 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-14 08:08:58 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . DeleteChannel ( context . Background ( ) , privateChannel7 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-14 08:08:58 -04:00
CheckForbiddenStatus ( t , resp )
// successful delete by channel admin
2025-11-12 07:00:51 -05:00
th . MakeUserChannelAdmin ( t , user , publicChannel6 )
th . MakeUserChannelAdmin ( t , user , privateChannel7 )
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . Store ( ) . Channel ( ) . ClearCaches ( )
2017-03-14 08:08:58 -04:00
2023-06-06 17:29:29 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , publicChannel6 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-14 08:08:58 -04:00
2023-06-06 17:29:29 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , privateChannel7 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-14 08:08:58 -04:00
2018-02-06 10:34:08 -05:00
// Make sure team admins don't have permission to delete channels.
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionDeletePublicChannel . Id , model . ChannelAdminRoleId )
th . RemovePermissionFromRole ( t , model . PermissionDeletePrivateChannel . Id , model . ChannelAdminRoleId )
2017-05-11 16:32:14 -04:00
2017-07-04 18:32:27 -04:00
// last member of a public channel should have required permission to delete
2025-11-12 07:00:51 -05:00
publicChannel6 = th . CreateChannelWithClient ( t , th . Client , model . ChannelTypeOpen )
2023-06-06 17:29:29 -04:00
resp , err = client . DeleteChannel ( context . Background ( ) , publicChannel6 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-07-04 18:32:27 -04:00
CheckForbiddenStatus ( t , resp )
2017-08-09 09:34:09 -04:00
// last member of a private channel should not be able to delete it if they don't have required permissions
2025-11-12 07:00:51 -05:00
privateChannel7 = th . CreateChannelWithClient ( t , th . Client , model . ChannelTypePrivate )
2023-06-06 17:29:29 -04:00
resp , err = client . DeleteChannel ( context . Background ( ) , privateChannel7 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-08-09 09:34:09 -04:00
CheckForbiddenStatus ( t , resp )
2017-03-14 08:08:58 -04:00
}
2018-04-23 08:18:58 -04:00
2020-08-14 04:42:39 -04:00
func TestPermanentDeleteChannel ( 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-08-14 04:42:39 -04:00
enableAPIChannelDeletion := * th . App . Config ( ) . ServiceSettings . EnableAPIChannelDeletion
defer func ( ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) { cfg . ServiceSettings . EnableAPIChannelDeletion = & enableAPIChannelDeletion } )
} ( )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . ServiceSettings . EnableAPIChannelDeletion = false } )
2025-11-12 07:00:51 -05:00
publicChannel1 := th . CreatePublicChannel ( t )
2020-08-14 04:42:39 -04:00
t . Run ( "Permanent deletion not available through API if EnableAPIChannelDeletion is not set" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
resp , err := th . SystemAdminClient . PermanentDeleteChannel ( context . Background ( ) , publicChannel1 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-08-14 04:42:39 -04:00
CheckUnauthorizedStatus ( t , resp )
} )
t . Run ( "Permanent deletion available through local mode even if EnableAPIChannelDeletion is not set" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , err := th . LocalClient . PermanentDeleteChannel ( context . Background ( ) , publicChannel1 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-08-14 04:42:39 -04:00
} )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . ServiceSettings . EnableAPIChannelDeletion = true } )
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , c * model . Client4 ) {
2025-11-12 07:00:51 -05:00
publicChannel := th . CreatePublicChannel ( t )
2023-06-06 17:29:29 -04:00
_ , err := c . PermanentDeleteChannel ( context . Background ( ) , publicChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-08-14 04:42:39 -04:00
2022-07-14 05:01:29 -04:00
_ , appErr := th . App . GetChannel ( th . Context , publicChannel . Id )
2021-08-13 07:12:16 -04:00
assert . NotNil ( t , appErr )
2020-08-14 04:42:39 -04:00
2023-06-06 17:29:29 -04:00
resp , err := c . PermanentDeleteChannel ( context . Background ( ) , "junk" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-08-14 04:42:39 -04:00
CheckBadRequestStatus ( t , resp )
} , "Permanent deletion with EnableAPIChannelDeletion set" )
}
2019-09-16 13:34:36 -04:00
func TestUpdateChannelPrivacy ( 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-06-09 11:40:22 -04:00
2025-06-19 08:13:45 -04:00
defaultChannel , appErr := th . App . GetChannelByName ( th . Context , model . DefaultChannelName , th . BasicTeam . Id , false )
require . Nil ( t , appErr )
2019-09-16 13:34:36 -04:00
type testTable [ ] struct {
name string
channel * model . Channel
2021-07-20 03:15:26 -04:00
expectedPrivacy model . ChannelType
2019-09-16 13:34:36 -04:00
}
2021-06-09 11:40:22 -04:00
t . Run ( "Should get a forbidden response if not logged in" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
privateChannel := th . CreatePrivateChannel ( t )
publicChannel := th . CreatePublicChannel ( t )
2019-09-16 13:34:36 -04:00
2021-06-09 11:40:22 -04:00
tt := testTable {
2021-07-12 14:05:36 -04:00
{ "Updating default channel should fail with forbidden status if not logged in" , defaultChannel , model . ChannelTypeOpen } ,
{ "Updating private channel should fail with forbidden status if not logged in" , privateChannel , model . ChannelTypePrivate } ,
{ "Updating public channel should fail with forbidden status if not logged in" , publicChannel , model . ChannelTypeOpen } ,
2021-06-09 11:40:22 -04:00
}
2019-09-16 13:34:36 -04:00
2021-06-09 11:40:22 -04:00
for _ , tc := range tt {
t . Run ( tc . name , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , resp , err := th . Client . UpdateChannelPrivacy ( context . Background ( ) , tc . channel . Id , tc . expectedPrivacy )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2021-06-09 11:40:22 -04:00
CheckForbiddenStatus ( t , resp )
} )
}
} )
2019-09-16 13:34:36 -04:00
2021-06-09 11:40:22 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2025-11-12 07:00:51 -05:00
privateChannel := th . CreatePrivateChannel ( t )
publicChannel := th . CreatePublicChannel ( t )
2019-09-16 13:34:36 -04:00
2021-06-09 11:40:22 -04:00
tt := testTable {
2021-07-12 14:05:36 -04:00
{ "Converting default channel to private should fail" , defaultChannel , model . ChannelTypePrivate } ,
2021-06-09 11:40:22 -04:00
{ "Updating privacy to an invalid setting should fail" , publicChannel , "invalid" } ,
}
2019-09-16 13:34:36 -04:00
2021-06-09 11:40:22 -04:00
for _ , tc := range tt {
t . Run ( tc . name , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , resp , err := client . UpdateChannelPrivacy ( context . Background ( ) , tc . channel . Id , tc . expectedPrivacy )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2021-06-09 11:40:22 -04:00
CheckBadRequestStatus ( t , resp )
} )
}
2019-09-16 13:34:36 -04:00
2021-06-09 11:40:22 -04:00
tt = testTable {
2021-07-12 14:05:36 -04:00
{ "Default channel should stay public" , defaultChannel , model . ChannelTypeOpen } ,
{ "Public channel should stay public" , publicChannel , model . ChannelTypeOpen } ,
{ "Private channel should stay private" , privateChannel , model . ChannelTypePrivate } ,
{ "Public channel should convert to private" , publicChannel , model . ChannelTypePrivate } ,
{ "Private channel should convert to public" , privateChannel , model . ChannelTypeOpen } ,
2021-06-09 11:40:22 -04:00
}
2019-09-16 13:34:36 -04:00
2021-06-09 11:40:22 -04:00
for _ , tc := range tt {
t . Run ( tc . name , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
updatedChannel , _ , err := client . UpdateChannelPrivacy ( context . Background ( ) , tc . channel . Id , tc . expectedPrivacy )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-06-09 11:40:22 -04:00
assert . Equal ( t , tc . expectedPrivacy , updatedChannel . Type )
2022-07-14 05:01:29 -04:00
updatedChannel , appErr := th . App . GetChannel ( th . Context , tc . channel . Id )
2021-08-13 07:12:16 -04:00
require . Nil ( t , appErr )
2021-06-09 11:40:22 -04:00
assert . Equal ( t , tc . expectedPrivacy , updatedChannel . Type )
} )
}
} )
2020-09-03 13:41:07 -04:00
t . Run ( "Enforces convert channel permissions" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
privateChannel := th . CreatePrivateChannel ( t )
publicChannel := th . CreatePublicChannel ( t )
2021-06-09 11:40:22 -04:00
2025-11-12 07:00:51 -05:00
th . LoginTeamAdmin ( t )
2021-06-09 11:40:22 -04:00
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionConvertPublicChannelToPrivate . Id , model . TeamAdminRoleId )
th . RemovePermissionFromRole ( t , model . PermissionConvertPrivateChannelToPublic . Id , model . TeamAdminRoleId )
2020-09-03 13:41:07 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err := th . Client . UpdateChannelPrivacy ( context . Background ( ) , publicChannel . Id , model . ChannelTypePrivate )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-09-03 13:41:07 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = th . Client . UpdateChannelPrivacy ( context . Background ( ) , privateChannel . Id , model . ChannelTypeOpen )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-09-03 13:41:07 -04:00
CheckForbiddenStatus ( t , resp )
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionConvertPublicChannelToPrivate . Id , model . TeamAdminRoleId )
th . AddPermissionToRole ( t , model . PermissionConvertPrivateChannelToPublic . Id , model . TeamAdminRoleId )
2020-09-03 13:41:07 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err = th . Client . UpdateChannelPrivacy ( context . Background ( ) , privateChannel . Id , model . ChannelTypeOpen )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . Client . UpdateChannelPrivacy ( context . Background ( ) , publicChannel . Id , model . ChannelTypePrivate )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-09-03 13:41:07 -04:00
} )
2019-09-16 13:34:36 -04:00
}
2017-05-15 16:12:30 -04:00
func TestRestoreChannel ( 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-05-15 16:12:30 -04:00
2025-11-12 07:00:51 -05:00
publicChannel1 := th . CreatePublicChannel ( t )
2025-06-19 08:13:45 -04:00
_ , err := th . Client . DeleteChannel ( context . Background ( ) , publicChannel1 . Id )
require . NoError ( t , err )
2017-05-15 16:12:30 -04:00
2025-11-12 07:00:51 -05:00
privateChannel1 := th . CreatePrivateChannel ( t )
2025-06-19 08:13:45 -04:00
_ , err = th . Client . DeleteChannel ( context . Background ( ) , privateChannel1 . Id )
require . NoError ( t , err )
2017-05-15 16:12:30 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err := th . Client . RestoreChannel ( context . Background ( ) , publicChannel1 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-05-15 16:12:30 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = th . Client . RestoreChannel ( context . Background ( ) , privateChannel1 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-05-15 16:12:30 -04:00
CheckForbiddenStatus ( t , resp )
2024-07-09 10:21:29 -04:00
// Make BasicUser, a User Manager
oldRoles := th . BasicUser . Roles
// Because the permissions get set on initialization,
// remove the manage_team permission from the User Management Role
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionManageTeam . Id , model . SystemUserManagerRoleId )
2025-06-19 08:13:45 -04:00
_ , appErr := th . App . UpdateUserRoles ( th . Context , th . BasicUser . Id , model . SystemUserManagerRoleId , false )
require . Nil ( t , appErr )
2024-07-09 10:21:29 -04:00
defer func ( ) {
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . UpdateUserRoles ( th . Context , th . BasicUser . Id , oldRoles , false )
require . Nil ( t , appErr )
2024-07-09 10:21:29 -04:00
} ( )
2025-06-19 08:13:45 -04:00
appErr = th . App . Srv ( ) . InvalidateAllCaches ( )
require . Nil ( t , appErr )
_ , _ , err = th . Client . Login ( context . Background ( ) , th . BasicUser . Email , th . BasicUser . Password )
require . NoError ( t , err )
2024-07-09 10:21:29 -04:00
_ , resp , err = th . Client . RestoreChannel ( context . Background ( ) , publicChannel1 . Id )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
_ , resp , err = th . Client . RestoreChannel ( context . Background ( ) , privateChannel1 . Id )
require . NoError ( t , err )
CheckOKStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = th . Client . DeleteChannel ( context . Background ( ) , publicChannel1 . Id )
require . NoError ( t , err )
_ , err = th . Client . DeleteChannel ( context . Background ( ) , privateChannel1 . Id )
require . NoError ( t , err )
2024-07-09 10:21:29 -04:00
2021-06-09 11:40:22 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
defer func ( ) {
2025-06-19 08:13:45 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , publicChannel1 . Id )
require . NoError ( t , err )
_ , err = client . DeleteChannel ( context . Background ( ) , privateChannel1 . Id )
require . NoError ( t , err )
2021-06-09 11:40:22 -04:00
} ( )
2017-05-15 16:12:30 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . RestoreChannel ( context . Background ( ) , publicChannel1 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-06-09 11:40:22 -04:00
CheckOKStatus ( t , resp )
2017-05-15 16:12:30 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . RestoreChannel ( context . Background ( ) , privateChannel1 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-06-09 11:40:22 -04:00
CheckOKStatus ( t , resp )
} )
2017-06-26 08:16:57 -04:00
}
2017-05-15 16:12:30 -04:00
2017-02-14 10:28:08 -05:00
func TestGetChannelByName ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-02-14 10:28:08 -05:00
2023-06-06 17:29:29 -04:00
channel , _ , err := client . GetChannelByName ( context . Background ( ) , th . BasicChannel . Name , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , th . BasicChannel . Name , channel . Name , "names did not match" )
2017-02-20 11:31:52 -05:00
2023-06-06 17:29:29 -04:00
channel , _ , err = client . GetChannelByName ( context . Background ( ) , th . BasicPrivateChannel . Name , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , th . BasicPrivateChannel . Name , channel . Name , "names did not match" )
2017-03-22 11:13:44 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err = client . GetChannelByName ( context . Background ( ) , strings . ToUpper ( th . BasicPrivateChannel . Name ) , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-01-12 09:01:45 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . GetChannelByName ( context . Background ( ) , th . BasicDeletedChannel . Name , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-07-30 15:06:08 -04:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
channel , _ , err = client . GetChannelByNameIncludeDeleted ( context . Background ( ) , th . BasicDeletedChannel . Name , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , th . BasicDeletedChannel . Name , channel . Name , "names did not match" )
2018-07-30 15:06:08 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . GetChannelByName ( context . Background ( ) , th . BasicChannel . Name , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-22 11:13:44 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , th . BasicPrivateChannel . Id , th . BasicUser . Id )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelByName ( context . Background ( ) , th . BasicPrivateChannel . Name , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-12-23 15:46:52 -05:00
CheckNotFoundStatus ( t , resp )
2017-03-22 11:13:44 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelByName ( context . Background ( ) , GenerateTestChannelName ( ) , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-14 10:28:08 -05:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelByName ( context . Background ( ) , GenerateTestChannelName ( ) , "junk" , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-22 11:13:44 -04:00
CheckBadRequestStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelByName ( context . Background ( ) , th . BasicChannel . Name , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-14 10:28:08 -05:00
CheckUnauthorizedStatus ( t , resp )
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelByName ( context . Background ( ) , th . BasicChannel . Name , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-14 10:28:08 -05:00
CheckForbiddenStatus ( t , resp )
2020-06-14 04:31:20 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
_ , _ , err = client . GetChannelByName ( context . Background ( ) , th . BasicChannel . Name , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-14 04:31:20 -04:00
} )
2025-01-30 10:21:09 -05:00
2025-06-19 08:13:45 -04:00
_ , err = th . SystemAdminClient . RemoveUserFromChannel ( context . Background ( ) , th . BasicPrivateChannel . Id , th . TeamAdminUser . Id )
require . NoError ( t , err )
2025-01-30 10:21:09 -05:00
TeamAdminClient := th . CreateClient ( )
2025-11-12 07:00:51 -05:00
th . LoginTeamAdminWithClient ( t , TeamAdminClient )
2025-01-30 10:21:09 -05:00
channel , _ , err = TeamAdminClient . GetChannelByName ( context . Background ( ) , th . BasicPrivateChannel . Name , th . BasicTeam . Id , "" )
require . NoError ( t , err )
require . Equal ( t , th . BasicPrivateChannel . Name , channel . Name , "names did not match" )
2017-02-14 10:28:08 -05:00
}
func TestGetChannelByNameForTeamName ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-02-14 10:28:08 -05:00
2023-06-06 17:29:29 -04:00
channel , _ , err := th . SystemAdminClient . GetChannelByNameForTeamName ( context . Background ( ) , th . BasicChannel . Name , th . BasicTeam . Name , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , th . BasicChannel . Name , channel . Name , "names did not match" )
2017-02-20 11:31:52 -05:00
2025-06-19 08:13:45 -04:00
_ , err = th . SystemAdminClient . RemoveUserFromChannel ( context . Background ( ) , th . BasicPrivateChannel . Id , th . TeamAdminUser . Id )
require . NoError ( t , err )
2025-01-30 10:21:09 -05:00
TeamAdminClient := th . CreateClient ( )
2025-11-12 07:00:51 -05:00
th . LoginTeamAdminWithClient ( t , TeamAdminClient )
2025-01-30 10:21:09 -05:00
channel , _ , err = TeamAdminClient . GetChannelByNameForTeamName ( context . Background ( ) , th . BasicPrivateChannel . Name , th . BasicTeam . Name , "" )
require . NoError ( t , err )
require . Equal ( t , th . BasicPrivateChannel . Name , channel . Name , "names did not match" )
channel , _ , err = client . GetChannelByNameForTeamName ( context . Background ( ) , th . BasicChannel . Name , th . BasicTeam . Name , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-09-22 04:22:42 -04:00
require . Equal ( t , th . BasicChannel . Name , channel . Name , "names did not match" )
2023-06-06 17:29:29 -04:00
channel , _ , err = client . GetChannelByNameForTeamName ( context . Background ( ) , th . BasicPrivateChannel . Name , th . BasicTeam . Name , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-09-22 04:22:42 -04:00
require . Equal ( t , th . BasicPrivateChannel . Name , channel . Name , "names did not match" )
2017-02-14 10:28:08 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . GetChannelByNameForTeamName ( context . Background ( ) , th . BasicDeletedChannel . Name , th . BasicTeam . Name , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-07-30 15:06:08 -04:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
channel , _ , err = client . GetChannelByNameForTeamNameIncludeDeleted ( context . Background ( ) , th . BasicDeletedChannel . Name , th . BasicTeam . Name , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , th . BasicDeletedChannel . Name , channel . Name , "names did not match" )
2018-07-30 15:06:08 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . GetChannelByNameForTeamName ( context . Background ( ) , th . BasicChannel . Name , th . BasicTeam . Name , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-09-22 04:22:42 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , th . BasicPrivateChannel . Id , th . BasicUser . Id )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelByNameForTeamName ( context . Background ( ) , th . BasicPrivateChannel . Name , th . BasicTeam . Name , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-09-22 04:22:42 -04:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelByNameForTeamName ( context . Background ( ) , th . BasicChannel . Name , model . NewRandomString ( 15 ) , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-14 10:28:08 -05:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelByNameForTeamName ( context . Background ( ) , GenerateTestChannelName ( ) , th . BasicTeam . Name , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-14 10:28:08 -05:00
CheckNotFoundStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelByNameForTeamName ( context . Background ( ) , th . BasicChannel . Name , th . BasicTeam . Name , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-14 10:28:08 -05:00
CheckUnauthorizedStatus ( t , resp )
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelByNameForTeamName ( context . Background ( ) , th . BasicChannel . Name , th . BasicTeam . Name , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-09-22 04:22:42 -04:00
CheckForbiddenStatus ( t , resp )
2017-02-14 10:28:08 -05:00
}
2017-02-07 17:58:27 -05:00
func TestGetChannelMembers ( 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-27 07:58:39 -04:00
th . TestForAllClients ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
members , _ , err := client . GetChannelMembers ( context . Background ( ) , th . BasicChannel . Id , 0 , 60 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Len ( t , members , 3 , "should only be 3 users in channel" )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
members , _ , err = client . GetChannelMembers ( context . Background ( ) , th . BasicChannel . Id , 0 , 2 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Len ( t , members , 2 , "should only be 2 users" )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
members , _ , err = client . GetChannelMembers ( context . Background ( ) , th . BasicChannel . Id , 1 , 1 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Len ( t , members , 1 , "should only be 1 user" )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
members , _ , err = client . GetChannelMembers ( context . Background ( ) , th . BasicChannel . Id , 1000 , 100000 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Empty ( t , members , "should be 0 users" )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . GetChannelMembers ( context . Background ( ) , "junk" , 0 , 60 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-05-27 07:58:39 -04:00
CheckBadRequestStatus ( t , resp )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembers ( context . Background ( ) , "" , 0 , 60 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-06-14 04:31:20 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . GetChannelMembers ( context . Background ( ) , th . BasicChannel . Id , 0 , 60 , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-05-27 07:58:39 -04:00
} )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := th . Client . GetChannelMembers ( context . Background ( ) , model . NewId ( ) , 0 , 60 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckForbiddenStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = th . Client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = th . Client . GetChannelMembers ( context . Background ( ) , th . BasicChannel . Id , 0 , 60 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckUnauthorizedStatus ( t , resp )
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = th . Client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = th . Client . GetChannelMembers ( context . Background ( ) , th . BasicChannel . Id , 0 , 60 , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckForbiddenStatus ( t , resp )
}
2017-03-16 14:58:33 -04:00
func TestGetChannelMembersByIds ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
cm , _ , err := client . GetChannelMembersByIds ( context . Background ( ) , th . BasicChannel . Id , [ ] string { th . BasicUser . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Equal ( t , th . BasicUser . Id , cm [ 0 ] . UserId , "returned wrong user" )
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . GetChannelMembersByIds ( context . Background ( ) , th . BasicChannel . Id , [ ] string { } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
cm1 , _ , err := client . GetChannelMembersByIds ( context . Background ( ) , th . BasicChannel . Id , [ ] string { "junk" } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Empty ( t , cm1 , "no users should be returned" )
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
cm1 , _ , err = client . GetChannelMembersByIds ( context . Background ( ) , th . BasicChannel . Id , [ ] string { "junk" , th . BasicUser . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Len ( t , cm1 , 1 , "1 member should be returned" )
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
cm1 , _ , err = client . GetChannelMembersByIds ( context . Background ( ) , th . BasicChannel . Id , [ ] string { th . BasicUser2 . Id , th . BasicUser . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Len ( t , cm1 , 2 , "2 members should be returned" )
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembersByIds ( context . Background ( ) , "junk" , [ ] string { th . BasicUser . Id } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembersByIds ( context . Background ( ) , model . NewId ( ) , [ ] string { th . BasicUser . Id } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckForbiddenStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembersByIds ( context . Background ( ) , th . BasicChannel . Id , [ ] string { th . BasicUser . Id } )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckUnauthorizedStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . GetChannelMembersByIds ( context . Background ( ) , th . BasicChannel . Id , [ ] string { th . BasicUser2 . Id , th . BasicUser . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-16 14:58:33 -04:00
}
2017-02-07 17:58:27 -05:00
func TestGetChannelMember ( 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-27 07:58:39 -04:00
c := th . Client
th . TestForAllClients ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
member , _ , err := client . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-05-27 07:58:39 -04:00
require . Equal ( t , th . BasicChannel . Id , member . ChannelId , "wrong channel id" )
require . Equal ( t , th . BasicUser . Id , member . UserId , "wrong user id" )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . GetChannelMember ( context . Background ( ) , "" , th . BasicUser . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-05-27 07:58:39 -04:00
CheckNotFoundStatus ( t , resp )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMember ( context . Background ( ) , "junk" , th . BasicUser . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-05-27 07:58:39 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , "" , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-05-27 07:58:39 -04:00
CheckNotFoundStatus ( t , resp )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , "junk" , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-05-27 07:58:39 -04:00
CheckBadRequestStatus ( t , resp )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , model . NewId ( ) , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-05-27 07:58:39 -04:00
CheckNotFoundStatus ( t , resp )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
_ , _ , err = client . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-05-27 07:58:39 -04:00
} )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := c . GetChannelMember ( context . Background ( ) , model . NewId ( ) , th . BasicUser . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-05-27 07:58:39 -04:00
CheckForbiddenStatus ( t , resp )
2017-02-07 17:58:27 -05:00
2025-06-19 08:13:45 -04:00
_ , err = c . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = c . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckUnauthorizedStatus ( t , resp )
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = c . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = c . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckForbiddenStatus ( t , resp )
}
func TestGetChannelMembersForUser ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
members , _ , err := client . GetChannelMembersForUser ( context . Background ( ) , th . BasicUser . Id , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
require . Len ( t , members , 6 , "should have 6 members on team" )
2017-02-07 17:58:27 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . GetChannelMembersForUser ( context . Background ( ) , "" , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembersForUser ( context . Background ( ) , "junk" , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembersForUser ( context . Background ( ) , model . NewId ( ) , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembersForUser ( context . Background ( ) , th . BasicUser . Id , "" , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembersForUser ( context . Background ( ) , th . BasicUser . Id , "junk" , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembersForUser ( context . Background ( ) , th . BasicUser . Id , model . NewId ( ) , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckForbiddenStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembersForUser ( context . Background ( ) , th . BasicUser . Id , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckUnauthorizedStatus ( t , resp )
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelMembersForUser ( context . Background ( ) , th . BasicUser . Id , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-07 17:58:27 -05:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . GetChannelMembersForUser ( context . Background ( ) , th . BasicUser . Id , th . BasicTeam . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-07 17:58:27 -05:00
}
2017-02-20 11:31:52 -05:00
func TestViewChannel ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-02-20 11:31:52 -05:00
view := & model . ChannelView {
ChannelId : th . BasicChannel . Id ,
}
2023-06-06 17:29:29 -04:00
viewResp , _ , err := client . ViewChannel ( context . Background ( ) , th . BasicUser . Id , view )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , "OK" , viewResp . Status , "should have passed" )
2017-02-20 11:31:52 -05:00
2025-06-19 08:13:45 -04:00
channel , appErr := th . App . GetChannel ( th . Context , th . BasicChannel . Id )
require . Nil ( t , appErr )
2017-09-29 11:45:59 -04:00
2019-11-13 13:53:57 -05:00
require . Equal ( t , channel . LastPostAt , viewResp . LastViewedAtTimes [ channel . Id ] , "LastPostAt does not match returned LastViewedAt time" )
2017-09-29 11:45:59 -04:00
2017-02-20 11:31:52 -05:00
view . PrevChannelId = th . BasicChannel . Id
2023-06-06 17:29:29 -04:00
_ , _ , err = client . ViewChannel ( context . Background ( ) , th . BasicUser . Id , view )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
view . PrevChannelId = ""
2023-06-06 17:29:29 -04:00
_ , _ , err = client . ViewChannel ( context . Background ( ) , th . BasicUser . Id , view )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
view . PrevChannelId = "junk"
2023-06-06 17:29:29 -04:00
_ , resp , err := client . ViewChannel ( context . Background ( ) , th . BasicUser . Id , view )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-12-12 13:08:55 -05:00
CheckBadRequestStatus ( t , resp )
2019-01-07 17:07:41 -05:00
// All blank is OK we use it for clicking off of the browser.
view . PrevChannelId = ""
view . ChannelId = ""
2023-06-06 17:29:29 -04:00
_ , _ , err = client . ViewChannel ( context . Background ( ) , th . BasicUser . Id , view )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-01-07 17:07:41 -05:00
2018-12-12 13:08:55 -05:00
view . PrevChannelId = ""
view . ChannelId = "junk"
2023-06-06 17:29:29 -04:00
_ , resp , err = client . ViewChannel ( context . Background ( ) , th . BasicUser . Id , view )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-12-12 13:08:55 -05:00
CheckBadRequestStatus ( t , resp )
view . ChannelId = "correctlysizedjunkdddfdfdf"
2023-08-14 04:01:02 -04:00
viewResult , _ , err := client . ViewChannel ( context . Background ( ) , th . BasicUser . Id , view )
require . NoError ( t , err )
require . Len ( t , viewResult . LastViewedAtTimes , 0 )
2018-12-12 13:08:55 -05:00
view . ChannelId = th . BasicChannel . Id
2017-02-20 11:31:52 -05:00
2023-06-06 17:29:29 -04:00
member , _ , err := client . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2026-01-20 11:46:17 -05:00
channel , _ , err = client . GetChannel ( context . Background ( ) , th . BasicChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , channel . TotalMsgCount , member . MsgCount , "should match message counts" )
require . Equal ( t , int64 ( 0 ) , member . MentionCount , "should have no mentions" )
2021-04-01 07:43:09 -04:00
require . Equal ( t , int64 ( 0 ) , member . MentionCountRoot , "should have no mentions" )
2017-02-20 11:31:52 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . ViewChannel ( context . Background ( ) , "junk" , view )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . ViewChannel ( context . Background ( ) , th . BasicUser2 . Id , view )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
r , err := client . DoAPIPost ( context . Background ( ) , fmt . Sprintf ( "/channels/members/%v/view" , th . BasicUser . Id ) , "garbage" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , http . StatusBadRequest , r . StatusCode )
2017-03-03 12:25:32 -05:00
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . ViewChannel ( context . Background ( ) , th . BasicUser . Id , view )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckUnauthorizedStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . ViewChannel ( context . Background ( ) , th . BasicUser . Id , view )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
}
2024-07-02 07:35:43 -04:00
func TestReadMultipleChannels ( 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-07-02 07:35:43 -04:00
client := th . Client
user := th . BasicUser
t . Run ( "Should successfully mark public channels as read for self" , func ( t * testing . T ) {
2026-01-20 11:46:17 -05:00
channel , _ , err := client . GetChannel ( context . Background ( ) , th . BasicChannel . Id )
2024-07-02 07:35:43 -04:00
require . NoError ( t , err )
2026-01-20 11:46:17 -05:00
channel2 , _ , err := client . GetChannel ( context . Background ( ) , th . BasicChannel2 . Id )
2024-07-02 07:35:43 -04:00
require . NoError ( t , err )
channelResponse , _ , err := client . ReadMultipleChannels ( context . Background ( ) , user . Id , [ ] string { channel . Id , channel2 . Id } )
require . NoError ( t , err )
require . Equal ( t , "OK" , channelResponse . Status , "invalid status return" )
require . Equal ( t , channel . LastPostAt , channelResponse . LastViewedAtTimes [ channel . Id ] , "wrong number of viewed at times" )
require . Equal ( t , channel2 . LastPostAt , channelResponse . LastViewedAtTimes [ channel2 . Id ] , "wrong number of viewed at times" )
} )
t . Run ( "Should successfully mark private channels as read for self" , func ( t * testing . T ) {
2026-01-20 11:46:17 -05:00
channel , _ , err := client . GetChannel ( context . Background ( ) , th . BasicPrivateChannel . Id )
2024-07-02 07:35:43 -04:00
require . NoError ( t , err )
// private channel without membership should be ignored
channelResponse , _ , err := client . ReadMultipleChannels ( context . Background ( ) , user . Id , [ ] string { channel . Id , th . BasicPrivateChannel2 . Id } )
require . NoError ( t , err )
require . Equal ( t , "OK" , channelResponse . Status , "invalid status return" )
require . Equal ( t , 1 , len ( channelResponse . LastViewedAtTimes ) , "unexpected response" )
require . Equal ( t , channel . LastPostAt , channelResponse . LastViewedAtTimes [ channel . Id ] , "wrong number of viewed at times" )
} )
t . Run ( "Should fail marking public/private channels for other user" , func ( t * testing . T ) {
2026-01-20 11:46:17 -05:00
channel , _ , err := client . GetChannel ( context . Background ( ) , th . BasicChannel . Id )
2024-07-02 07:35:43 -04:00
require . NoError ( t , err )
_ , _ , err = client . ReadMultipleChannels ( context . Background ( ) , th . BasicUser2 . Id , [ ] string { channel . Id } )
require . Error ( t , err )
} )
t . Run ( "Admin should succeed in marking public/private channels for other user" , func ( t * testing . T ) {
adminClient := th . SystemAdminClient
2026-01-20 11:46:17 -05:00
channel , _ , err := adminClient . GetChannel ( context . Background ( ) , th . BasicChannel . Id )
2024-07-02 07:35:43 -04:00
require . NoError ( t , err )
2026-01-20 11:46:17 -05:00
privateChannel , _ , err := adminClient . GetChannel ( context . Background ( ) , th . BasicPrivateChannel . Id )
2024-07-02 07:35:43 -04:00
require . NoError ( t , err )
channelResponse , _ , err := adminClient . ReadMultipleChannels ( context . Background ( ) , th . BasicUser2 . Id , [ ] string { channel . Id , privateChannel . Id } )
require . NoError ( t , err )
require . Equal ( t , "OK" , channelResponse . Status , "invalid status return" )
require . Equal ( t , channel . LastPostAt , channelResponse . LastViewedAtTimes [ channel . Id ] , "wrong number of viewed at times" )
require . Equal ( t , privateChannel . LastPostAt , channelResponse . LastViewedAtTimes [ privateChannel . Id ] , "wrong number of viewed at times" )
} )
t . Run ( "SystemManager should succeed in marking public/private channels for other user" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th . LoginSystemManager ( t )
2024-07-02 07:35:43 -04:00
sysMgrClient := th . SystemManagerClient
2026-01-20 11:46:17 -05:00
channel , _ , err := sysMgrClient . GetChannel ( context . Background ( ) , th . BasicChannel . Id )
2024-07-02 07:35:43 -04:00
require . NoError ( t , err )
2026-01-20 11:46:17 -05:00
privateChannel , _ , err := sysMgrClient . GetChannel ( context . Background ( ) , th . BasicPrivateChannel . Id )
2024-07-02 07:35:43 -04:00
require . NoError ( t , err )
_ , _ , err = sysMgrClient . ReadMultipleChannels ( context . Background ( ) , th . BasicUser2 . Id , [ ] string { channel . Id , privateChannel . Id } )
require . Error ( t , err )
} )
t . Run ( "SystemManager without editOtherUsers should fail in marking public/private channels for other user" , func ( t * testing . T ) {
sysMgrClient := th . SystemManagerClient
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionEditOtherUsers . Id , model . SystemManagerRoleId )
2024-07-02 07:35:43 -04:00
defer func ( ) {
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionEditOtherUsers . Id , model . SystemManagerRoleId )
2024-07-02 07:35:43 -04:00
} ( )
_ , _ , err := sysMgrClient . ReadMultipleChannels ( context . Background ( ) , th . BasicUser2 . Id , [ ] string { th . BasicChannel . Id } )
require . Error ( t , err )
} )
}
2017-03-16 14:58:33 -04:00
func TestGetChannelUnread ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-16 14:58:33 -04:00
user := th . BasicUser
channel := th . BasicChannel
2023-06-06 17:29:29 -04:00
channelUnread , _ , err := client . GetChannelUnread ( context . Background ( ) , channel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , th . BasicTeam . Id , channelUnread . TeamId , "wrong team id returned for a regular user call" )
require . Equal ( t , channel . Id , channelUnread . ChannelId , "wrong team id returned for a regular user call" )
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err := client . GetChannelUnread ( context . Background ( ) , "junk" , user . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelUnread ( context . Background ( ) , channel . Id , "junk" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelUnread ( context . Background ( ) , channel . Id , model . NewId ( ) )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelUnread ( context . Background ( ) , model . NewId ( ) , user . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckForbiddenStatus ( t , resp )
2025-11-12 07:00:51 -05:00
newUser := th . CreateUser ( t )
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , newUser . Email , newUser . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelUnread ( context . Background ( ) , th . BasicChannel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckForbiddenStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . GetChannelUnread ( context . Background ( ) , channel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = th . SystemAdminClient . GetChannelUnread ( context . Background ( ) , model . NewId ( ) , user . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-10-04 11:42:38 -04:00
CheckForbiddenStatus ( t , resp )
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = th . SystemAdminClient . GetChannelUnread ( context . Background ( ) , channel . Id , model . NewId ( ) )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckNotFoundStatus ( t , resp )
}
func TestGetChannelStats ( 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-08-13 07:12:16 -04:00
client := th . Client
2025-11-12 07:00:51 -05:00
channel := th . CreatePrivateChannel ( t )
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
stats , _ , err := client . GetChannelStats ( context . Background ( ) , channel . Id , "" , false )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-16 14:58:33 -04:00
2022-02-28 04:31:00 -05:00
require . Equal ( t , channel . Id , stats . ChannelId , "couldn't get extra info" )
2019-11-13 13:53:57 -05:00
require . Equal ( t , int64 ( 1 ) , stats . MemberCount , "got incorrect member count" )
require . Equal ( t , int64 ( 0 ) , stats . PinnedPostCount , "got incorrect pinned post count" )
2022-03-25 16:28:14 -04:00
require . Equal ( t , int64 ( 0 ) , stats . FilesCount , "got incorrect file count" )
2019-09-04 07:41:29 -04:00
2025-11-12 07:00:51 -05:00
th . CreatePinnedPostWithClient ( t , th . Client , channel )
2023-06-06 17:29:29 -04:00
stats , _ , err = client . GetChannelStats ( context . Background ( ) , channel . Id , "" , false )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , int64 ( 1 ) , stats . PinnedPostCount , "should have returned 1 pinned post count" )
2017-03-16 14:58:33 -04:00
2022-03-25 16:28:14 -04:00
// create a post with a file
sent , err := testutils . ReadTestFile ( "test.png" )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
fileResp , _ , err := client . UploadFile ( context . Background ( ) , sent , channel . Id , "test.png" )
2022-03-25 16:28:14 -04:00
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
th . CreatePostInChannelWithFiles ( t , channel , fileResp . FileInfos ... )
2022-03-25 16:28:14 -04:00
// make sure the file count channel stats is updated
2023-06-06 17:29:29 -04:00
stats , _ , err = client . GetChannelStats ( context . Background ( ) , channel . Id , "" , false )
2022-03-25 16:28:14 -04:00
require . NoError ( t , err )
require . Equal ( t , int64 ( 1 ) , stats . FilesCount , "should have returned 1 file count" )
2023-01-27 21:03:33 -05:00
// exclude file counts
2023-06-06 17:29:29 -04:00
stats , _ , err = client . GetChannelStats ( context . Background ( ) , channel . Id , "" , true )
2023-01-27 21:03:33 -05:00
require . NoError ( t , err )
require . Equal ( t , int64 ( - 1 ) , stats . FilesCount , "should have returned -1 file count for exclude_files_count=true" )
2023-06-06 17:29:29 -04:00
_ , resp , err := client . GetChannelStats ( context . Background ( ) , "junk" , "" , false )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelStats ( context . Background ( ) , model . NewId ( ) , "" , false )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckForbiddenStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelStats ( context . Background ( ) , channel . Id , "" , false )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckUnauthorizedStatus ( t , resp )
2025-11-12 07:00:51 -05:00
th . LoginBasic2 ( t )
2017-03-16 14:58:33 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetChannelStats ( context . Background ( ) , channel . Id , "" , false )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-16 14:58:33 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . GetChannelStats ( context . Background ( ) , channel . Id , "" , false )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-16 14:58:33 -04:00
}
2017-03-29 11:09:05 -04:00
func TestGetPinnedPosts ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-29 11:09:05 -04:00
channel := th . BasicChannel
2023-06-06 17:29:29 -04:00
posts , _ , err := client . GetPinnedPosts ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-12-22 06:35:31 -05:00
require . Empty ( t , posts . Posts , "should not have gotten a pinned post" )
2017-03-29 11:09:05 -04:00
2025-11-12 07:00:51 -05:00
pinnedPost := th . CreatePinnedPost ( t )
2023-06-06 17:29:29 -04:00
posts , resp , err := client . GetPinnedPosts ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Len ( t , posts . Posts , 1 , "should have returned 1 pinned post" )
require . Contains ( t , posts . Posts , pinnedPost . Id , "missing pinned post" )
2017-03-29 11:09:05 -04:00
2025-06-19 08:13:45 -04:00
posts , resp , err = client . GetPinnedPosts ( context . Background ( ) , channel . Id , resp . Etag )
require . NoError ( t , err )
2017-03-29 11:09:05 -04:00
CheckEtag ( t , posts , resp )
2023-12-20 00:46:54 -05:00
_ , resp , err = client . GetPinnedPosts ( context . Background ( ) , GenerateTestID ( ) , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2024-07-29 10:05:20 -04:00
CheckNotFoundStatus ( t , resp )
2017-03-29 11:09:05 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetPinnedPosts ( context . Background ( ) , "junk" , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-29 11:09:05 -04:00
CheckBadRequestStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . GetPinnedPosts ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-29 11:09:05 -04:00
CheckUnauthorizedStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . GetPinnedPosts ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-29 11:09:05 -04:00
}
2017-02-20 11:31:52 -05:00
func TestUpdateChannelRoles ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-02-20 11:31:52 -05:00
2021-01-04 01:02:29 -05:00
const ChannelAdmin = "channel_user channel_admin"
const ChannelMember = "channel_user"
2017-02-20 11:31:52 -05:00
// User 1 creates a channel, making them channel admin by default.
2025-11-12 07:00:51 -05:00
channel := th . CreatePublicChannel ( t )
2017-02-20 11:31:52 -05:00
// Adds User 2 to the channel, making them a channel member by default.
2025-06-19 08:13:45 -04:00
_ , appErr := th . App . AddUserToChannel ( th . Context , th . BasicUser2 , channel , false )
require . Nil ( t , appErr )
2017-02-20 11:31:52 -05:00
// User 1 promotes User 2
2023-06-06 17:29:29 -04:00
_ , err := client . UpdateChannelRoles ( context . Background ( ) , channel . Id , th . BasicUser2 . Id , ChannelAdmin )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
2023-06-06 17:29:29 -04:00
member , _ , err := client . GetChannelMember ( context . Background ( ) , channel . Id , th . BasicUser2 . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-01-04 01:02:29 -05:00
require . Equal ( t , ChannelAdmin , member . Roles , "roles don't match" )
2017-02-20 11:31:52 -05:00
// User 1 demotes User 2
2023-06-06 17:29:29 -04:00
_ , err = client . UpdateChannelRoles ( context . Background ( ) , channel . Id , th . BasicUser2 . Id , ChannelMember )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
2025-11-12 07:00:51 -05:00
th . LoginBasic2 ( t )
2017-02-20 11:31:52 -05:00
// User 2 cannot demote User 1
2023-06-06 17:29:29 -04:00
resp , err := client . UpdateChannelRoles ( context . Background ( ) , channel . Id , th . BasicUser . Id , ChannelMember )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckForbiddenStatus ( t , resp )
// User 2 cannot promote self
2023-06-06 17:29:29 -04:00
resp , err = client . UpdateChannelRoles ( context . Background ( ) , channel . Id , th . BasicUser2 . Id , ChannelAdmin )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckForbiddenStatus ( t , resp )
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2017-02-20 11:31:52 -05:00
// User 1 demotes self
2023-06-06 17:29:29 -04:00
_ , err = client . UpdateChannelRoles ( context . Background ( ) , channel . Id , th . BasicUser . Id , ChannelMember )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
// System Admin promotes User 1
2023-06-06 17:29:29 -04:00
_ , err = th . SystemAdminClient . UpdateChannelRoles ( context . Background ( ) , channel . Id , th . BasicUser . Id , ChannelAdmin )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
// System Admin demotes User 1
2023-06-06 17:29:29 -04:00
_ , err = th . SystemAdminClient . UpdateChannelRoles ( context . Background ( ) , channel . Id , th . BasicUser . Id , ChannelMember )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
// System Admin promotes User 1
2023-06-06 17:29:29 -04:00
_ , err = th . SystemAdminClient . UpdateChannelRoles ( context . Background ( ) , channel . Id , th . BasicUser . Id , ChannelAdmin )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2017-02-20 11:31:52 -05:00
2023-06-06 17:29:29 -04:00
resp , err = client . UpdateChannelRoles ( context . Background ( ) , channel . Id , th . BasicUser . Id , "junk" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . UpdateChannelRoles ( context . Background ( ) , channel . Id , "junk" , ChannelMember )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . UpdateChannelRoles ( context . Background ( ) , "junk" , th . BasicUser . Id , ChannelMember )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . UpdateChannelRoles ( context . Background ( ) , channel . Id , model . NewId ( ) , ChannelMember )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . UpdateChannelRoles ( context . Background ( ) , model . NewId ( ) , th . BasicUser . Id , ChannelMember )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckForbiddenStatus ( t , resp )
}
2018-06-05 07:41:03 -04:00
func TestUpdateChannelMemberSchemeRoles ( 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-03-07 10:44:43 -05:00
enableGuestAccounts := * th . App . Config ( ) . GuestAccountsSettings . Enable
defer func ( ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = enableGuestAccounts } )
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2024-03-07 10:44:43 -05:00
} ( )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = true } )
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
id := model . NewId ( )
guest := & model . User {
Email : th . GenerateTestEmail ( ) ,
Nickname : "nn_" + id ,
FirstName : "f_" + id ,
LastName : "l_" + id ,
2026-04-08 15:49:43 -04:00
Password : model . NewTestPassword ( ) ,
2024-03-07 10:44:43 -05:00
EmailVerified : true ,
}
guest , appError := th . App . CreateGuest ( th . Context , guest )
require . Nil ( t , appError )
_ , _ , appError = th . App . AddUserToTeam ( th . Context , th . BasicTeam . Id , guest . Id , "" )
2025-11-12 07:00:51 -05:00
th . AddUserToChannel ( t , guest , th . BasicChannel )
2024-03-07 10:44:43 -05:00
require . Nil ( t , appError )
2018-06-05 07:41:03 -04:00
SystemAdminClient := th . SystemAdminClient
2025-01-29 08:58:43 -05:00
WebSocketClient := th . CreateConnectedWebSocketClient ( t )
2019-12-19 08:53:29 -05:00
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
2018-06-05 07:41:03 -04:00
2025-06-30 17:43:59 -04:00
// cannot change the user scheme to false
2018-06-05 07:41:03 -04:00
s1 := & model . SchemeRoles {
SchemeAdmin : false ,
SchemeUser : false ,
2019-04-30 14:36:21 -04:00
SchemeGuest : false ,
2018-06-05 07:41:03 -04:00
}
2025-01-29 08:58:43 -05:00
_ , err := SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , s1 )
2025-06-30 17:43:59 -04:00
require . Error ( t , err )
tm1 , _ , err := SystemAdminClient . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , "" )
require . NoError ( t , err )
assert . Equal ( t , false , tm1 . SchemeGuest )
assert . Equal ( t , true , tm1 . SchemeUser )
assert . Equal ( t , false , tm1 . SchemeAdmin )
s2 := & model . SchemeRoles {
SchemeAdmin : false ,
SchemeUser : true ,
SchemeGuest : false ,
}
_ , err = SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , s2 )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-06-05 07:41:03 -04:00
2019-12-19 08:53:29 -05:00
waiting := true
for waiting {
select {
case event := <- WebSocketClient . EventChannel :
2021-07-13 10:35:02 -04:00
if event . EventType ( ) == model . WebsocketEventChannelMemberUpdated {
require . Equal ( t , model . WebsocketEventChannelMemberUpdated , event . EventType ( ) )
2019-12-19 08:53:29 -05:00
waiting = false
}
2024-04-05 09:58:49 -04:00
case <- time . After ( 2 * time . Second ) :
2019-12-19 08:53:29 -05:00
require . Fail ( t , "Should have received event channel member websocket event and not timedout" )
waiting = false
}
}
2023-06-06 17:29:29 -04:00
tm2 , _ , err := SystemAdminClient . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-04-30 14:36:21 -04:00
assert . Equal ( t , false , tm2 . SchemeGuest )
2018-06-05 07:41:03 -04:00
assert . Equal ( t , true , tm2 . SchemeUser )
assert . Equal ( t , false , tm2 . SchemeAdmin )
2025-01-29 08:58:43 -05:00
// cannot set Guest to User for single channel
2024-03-07 10:44:43 -05:00
resp , err := SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , th . BasicChannel . Id , guest . Id , s2 )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
2018-06-05 07:41:03 -04:00
s3 := & model . SchemeRoles {
SchemeAdmin : true ,
2025-06-30 17:43:59 -04:00
SchemeUser : true ,
2019-04-30 14:36:21 -04:00
SchemeGuest : false ,
2018-06-05 07:41:03 -04:00
}
2023-06-06 17:29:29 -04:00
_ , err = SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , s3 )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-06-05 07:41:03 -04:00
2023-06-06 17:29:29 -04:00
tm3 , _ , err := SystemAdminClient . GetChannelMember ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-04-30 14:36:21 -04:00
assert . Equal ( t , false , tm3 . SchemeGuest )
2025-06-30 17:43:59 -04:00
assert . Equal ( t , true , tm3 . SchemeUser )
2018-06-05 07:41:03 -04:00
assert . Equal ( t , true , tm3 . SchemeAdmin )
s4 := & model . SchemeRoles {
2019-04-30 14:36:21 -04:00
SchemeAdmin : false ,
SchemeUser : false ,
SchemeGuest : true ,
}
2024-03-07 10:44:43 -05:00
// cannot set user to guest for a single channel
2025-06-30 17:43:59 -04:00
resp , err = SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , s4 )
2024-03-07 10:44:43 -05:00
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
2019-04-30 14:36:21 -04:00
2025-06-30 17:43:59 -04:00
s5 := & model . SchemeRoles {
2019-04-30 14:36:21 -04:00
SchemeAdmin : false ,
SchemeUser : true ,
SchemeGuest : true ,
}
2025-06-30 17:43:59 -04:00
resp , err = SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , s5 )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2019-04-30 14:36:21 -04:00
CheckBadRequestStatus ( t , resp )
2025-06-30 17:43:59 -04:00
resp , err = SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , model . NewId ( ) , th . BasicUser . Id , s3 )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-06-05 07:41:03 -04:00
CheckForbiddenStatus ( t , resp )
2025-06-30 17:43:59 -04:00
resp , err = SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , th . BasicChannel . Id , model . NewId ( ) , s3 )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-06-05 07:41:03 -04:00
CheckNotFoundStatus ( t , resp )
2025-06-30 17:43:59 -04:00
resp , err = SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , "ASDF" , th . BasicUser . Id , s3 )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-06-05 07:41:03 -04:00
CheckBadRequestStatus ( t , resp )
2025-06-30 17:43:59 -04:00
resp , err = SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , th . BasicChannel . Id , "ASDF" , s3 )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-06-05 07:41:03 -04:00
CheckBadRequestStatus ( t , resp )
2025-11-12 07:00:51 -05:00
th . LoginBasic2 ( t )
2025-06-30 17:43:59 -04:00
resp , err = th . Client . UpdateChannelMemberSchemeRoles ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , s3 )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-06-05 07:41:03 -04:00
CheckForbiddenStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = SystemAdminClient . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
resp , err = SystemAdminClient . UpdateChannelMemberSchemeRoles ( context . Background ( ) , th . BasicChannel . Id , th . SystemAdminUser . Id , s4 )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-06-05 07:41:03 -04:00
CheckUnauthorizedStatus ( t , resp )
}
2026-02-05 07:43:50 -05:00
func TestUpdateChannelMemberAutotranslation ( t * testing . T ) {
mainHelper . Parallel ( t )
th := Setup ( t ) . InitBasic ( t )
client := th . Client
mockAutotranslation := & einterfacesmocks . AutoTranslationInterface { }
mockAutotranslation . On ( "IsFeatureAvailable" ) . Return ( true )
mockAutotranslation . On ( "IsChannelEnabled" , mock . Anything ) . Return ( true , nil )
mockAutotranslation . On ( "Translate" , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil , nil )
originalAutoTranslation := th . Server . AutoTranslation
th . Server . AutoTranslation = mockAutotranslation
defer func ( ) {
th . Server . AutoTranslation = originalAutoTranslation
} ( )
channel := th . CreatePublicChannel ( t )
_ , appErr := th . App . AddUserToChannel ( th . Context , th . BasicUser2 , channel , false )
require . Nil ( t , appErr )
t . Run ( "user can disable own autotranslation" , func ( t * testing . T ) {
_ , err := client . UpdateChannelMemberAutotranslation ( context . Background ( ) , channel . Id , th . BasicUser . Id , true )
require . NoError ( t , err )
member , _ , err := client . GetChannelMember ( context . Background ( ) , channel . Id , th . BasicUser . Id , "" )
require . NoError ( t , err )
require . True ( t , member . AutoTranslationDisabled , "autotranslation should be disabled" )
} )
t . Run ( "user can enable own autotranslation" , func ( t * testing . T ) {
_ , err := client . UpdateChannelMemberAutotranslation ( context . Background ( ) , channel . Id , th . BasicUser . Id , false )
require . NoError ( t , err )
member , _ , err := client . GetChannelMember ( context . Background ( ) , channel . Id , th . BasicUser . Id , "" )
require . NoError ( t , err )
require . False ( t , member . AutoTranslationDisabled , "autotranslation should be enabled" )
} )
t . Run ( "user cannot update other user autotranslation without permission" , func ( t * testing . T ) {
resp , err := client . UpdateChannelMemberAutotranslation ( context . Background ( ) , channel . Id , th . BasicUser2 . Id , true )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
member , _ , err := client . GetChannelMember ( context . Background ( ) , channel . Id , th . BasicUser2 . Id , "" )
require . NoError ( t , err )
require . False ( t , member . AutoTranslationDisabled , "autotranslation should remain enabled when update is forbidden" )
} )
t . Run ( "user with PermissionEditOtherUsers can update other user autotranslation" , func ( t * testing . T ) {
_ , err := th . SystemAdminClient . UpdateChannelMemberAutotranslation ( context . Background ( ) , channel . Id , th . BasicUser2 . Id , true )
require . NoError ( t , err )
member , _ , err := th . SystemAdminClient . GetChannelMember ( context . Background ( ) , channel . Id , th . BasicUser2 . Id , "" )
require . NoError ( t , err )
require . True ( t , member . AutoTranslationDisabled , "autotranslation should be disabled" )
} )
t . Run ( "feature is disabled returns forbidden response" , func ( t * testing . T ) {
// Use a dedicated mock so IsFeatureAvailable returns false. The handler returns
// before calling IsChannelEnabled/Translate, so we only need this expectation.
featureDisabledMock := & einterfacesmocks . AutoTranslationInterface { }
featureDisabledMock . On ( "IsFeatureAvailable" ) . Return ( false )
th . Server . AutoTranslation = featureDisabledMock
defer func ( ) { th . Server . AutoTranslation = mockAutotranslation } ( )
resp , err := client . UpdateChannelMemberAutotranslation ( context . Background ( ) , channel . Id , th . BasicUser . Id , true )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
} )
t . Run ( "channel autotranslation is disabled returns bad request" , func ( t * testing . T ) {
// Use a dedicated mock so IsChannelEnabled returns false.
channelDisabledMock := & einterfacesmocks . AutoTranslationInterface { }
channelDisabledMock . On ( "IsFeatureAvailable" ) . Return ( true )
channelDisabledMock . On ( "IsChannelEnabled" , channel . Id ) . Return ( false , nil )
th . Server . AutoTranslation = channelDisabledMock
defer func ( ) { th . Server . AutoTranslation = mockAutotranslation } ( )
resp , err := client . UpdateChannelMemberAutotranslation ( context . Background ( ) , channel . Id , th . BasicUser . Id , true )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
t . Run ( "invalid channel id returns bad request" , func ( t * testing . T ) {
resp , err := client . UpdateChannelMemberAutotranslation ( context . Background ( ) , "junk" , th . BasicUser . Id , true )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
t . Run ( "invalid user id returns bad request" , func ( t * testing . T ) {
resp , err := client . UpdateChannelMemberAutotranslation ( context . Background ( ) , channel . Id , "junk" , true )
require . Error ( t , err )
CheckBadRequestStatus ( t , resp )
} )
t . Run ( "nonexistent channel returns not found" , func ( t * testing . T ) {
resp , err := client . UpdateChannelMemberAutotranslation ( context . Background ( ) , model . NewId ( ) , th . BasicUser . Id , true )
require . Error ( t , err )
CheckNotFoundStatus ( t , resp )
} )
t . Run ( "nonexistent user returns not found" , func ( t * testing . T ) {
// Use SystemAdminClient so permission check passes and we get the "member not found" error from the app
resp , err := th . SystemAdminClient . UpdateChannelMemberAutotranslation ( context . Background ( ) , channel . Id , model . NewId ( ) , true )
require . Error ( t , err )
CheckNotFoundStatus ( t , resp )
} )
t . Run ( "unauthorized when not logged in" , func ( t * testing . T ) {
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
defer th . LoginBasic ( t )
resp , err := client . UpdateChannelMemberAutotranslation ( context . Background ( ) , channel . Id , th . BasicUser . Id , true )
require . Error ( t , err )
CheckUnauthorizedStatus ( t , resp )
} )
}
2017-03-31 09:55:37 -04:00
func TestUpdateChannelNotifyProps ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-31 09:55:37 -04:00
props := map [ string ] string { }
2021-07-12 14:05:36 -04:00
props [ model . DesktopNotifyProp ] = model . ChannelNotifyMention
props [ model . MarkUnreadNotifyProp ] = model . ChannelMarkUnreadMention
2017-03-31 09:55:37 -04:00
2023-06-06 17:29:29 -04:00
_ , err := client . UpdateChannelNotifyProps ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , props )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-31 09:55:37 -04:00
2022-07-14 05:01:29 -04:00
member , appErr := th . App . GetChannelMember ( th . Context , th . BasicChannel . Id , th . BasicUser . Id )
2021-08-13 07:12:16 -04:00
require . Nil ( t , appErr )
2021-07-12 14:05:36 -04:00
require . Equal ( t , model . ChannelNotifyMention , member . NotifyProps [ model . DesktopNotifyProp ] , "bad update" )
require . Equal ( t , model . ChannelMarkUnreadMention , member . NotifyProps [ model . MarkUnreadNotifyProp ] , "bad update" )
2017-03-31 09:55:37 -04:00
2023-06-06 17:29:29 -04:00
resp , err := client . UpdateChannelNotifyProps ( context . Background ( ) , "junk" , th . BasicUser . Id , props )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-31 09:55:37 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . UpdateChannelNotifyProps ( context . Background ( ) , th . BasicChannel . Id , "junk" , props )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-31 09:55:37 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . UpdateChannelNotifyProps ( context . Background ( ) , model . NewId ( ) , th . BasicUser . Id , props )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-31 09:55:37 -04:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . UpdateChannelNotifyProps ( context . Background ( ) , th . BasicChannel . Id , model . NewId ( ) , props )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-31 09:55:37 -04:00
CheckForbiddenStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , err = client . UpdateChannelNotifyProps ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , map [ string ] string { } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-31 09:55:37 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
resp , err = client . UpdateChannelNotifyProps ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , props )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-31 09:55:37 -04:00
CheckUnauthorizedStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , err = th . SystemAdminClient . UpdateChannelNotifyProps ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id , props )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-31 09:55:37 -04:00
}
2017-03-20 08:44:08 -04:00
func TestAddChannelMember ( 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-08-13 07:12:16 -04:00
client := th . Client
2017-03-20 08:44:08 -04:00
user := th . BasicUser
user2 := th . BasicUser2
2017-04-03 13:13:28 -04:00
team := th . BasicTeam
2025-11-12 07:00:51 -05:00
publicChannel := th . CreatePublicChannel ( t )
privateChannel := th . CreatePrivateChannel ( t )
2017-03-20 08:44:08 -04:00
2025-11-12 07:00:51 -05:00
user3 := th . CreateUserWithClient ( t , th . SystemAdminClient )
2023-06-06 17:29:29 -04:00
_ , _ , err := th . SystemAdminClient . AddTeamMember ( context . Background ( ) , team . Id , user3 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-04-03 13:13:28 -04:00
2023-06-06 17:29:29 -04:00
cm , resp , err := client . AddChannelMember ( context . Background ( ) , publicChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-31 09:56:20 -04:00
CheckCreatedStatus ( t , resp )
2019-11-13 13:53:57 -05:00
require . Equal ( t , publicChannel . Id , cm . ChannelId , "should have returned exact channel" )
require . Equal ( t , user2 . Id , cm . UserId , "should have returned exact user added to public channel" )
2017-03-20 08:44:08 -04:00
2023-06-06 17:29:29 -04:00
cm , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Equal ( t , privateChannel . Id , cm . ChannelId , "should have returned exact channel" )
require . Equal ( t , user2 . Id , cm . UserId , "should have returned exact user added to private channel" )
2017-03-20 08:44:08 -04:00
2023-12-20 00:46:54 -05:00
post := & model . Post { ChannelId : publicChannel . Id , Message : "a" + GenerateTestID ( ) + "a" }
2023-06-06 17:29:29 -04:00
rpost , _ , err := client . CreatePost ( context . Background ( ) , post )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-10-31 15:48:58 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , publicChannel . Id , user . Id )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMemberWithRootId ( context . Background ( ) , publicChannel . Id , user . Id , rpost . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-10-31 15:48:58 -04:00
CheckCreatedStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , publicChannel . Id , user . Id )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMemberWithRootId ( context . Background ( ) , publicChannel . Id , user . Id , "junk" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-10-31 15:48:58 -04:00
CheckBadRequestStatus ( t , resp )
2023-12-20 00:46:54 -05:00
_ , resp , err = client . AddChannelMemberWithRootId ( context . Background ( ) , publicChannel . Id , user . Id , GenerateTestID ( ) )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-10-31 15:48:58 -04:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , publicChannel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-22 11:13:44 -04:00
2023-06-06 17:29:29 -04:00
cm , resp , err = client . AddChannelMember ( context . Background ( ) , publicChannel . Id , "junk" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-20 08:44:08 -04:00
CheckBadRequestStatus ( t , resp )
2019-11-13 13:53:57 -05:00
require . Nil ( t , cm , "should return nothing" )
2017-03-20 08:44:08 -04:00
2023-12-20 00:46:54 -05:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , publicChannel . Id , GenerateTestID ( ) )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-20 08:44:08 -04:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , "junk" , user2 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-20 08:44:08 -04:00
CheckBadRequestStatus ( t , resp )
2023-12-20 00:46:54 -05:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , GenerateTestID ( ) , user2 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-20 08:44:08 -04:00
CheckNotFoundStatus ( t , resp )
2025-11-12 07:00:51 -05:00
otherUser := th . CreateUser ( t )
otherChannel := th . CreatePublicChannel ( t )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user2 . Email , user2 . Password )
require . NoError ( t , err )
2017-03-20 08:44:08 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , publicChannel . Id , otherUser . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2025-06-19 08:13:45 -04:00
CheckNotFoundStatus ( t , resp )
2017-03-20 08:44:08 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , otherUser . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2025-06-19 08:13:45 -04:00
CheckNotFoundStatus ( t , resp )
2017-03-20 08:44:08 -04:00
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , otherChannel . Id , otherUser . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2025-06-19 08:13:45 -04:00
CheckForbiddenStatus ( t , resp )
2017-03-20 08:44:08 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
_ , _ , err = client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2017-03-20 08:44:08 -04:00
// should fail adding user who is not a member of the team
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , otherChannel . Id , otherUser . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2025-06-19 08:13:45 -04:00
CheckNotFoundStatus ( t , resp )
2017-03-20 08:44:08 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . DeleteChannel ( context . Background ( ) , otherChannel . Id )
require . NoError ( t , err )
2017-03-20 08:44:08 -04:00
2025-06-19 08:13:45 -04:00
// Adding user to a deleted channel is fine
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , otherChannel . Id , user2 . Id )
2025-06-19 08:13:45 -04:00
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
2017-03-20 08:44:08 -04:00
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , publicChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-20 08:44:08 -04:00
CheckUnauthorizedStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-20 08:44:08 -04:00
CheckUnauthorizedStatus ( t , resp )
2020-06-03 05:20:52 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , publicChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-03-20 08:44:08 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-03 05:20:52 -04:00
} )
2017-04-03 13:13:28 -04:00
2018-02-06 10:34:08 -05:00
// Check the appropriate permissions are enforced.
2025-11-12 07:00:51 -05:00
defaultRolePermissions := th . SaveDefaultRolePermissions ( t )
2017-04-03 13:13:28 -04:00
defer func ( ) {
2025-11-12 07:00:51 -05:00
th . RestoreDefaultRolePermissions ( t , defaultRolePermissions )
2017-04-03 13:13:28 -04:00
} ( )
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionManagePrivateChannelMembers . Id , model . ChannelUserRoleId )
2017-04-03 13:13:28 -04:00
// Check that a regular channel user can add other users.
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user2 . Username , user2 . Password )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
privateChannel = th . CreatePrivateChannel ( t )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2017-04-03 13:13:28 -04:00
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user . Username , user . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user3 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2017-04-03 13:13:28 -04:00
2018-02-06 10:34:08 -05:00
// Restrict the permission for adding users to Channel Admins
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionManagePrivateChannelMembers . Id , model . ChannelAdminRoleId )
th . RemovePermissionFromRole ( t , model . PermissionManagePrivateChannelMembers . Id , model . ChannelUserRoleId )
2017-04-03 13:13:28 -04:00
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user2 . Username , user2 . Password )
require . NoError ( t , err )
2025-11-12 07:00:51 -05:00
privateChannel = th . CreatePrivateChannel ( t )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2017-04-03 13:13:28 -04:00
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user . Username , user . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , resp , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user3 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-04-03 13:13:28 -04:00
CheckForbiddenStatus ( t , resp )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2017-04-03 13:13:28 -04:00
2025-11-12 07:00:51 -05:00
th . MakeUserChannelAdmin ( t , user , privateChannel )
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . InvalidateAllCaches ( )
require . Nil ( t , appErr )
2017-04-03 13:13:28 -04:00
2025-06-19 08:13:45 -04:00
_ , _ , err = client . Login ( context . Background ( ) , user . Username , user . Password )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user3 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2025-06-19 08:13:45 -04:00
_ , err = client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2019-04-09 07:09:57 -04:00
// Set a channel to group-constrained
2024-08-05 23:45:00 -04:00
privateChannel . GroupConstrained = model . NewPointer ( true )
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . UpdateChannel ( th . Context , privateChannel )
2019-04-09 07:09:57 -04:00
require . Nil ( t , appErr )
2020-06-03 05:20:52 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
// User is not in associated groups so shouldn't be allowed
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user . Id )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.channel.add_members.user_denied" )
2020-06-03 05:20:52 -04:00
} )
2019-04-09 07:09:57 -04:00
// Associate group to team
2019-12-18 08:54:09 -05:00
_ , appErr = th . App . UpsertGroupSyncable ( & model . GroupSyncable {
2019-04-09 07:09:57 -04:00
GroupId : th . Group . Id ,
SyncableId : privateChannel . Id ,
Type : model . GroupSyncableTypeChannel ,
} )
require . Nil ( t , appErr )
// Add user to group
2019-06-28 07:31:32 -04:00
_ , appErr = th . App . UpsertGroupMember ( th . Group . Id , user . Id )
2019-04-09 07:09:57 -04:00
require . Nil ( t , appErr )
2020-06-03 05:20:52 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-03 05:20:52 -04:00
} )
2024-10-28 14:52:23 -04:00
2025-09-17 06:20:58 -04:00
t . Run ( "requester is not a member of the team and tries to add a user to a channel where it is already a member" , func ( t * testing . T ) {
// Create two teams using SystemAdminClient
2025-11-12 07:00:51 -05:00
t1 := th . CreateTeamWithClient ( t , th . SystemAdminClient )
t2 := th . CreateTeamWithClient ( t , th . SystemAdminClient )
2025-09-17 06:20:58 -04:00
// Use existing users - user will be BasicUser, user2 will be BasicUser2
u1 := th . BasicUser
u2 := th . BasicUser2
// Add user1 to team1 and user2 to team2 (they're already on BasicTeam)
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , u1 , t1 )
th . LinkUserToTeam ( t , u2 , t2 )
2025-09-17 06:20:58 -04:00
// Create a public channel in team1
2025-11-12 07:00:51 -05:00
pubChannel := th . CreateChannelWithClientAndTeam ( t , th . SystemAdminClient , model . ChannelTypeOpen , t1 . Id )
2025-09-17 06:20:58 -04:00
// Add user1 to the public channel
2025-11-12 07:00:51 -05:00
th . AddUserToChannel ( t , u1 , pubChannel )
2025-09-17 06:20:58 -04:00
// Create client for user2
client2 := th . CreateClient ( )
_ , _ , err := client2 . Login ( context . Background ( ) , u2 . Email , u2 . Password )
require . NoError ( t , err )
// Try to add user1 to the public channel using user2's credentials
// This should fail with 403 since user2 is not a member of the team
_ , resp , err := client2 . AddChannelMember ( context . Background ( ) , pubChannel . Id , u1 . Id )
CheckForbiddenStatus ( t , resp )
require . Error ( t , err )
} )
2024-10-28 14:52:23 -04:00
t . Run ( "invalid request data" , func ( t * testing . T ) {
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
// correct type for user ids (string) but invalid value.
requestBody := map [ string ] any { "user_ids" : [ ] string { "invalid" , user2 . Id } }
requestData , err := json . Marshal ( requestBody )
require . NoError ( t , err )
res , err := client . DoAPIPost ( context . Background ( ) , "/channels/" + publicChannel . Id + "/members" , string ( requestData ) )
if client == th . LocalClient {
require . EqualError ( t , err , "Invalid or missing user_id in request body." )
} else {
require . EqualError ( t , err , "Invalid or missing user_id in user_ids in request body." )
}
2025-06-19 08:13:45 -04:00
require . Equal ( t , http . StatusBadRequest , res . StatusCode )
2024-10-28 14:52:23 -04:00
// invalid type for user ids (should be string).
requestBody = map [ string ] any { "user_ids" : [ ] any { 45 , user2 . Id } }
requestData , err = json . Marshal ( requestBody )
require . NoError ( t , err )
res , err = client . DoAPIPost ( context . Background ( ) , "/channels/" + privateChannel . Id + "/members" , string ( requestData ) )
if client == th . LocalClient {
require . EqualError ( t , err , "Invalid or missing user_id in request body." )
} else {
require . EqualError ( t , err , "Invalid or missing user_id in user_ids in request body." )
}
2025-06-19 08:13:45 -04:00
require . Equal ( t , http . StatusBadRequest , res . StatusCode )
2024-10-28 14:52:23 -04:00
} )
} )
2017-03-20 08:44:08 -04:00
}
2024-06-25 14:25:28 -04:00
func TestAddChannelMembers ( 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-06-25 14:25:28 -04:00
client := th . Client
user := th . BasicUser
user2 := th . BasicUser2
team := th . BasicTeam
2025-11-12 07:00:51 -05:00
publicChannel := th . CreatePublicChannel ( t )
privateChannel := th . CreatePrivateChannel ( t )
2024-06-25 14:25:28 -04:00
2025-11-12 07:00:51 -05:00
user3 := th . CreateUserWithClient ( t , th . SystemAdminClient )
2024-06-25 14:25:28 -04:00
_ , _ , err := th . SystemAdminClient . AddTeamMember ( context . Background ( ) , team . Id , user3 . Id )
require . NoError ( t , err )
cm , resp , err := client . AddChannelMembers ( context . Background ( ) , publicChannel . Id , "" , [ ] string { user . Id , user2 . Id , user3 . Id } )
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
require . Equal ( t , publicChannel . Id , cm [ 0 ] . ChannelId , "should have returned exact channel" )
require . Equal ( t , user . Id , cm [ 0 ] . UserId , "should have returned exact user added to public channel" )
require . Equal ( t , user2 . Id , cm [ 1 ] . UserId , "should have returned exact user added to public channel" )
require . Equal ( t , user3 . Id , cm [ 2 ] . UserId , "should have returned exact user added to public channel" )
cm , _ , err = client . AddChannelMembers ( context . Background ( ) , privateChannel . Id , "" , [ ] string { user . Id , user2 . Id , user3 . Id } )
require . NoError ( t , err )
require . Equal ( t , privateChannel . Id , cm [ 0 ] . ChannelId , "should have returned exact channel" )
require . Equal ( t , user . Id , cm [ 0 ] . UserId , "should have returned exact user added to public channel" )
require . Equal ( t , user2 . Id , cm [ 1 ] . UserId , "should have returned exact user added to public channel" )
require . Equal ( t , user3 . Id , cm [ 2 ] . UserId , "should have returned exact user added to public channel" )
}
2022-01-25 14:44:18 -05:00
func TestAddChannelMemberFromThread ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2022-01-26 03:38:55 -05:00
t . Skip ( "MM-41285" )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2022-01-25 14:44:18 -05:00
team := th . BasicTeam
user := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
user3 := th . CreateUserWithClient ( t , th . SystemAdminClient )
2023-06-06 17:29:29 -04:00
_ , _ , err := th . SystemAdminClient . AddTeamMember ( context . Background ( ) , team . Id , user3 . Id )
2022-01-25 14:44:18 -05:00
require . NoError ( t , err )
2025-01-29 08:58:43 -05:00
wsClient := th . CreateConnectedWebSocketClient ( t )
2022-06-08 14:01:58 -04:00
2025-11-12 07:00:51 -05:00
publicChannel := th . CreatePublicChannel ( t )
2022-01-25 14:44:18 -05:00
2023-06-06 17:29:29 -04:00
_ , resp , err := th . Client . AddChannelMember ( context . Background ( ) , publicChannel . Id , user3 . Id )
2022-01-25 14:44:18 -05:00
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
2023-06-06 17:29:29 -04:00
_ , resp , err = th . Client . AddChannelMember ( context . Background ( ) , publicChannel . Id , user2 . Id )
2022-01-25 14:44:18 -05:00
require . NoError ( t , err )
CheckCreatedStatus ( t , resp )
post := & model . Post {
ChannelId : publicChannel . Id ,
Message : "A root post" ,
2022-06-08 14:01:58 -04:00
UserId : user3 . Id ,
2022-01-25 14:44:18 -05:00
}
2023-06-06 17:29:29 -04:00
rpost , _ , err := th . SystemAdminClient . CreatePost ( context . Background ( ) , post )
2022-01-25 14:44:18 -05:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . CreatePost ( context . Background ( ) ,
2022-01-25 14:44:18 -05:00
& model . Post {
ChannelId : publicChannel . Id ,
2022-06-08 14:01:58 -04:00
Message : "A reply post with mention @" + user . Username ,
2022-01-25 14:44:18 -05:00
UserId : user2 . Id ,
RootId : rpost . Id ,
} )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . CreatePost ( context . Background ( ) ,
2022-01-25 14:44:18 -05:00
& model . Post {
ChannelId : publicChannel . Id ,
2022-06-08 14:01:58 -04:00
Message : "Another reply post with mention @" + user . Username ,
2022-01-25 14:44:18 -05:00
UserId : user2 . Id ,
RootId : rpost . Id ,
} )
require . NoError ( t , err )
// Simulate adding a user to a channel from a thread
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . AddChannelMemberWithRootId ( context . Background ( ) , publicChannel . Id , user . Id , rpost . Id )
2022-01-25 14:44:18 -05:00
require . NoError ( t , err )
// Threadmembership should exist for added user
2023-06-06 17:29:29 -04:00
ut , _ , err := th . Client . GetUserThread ( context . Background ( ) , user . Id , team . Id , rpost . Id , false )
2022-01-25 14:44:18 -05:00
require . NoError ( t , err )
// Should have two mentions. There might be a race condition
// here between the "added user to the channel" message and the GetUserThread call
require . LessOrEqual ( t , int64 ( 2 ) , ut . UnreadMentions )
2022-06-08 14:01:58 -04:00
var caught bool
func ( ) {
for {
select {
case ev := <- wsClient . EventChannel :
if ev . EventType ( ) == model . WebsocketEventThreadUpdated {
caught = true
var thread model . ThreadResponse
data := ev . GetData ( )
jsonErr := json . Unmarshal ( [ ] byte ( data [ "thread" ] . ( string ) ) , & thread )
require . NoError ( t , jsonErr )
require . EqualValues ( t , int64 ( 2 ) , thread . UnreadReplies )
require . EqualValues ( t , int64 ( 2 ) , thread . UnreadMentions )
require . EqualValues ( t , float64 ( 0 ) , data [ "previous_unread_replies" ] )
require . EqualValues ( t , float64 ( 0 ) , data [ "previous_unread_mentions" ] )
}
2024-04-05 09:58:49 -04:00
case <- time . After ( 2 * time . Second ) :
2022-06-08 14:01:58 -04:00
return
}
}
} ( )
require . Truef ( t , caught , "User should have received %s event" , model . WebsocketEventThreadUpdated )
2022-01-25 14:44:18 -05:00
}
2025-04-14 07:07:50 -04:00
func TestAddChannelMemberGuestAccessControl ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-04-14 07:07:50 -04:00
// Enable guest accounts and add license
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . GuestAccountsSettings . Enable = true
} )
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
// Create a guest user
2025-04-22 09:12:22 -04:00
guest , guestClient := th . CreateGuestAndClient ( t )
2025-04-14 07:07:50 -04:00
// Create a public channel to which the guest doesn't belong
2025-11-12 07:00:51 -05:00
publicChannel := th . CreatePublicChannel ( t )
2025-04-14 07:07:50 -04:00
// Try to add another user to the channel using the guest's client
// This should fail with a permission error, validating our fix
_ , resp , err := guestClient . AddChannelMember ( context . Background ( ) , publicChannel . Id , th . BasicUser2 . Id )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
// Also verify that using user IDs in the request body doesn't bypass the check
_ , resp , err = guestClient . AddChannelMembers ( context . Background ( ) , publicChannel . Id , "" , [ ] string { th . BasicUser2 . Id } )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
// Verify that the guest can get channel members for channels they belong to
2025-11-12 07:00:51 -05:00
channelWithGuest := th . CreatePublicChannel ( t )
th . AddUserToChannel ( t , guest , channelWithGuest )
2025-04-14 07:07:50 -04:00
// Guest should be able to read members of channels they belong to
members , _ , err := guestClient . GetChannelMembers ( context . Background ( ) , channelWithGuest . Id , 0 , 100 , "" )
require . NoError ( t , err )
require . NotEmpty ( t , members )
}
2019-04-08 05:10:16 -04:00
func TestAddChannelMemberAddMyself ( 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-08-13 07:12:16 -04:00
client := th . Client
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
th . LinkUserToTeam ( t , user , th . BasicTeam )
notMemberPublicChannel1 := th . CreatePublicChannel ( t )
notMemberPublicChannel2 := th . CreatePublicChannel ( t )
notMemberPrivateChannel := th . CreatePrivateChannel ( t )
2019-04-08 05:10:16 -04:00
2025-11-12 07:00:51 -05:00
memberPublicChannel := th . CreatePublicChannel ( t )
memberPrivateChannel := th . CreatePrivateChannel ( t )
th . AddUserToChannel ( t , user , memberPublicChannel )
th . AddUserToChannel ( t , user , memberPrivateChannel )
2019-04-08 05:10:16 -04:00
testCases := [ ] struct {
Name string
Channel * model . Channel
WithJoinPublicPermission bool
ExpectedError string
} {
{
2021-07-12 14:05:36 -04:00
"Add myself to a public channel with JoinPublicChannel permission" ,
2019-04-08 05:10:16 -04:00
notMemberPublicChannel1 ,
true ,
"" ,
} ,
{
2021-07-12 14:05:36 -04:00
"Try to add myself to a private channel with the JoinPublicChannel permission" ,
2019-04-08 05:10:16 -04:00
notMemberPrivateChannel ,
true ,
"api.context.permissions.app_error" ,
} ,
{
2021-07-12 14:05:36 -04:00
"Try to add myself to a public channel without the JoinPublicChannel permission" ,
2019-04-08 05:10:16 -04:00
notMemberPublicChannel2 ,
false ,
"api.context.permissions.app_error" ,
} ,
{
2021-07-12 14:05:36 -04:00
"Add myself a public channel where I'm already a member, not having JoinPublicChannel or ManageMembers permission" ,
2019-04-08 05:10:16 -04:00
memberPublicChannel ,
false ,
"" ,
} ,
{
2021-07-12 14:05:36 -04:00
"Add myself a private channel where I'm already a member, not having JoinPublicChannel or ManageMembers permission" ,
2019-04-08 05:10:16 -04:00
memberPrivateChannel ,
false ,
"" ,
} ,
}
2025-06-19 08:13:45 -04:00
_ , _ , err := client . Login ( context . Background ( ) , user . Email , user . Password )
require . NoError ( t , err )
2019-04-08 05:10:16 -04:00
for _ , tc := range testCases {
t . Run ( tc . Name , func ( t * testing . T ) {
// Check the appropriate permissions are enforced.
2025-11-12 07:00:51 -05:00
defaultRolePermissions := th . SaveDefaultRolePermissions ( t )
2019-04-08 05:10:16 -04:00
defer func ( ) {
2025-11-12 07:00:51 -05:00
th . RestoreDefaultRolePermissions ( t , defaultRolePermissions )
2019-04-08 05:10:16 -04:00
} ( )
if ! tc . WithJoinPublicPermission {
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionJoinPublicChannels . Id , model . TeamUserRoleId )
2019-04-08 05:10:16 -04:00
}
2023-06-06 17:29:29 -04:00
_ , _ , err := client . AddChannelMember ( context . Background ( ) , tc . Channel . Id , user . Id )
2019-04-08 05:10:16 -04:00
if tc . ExpectedError == "" {
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-04-08 05:10:16 -04:00
} else {
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , tc . ExpectedError )
2019-04-08 05:10:16 -04:00
}
} )
}
}
2017-02-20 11:31:52 -05:00
func TestRemoveChannelMember ( 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-04-03 13:13:28 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
team := th . BasicTeam
2021-08-13 07:12:16 -04:00
client := th . Client
2017-02-20 11:31:52 -05:00
2020-01-29 11:01:06 -05:00
th . App . UpdateConfig ( func ( cfg * model . Config ) {
* cfg . ServiceSettings . EnableBotAccountCreation = true
} )
2025-11-12 07:00:51 -05:00
bot := th . CreateBotWithSystemAdminClient ( t )
2025-06-19 08:13:45 -04:00
_ , _ , appErr := th . App . AddUserToTeam ( th . Context , team . Id , bot . UserId , "" )
require . Nil ( t , appErr )
2020-01-29 11:01:06 -05:00
2023-06-06 17:29:29 -04:00
_ , err := client . RemoveUserFromChannel ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
2023-06-06 17:29:29 -04:00
resp , err := client . RemoveUserFromChannel ( context . Background ( ) , th . BasicChannel . Id , "junk" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . RemoveUserFromChannel ( context . Background ( ) , th . BasicChannel . Id , model . NewId ( ) )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckNotFoundStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . RemoveUserFromChannel ( context . Background ( ) , model . NewId ( ) , th . BasicUser2 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-22 11:13:44 -04:00
CheckNotFoundStatus ( t , resp )
2025-11-12 07:00:51 -05:00
th . LoginBasic2 ( t )
2023-06-06 17:29:29 -04:00
resp , err = client . RemoveUserFromChannel ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-02-20 11:31:52 -05:00
CheckForbiddenStatus ( t , resp )
2019-10-21 13:56:08 -04:00
t . Run ( "success" , func ( t * testing . T ) {
// Setup the system administrator to listen for websocket events from the channels.
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , th . SystemAdminUser , th . BasicTeam )
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . AddUserToChannel ( th . Context , th . SystemAdminUser , th . BasicChannel , false )
2021-08-13 07:12:16 -04:00
require . Nil ( t , appErr )
2022-07-14 05:01:29 -04:00
_ , appErr = th . App . AddUserToChannel ( th . Context , th . SystemAdminUser , th . BasicChannel2 , false )
2021-08-13 07:12:16 -04:00
require . Nil ( t , appErr )
2019-10-21 13:56:08 -04:00
props := map [ string ] string { }
2021-07-12 14:05:36 -04:00
props [ model . DesktopNotifyProp ] = model . ChannelNotifyAll
2023-06-06 17:29:29 -04:00
_ , err = th . SystemAdminClient . UpdateChannelNotifyProps ( context . Background ( ) , th . BasicChannel . Id , th . SystemAdminUser . Id , props )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , err = th . SystemAdminClient . UpdateChannelNotifyProps ( context . Background ( ) , th . BasicChannel2 . Id , th . SystemAdminUser . Id , props )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
2025-01-29 08:58:43 -05:00
wsClient := th . CreateConnectedWebSocketClientWithClient ( t , th . SystemAdminClient )
2019-10-21 13:56:08 -04:00
// requirePost listens for websocket events and tries to find the post matching
// the expected post's channel and message.
requirePost := func ( expectedPost * model . Post ) {
t . Helper ( )
for {
select {
case event := <- wsClient . EventChannel :
2019-12-24 03:32:11 -05:00
postData , ok := event . GetData ( ) [ "post" ]
2019-10-21 13:56:08 -04:00
if ! ok {
continue
}
2021-09-01 08:43:12 -04:00
var post model . Post
2025-06-19 08:13:45 -04:00
err = json . Unmarshal ( [ ] byte ( postData . ( string ) ) , & post )
require . NoError ( t , err )
2019-10-21 13:56:08 -04:00
if post . ChannelId == expectedPost . ChannelId && post . Message == expectedPost . Message {
return
}
case <- time . After ( 5 * time . Second ) :
2019-11-13 13:53:57 -05:00
require . FailNow ( t , "failed to find expected post after 5 seconds" )
2019-10-21 13:56:08 -04:00
return
}
}
}
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . AddUserToChannel ( th . Context , th . BasicUser2 , th . BasicChannel , false )
require . Nil ( t , appErr )
2025-01-29 08:58:43 -05:00
_ , err2 := client . RemoveUserFromChannel ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err2 )
2019-10-21 13:56:08 -04:00
requirePost ( & model . Post {
Message : fmt . Sprintf ( "@%s left the channel." , th . BasicUser2 . Username ) ,
ChannelId : th . BasicChannel . Id ,
} )
2023-06-06 17:29:29 -04:00
_ , err2 = client . RemoveUserFromChannel ( context . Background ( ) , th . BasicChannel2 . Id , th . BasicUser . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err2 )
2019-10-21 13:56:08 -04:00
requirePost ( & model . Post {
Message : fmt . Sprintf ( "@%s removed from the channel." , th . BasicUser . Username ) ,
ChannelId : th . BasicChannel2 . Id ,
} )
2023-06-06 17:29:29 -04:00
_ , err2 = th . SystemAdminClient . RemoveUserFromChannel ( context . Background ( ) , th . BasicChannel . Id , th . BasicUser . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err2 )
2019-10-21 13:56:08 -04:00
requirePost ( & model . Post {
Message : fmt . Sprintf ( "@%s removed from the channel." , th . BasicUser . Username ) ,
ChannelId : th . BasicChannel . Id ,
} )
} )
2017-02-20 11:31:52 -05:00
2018-08-03 04:44:32 -04:00
// Leave deleted channel
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
deletedChannel := th . CreatePublicChannel ( t )
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . AddUserToChannel ( th . Context , th . BasicUser , deletedChannel , false )
require . Nil ( t , appErr )
_ , appErr = th . App . AddUserToChannel ( th . Context , th . BasicUser2 , deletedChannel , false )
require . Nil ( t , appErr )
2018-08-03 04:44:32 -04:00
2025-06-19 08:13:45 -04:00
appErr = th . App . DeleteChannel ( th . Context , deletedChannel , "" )
require . Nil ( t , appErr )
2018-08-03 04:44:32 -04:00
2023-06-06 17:29:29 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , deletedChannel . Id , th . BasicUser . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-08-03 04:44:32 -04:00
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
private := th . CreatePrivateChannel ( t )
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . AddUserToChannel ( th . Context , th . BasicUser2 , private , false )
require . Nil ( t , appErr )
2017-02-20 11:31:52 -05:00
2023-06-06 17:29:29 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , private . Id , th . BasicUser2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-02-20 11:31:52 -05:00
2025-11-12 07:00:51 -05:00
th . LoginBasic2 ( t )
2023-06-06 17:29:29 -04:00
resp , err = client . RemoveUserFromChannel ( context . Background ( ) , private . Id , th . BasicUser . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-03-22 11:13:44 -04:00
CheckForbiddenStatus ( t , resp )
2020-06-03 05:20:52 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . AddUserToChannel ( th . Context , th . BasicUser , private , false )
require . Nil ( t , appErr )
2023-06-06 17:29:29 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , private . Id , th . BasicUser . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-03 05:20:52 -04:00
} )
2017-04-03 13:13:28 -04:00
2025-11-12 07:00:51 -05:00
th . LoginBasic ( t )
th . UpdateUserToNonTeamAdmin ( t , user1 , team )
2025-06-19 08:13:45 -04:00
appErr = th . App . Srv ( ) . InvalidateAllCaches ( )
require . Nil ( t , appErr )
2017-04-03 13:13:28 -04:00
2018-02-06 10:34:08 -05:00
// Check the appropriate permissions are enforced.
2025-11-12 07:00:51 -05:00
defaultRolePermissions := th . SaveDefaultRolePermissions ( t )
2017-04-03 13:13:28 -04:00
defer func ( ) {
2025-11-12 07:00:51 -05:00
th . RestoreDefaultRolePermissions ( t , defaultRolePermissions )
2017-04-03 13:13:28 -04:00
} ( )
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionManagePrivateChannelMembers . Id , model . ChannelUserRoleId )
2017-04-03 13:13:28 -04:00
2020-06-03 05:20:52 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
// Check that a regular channel user can remove other users.
2025-11-12 07:00:51 -05:00
privateChannel := th . CreateChannelWithClient ( t , client , model . ChannelTypePrivate )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user1 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2017-04-03 13:13:28 -04:00
2023-06-06 17:29:29 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , privateChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-03 05:20:52 -04:00
} )
2017-04-03 13:13:28 -04:00
2018-02-06 10:34:08 -05:00
// Restrict the permission for adding users to Channel Admins
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , model . PermissionManagePrivateChannelMembers . Id , model . ChannelAdminRoleId )
th . RemovePermissionFromRole ( t , model . PermissionManagePrivateChannelMembers . Id , model . ChannelUserRoleId )
2017-04-03 13:13:28 -04:00
2025-11-12 07:00:51 -05:00
privateChannel := th . CreateChannelWithClient ( t , th . SystemAdminClient , model . ChannelTypePrivate )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . AddChannelMember ( context . Background ( ) , privateChannel . Id , user1 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . AddChannelMember ( context . Background ( ) , privateChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . AddChannelMember ( context . Background ( ) , privateChannel . Id , bot . UserId )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
resp , err = client . RemoveUserFromChannel ( context . Background ( ) , privateChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2017-04-03 13:13:28 -04:00
CheckForbiddenStatus ( t , resp )
2025-11-12 07:00:51 -05:00
th . MakeUserChannelAdmin ( t , user1 , privateChannel )
2025-06-19 08:13:45 -04:00
appErr = th . App . Srv ( ) . InvalidateAllCaches ( )
require . Nil ( t , appErr )
2017-04-03 13:13:28 -04:00
2023-06-06 17:29:29 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , privateChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-08-21 08:53:32 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . AddChannelMember ( context . Background ( ) , privateChannel . Id , th . SystemAdminUser . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-04-12 10:15:46 -04:00
// If the channel is group-constrained the user cannot be removed
2024-08-05 23:45:00 -04:00
privateChannel . GroupConstrained = model . NewPointer ( true )
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . UpdateChannel ( th . Context , privateChannel )
2021-08-13 07:12:16 -04:00
require . Nil ( t , appErr )
2023-06-06 17:29:29 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , privateChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.channel.remove_member.group_constrained.app_error" )
2019-04-12 10:15:46 -04:00
// If the channel is group-constrained user can remove self
2023-06-06 17:29:29 -04:00
_ , err = th . SystemAdminClient . RemoveUserFromChannel ( context . Background ( ) , privateChannel . Id , th . SystemAdminUser . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-04-12 10:15:46 -04:00
2018-08-21 08:53:32 -04:00
// Test on preventing removal of user from a direct channel
2023-06-06 17:29:29 -04:00
directChannel , _ , err := client . CreateDirectChannel ( context . Background ( ) , user1 . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-08-21 08:53:32 -04:00
2020-01-29 11:01:06 -05:00
// If the channel is group-constrained a user can remove a bot
2023-06-06 17:29:29 -04:00
_ , err = client . RemoveUserFromChannel ( context . Background ( ) , privateChannel . Id , bot . UserId )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-01-29 11:01:06 -05:00
2023-06-06 17:29:29 -04:00
resp , err = client . RemoveUserFromChannel ( context . Background ( ) , directChannel . Id , user1 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-08-21 08:53:32 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = client . RemoveUserFromChannel ( context . Background ( ) , directChannel . Id , user2 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-08-21 08:53:32 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = th . SystemAdminClient . RemoveUserFromChannel ( context . Background ( ) , directChannel . Id , user1 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-08-21 08:53:32 -04:00
CheckBadRequestStatus ( t , resp )
// Test on preventing removal of user from a group channel
2025-11-12 07:00:51 -05:00
user3 := th . CreateUser ( t )
2023-06-06 17:29:29 -04:00
groupChannel , _ , err := client . CreateGroupChannel ( context . Background ( ) , [ ] string { user1 . Id , user2 . Id , user3 . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-08-21 08:53:32 -04:00
2020-06-03 05:20:52 -04:00
th . TestForAllClients ( t , func ( t * testing . T , client * model . Client4 ) {
2023-06-06 17:29:29 -04:00
resp , err = client . RemoveUserFromChannel ( context . Background ( ) , groupChannel . Id , user1 . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2020-06-03 05:20:52 -04:00
CheckBadRequestStatus ( t , resp )
} )
2017-02-20 11:31:52 -05:00
}
2018-03-26 15:41:06 -04:00
func TestAutocompleteChannels ( 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-03-26 15:41:06 -04:00
2021-10-26 02:00:59 -04:00
// A private channel to make sure private channels are used.
2025-06-19 08:13:45 -04:00
ptown , _ , err := th . Client . CreateChannel ( context . Background ( ) , & model . Channel {
2018-03-26 15:41:06 -04:00
DisplayName : "Town" ,
Name : "town" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypePrivate ,
2018-03-26 15:41:06 -04:00
TeamId : th . BasicTeam . Id ,
} )
2025-06-19 08:13:45 -04:00
require . NoError ( t , err )
tower , _ , err := th . Client . CreateChannel ( context . Background ( ) , & model . Channel {
2020-02-06 09:15:18 -05:00
DisplayName : "Tower" ,
Name : "tower" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypeOpen ,
2020-02-06 09:15:18 -05:00
TeamId : th . BasicTeam . Id ,
} )
2025-06-19 08:13:45 -04:00
require . NoError ( t , err )
2018-03-26 15:41:06 -04:00
defer func ( ) {
2025-06-19 08:13:45 -04:00
_ , err = th . Client . DeleteChannel ( context . Background ( ) , ptown . Id )
require . NoError ( t , err )
_ , err = th . Client . DeleteChannel ( context . Background ( ) , tower . Id )
require . NoError ( t , err )
2018-03-26 15:41:06 -04:00
} ( )
for _ , tc := range [ ] struct {
description string
teamId string
fragment string
expectedIncludes [ ] string
expectedExcludes [ ] string
} {
{
"Basic town-square" ,
th . BasicTeam . Id ,
"town" ,
2021-10-26 02:00:59 -04:00
[ ] string { "town-square" , "town" } ,
[ ] string { "off-topic" , "tower" } ,
2018-03-26 15:41:06 -04:00
} ,
{
"Basic off-topic" ,
th . BasicTeam . Id ,
"off-to" ,
[ ] string { "off-topic" } ,
2020-02-06 09:15:18 -05:00
[ ] string { "town-square" , "town" , "tower" } ,
2018-03-26 15:41:06 -04:00
} ,
{
"Basic town square and off topic" ,
th . BasicTeam . Id ,
2020-02-06 09:15:18 -05:00
"tow" ,
2021-10-26 02:00:59 -04:00
[ ] string { "town-square" , "tower" , "town" } ,
[ ] string { "off-topic" } ,
2018-03-26 15:41:06 -04:00
} ,
} {
2018-09-27 10:15:41 -04:00
t . Run ( tc . description , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
channels , _ , err := th . Client . AutocompleteChannelsForTeam ( context . Background ( ) , tc . teamId , tc . fragment )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
names := make ( [ ] string , len ( channels ) )
for i , c := range channels {
2019-11-13 13:53:57 -05:00
names [ i ] = c . Name
2018-09-27 10:15:41 -04:00
}
2019-11-13 13:53:57 -05:00
for _ , name := range tc . expectedIncludes {
require . Contains ( t , names , name , "channel not included" )
2018-03-26 15:41:06 -04:00
}
2019-11-13 13:53:57 -05:00
for _ , name := range tc . expectedExcludes {
require . NotContains ( t , names , name , "channel not excluded" )
2018-03-26 15:41:06 -04:00
}
2018-09-27 10:15:41 -04:00
} )
}
}
func TestAutocompleteChannelsForSearch ( 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-09-27 10:15:41 -04:00
2025-11-12 07:00:51 -05:00
th . LoginSystemAdminWithClient ( t , th . SystemAdminClient )
th . LoginBasicWithClient ( t , th . Client )
2018-09-27 10:15:41 -04:00
2025-11-12 07:00:51 -05:00
u1 := th . CreateUserWithClient ( t , th . SystemAdminClient )
2025-06-19 08:13:45 -04:00
defer func ( ) {
appErr := th . App . PermanentDeleteUser ( th . Context , u1 )
require . Nil ( t , appErr )
} ( )
2025-11-12 07:00:51 -05:00
u2 := th . CreateUserWithClient ( t , th . SystemAdminClient )
2025-06-19 08:13:45 -04:00
defer func ( ) {
appErr := th . App . PermanentDeleteUser ( th . Context , u2 )
require . Nil ( t , appErr )
} ( )
2025-11-12 07:00:51 -05:00
u3 := th . CreateUserWithClient ( t , th . SystemAdminClient )
2025-06-19 08:13:45 -04:00
defer func ( ) {
appErr := th . App . PermanentDeleteUser ( th . Context , u3 )
require . Nil ( t , appErr )
} ( )
2025-11-12 07:00:51 -05:00
u4 := th . CreateUserWithClient ( t , th . SystemAdminClient )
2025-06-19 08:13:45 -04:00
defer func ( ) {
appErr := th . App . PermanentDeleteUser ( th . Context , u4 )
require . Nil ( t , appErr )
} ( )
2018-09-27 10:15:41 -04:00
// A private channel to make sure private channels are not used
2025-06-19 08:13:45 -04:00
ptown , _ , err := th . SystemAdminClient . CreateChannel ( context . Background ( ) , & model . Channel {
2018-09-27 10:15:41 -04:00
DisplayName : "Town" ,
Name : "town" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypePrivate ,
2018-09-27 10:15:41 -04:00
TeamId : th . BasicTeam . Id ,
} )
2025-06-19 08:13:45 -04:00
require . NoError ( t , err )
2018-09-27 10:15:41 -04:00
defer func ( ) {
2025-06-19 08:13:45 -04:00
_ , err = th . SystemAdminClient . DeleteChannel ( context . Background ( ) , ptown . Id )
require . NoError ( t , err )
2018-09-27 10:15:41 -04:00
} ( )
2025-06-19 08:13:45 -04:00
mypriv , _ , err := th . Client . CreateChannel ( context . Background ( ) , & model . Channel {
2018-09-27 10:15:41 -04:00
DisplayName : "My private town" ,
Name : "townpriv" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypePrivate ,
2018-09-27 10:15:41 -04:00
TeamId : th . BasicTeam . Id ,
} )
2025-06-19 08:13:45 -04:00
require . NoError ( t , err )
2018-09-27 10:15:41 -04:00
defer func ( ) {
2025-06-19 08:13:45 -04:00
_ , err = th . SystemAdminClient . DeleteChannel ( context . Background ( ) , mypriv . Id )
require . NoError ( t , err )
2018-09-27 10:15:41 -04:00
} ( )
2023-06-06 17:29:29 -04:00
dc1 , _ , err := th . Client . CreateDirectChannel ( context . Background ( ) , th . BasicUser . Id , u1 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
dc2 , _ , err := th . SystemAdminClient . CreateDirectChannel ( context . Background ( ) , u2 . Id , u3 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-09-27 10:15:41 -04:00
2023-06-06 17:29:29 -04:00
gc1 , _ , err := th . Client . CreateGroupChannel ( context . Background ( ) , [ ] string { th . BasicUser . Id , u2 . Id , u3 . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
gc2 , _ , err := th . SystemAdminClient . CreateGroupChannel ( context . Background ( ) , [ ] string { u2 . Id , u3 . Id , u4 . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-09-27 10:15:41 -04:00
for _ , tc := range [ ] struct {
description string
2020-04-20 05:21:51 -04:00
teamID string
2018-09-27 10:15:41 -04:00
fragment string
expectedIncludes [ ] string
expectedExcludes [ ] string
} {
{
"Basic town-square" ,
th . BasicTeam . Id ,
"town" ,
[ ] string { "town-square" , "townpriv" } ,
[ ] string { "off-topic" , "town" } ,
} ,
{
"Basic off-topic" ,
th . BasicTeam . Id ,
"off-to" ,
[ ] string { "off-topic" } ,
[ ] string { "town-square" , "town" , "townpriv" } ,
} ,
{
2020-02-06 09:15:18 -05:00
"Basic town square and townpriv" ,
2018-09-27 10:15:41 -04:00
th . BasicTeam . Id ,
2020-02-06 09:15:18 -05:00
"tow" ,
[ ] string { "town-square" , "townpriv" } ,
[ ] string { "off-topic" , "town" } ,
2018-09-27 10:15:41 -04:00
} ,
{
"Direct and group messages" ,
th . BasicTeam . Id ,
"fakeuser" ,
[ ] string { dc1 . Name , gc1 . Name } ,
[ ] string { dc2 . Name , gc2 . Name } ,
} ,
} {
t . Run ( tc . description , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
channels , _ , err := th . Client . AutocompleteChannelsForTeamForSearch ( context . Background ( ) , tc . teamID , tc . fragment )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
names := make ( [ ] string , len ( channels ) )
for i , c := range channels {
2020-04-20 05:21:51 -04:00
names [ i ] = c . Name
}
for _ , name := range tc . expectedIncludes {
require . Contains ( t , names , name , "channel not included" )
}
for _ , name := range tc . expectedExcludes {
require . NotContains ( t , names , name , "channel not excluded" )
}
} )
}
}
func TestAutocompleteChannelsForSearchGuestUsers ( 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-20 05:21:51 -04:00
2025-11-12 07:00:51 -05:00
u1 := th . CreateUserWithClient ( t , th . SystemAdminClient )
2025-06-19 08:13:45 -04:00
defer func ( ) {
appErr := th . App . PermanentDeleteUser ( th . Context , u1 )
require . Nil ( t , appErr )
} ( )
2020-04-20 05:21:51 -04:00
enableGuestAccounts := * th . App . Config ( ) . GuestAccountsSettings . Enable
defer func ( ) {
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = enableGuestAccounts } )
2025-06-19 08:13:45 -04:00
appErr := th . App . Srv ( ) . RemoveLicense ( )
require . Nil ( t , appErr )
2020-04-20 05:21:51 -04:00
} ( )
th . App . UpdateConfig ( func ( cfg * model . Config ) { * cfg . GuestAccountsSettings . Enable = true } )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2020-04-20 05:21:51 -04:00
id := model . NewId ( )
2026-04-08 15:49:43 -04:00
guestPassword := model . NewTestPassword ( )
2020-04-20 05:21:51 -04:00
guest := & model . User {
Email : "success+" + id + "@simulator.amazonses.com" ,
Username : "un_" + id ,
Nickname : "nn_" + id ,
2026-04-08 15:49:43 -04:00
Password : guestPassword ,
2020-04-20 05:21:51 -04:00
EmailVerified : true ,
}
2021-08-13 07:12:16 -04:00
guest , appErr := th . App . CreateGuest ( th . Context , guest )
require . Nil ( t , appErr )
2020-04-20 05:21:51 -04:00
2025-11-12 07:00:51 -05:00
th . LoginSystemAdminWithClient ( t , th . SystemAdminClient )
2020-04-20 05:21:51 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err := th . SystemAdminClient . AddTeamMember ( context . Background ( ) , th . BasicTeam . Id , guest . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-20 05:21:51 -04:00
// A private channel to make sure private channels are not used
2025-06-19 08:13:45 -04:00
town , _ , err := th . SystemAdminClient . CreateChannel ( context . Background ( ) , & model . Channel {
2020-04-20 05:21:51 -04:00
DisplayName : "Town" ,
Name : "town" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypeOpen ,
2020-04-20 05:21:51 -04:00
TeamId : th . BasicTeam . Id ,
} )
2025-06-19 08:13:45 -04:00
require . NoError ( t , err )
2020-04-20 05:21:51 -04:00
defer func ( ) {
2025-06-19 08:13:45 -04:00
_ , err = th . SystemAdminClient . DeleteChannel ( context . Background ( ) , town . Id )
require . NoError ( t , err )
2020-04-20 05:21:51 -04:00
} ( )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . AddChannelMember ( context . Background ( ) , town . Id , guest . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-20 05:21:51 -04:00
2025-06-19 08:13:45 -04:00
mypriv , _ , err := th . SystemAdminClient . CreateChannel ( context . Background ( ) , & model . Channel {
2020-04-20 05:21:51 -04:00
DisplayName : "My private town" ,
Name : "townpriv" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypePrivate ,
2020-04-20 05:21:51 -04:00
TeamId : th . BasicTeam . Id ,
} )
2025-06-19 08:13:45 -04:00
require . NoError ( t , err )
2020-04-20 05:21:51 -04:00
defer func ( ) {
2025-06-19 08:13:45 -04:00
_ , err = th . SystemAdminClient . DeleteChannel ( context . Background ( ) , mypriv . Id )
require . NoError ( t , err )
2020-04-20 05:21:51 -04:00
} ( )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . AddChannelMember ( context . Background ( ) , mypriv . Id , guest . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-20 05:21:51 -04:00
2023-06-06 17:29:29 -04:00
dc1 , _ , err := th . SystemAdminClient . CreateDirectChannel ( context . Background ( ) , th . BasicUser . Id , guest . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
dc2 , _ , err := th . SystemAdminClient . CreateDirectChannel ( context . Background ( ) , th . BasicUser . Id , th . BasicUser2 . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-20 05:21:51 -04:00
2023-06-06 17:29:29 -04:00
gc1 , _ , err := th . SystemAdminClient . CreateGroupChannel ( context . Background ( ) , [ ] string { th . BasicUser . Id , th . BasicUser2 . Id , guest . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
gc2 , _ , err := th . SystemAdminClient . CreateGroupChannel ( context . Background ( ) , [ ] string { th . BasicUser . Id , th . BasicUser2 . Id , u1 . Id } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-20 05:21:51 -04:00
2026-04-08 15:49:43 -04:00
_ , _ , err = th . Client . Login ( context . Background ( ) , guest . Username , guestPassword )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-20 05:21:51 -04:00
for _ , tc := range [ ] struct {
description string
teamID string
fragment string
expectedIncludes [ ] string
expectedExcludes [ ] string
} {
{
"Should return those channel where is member" ,
th . BasicTeam . Id ,
"town" ,
[ ] string { "town" , "townpriv" } ,
[ ] string { "town-square" , "off-topic" } ,
} ,
{
"Should return empty if not member of the searched channels" ,
th . BasicTeam . Id ,
"off-to" ,
[ ] string { } ,
[ ] string { "off-topic" , "town-square" , "town" , "townpriv" } ,
} ,
{
"Should return direct and group messages" ,
th . BasicTeam . Id ,
"fakeuser" ,
[ ] string { dc1 . Name , gc1 . Name } ,
[ ] string { dc2 . Name , gc2 . Name } ,
} ,
} {
t . Run ( tc . description , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
channels , _ , err := th . Client . AutocompleteChannelsForTeamForSearch ( context . Background ( ) , tc . teamID , tc . fragment )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-08-17 05:18:33 -04:00
names := make ( [ ] string , len ( channels ) )
for i , c := range channels {
2019-11-13 13:53:57 -05:00
names [ i ] = c . Name
2018-09-27 10:15:41 -04:00
}
2019-11-13 13:53:57 -05:00
for _ , name := range tc . expectedIncludes {
require . Contains ( t , names , name , "channel not included" )
2018-09-27 10:15:41 -04:00
}
2019-11-13 13:53:57 -05:00
for _ , name := range tc . expectedExcludes {
require . NotContains ( t , names , name , "channel not excluded" )
2018-09-27 10:15:41 -04:00
}
} )
2018-03-26 15:41:06 -04:00
}
}
2018-05-02 07:31:14 -04:00
func TestUpdateChannelScheme ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2020-07-22 04:20:33 -04:00
th := Setup ( t )
2018-05-02 07:31:14 -04:00
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( "" ) )
2018-05-02 07:31:14 -04:00
2025-06-19 08:13:45 -04:00
err := th . App . SetPhase2PermissionsMigrationStatus ( true )
require . NoError ( t , err )
2018-05-14 10:59:04 -04:00
2023-06-06 17:29:29 -04:00
team , _ , err := th . SystemAdminClient . CreateTeam ( context . Background ( ) , & model . Team {
2018-05-02 07:31:14 -04:00
DisplayName : "Name" ,
Description : "Some description" ,
CompanyName : "Some company name" ,
AllowOpenInvite : false ,
InviteId : "inviteid0" ,
Name : "z-z-" + model . NewId ( ) + "a" ,
Email : "success+" + model . NewId ( ) + "@simulator.amazonses.com" ,
2021-07-12 14:05:36 -04:00
Type : model . TeamOpen ,
2019-01-24 15:19:32 -05:00
} )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-05-02 07:31:14 -04:00
2023-06-06 17:29:29 -04:00
channel , _ , err := th . SystemAdminClient . CreateChannel ( context . Background ( ) , & model . Channel {
2018-05-02 07:31:14 -04:00
DisplayName : "Name" ,
Name : "z-z-" + model . NewId ( ) + "a" ,
2021-07-12 14:05:36 -04:00
Type : model . ChannelTypeOpen ,
2018-05-02 07:31:14 -04:00
TeamId : team . Id ,
2019-01-24 15:19:32 -05:00
} )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-05-02 07:31:14 -04:00
2023-06-06 17:29:29 -04:00
channelScheme , _ , err := th . SystemAdminClient . CreateScheme ( context . Background ( ) , & model . Scheme {
2018-05-17 11:37:00 -04:00
DisplayName : "DisplayName" ,
Name : model . NewId ( ) ,
2018-05-02 07:31:14 -04:00
Description : "Some description" ,
2021-07-12 14:05:36 -04:00
Scope : model . SchemeScopeChannel ,
2019-01-24 15:19:32 -05:00
} )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-01-24 15:19:32 -05:00
2023-06-06 17:29:29 -04:00
teamScheme , _ , err := th . SystemAdminClient . CreateScheme ( context . Background ( ) , & model . Scheme {
2018-05-17 11:37:00 -04:00
DisplayName : "DisplayName" ,
Name : model . NewId ( ) ,
2018-05-02 07:31:14 -04:00
Description : "Some description" ,
2021-07-12 14:05:36 -04:00
Scope : model . SchemeScopeTeam ,
2019-01-24 15:19:32 -05:00
} )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-05-02 07:31:14 -04:00
// Test the setup/base case.
2023-06-06 17:29:29 -04:00
_ , err = th . SystemAdminClient . UpdateChannelScheme ( context . Background ( ) , channel . Id , channelScheme . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-05-02 07:31:14 -04:00
// Test various invalid channel and scheme id combinations.
2023-06-06 17:29:29 -04:00
resp , err := th . SystemAdminClient . UpdateChannelScheme ( context . Background ( ) , channel . Id , "x" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-05-02 07:31:14 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = th . SystemAdminClient . UpdateChannelScheme ( context . Background ( ) , "x" , channelScheme . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-05-02 07:31:14 -04:00
CheckBadRequestStatus ( t , resp )
2023-06-06 17:29:29 -04:00
resp , err = th . SystemAdminClient . UpdateChannelScheme ( context . Background ( ) , "x" , "x" )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-05-02 07:31:14 -04:00
CheckBadRequestStatus ( t , resp )
// Test that permissions are required.
2023-06-06 17:29:29 -04:00
resp , err = th . Client . UpdateChannelScheme ( context . Background ( ) , channel . Id , channelScheme . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-05-02 07:31:14 -04:00
CheckForbiddenStatus ( t , resp )
2020-03-02 11:10:41 -05:00
// Test that a license is required.
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( nil )
2023-06-06 17:29:29 -04:00
resp , err = th . SystemAdminClient . UpdateChannelScheme ( context . Background ( ) , channel . Id , channelScheme . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2022-08-08 10:32:49 -04:00
CheckForbiddenStatus ( t , resp )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( "" ) )
2018-05-02 07:31:14 -04:00
// Test an invalid scheme scope.
2023-06-06 17:29:29 -04:00
resp , err = th . SystemAdminClient . UpdateChannelScheme ( context . Background ( ) , channel . Id , teamScheme . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-05-02 07:31:14 -04:00
CheckBadRequestStatus ( t , resp )
// Test that an unauthenticated user gets rejected.
2025-06-19 08:13:45 -04:00
_ , err = th . SystemAdminClient . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-06-06 17:29:29 -04:00
resp , err = th . SystemAdminClient . UpdateChannelScheme ( context . Background ( ) , channel . Id , channelScheme . Id )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
2018-05-02 07:31:14 -04:00
CheckUnauthorizedStatus ( t , resp )
}
2018-10-13 06:35:57 -04:00
func TestGetChannelMembersTimezones ( 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-08-13 07:12:16 -04:00
client := th . Client
2018-10-13 06:35:57 -04:00
user := th . BasicUser
user . Timezone [ "useAutomaticTimezone" ] = "false"
user . Timezone [ "manualTimezone" ] = "XOXO/BLABLA"
2023-06-06 17:29:29 -04:00
_ , _ , err := client . UpdateUser ( context . Background ( ) , user )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-10-13 06:35:57 -04:00
user2 := th . BasicUser2
user2 . Timezone [ "automaticTimezone" ] = "NoWhere/Island"
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . UpdateUser ( context . Background ( ) , user2 )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-10-13 06:35:57 -04:00
2023-06-06 17:29:29 -04:00
timezone , _ , err := client . GetChannelMembersTimezones ( context . Background ( ) , th . BasicChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Len ( t , timezone , 2 , "should return 2 timezones" )
2018-10-13 06:35:57 -04:00
2025-01-29 08:58:43 -05:00
// both users have same timezone
2018-10-13 06:35:57 -04:00
user2 . Timezone [ "automaticTimezone" ] = "XOXO/BLABLA"
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . UpdateUser ( context . Background ( ) , user2 )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-10-13 06:35:57 -04:00
2023-06-06 17:29:29 -04:00
timezone , _ , err = client . GetChannelMembersTimezones ( context . Background ( ) , th . BasicChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-11-13 13:53:57 -05:00
require . Len ( t , timezone , 1 , "should return 1 timezone" )
2018-10-13 06:35:57 -04:00
2025-01-29 08:58:43 -05:00
// no timezone set should return empty
2018-10-13 06:35:57 -04:00
user2 . Timezone [ "automaticTimezone" ] = ""
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . UpdateUser ( context . Background ( ) , user2 )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-10-13 06:35:57 -04:00
user . Timezone [ "manualTimezone" ] = ""
2023-06-06 17:29:29 -04:00
_ , _ , err = client . UpdateUser ( context . Background ( ) , user )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2018-10-13 06:35:57 -04:00
2023-06-06 17:29:29 -04:00
timezone , _ , err = client . GetChannelMembersTimezones ( context . Background ( ) , th . BasicChannel . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-12-22 06:35:31 -05:00
require . Empty ( t , timezone , "should return 0 timezone" )
2018-10-13 06:35:57 -04:00
}
2019-06-17 11:04:27 -04:00
func TestChannelMembersMinusGroupMembers ( 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-06-17 11:04:27 -04:00
user1 := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreatePrivateChannel ( t )
2019-06-17 11:04:27 -04:00
2021-08-13 07:12:16 -04:00
_ , appErr := th . App . AddChannelMember ( th . Context , user1 . Id , channel , app . ChannelMemberOpts { } )
require . Nil ( t , appErr )
_ , appErr = th . App . AddChannelMember ( th . Context , user2 . Id , channel , app . ChannelMemberOpts { } )
require . Nil ( t , appErr )
2019-06-17 11:04:27 -04:00
2024-08-05 23:45:00 -04:00
channel . GroupConstrained = model . NewPointer ( true )
2022-07-14 05:01:29 -04:00
channel , appErr = th . App . UpdateChannel ( th . Context , channel )
2021-08-13 07:12:16 -04:00
require . Nil ( t , appErr )
2019-06-17 11:04:27 -04:00
2025-11-12 07:00:51 -05:00
group1 := th . CreateGroup ( t )
group2 := th . CreateGroup ( t )
2019-06-17 11:04:27 -04:00
2021-08-13 07:12:16 -04:00
_ , appErr = th . App . UpsertGroupMember ( group1 . Id , user1 . Id )
require . Nil ( t , appErr )
_ , appErr = th . App . UpsertGroupMember ( group2 . Id , user2 . Id )
require . Nil ( t , appErr )
2019-06-17 11:04:27 -04:00
// No permissions
2023-06-06 17:29:29 -04:00
_ , _ , _ , err := th . Client . ChannelMembersMinusGroupMembers ( context . Background ( ) , channel . Id , [ ] string { group1 . Id , group2 . Id } , 0 , 100 , "" )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.context.permissions.app_error" )
2019-06-17 11:04:27 -04:00
testCases := map [ string ] struct {
groupIDs [ ] string
page int
perPage int
length int
count int
otherAssertions func ( [ ] * model . UserWithGroups )
} {
"All groups, expect no users removed" : {
groupIDs : [ ] string { group1 . Id , group2 . Id } ,
page : 0 ,
perPage : 100 ,
length : 0 ,
count : 0 ,
} ,
"Some nonexistent group, page 0" : {
groupIDs : [ ] string { model . NewId ( ) } ,
page : 0 ,
perPage : 1 ,
length : 1 ,
count : 2 ,
} ,
"Some nonexistent group, page 1" : {
groupIDs : [ ] string { model . NewId ( ) } ,
page : 1 ,
perPage : 1 ,
length : 1 ,
count : 2 ,
} ,
"One group, expect one user removed" : {
groupIDs : [ ] string { group1 . Id } ,
page : 0 ,
perPage : 100 ,
length : 1 ,
count : 1 ,
otherAssertions : func ( uwg [ ] * model . UserWithGroups ) {
require . Equal ( t , uwg [ 0 ] . Id , user2 . Id )
} ,
} ,
"Other group, expect other user removed" : {
groupIDs : [ ] string { group2 . Id } ,
page : 0 ,
perPage : 100 ,
length : 1 ,
count : 1 ,
otherAssertions : func ( uwg [ ] * model . UserWithGroups ) {
require . Equal ( t , uwg [ 0 ] . Id , user1 . Id )
} ,
} ,
}
for name , tc := range testCases {
t . Run ( name , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
uwg , count , _ , err := th . SystemAdminClient . ChannelMembersMinusGroupMembers ( context . Background ( ) , channel . Id , tc . groupIDs , tc . page , tc . perPage , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2019-06-17 11:04:27 -04:00
require . Len ( t , uwg , tc . length )
require . Equal ( t , tc . count , int ( count ) )
if tc . otherAssertions != nil {
tc . otherAssertions ( uwg )
}
} )
}
}
2020-03-05 10:04:34 -05:00
func TestGetChannelModerations ( 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-05 10:04:34 -05:00
channel := th . BasicChannel
team := th . BasicTeam
2025-06-19 08:13:45 -04:00
err := th . App . SetPhase2PermissionsMigrationStatus ( true )
require . NoError ( t , err )
2020-03-05 10:04:34 -05:00
t . Run ( "Errors without a license" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , _ , err := th . SystemAdminClient . GetChannelModerations ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.channel.get_channel_moderations.license.error" )
2020-03-05 10:04:34 -05:00
} )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2020-03-05 10:04:34 -05:00
t . Run ( "Errors as a non sysadmin" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , _ , err := th . Client . GetChannelModerations ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.context.permissions.app_error" )
2020-03-05 10:04:34 -05:00
} )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2020-03-05 10:04:34 -05:00
t . Run ( "Returns default moderations with default roles" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
moderations , _ , err := th . SystemAdminClient . GetChannelModerations ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2024-03-12 10:36:05 -04:00
require . Equal ( t , len ( moderations ) , 5 )
2020-03-05 10:04:34 -05:00
for _ , moderation := range moderations {
2024-03-12 10:36:05 -04:00
if moderation . Name == "manage_members" || moderation . Name == "manage_bookmarks" {
2020-03-05 10:04:34 -05:00
require . Empty ( t , moderation . Roles . Guests )
} else {
require . Equal ( t , moderation . Roles . Guests . Value , true )
require . Equal ( t , moderation . Roles . Guests . Enabled , true )
}
require . Equal ( t , moderation . Roles . Members . Value , true )
require . Equal ( t , moderation . Roles . Members . Enabled , true )
}
} )
t . Run ( "Returns value false and enabled false for permissions that are not present in higher scoped scheme when no channel scheme present" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
scheme := th . SetupTeamScheme ( t )
2020-03-05 10:04:34 -05:00
team . SchemeId = & scheme . Id
2021-08-13 07:12:16 -04:00
_ , appErr := th . App . UpdateTeamScheme ( team )
require . Nil ( t , appErr )
2020-03-05 10:04:34 -05:00
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionCreatePost . Id , scheme . DefaultChannelGuestRole )
defer th . AddPermissionToRole ( t , model . PermissionCreatePost . Id , scheme . DefaultChannelGuestRole )
2020-03-05 10:04:34 -05:00
2023-06-06 17:29:29 -04:00
moderations , _ , err := th . SystemAdminClient . GetChannelModerations ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-03-05 10:04:34 -05:00
for _ , moderation := range moderations {
2021-07-12 14:05:36 -04:00
if moderation . Name == model . PermissionCreatePost . Id {
2020-03-05 10:04:34 -05:00
require . Equal ( t , moderation . Roles . Members . Value , true )
require . Equal ( t , moderation . Roles . Members . Enabled , true )
require . Equal ( t , moderation . Roles . Guests . Value , false )
require . Equal ( t , moderation . Roles . Guests . Enabled , false )
}
}
} )
t . Run ( "Returns value false and enabled true for permissions that are not present in channel scheme but present in team scheme" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
scheme := th . SetupChannelScheme ( t )
2020-03-05 10:04:34 -05:00
channel . SchemeId = & scheme . Id
2022-07-14 05:01:29 -04:00
_ , appErr := th . App . UpdateChannelScheme ( th . Context , channel )
2021-08-13 07:12:16 -04:00
require . Nil ( t , appErr )
2020-03-05 10:04:34 -05:00
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionCreatePost . Id , scheme . DefaultChannelGuestRole )
defer th . AddPermissionToRole ( t , model . PermissionCreatePost . Id , scheme . DefaultChannelGuestRole )
2020-03-05 10:04:34 -05:00
2023-06-06 17:29:29 -04:00
moderations , _ , err := th . SystemAdminClient . GetChannelModerations ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-03-05 10:04:34 -05:00
for _ , moderation := range moderations {
2021-07-12 14:05:36 -04:00
if moderation . Name == model . PermissionCreatePost . Id {
2020-03-05 10:04:34 -05:00
require . Equal ( t , moderation . Roles . Members . Value , true )
require . Equal ( t , moderation . Roles . Members . Enabled , true )
require . Equal ( t , moderation . Roles . Guests . Value , false )
require . Equal ( t , moderation . Roles . Guests . Enabled , true )
}
}
} )
t . Run ( "Returns value false and enabled false for permissions that are not present in channel & team scheme" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
teamScheme := th . SetupTeamScheme ( t )
2020-03-05 10:04:34 -05:00
team . SchemeId = & teamScheme . Id
2025-06-19 08:13:45 -04:00
_ , appErr := th . App . UpdateTeamScheme ( team )
require . Nil ( t , appErr )
2020-03-05 10:04:34 -05:00
2025-11-12 07:00:51 -05:00
scheme := th . SetupChannelScheme ( t )
2020-03-05 10:04:34 -05:00
channel . SchemeId = & scheme . Id
2025-06-19 08:13:45 -04:00
_ , appErr = th . App . UpdateChannelScheme ( th . Context , channel )
require . Nil ( t , appErr )
2020-03-05 10:04:34 -05:00
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionCreatePost . Id , scheme . DefaultChannelGuestRole )
th . RemovePermissionFromRole ( t , model . PermissionCreatePost . Id , teamScheme . DefaultChannelGuestRole )
2020-03-05 10:04:34 -05:00
2025-11-12 07:00:51 -05:00
defer th . AddPermissionToRole ( t , model . PermissionCreatePost . Id , scheme . DefaultChannelGuestRole )
defer th . AddPermissionToRole ( t , model . PermissionCreatePost . Id , teamScheme . DefaultChannelGuestRole )
2020-03-05 10:04:34 -05:00
2023-06-06 17:29:29 -04:00
moderations , _ , err := th . SystemAdminClient . GetChannelModerations ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-03-05 10:04:34 -05:00
for _ , moderation := range moderations {
2021-07-12 14:05:36 -04:00
if moderation . Name == model . PermissionCreatePost . Id {
2020-03-05 10:04:34 -05:00
require . Equal ( t , moderation . Roles . Members . Value , true )
require . Equal ( t , moderation . Roles . Members . Enabled , true )
require . Equal ( t , moderation . Roles . Guests . Value , false )
require . Equal ( t , moderation . Roles . Guests . Enabled , false )
}
}
} )
2020-03-23 13:44:20 -04:00
2020-12-01 10:27:05 -05:00
t . Run ( "Returns the correct value for manage_members depending on whether the channel is public or private" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
scheme := th . SetupTeamScheme ( t )
2020-03-23 13:44:20 -04:00
team . SchemeId = & scheme . Id
2021-08-13 07:12:16 -04:00
_ , appErr := th . App . UpdateTeamScheme ( team )
require . Nil ( t , appErr )
2020-03-23 13:44:20 -04:00
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , model . PermissionManagePublicChannelMembers . Id , scheme . DefaultChannelUserRole )
defer th . AddPermissionToRole ( t , model . PermissionManagePublicChannelMembers . Id , scheme . DefaultChannelUserRole )
2020-03-23 13:44:20 -04:00
// public channel does not have the permission
2023-06-06 17:29:29 -04:00
moderations , _ , err := th . SystemAdminClient . GetChannelModerations ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-03-23 13:44:20 -04:00
for _ , moderation := range moderations {
if moderation . Name == "manage_members" {
require . Equal ( t , moderation . Roles . Members . Value , false )
}
}
// private channel does have the permission
2023-06-06 17:29:29 -04:00
moderations , _ , err = th . SystemAdminClient . GetChannelModerations ( context . Background ( ) , th . BasicPrivateChannel . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-03-23 13:44:20 -04:00
for _ , moderation := range moderations {
if moderation . Name == "manage_members" {
require . Equal ( t , moderation . Roles . Members . Value , true )
}
}
} )
2020-04-02 08:09:23 -04:00
2024-03-12 10:36:05 -04:00
t . Run ( "Returns the correct value for manage_bookmarks depending on whether the channel is public or private" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
scheme := th . SetupTeamScheme ( t )
2024-03-12 10:36:05 -04:00
team . SchemeId = & scheme . Id
_ , appErr := th . App . UpdateTeamScheme ( team )
require . Nil ( t , appErr )
bookmarkPublicPermissions := [ ] string {
model . PermissionAddBookmarkPublicChannel . Id ,
model . PermissionEditBookmarkPublicChannel . Id ,
model . PermissionDeleteBookmarkPublicChannel . Id ,
model . PermissionOrderBookmarkPublicChannel . Id ,
}
for _ , p := range bookmarkPublicPermissions {
2025-11-12 07:00:51 -05:00
th . RemovePermissionFromRole ( t , p , scheme . DefaultChannelUserRole )
2024-03-12 10:36:05 -04:00
}
defer func ( ) {
for _ , p := range bookmarkPublicPermissions {
2025-11-12 07:00:51 -05:00
th . AddPermissionToRole ( t , p , scheme . DefaultChannelUserRole )
2024-03-12 10:36:05 -04:00
}
} ( )
// public channel does not have the permissions
moderations , _ , err := th . SystemAdminClient . GetChannelModerations ( context . Background ( ) , channel . Id , "" )
require . NoError ( t , err )
for _ , moderation := range moderations {
if moderation . Name == "manage_bookmarks" {
require . Equal ( t , moderation . Roles . Members . Value , false )
}
}
// private channel does have the permissions
moderations , _ , err = th . SystemAdminClient . GetChannelModerations ( context . Background ( ) , th . BasicPrivateChannel . Id , "" )
require . NoError ( t , err )
for _ , moderation := range moderations {
if moderation . Name == "manage_bookmarks" {
require . Equal ( t , moderation . Roles . Members . Value , true )
}
}
} )
2020-04-02 08:09:23 -04:00
t . Run ( "Does not return an error if the team scheme has a blank DefaultChannelGuestRole field" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
scheme := th . SetupTeamScheme ( t )
2020-04-02 08:09:23 -04:00
scheme . DefaultChannelGuestRole = ""
mockStore := mocks . Store { }
2023-04-18 07:58:33 -04:00
// Playbooks DB job requires a plugin mock
pluginStore := mocks . PluginStore { }
pluginStore . On ( "List" , mock . Anything , mock . Anything , mock . Anything ) . Return ( [ ] string { } , nil )
mockStore . On ( "Plugin" ) . Return ( & pluginStore )
2020-04-02 08:09:23 -04:00
mockSchemeStore := mocks . SchemeStore { }
mockSchemeStore . On ( "Get" , mock . Anything ) . Return ( scheme , nil )
mockStore . On ( "Scheme" ) . Return ( & mockSchemeStore )
2022-10-06 04:04:21 -04:00
mockStore . On ( "Team" ) . Return ( th . App . Srv ( ) . Store ( ) . Team ( ) )
mockStore . On ( "Channel" ) . Return ( th . App . Srv ( ) . Store ( ) . Channel ( ) )
mockStore . On ( "User" ) . Return ( th . App . Srv ( ) . Store ( ) . User ( ) )
mockStore . On ( "Post" ) . Return ( th . App . Srv ( ) . Store ( ) . Post ( ) )
mockStore . On ( "FileInfo" ) . Return ( th . App . Srv ( ) . Store ( ) . FileInfo ( ) )
mockStore . On ( "Webhook" ) . Return ( th . App . Srv ( ) . Store ( ) . Webhook ( ) )
mockStore . On ( "System" ) . Return ( th . App . Srv ( ) . Store ( ) . System ( ) )
mockStore . On ( "License" ) . Return ( th . App . Srv ( ) . Store ( ) . License ( ) )
mockStore . On ( "Role" ) . Return ( th . App . Srv ( ) . Store ( ) . Role ( ) )
2020-04-02 08:09:23 -04:00
mockStore . On ( "Close" ) . Return ( nil )
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . SetStore ( & mockStore )
2020-04-02 08:09:23 -04:00
team . SchemeId = & scheme . Id
2021-08-13 07:12:16 -04:00
_ , appErr := th . App . UpdateTeamScheme ( team )
require . Nil ( t , appErr )
2020-04-02 08:09:23 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err := th . SystemAdminClient . GetChannelModerations ( context . Background ( ) , channel . Id , "" )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-02 08:09:23 -04:00
} )
2020-03-05 10:04:34 -05:00
}
func TestPatchChannelModerations ( 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-05 10:04:34 -05:00
channel := th . BasicChannel
emptyPatch := [ ] * model . ChannelModerationPatch { }
2020-08-21 16:49:31 -04:00
createPosts := model . ChannelModeratedPermissions [ 0 ]
2020-03-05 10:04:34 -05:00
2025-06-19 08:13:45 -04:00
err := th . App . SetPhase2PermissionsMigrationStatus ( true )
require . NoError ( t , err )
2020-03-05 10:04:34 -05:00
t . Run ( "Errors without a license" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , _ , err := th . SystemAdminClient . PatchChannelModerations ( context . Background ( ) , channel . Id , emptyPatch )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.channel.patch_channel_moderations.license.error" )
2020-03-05 10:04:34 -05:00
} )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2020-03-05 10:04:34 -05:00
t . Run ( "Errors as a non sysadmin" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , _ , err := th . Client . PatchChannelModerations ( context . Background ( ) , channel . Id , emptyPatch )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.context.permissions.app_error" )
2020-03-05 10:04:34 -05:00
} )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2020-03-05 10:04:34 -05:00
t . Run ( "Returns default moderations with empty patch" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
moderations , _ , err := th . SystemAdminClient . PatchChannelModerations ( context . Background ( ) , channel . Id , emptyPatch )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2024-03-12 10:36:05 -04:00
require . Equal ( t , len ( moderations ) , 5 )
2020-03-05 10:04:34 -05:00
for _ , moderation := range moderations {
2024-03-12 10:36:05 -04:00
if moderation . Name == "manage_members" || moderation . Name == "manage_bookmarks" {
2020-03-05 10:04:34 -05:00
require . Empty ( t , moderation . Roles . Guests )
} else {
require . Equal ( t , moderation . Roles . Guests . Value , true )
require . Equal ( t , moderation . Roles . Guests . Enabled , true )
}
require . Equal ( t , moderation . Roles . Members . Value , true )
require . Equal ( t , moderation . Roles . Members . Enabled , true )
}
require . Nil ( t , channel . SchemeId )
} )
t . Run ( "Creates a scheme and returns the updated channel moderations when patching an existing permission" , func ( t * testing . T ) {
patch := [ ] * model . ChannelModerationPatch {
{
Name : & createPosts ,
2024-08-05 23:45:00 -04:00
Roles : & model . ChannelModeratedRolesPatch { Members : model . NewPointer ( false ) } ,
2020-03-05 10:04:34 -05:00
} ,
}
2023-06-06 17:29:29 -04:00
moderations , _ , err := th . SystemAdminClient . PatchChannelModerations ( context . Background ( ) , channel . Id , patch )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2024-03-12 10:36:05 -04:00
require . Equal ( t , len ( moderations ) , 5 )
2020-03-05 10:04:34 -05:00
for _ , moderation := range moderations {
2024-03-12 10:36:05 -04:00
if moderation . Name == "manage_members" || moderation . Name == "manage_bookmarks" {
2020-03-05 10:04:34 -05:00
require . Empty ( t , moderation . Roles . Guests )
} else {
require . Equal ( t , moderation . Roles . Guests . Value , true )
require . Equal ( t , moderation . Roles . Guests . Enabled , true )
}
if moderation . Name == createPosts {
require . Equal ( t , moderation . Roles . Members . Value , false )
require . Equal ( t , moderation . Roles . Members . Enabled , true )
} else {
require . Equal ( t , moderation . Roles . Members . Value , true )
require . Equal ( t , moderation . Roles . Members . Enabled , true )
}
}
2025-06-19 08:13:45 -04:00
var appErr * model . AppError
channel , appErr = th . App . GetChannel ( th . Context , channel . Id )
require . Nil ( t , appErr )
2020-03-05 10:04:34 -05:00
require . NotNil ( t , channel . SchemeId )
} )
t . Run ( "Removes the existing scheme when moderated permissions are set back to higher scoped values" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
var appErr * model . AppError
channel , appErr = th . App . GetChannel ( th . Context , channel . Id )
require . Nil ( t , appErr )
2020-03-05 10:04:34 -05:00
schemeId := channel . SchemeId
2025-06-19 08:13:45 -04:00
scheme , appErr := th . App . GetScheme ( * schemeId )
require . Nil ( t , appErr )
2020-03-05 10:04:34 -05:00
require . Equal ( t , scheme . DeleteAt , int64 ( 0 ) )
patch := [ ] * model . ChannelModerationPatch {
{
Name : & createPosts ,
2024-08-05 23:45:00 -04:00
Roles : & model . ChannelModeratedRolesPatch { Members : model . NewPointer ( true ) } ,
2020-03-05 10:04:34 -05:00
} ,
}
2023-06-06 17:29:29 -04:00
moderations , _ , err := th . SystemAdminClient . PatchChannelModerations ( context . Background ( ) , channel . Id , patch )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2024-03-12 10:36:05 -04:00
require . Equal ( t , len ( moderations ) , 5 )
2020-03-05 10:04:34 -05:00
for _ , moderation := range moderations {
2024-03-12 10:36:05 -04:00
if moderation . Name == "manage_members" || moderation . Name == "manage_bookmarks" {
2020-03-05 10:04:34 -05:00
require . Empty ( t , moderation . Roles . Guests )
} else {
require . Equal ( t , moderation . Roles . Guests . Value , true )
require . Equal ( t , moderation . Roles . Guests . Enabled , true )
}
require . Equal ( t , moderation . Roles . Members . Value , true )
require . Equal ( t , moderation . Roles . Members . Enabled , true )
}
2025-06-19 08:13:45 -04:00
channel , appErr = th . App . GetChannel ( th . Context , channel . Id )
require . Nil ( t , appErr )
2020-03-05 10:04:34 -05:00
require . Nil ( t , channel . SchemeId )
2025-06-19 08:13:45 -04:00
scheme , appErr = th . App . GetScheme ( * schemeId )
require . Nil ( t , appErr )
2020-03-05 10:04:34 -05:00
require . NotEqual ( t , scheme . DeleteAt , int64 ( 0 ) )
} )
2020-04-07 08:05:03 -04:00
t . Run ( "Does not return an error if the team scheme has a blank DefaultChannelGuestRole field" , func ( t * testing . T ) {
team := th . BasicTeam
2025-11-12 07:00:51 -05:00
scheme := th . SetupTeamScheme ( t )
2020-04-07 08:05:03 -04:00
scheme . DefaultChannelGuestRole = ""
mockStore := mocks . Store { }
2023-04-18 07:58:33 -04:00
// Playbooks DB job requires a plugin mock
pluginStore := mocks . PluginStore { }
pluginStore . On ( "List" , mock . Anything , mock . Anything , mock . Anything ) . Return ( [ ] string { } , nil )
mockStore . On ( "Plugin" ) . Return ( & pluginStore )
2020-04-07 08:05:03 -04:00
mockSchemeStore := mocks . SchemeStore { }
mockSchemeStore . On ( "Get" , mock . Anything ) . Return ( scheme , nil )
mockSchemeStore . On ( "Save" , mock . Anything ) . Return ( scheme , nil )
mockSchemeStore . On ( "Delete" , mock . Anything ) . Return ( scheme , nil )
mockStore . On ( "Scheme" ) . Return ( & mockSchemeStore )
2022-10-06 04:04:21 -04:00
mockStore . On ( "Team" ) . Return ( th . App . Srv ( ) . Store ( ) . Team ( ) )
mockStore . On ( "Channel" ) . Return ( th . App . Srv ( ) . Store ( ) . Channel ( ) )
mockStore . On ( "User" ) . Return ( th . App . Srv ( ) . Store ( ) . User ( ) )
mockStore . On ( "Post" ) . Return ( th . App . Srv ( ) . Store ( ) . Post ( ) )
mockStore . On ( "FileInfo" ) . Return ( th . App . Srv ( ) . Store ( ) . FileInfo ( ) )
mockStore . On ( "Webhook" ) . Return ( th . App . Srv ( ) . Store ( ) . Webhook ( ) )
mockStore . On ( "System" ) . Return ( th . App . Srv ( ) . Store ( ) . System ( ) )
mockStore . On ( "License" ) . Return ( th . App . Srv ( ) . Store ( ) . License ( ) )
mockStore . On ( "Role" ) . Return ( th . App . Srv ( ) . Store ( ) . Role ( ) )
2020-04-07 08:05:03 -04:00
mockStore . On ( "Close" ) . Return ( nil )
2022-10-06 04:04:21 -04:00
th . App . Srv ( ) . SetStore ( & mockStore )
2020-04-07 08:05:03 -04:00
team . SchemeId = & scheme . Id
2021-08-13 07:12:16 -04:00
_ , appErr := th . App . UpdateTeamScheme ( team )
require . Nil ( t , appErr )
2020-04-07 08:05:03 -04:00
2023-06-06 17:29:29 -04:00
moderations , _ , err := th . SystemAdminClient . PatchChannelModerations ( context . Background ( ) , channel . Id , emptyPatch )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2024-03-12 10:36:05 -04:00
require . Equal ( t , len ( moderations ) , 5 )
2020-04-07 08:05:03 -04:00
for _ , moderation := range moderations {
2024-03-12 10:36:05 -04:00
if moderation . Name == "manage_members" || moderation . Name == "manage_bookmarks" {
2020-04-07 08:05:03 -04:00
require . Empty ( t , moderation . Roles . Guests )
} else {
require . Equal ( t , moderation . Roles . Guests . Value , false )
require . Equal ( t , moderation . Roles . Guests . Enabled , false )
}
require . Equal ( t , moderation . Roles . Members . Value , true )
require . Equal ( t , moderation . Roles . Members . Enabled , true )
}
patch := [ ] * model . ChannelModerationPatch {
{
Name : & createPosts ,
2024-08-05 23:45:00 -04:00
Roles : & model . ChannelModeratedRolesPatch { Members : model . NewPointer ( true ) } ,
2020-04-07 08:05:03 -04:00
} ,
}
2023-06-06 17:29:29 -04:00
moderations , _ , err = th . SystemAdminClient . PatchChannelModerations ( context . Background ( ) , channel . Id , patch )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2024-03-12 10:36:05 -04:00
require . Equal ( t , len ( moderations ) , 5 )
2020-04-07 08:05:03 -04:00
for _ , moderation := range moderations {
2024-03-12 10:36:05 -04:00
if moderation . Name == "manage_members" || moderation . Name == "manage_bookmarks" {
2020-04-07 08:05:03 -04:00
require . Empty ( t , moderation . Roles . Guests )
} else {
require . Equal ( t , moderation . Roles . Guests . Value , false )
require . Equal ( t , moderation . Roles . Guests . Enabled , false )
}
require . Equal ( t , moderation . Roles . Members . Value , true )
require . Equal ( t , moderation . Roles . Members . Enabled , true )
}
} )
2020-03-05 10:04:34 -05:00
}
2020-04-24 17:12:54 -04:00
func TestGetChannelMemberCountsByGroup ( 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-24 17:12:54 -04:00
channel := th . BasicChannel
t . Run ( "Errors without a license" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , _ , err := th . SystemAdminClient . GetChannelMemberCountsByGroup ( context . Background ( ) , channel . Id , false , "" )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.channel.channel_member_counts_by_group.license.error" )
2020-04-24 17:12:54 -04:00
} )
2020-06-12 07:43:50 -04:00
th . App . Srv ( ) . SetLicense ( model . NewTestLicense ( ) )
2020-04-24 17:12:54 -04:00
t . Run ( "Errors without read permission to the channel" , func ( t * testing . T ) {
2023-06-06 17:29:29 -04:00
_ , _ , err := th . Client . GetChannelMemberCountsByGroup ( context . Background ( ) , model . NewId ( ) , false , "" )
2021-08-13 07:12:16 -04:00
CheckErrorID ( t , err , "api.context.permissions.app_error" )
2020-04-24 17:12:54 -04:00
} )
t . Run ( "Returns empty for a channel with no members or groups" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
memberCounts , _ , err := th . SystemAdminClient . GetChannelMemberCountsByGroup ( context . Background ( ) , channel . Id , false , "" )
require . NoError ( t , err )
2020-04-24 17:12:54 -04:00
require . Equal ( t , [ ] * model . ChannelMemberCountByGroup { } , memberCounts )
} )
user := th . BasicUser
user . Timezone [ "useAutomaticTimezone" ] = "false"
user . Timezone [ "manualTimezone" ] = "XOXO/BLABLA"
2021-08-13 07:12:16 -04:00
_ , appErr := th . App . UpsertGroupMember ( th . Group . Id , user . Id )
require . Nil ( t , appErr )
2023-06-06 17:29:29 -04:00
_ , _ , err := th . SystemAdminClient . UpdateUser ( context . Background ( ) , user )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-24 17:12:54 -04:00
user2 := th . BasicUser2
user2 . Timezone [ "automaticTimezone" ] = "NoWhere/Island"
2021-08-13 07:12:16 -04:00
_ , appErr = th . App . UpsertGroupMember ( th . Group . Id , user2 . Id )
require . Nil ( t , appErr )
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . UpdateUser ( context . Background ( ) , user2 )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-04-24 17:12:54 -04:00
t . Run ( "Returns users in group without timezones" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
memberCounts , _ , err := th . SystemAdminClient . GetChannelMemberCountsByGroup ( context . Background ( ) , channel . Id , false , "" )
require . NoError ( t , err )
2020-04-24 17:12:54 -04:00
expectedMemberCounts := [ ] * model . ChannelMemberCountByGroup {
{
GroupId : th . Group . Id ,
ChannelMemberCount : 2 ,
ChannelMemberTimezonesCount : 0 ,
} ,
}
require . Equal ( t , expectedMemberCounts , memberCounts )
} )
t . Run ( "Returns users in group with timezones" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
memberCounts , _ , err := th . SystemAdminClient . GetChannelMemberCountsByGroup ( context . Background ( ) , channel . Id , true , "" )
require . NoError ( t , err )
2020-04-24 17:12:54 -04:00
expectedMemberCounts := [ ] * model . ChannelMemberCountByGroup {
{
GroupId : th . Group . Id ,
ChannelMemberCount : 2 ,
ChannelMemberTimezonesCount : 2 ,
} ,
}
require . Equal ( t , expectedMemberCounts , memberCounts )
} )
id := model . NewId ( )
group := & model . Group {
DisplayName : "dn_" + id ,
2024-08-05 23:45:00 -04:00
Name : model . NewPointer ( "name" + id ) ,
2020-04-24 17:12:54 -04:00
Source : model . GroupSourceLdap ,
2024-08-05 23:45:00 -04:00
RemoteId : model . NewPointer ( model . NewId ( ) ) ,
2020-04-24 17:12:54 -04:00
}
2021-08-13 07:12:16 -04:00
_ , appErr = th . App . CreateGroup ( group )
require . Nil ( t , appErr )
_ , appErr = th . App . UpsertGroupMember ( group . Id , user . Id )
require . Nil ( t , appErr )
2020-04-24 17:12:54 -04:00
t . Run ( "Returns multiple groups with users in group with timezones" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
memberCounts , _ , err := th . SystemAdminClient . GetChannelMemberCountsByGroup ( context . Background ( ) , channel . Id , true , "" )
require . NoError ( t , err )
2020-04-24 17:12:54 -04:00
expectedMemberCounts := [ ] * model . ChannelMemberCountByGroup {
{
GroupId : group . Id ,
ChannelMemberCount : 1 ,
ChannelMemberTimezonesCount : 1 ,
} ,
{
GroupId : th . Group . Id ,
ChannelMemberCount : 2 ,
ChannelMemberTimezonesCount : 2 ,
} ,
}
require . ElementsMatch ( t , expectedMemberCounts , memberCounts )
} )
}
2020-06-22 09:57:49 -04:00
2023-07-19 02:15:27 -04:00
func TestGetChannelsMemberCount ( t * testing . T ) {
2025-05-30 07:58:26 -04:00
mainHelper . Parallel ( t )
2023-07-19 02:15:27 -04:00
// Setup
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2023-07-19 02:15:27 -04:00
client := th . Client
2025-11-12 07:00:51 -05:00
channel1 := th . CreatePublicChannel ( t )
channel2 := th . CreatePrivateChannel ( t )
channel3 := th . CreatePrivateChannel ( t )
th . RemoveUserFromChannel ( t , th . BasicUser , channel3 )
2023-07-19 02:15:27 -04:00
2025-11-12 07:00:51 -05:00
user1 := th . CreateUser ( t )
user2 := th . CreateUser ( t )
user3 := th . CreateUser ( t )
2023-07-19 02:15:27 -04:00
2025-11-12 07:00:51 -05:00
th . LinkUserToTeam ( t , user1 , th . BasicTeam )
th . LinkUserToTeam ( t , user2 , th . BasicTeam )
th . LinkUserToTeam ( t , user3 , th . BasicTeam )
2023-07-19 02:15:27 -04:00
2025-11-12 07:00:51 -05:00
th . AddUserToChannel ( t , user1 , channel1 )
th . AddUserToChannel ( t , user2 , channel1 )
th . AddUserToChannel ( t , user3 , channel1 )
th . AddUserToChannel ( t , user2 , channel2 )
2023-07-19 02:15:27 -04:00
t . Run ( "Should return correct member count" , func ( t * testing . T ) {
// Create a request with channel IDs
channelIDs := [ ] string { channel1 . Id , channel2 . Id }
channelsMemberCount , _ , err := client . GetChannelsMemberCount ( context . Background ( ) , channelIDs )
require . NoError ( t , err )
// Verify the member counts
require . Contains ( t , channelsMemberCount , channel1 . Id )
require . Contains ( t , channelsMemberCount , channel2 . Id )
require . Equal ( t , int64 ( 4 ) , channelsMemberCount [ channel1 . Id ] )
require . Equal ( t , int64 ( 2 ) , channelsMemberCount [ channel2 . Id ] )
} )
t . Run ( "Should return empty object when empty array is passed" , func ( t * testing . T ) {
channelsMemberCount , _ , err := client . GetChannelsMemberCount ( context . Background ( ) , [ ] string { } )
require . NoError ( t , err )
require . Equal ( t , 0 , len ( channelsMemberCount ) )
} )
t . Run ( "Should fail due to permissions" , func ( t * testing . T ) {
2024-01-05 12:28:22 -05:00
_ , resp , err := client . GetChannelsMemberCount ( context . Background ( ) , [ ] string { channel3 . Id } )
2023-07-19 02:15:27 -04:00
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
CheckErrorID ( t , err , "api.context.permissions.app_error" )
} )
t . Run ( "Should fail due to expired session when logged out" , func ( t * testing . T ) {
2025-06-19 08:13:45 -04:00
_ , err := client . Logout ( context . Background ( ) )
require . NoError ( t , err )
2023-07-19 02:15:27 -04:00
channelIDs := [ ] string { channel1 . Id , channel2 . Id }
_ , resp , err := client . GetChannelsMemberCount ( context . Background ( ) , channelIDs )
require . Error ( t , err )
CheckUnauthorizedStatus ( t , resp )
CheckErrorID ( t , err , "api.context.session_expired.app_error" )
} )
t . Run ( "Should fail due to expired session when logged out" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th . LoginBasic2 ( t )
2023-07-19 02:15:27 -04:00
channelIDs := [ ] string { channel1 . Id , channel2 . Id }
_ , resp , err := client . GetChannelsMemberCount ( context . Background ( ) , channelIDs )
require . Error ( t , err )
CheckForbiddenStatus ( t , resp )
CheckErrorID ( t , err , "api.context.permissions.app_error" )
} )
2024-01-05 12:28:22 -05:00
t . Run ( "Should not fail for public channels that the user is not a member of" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th . LoginBasic2 ( t )
2024-01-05 12:28:22 -05:00
channelIDs := [ ] string { channel1 . Id }
_ , _ , err := client . GetChannelsMemberCount ( context . Background ( ) , channelIDs )
require . NoError ( t , err )
} )
2024-06-25 14:25:28 -04:00
t . Run ( "Should fail for private channels that the user is not a member of" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
th . LoginBasic2 ( t )
2024-06-25 14:25:28 -04:00
channelIDs := [ ] string { channel2 . Id }
_ , _ , err := client . GetChannelsMemberCount ( context . Background ( ) , channelIDs )
require . Error ( t , err )
} )
2023-07-19 02:15:27 -04:00
}
2020-06-22 09:57:49 -04:00
func TestMoveChannel ( 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-22 09:57:49 -04:00
2021-08-13 07:12:16 -04:00
client := th . Client
2020-06-22 09:57:49 -04:00
team1 := th . BasicTeam
2025-11-12 07:00:51 -05:00
team2 := th . CreateTeam ( t )
2020-06-22 09:57:49 -04:00
t . Run ( "Should move channel" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
publicChannel := th . CreatePublicChannel ( t )
2023-06-06 17:29:29 -04:00
ch , _ , err := th . SystemAdminClient . MoveChannel ( context . Background ( ) , publicChannel . Id , team2 . Id , false )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-22 09:57:49 -04:00
require . Equal ( t , team2 . Id , ch . TeamId )
} )
2024-04-22 03:40:08 -04:00
t . Run ( "Should return custom error with repeated channel" , func ( t * testing . T ) {
channelT1 := & model . Channel {
DisplayName : "repeated" ,
Name : "repeated" ,
Type : model . ChannelTypePrivate ,
TeamId : team1 . Id ,
}
2025-06-19 08:13:45 -04:00
channelT1 , _ , err := th . Client . CreateChannel ( context . Background ( ) , channelT1 )
2024-04-22 03:40:08 -04:00
require . NoError ( t , err )
channelT2 := & model . Channel {
DisplayName : "repeated" ,
Name : "repeated" ,
Type : model . ChannelTypePrivate ,
TeamId : team2 . Id ,
}
2025-06-19 08:13:45 -04:00
_ , _ , err = th . Client . CreateChannel ( context . Background ( ) , channelT2 )
2024-04-22 03:40:08 -04:00
require . NoError ( t , err )
_ , _ , err = th . SystemAdminClient . MoveChannel ( context . Background ( ) , channelT1 . Id , team2 . Id , false )
require . EqualError ( t , err , "A channel with that name already exists on the same team." )
} )
2021-02-11 11:26:56 -05:00
t . Run ( "Should move private channel" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
channel := th . CreatePrivateChannel ( t )
2023-06-06 17:29:29 -04:00
ch , _ , err := th . SystemAdminClient . MoveChannel ( context . Background ( ) , channel . Id , team1 . Id , false )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-02-11 11:26:56 -05:00
require . Equal ( t , team1 . Id , ch . TeamId )
2020-06-22 09:57:49 -04:00
} )
t . Run ( "Should fail when trying to move a DM channel" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
dmChannel := th . CreateDmChannel ( t , user )
2023-06-06 17:29:29 -04:00
_ , _ , err := client . MoveChannel ( context . Background ( ) , dmChannel . Id , team1 . Id , false )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
CheckErrorID ( t , err , "api.channel.move_channel.type.invalid" )
2020-06-22 09:57:49 -04:00
} )
t . Run ( "Should fail when trying to move a group channel" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
user := th . CreateUser ( t )
2020-06-22 09:57:49 -04:00
2022-07-14 05:01:29 -04:00
gmChannel , appErr := th . App . CreateGroupChannel ( th . Context , [ ] string { th . BasicUser . Id , th . SystemAdminUser . Id , th . TeamAdminUser . Id } , user . Id )
2021-08-13 07:12:16 -04:00
require . Nil ( t , appErr )
2023-06-06 17:29:29 -04:00
_ , _ , err := client . MoveChannel ( context . Background ( ) , gmChannel . Id , team1 . Id , false )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
CheckErrorID ( t , err , "api.channel.move_channel.type.invalid" )
2020-06-22 09:57:49 -04:00
} )
t . Run ( "Should fail due to permissions" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
publicChannel := th . CreatePublicChannel ( t )
2023-06-06 17:29:29 -04:00
_ , _ , err := client . MoveChannel ( context . Background ( ) , publicChannel . Id , team1 . Id , false )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
CheckErrorID ( t , err , "api.context.permissions.app_error" )
2020-06-22 09:57:49 -04:00
} )
2020-08-10 11:44:13 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2025-11-12 07:00:51 -05:00
publicChannel := th . CreatePublicChannel ( t )
2020-06-22 09:57:49 -04:00
user := th . BasicUser
2023-06-06 17:29:29 -04:00
_ , err := client . RemoveTeamMember ( context . Background ( ) , team2 . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-22 09:57:49 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , publicChannel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-06-22 09:57:49 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err = client . MoveChannel ( context . Background ( ) , publicChannel . Id , team2 . Id , false )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
CheckErrorID ( t , err , "app.channel.move_channel.members_do_not_match.error" )
2021-02-11 11:26:56 -05:00
} , "Should fail to move public channel due to a member not member of target team" )
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2025-11-12 07:00:51 -05:00
privateChannel := th . CreatePrivateChannel ( t )
2021-02-11 11:26:56 -05:00
user := th . BasicUser
2023-06-06 17:29:29 -04:00
_ , err := client . RemoveTeamMember ( context . Background ( ) , team2 . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-02-11 11:26:56 -05:00
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-02-11 11:26:56 -05:00
2023-06-06 17:29:29 -04:00
_ , _ , err = client . MoveChannel ( context . Background ( ) , privateChannel . Id , team2 . Id , false )
2021-08-13 07:12:16 -04:00
require . Error ( t , err )
CheckErrorID ( t , err , "app.channel.move_channel.members_do_not_match.error" )
2021-02-11 11:26:56 -05:00
} , "Should fail to move private channel due to a member not member of target team" )
2020-07-16 04:26:19 -04:00
2020-08-10 11:44:13 -04:00
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2025-11-12 07:00:51 -05:00
publicChannel := th . CreatePublicChannel ( t )
2020-07-16 04:26:19 -04:00
user := th . BasicUser
2023-06-06 17:29:29 -04:00
_ , err := client . RemoveTeamMember ( context . Background ( ) , team2 . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-07-16 04:26:19 -04:00
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , publicChannel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-07-16 04:26:19 -04:00
2023-06-06 17:29:29 -04:00
newChannel , _ , err := client . MoveChannel ( context . Background ( ) , publicChannel . Id , team2 . Id , true )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2020-07-16 04:26:19 -04:00
require . Equal ( t , team2 . Id , newChannel . TeamId )
2021-02-11 11:26:56 -05:00
} , "Should be able to (force) move public channel by a member that is not member of target team" )
th . TestForSystemAdminAndLocal ( t , func ( t * testing . T , client * model . Client4 ) {
2025-11-12 07:00:51 -05:00
privateChannel := th . CreatePrivateChannel ( t )
2021-02-11 11:26:56 -05:00
user := th . BasicUser
2023-06-06 17:29:29 -04:00
_ , err := client . RemoveTeamMember ( context . Background ( ) , team2 . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-02-11 11:26:56 -05:00
2023-06-06 17:29:29 -04:00
_ , _ , err = client . AddChannelMember ( context . Background ( ) , privateChannel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-02-11 11:26:56 -05:00
2023-06-06 17:29:29 -04:00
newChannel , _ , err := client . MoveChannel ( context . Background ( ) , privateChannel . Id , team2 . Id , true )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-02-11 11:26:56 -05:00
require . Equal ( t , team2 . Id , newChannel . TeamId )
} , "Should be able to (force) move private channel by a member that is not member of target team" )
2020-07-06 18:20:35 -04:00
}
2021-04-01 07:43:09 -04:00
func TestRootMentionsCount ( 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-05-26 11:10:25 -04:00
2021-08-13 07:12:16 -04:00
client := th . Client
2021-04-01 07:43:09 -04:00
user := th . BasicUser
channel := th . BasicChannel
// initially, MentionCountRoot is 0 in the database
2025-09-18 10:14:24 -04:00
channelMember , err := th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , channel . Id , user . Id )
2021-04-01 07:43:09 -04:00
require . NoError ( t , err )
require . Equal ( t , int64 ( 0 ) , channelMember . MentionCountRoot )
require . Equal ( t , int64 ( 0 ) , channelMember . MentionCount )
// mention the user in a root post
2023-06-06 17:29:29 -04:00
post1 , _ , err := th . SystemAdminClient . CreatePost ( context . Background ( ) , & model . Post { ChannelId : channel . Id , Message : "hey @" + user . Username } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-01 07:43:09 -04:00
// mention the user in a reply post
post2 := & model . Post { ChannelId : channel . Id , Message : "reply at @" + user . Username , RootId : post1 . Id }
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . CreatePost ( context . Background ( ) , post2 )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-01 07:43:09 -04:00
// this should perform lazy migration and populate the field
2023-06-06 17:29:29 -04:00
channelUnread , _ , err := client . GetChannelUnread ( context . Background ( ) , channel . Id , user . Id )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-04-01 07:43:09 -04:00
// reply post is not counted, so we should have one root mention
require . EqualValues ( t , int64 ( 1 ) , channelUnread . MentionCountRoot )
// regular count stays the same
require . Equal ( t , int64 ( 2 ) , channelUnread . MentionCount )
// validate that DB is updated
2025-09-18 10:14:24 -04:00
channelMember , err = th . App . Srv ( ) . Store ( ) . Channel ( ) . GetMember ( th . Context , channel . Id , user . Id )
2021-04-01 07:43:09 -04:00
require . NoError ( t , err )
require . EqualValues ( t , int64 ( 1 ) , channelMember . MentionCountRoot )
// validate that Team level counts are calculated
counts , appErr := th . App . GetTeamUnread ( channel . TeamId , user . Id )
require . Nil ( t , appErr )
require . Equal ( t , int64 ( 1 ) , counts . MentionCountRoot )
require . Equal ( t , int64 ( 2 ) , counts . MentionCount )
}
2021-05-26 11:10:25 -04:00
func TestViewChannelWithoutCollapsedThreads ( 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-05-26 11:10:25 -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-05-26 11:10:25 -04:00
} )
2021-08-13 07:12:16 -04:00
client := th . Client
2021-05-26 11:10:25 -04:00
user := th . BasicUser
team := th . BasicTeam
channel := th . BasicChannel
// mention the user in a root post
2023-06-06 17:29:29 -04:00
post1 , _ , err := th . SystemAdminClient . CreatePost ( context . Background ( ) , & model . Post { ChannelId : channel . Id , Message : "hey @" + user . Username } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-05-26 11:10:25 -04:00
// mention the user in a reply post
post2 := & model . Post { ChannelId : channel . Id , Message : "reply at @" + user . Username , RootId : post1 . Id }
2023-06-06 17:29:29 -04:00
_ , _ , err = th . SystemAdminClient . CreatePost ( context . Background ( ) , post2 )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-05-26 11:10:25 -04:00
2023-06-06 17:29:29 -04:00
threads , _ , err := client . GetUserThreads ( context . Background ( ) , user . Id , team . Id , model . GetUserThreadsOpts { } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-05-26 11:10:25 -04:00
require . EqualValues ( t , int64 ( 1 ) , threads . TotalUnreadMentions )
// simulate opening the channel from an old client
2023-06-06 17:29:29 -04:00
_ , _ , err = client . ViewChannel ( context . Background ( ) , user . Id , & model . ChannelView {
2021-05-26 11:10:25 -04:00
ChannelId : channel . Id ,
PrevChannelId : "" ,
CollapsedThreadsSupported : false ,
} )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-05-26 11:10:25 -04:00
2023-06-06 17:29:29 -04:00
threads , _ , err = client . GetUserThreads ( context . Background ( ) , user . Id , team . Id , model . GetUserThreadsOpts { } )
2021-08-13 07:12:16 -04:00
require . NoError ( t , err )
2021-05-26 11:10:25 -04:00
require . Zero ( t , threads . TotalUnreadMentions )
}
2025-09-05 11:06:16 -04:00
func TestChannelMemberSanitization ( t * testing . T ) {
mainHelper . Parallel ( t )
2025-11-12 07:00:51 -05:00
th := Setup ( t ) . InitBasic ( t )
2025-09-05 11:06:16 -04:00
client := th . Client
user := th . BasicUser
user2 := th . BasicUser2
2025-11-12 07:00:51 -05:00
channel := th . CreatePublicChannel ( t )
2025-09-05 11:06:16 -04:00
// Add second user to channel
_ , _ , err := client . AddChannelMember ( context . Background ( ) , channel . Id , user2 . Id )
require . NoError ( t , err )
t . Run ( "getChannelMembers sanitizes LastViewedAt and LastUpdateAt for other users" , func ( t * testing . T ) {
members , _ , err := client . GetChannelMembers ( context . Background ( ) , channel . Id , 0 , 60 , "" )
require . NoError ( t , err )
for _ , member := range members {
if member . UserId == user . Id {
// Current user should see their own timestamps
assert . NotEqual ( t , int64 ( - 1 ) , member . LastViewedAt , "Current user should see their LastViewedAt" )
assert . NotEqual ( t , int64 ( - 1 ) , member . LastUpdateAt , "Current user should see their LastUpdateAt" )
} else {
// Other users' timestamps should be sanitized
assert . Equal ( t , int64 ( - 1 ) , member . LastViewedAt , "Other users' LastViewedAt should be sanitized" )
assert . Equal ( t , int64 ( - 1 ) , member . LastUpdateAt , "Other users' LastUpdateAt should be sanitized" )
}
}
} )
t . Run ( "getChannelMember sanitizes LastViewedAt and LastUpdateAt for other users" , func ( t * testing . T ) {
// Get other user's membership data
member , _ , err := client . GetChannelMember ( context . Background ( ) , channel . Id , user2 . Id , "" )
require . NoError ( t , err )
// Should be sanitized since it's not the current user
assert . Equal ( t , int64 ( - 1 ) , member . LastViewedAt , "Other user's LastViewedAt should be sanitized" )
assert . Equal ( t , int64 ( - 1 ) , member . LastUpdateAt , "Other user's LastUpdateAt should be sanitized" )
// Get current user's membership data
currentMember , _ , err := client . GetChannelMember ( context . Background ( ) , channel . Id , user . Id , "" )
require . NoError ( t , err )
// Should not be sanitized since it's the current user
assert . NotEqual ( t , int64 ( - 1 ) , currentMember . LastViewedAt , "Current user should see their LastViewedAt" )
assert . NotEqual ( t , int64 ( - 1 ) , currentMember . LastUpdateAt , "Current user should see their LastUpdateAt" )
} )
t . Run ( "getChannelMembersByIds sanitizes data appropriately" , func ( t * testing . T ) {
userIds := [ ] string { user . Id , user2 . Id }
members , _ , err := client . GetChannelMembersByIds ( context . Background ( ) , channel . Id , userIds )
require . NoError ( t , err )
require . Len ( t , members , 2 )
for _ , member := range members {
if member . UserId == user . Id {
// Current user should see their own timestamps
assert . NotEqual ( t , int64 ( - 1 ) , member . LastViewedAt , "Current user should see their LastViewedAt" )
assert . NotEqual ( t , int64 ( - 1 ) , member . LastUpdateAt , "Current user should see their LastUpdateAt" )
} else {
// Other users' timestamps should be sanitized
assert . Equal ( t , int64 ( - 1 ) , member . LastViewedAt , "Other users' LastViewedAt should be sanitized" )
assert . Equal ( t , int64 ( - 1 ) , member . LastUpdateAt , "Other users' LastUpdateAt should be sanitized" )
}
}
} )
t . Run ( "addChannelMember sanitizes returned member data" , func ( t * testing . T ) {
2025-11-12 07:00:51 -05:00
newUser := th . CreateUser ( t )
th . LinkUserToTeam ( t , newUser , th . BasicTeam )
2025-09-05 11:06:16 -04:00
// Add new user and check returned member data
returnedMember , _ , err := client . AddChannelMember ( context . Background ( ) , channel . Id , newUser . Id )
require . NoError ( t , err )
// The returned member should be sanitized since it's not the current user
assert . Equal ( t , int64 ( - 1 ) , returnedMember . LastViewedAt , "Returned member LastViewedAt should be sanitized" )
assert . Equal ( t , int64 ( - 1 ) , returnedMember . LastUpdateAt , "Returned member LastUpdateAt should be sanitized" )
assert . Equal ( t , newUser . Id , returnedMember . UserId , "UserId should be preserved" )
assert . Equal ( t , channel . Id , returnedMember . ChannelId , "ChannelId should be preserved" )
} )
}