1// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2// See LICENSE.txt for license information.
3
4package app
5
6import (
7	"testing"
8
9	"github.com/stretchr/testify/assert"
10
11	"github.com/mattermost/mattermost-server/v6/model"
12)
13
14func TestServerSyncSharedChannelHandler(t *testing.T) {
15	t.Run("sync service inactive, it does nothing", func(t *testing.T) {
16		th := SetupWithStoreMock(t)
17		defer th.TearDown()
18
19		mockService := NewMockSharedChannelService(nil)
20		mockService.active = false
21		th.App.srv.SetSharedChannelSyncService(mockService)
22
23		th.App.srv.SharedChannelSyncHandler(&model.WebSocketEvent{})
24		assert.Empty(t, mockService.channelNotifications)
25	})
26
27	t.Run("sync service active and broadcast envelope has ineligible event, it does nothing", func(t *testing.T) {
28		th := Setup(t).InitBasic()
29		defer th.TearDown()
30
31		mockService := NewMockSharedChannelService(nil)
32		mockService.active = true
33		th.App.srv.SetSharedChannelSyncService(mockService)
34		channel := th.CreateChannel(th.BasicTeam, WithShared(true))
35
36		websocketEvent := model.NewWebSocketEvent(model.WebsocketEventAddedToTeam, model.NewId(), channel.Id, "", nil)
37
38		th.App.srv.SharedChannelSyncHandler(websocketEvent)
39		assert.Empty(t, mockService.channelNotifications)
40	})
41
42	t.Run("sync service active and broadcast envelope has eligible event but channel does not exist, it does nothing", func(t *testing.T) {
43		th := Setup(t).InitBasic()
44		defer th.TearDown()
45
46		mockService := NewMockSharedChannelService(nil)
47		mockService.active = true
48		th.App.srv.SetSharedChannelSyncService(mockService)
49
50		websocketEvent := model.NewWebSocketEvent(model.WebsocketEventPosted, model.NewId(), model.NewId(), "", nil)
51
52		th.App.srv.SharedChannelSyncHandler(websocketEvent)
53		assert.Empty(t, mockService.channelNotifications)
54	})
55
56	t.Run("sync service active when received eligible event, it triggers a shared channel content sync", func(t *testing.T) {
57		th := Setup(t).InitBasic()
58		defer th.TearDown()
59
60		mockService := NewMockSharedChannelService(nil)
61		mockService.active = true
62		th.App.srv.SetSharedChannelSyncService(mockService)
63
64		channel := th.CreateChannel(th.BasicTeam, WithShared(true))
65		websocketEvent := model.NewWebSocketEvent(model.WebsocketEventPosted, model.NewId(), channel.Id, "", nil)
66
67		th.App.srv.SharedChannelSyncHandler(websocketEvent)
68		assert.Len(t, mockService.channelNotifications, 1)
69		assert.Equal(t, channel.Id, mockService.channelNotifications[0])
70	})
71}
72