// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. package app import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/mattermost/mattermost/server/public/model" ) func setupSharedChannels(tb testing.TB) *TestHelper { return SetupConfig(tb, func(cfg *model.Config) { *cfg.ConnectedWorkspacesSettings.EnableRemoteClusterService = true *cfg.ConnectedWorkspacesSettings.EnableSharedChannels = true }) } func TestApp_CheckCanInviteToSharedChannel(t *testing.T) { th := setupSharedChannels(t).InitBasic() channel1 := th.CreateChannel(th.Context, th.BasicTeam) channel2 := th.CreateChannel(th.Context, th.BasicTeam) channel3 := th.CreateChannel(th.Context, th.BasicTeam) data := []struct { channelID string home bool name string remoteID string }{ {channelID: channel1.Id, home: true, name: "test_home", remoteID: ""}, {channelID: channel2.Id, home: false, name: "test_remote", remoteID: model.NewId()}, } for _, d := range data { sc := &model.SharedChannel{ ChannelId: d.channelID, TeamId: th.BasicTeam.Id, Home: d.home, ShareName: d.name, CreatorId: th.BasicUser.Id, RemoteId: d.remoteID, } _, err := th.App.ShareChannel(th.Context, sc) require.NoError(t, err) } t.Run("Test checkChannelNotShared: not yet shared channel", func(t *testing.T) { err := th.App.checkChannelNotShared(th.Context, channel3.Id) assert.NoError(t, err, "unshared channel should not error") }) t.Run("Test checkChannelNotShared: already shared channel", func(t *testing.T) { err := th.App.checkChannelNotShared(th.Context, channel1.Id) assert.Error(t, err, "already shared channel should error") }) t.Run("Test checkChannelNotShared: invalid channel", func(t *testing.T) { err := th.App.checkChannelNotShared(th.Context, model.NewId()) assert.Error(t, err, "invalid channel should error") }) t.Run("Test checkChannelIsShared: not yet shared channel", func(t *testing.T) { err := th.App.checkChannelIsShared(channel3.Id) assert.Error(t, err, "unshared channel should error") }) t.Run("Test checkChannelIsShared: already shared channel", func(t *testing.T) { err := th.App.checkChannelIsShared(channel1.Id) assert.NoError(t, err, "already channel should not error") }) t.Run("Test checkChannelIsShared: invalid channel", func(t *testing.T) { err := th.App.checkChannelIsShared(model.NewId()) assert.Error(t, err, "invalid channel should error") }) t.Run("Test CheckCanInviteToSharedChannel: Home shared channel", func(t *testing.T) { err := th.App.CheckCanInviteToSharedChannel(data[0].channelID) assert.NoError(t, err, "home channel should allow invites") }) t.Run("Test CheckCanInviteToSharedChannel: Remote shared channel", func(t *testing.T) { err := th.App.CheckCanInviteToSharedChannel(data[1].channelID) assert.Error(t, err, "home channel should not allow invites") }) t.Run("Test CheckCanInviteToSharedChannel: Invalid shared channel", func(t *testing.T) { err := th.App.CheckCanInviteToSharedChannel(model.NewId()) assert.Error(t, err, "invalid channel should not allow invites") }) }