github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/app/slashcommands/command_share_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package slashcommands
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/masterhung0112/hk_server/v5/testlib"
    13  
    14  	"github.com/masterhung0112/hk_server/v5/app"
    15  	"github.com/masterhung0112/hk_server/v5/services/remotecluster"
    16  
    17  	"github.com/stretchr/testify/require"
    18  
    19  	"github.com/masterhung0112/hk_server/v5/model"
    20  )
    21  
    22  func TestShareProviderDoCommand(t *testing.T) {
    23  	t.Run("share command sends a websocket channel converted event", func(t *testing.T) {
    24  		th := setup(t).initBasic()
    25  		defer th.tearDown()
    26  
    27  		th.addPermissionToRole(model.PERMISSION_MANAGE_SHARED_CHANNELS.Id, th.BasicUser.Roles)
    28  
    29  		mockSyncService := app.NewMockSharedChannelService(nil)
    30  		th.Server.SetSharedChannelSyncService(mockSyncService)
    31  		mockRemoteCluster, err := remotecluster.NewRemoteClusterService(th.Server)
    32  		require.NoError(t, err)
    33  
    34  		th.Server.SetRemoteClusterService(mockRemoteCluster)
    35  		testCluster := &testlib.FakeClusterInterface{}
    36  		th.Server.Cluster = testCluster
    37  
    38  		commandProvider := ShareProvider{}
    39  		channel := th.CreateChannel(th.BasicTeam, WithShared(false))
    40  
    41  		args := &model.CommandArgs{
    42  			T:         func(s string, args ...interface{}) string { return s },
    43  			ChannelId: channel.Id,
    44  			UserId:    th.BasicUser.Id,
    45  			TeamId:    th.BasicTeam.Id,
    46  			Command:   "/share-channel share",
    47  		}
    48  
    49  		response := commandProvider.DoCommand(th.App, th.Context, args, "")
    50  		require.Equal(t, "##### "+args.T("api.command_share.channel_shared"), response.Text)
    51  
    52  		channelConvertedMessages := testCluster.SelectMessages(func(msg *model.ClusterMessage) bool {
    53  			event := model.WebSocketEventFromJson(strings.NewReader(msg.Data))
    54  			return event != nil && event.EventType() == model.WEBSOCKET_EVENT_CHANNEL_CONVERTED
    55  		})
    56  		assert.Len(t, channelConvertedMessages, 1)
    57  	})
    58  
    59  	t.Run("unshare command sends a websocket channel converted event", func(t *testing.T) {
    60  		th := setup(t).initBasic()
    61  		defer th.tearDown()
    62  
    63  		th.addPermissionToRole(model.PERMISSION_MANAGE_SHARED_CHANNELS.Id, th.BasicUser.Roles)
    64  
    65  		mockSyncService := app.NewMockSharedChannelService(nil)
    66  		th.Server.SetSharedChannelSyncService(mockSyncService)
    67  		mockRemoteCluster, err := remotecluster.NewRemoteClusterService(th.Server)
    68  		require.NoError(t, err)
    69  
    70  		th.Server.SetRemoteClusterService(mockRemoteCluster)
    71  		testCluster := &testlib.FakeClusterInterface{}
    72  		th.Server.Cluster = testCluster
    73  
    74  		commandProvider := ShareProvider{}
    75  		channel := th.CreateChannel(th.BasicTeam, WithShared(true))
    76  		args := &model.CommandArgs{
    77  			T:         func(s string, args ...interface{}) string { return s },
    78  			ChannelId: channel.Id,
    79  			UserId:    th.BasicUser.Id,
    80  			TeamId:    th.BasicTeam.Id,
    81  			Command:   "/share-channel unshare",
    82  		}
    83  
    84  		response := commandProvider.DoCommand(th.App, th.Context, args, "")
    85  		require.Equal(t, "##### "+args.T("api.command_share.shared_channel_unavailable"), response.Text)
    86  
    87  		channelConvertedMessages := testCluster.SelectMessages(func(msg *model.ClusterMessage) bool {
    88  			event := model.WebSocketEventFromJson(strings.NewReader(msg.Data))
    89  			return event != nil && event.EventType() == model.WEBSOCKET_EVENT_CHANNEL_CONVERTED
    90  		})
    91  		require.Len(t, channelConvertedMessages, 1)
    92  	})
    93  }