github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/api4/shared_channel_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package api4 5 6 import ( 7 "fmt" 8 "math/rand" 9 "sort" 10 "testing" 11 "time" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 16 "github.com/masterhung0112/hk_server/v5/app" 17 "github.com/masterhung0112/hk_server/v5/model" 18 ) 19 20 var ( 21 rnd = rand.New(rand.NewSource(time.Now().UnixNano())) 22 ) 23 24 func TestGetAllSharedChannels(t *testing.T) { 25 th := Setup(t).InitBasic() 26 defer th.TearDown() 27 28 const pages = 3 29 const pageSize = 7 30 31 mockService := app.NewMockRemoteClusterService(nil, app.MockOptionRemoteClusterServiceWithActive(true)) 32 th.App.Srv().SetRemoteClusterService(mockService) 33 34 savedIds := make([]string, 0, pages*pageSize) 35 36 // make some shared channels 37 for i := 0; i < pages*pageSize; i++ { 38 channel := th.CreateChannelWithClientAndTeam(th.Client, model.CHANNEL_OPEN, th.BasicTeam.Id) 39 sc := &model.SharedChannel{ 40 ChannelId: channel.Id, 41 TeamId: channel.TeamId, 42 Home: randomBool(), 43 ShareName: fmt.Sprintf("test_share_%d", i), 44 CreatorId: th.BasicChannel.CreatorId, 45 RemoteId: model.NewId(), 46 } 47 _, err := th.App.SaveSharedChannel(sc) 48 require.NoError(t, err) 49 savedIds = append(savedIds, channel.Id) 50 } 51 sort.Strings(savedIds) 52 53 t.Run("get shared channels paginated", func(t *testing.T) { 54 channelIds := make([]string, 0, 21) 55 for i := 0; i < pages; i++ { 56 channels, resp := th.Client.GetAllSharedChannels(th.BasicTeam.Id, i, pageSize) 57 CheckNoError(t, resp) 58 channelIds = append(channelIds, getIds(channels)...) 59 } 60 sort.Strings(channelIds) 61 62 // ids lists should now match 63 assert.Equal(t, savedIds, channelIds, "id lists should match") 64 }) 65 66 t.Run("get shared channels for invalid team", func(t *testing.T) { 67 channels, resp := th.Client.GetAllSharedChannels(model.NewId(), 0, 100) 68 CheckNoError(t, resp) 69 assert.Empty(t, channels) 70 }) 71 } 72 73 func getIds(channels []*model.SharedChannel) []string { 74 ids := make([]string, 0, len(channels)) 75 for _, c := range channels { 76 ids = append(ids, c.ChannelId) 77 } 78 return ids 79 } 80 81 func randomBool() bool { 82 return rnd.Intn(2) != 0 83 } 84 85 func TestGetRemoteClusterById(t *testing.T) { 86 th := Setup(t).InitBasic() 87 defer th.TearDown() 88 89 mockService := app.NewMockRemoteClusterService(nil, app.MockOptionRemoteClusterServiceWithActive(true)) 90 th.App.Srv().SetRemoteClusterService(mockService) 91 92 // for this test we need a user that belongs to a channel that 93 // is shared with the requested remote id. 94 95 // create a remote cluster 96 rc := &model.RemoteCluster{ 97 RemoteId: model.NewId(), 98 Name: "Test1", 99 RemoteTeamId: model.NewId(), 100 SiteURL: model.NewId(), 101 CreatorId: model.NewId(), 102 } 103 rc, appErr := th.App.AddRemoteCluster(rc) 104 require.Nil(t, appErr) 105 106 // create a shared channel 107 sc := &model.SharedChannel{ 108 ChannelId: th.BasicChannel.Id, 109 TeamId: th.BasicChannel.TeamId, 110 Home: false, 111 ShareName: "test_share", 112 CreatorId: th.BasicChannel.CreatorId, 113 RemoteId: rc.RemoteId, 114 } 115 sc, err := th.App.SaveSharedChannel(sc) 116 require.NoError(t, err) 117 118 // create a shared channel remote to connect them 119 scr := &model.SharedChannelRemote{ 120 Id: model.NewId(), 121 ChannelId: sc.ChannelId, 122 CreatorId: sc.CreatorId, 123 IsInviteAccepted: true, 124 IsInviteConfirmed: true, 125 RemoteId: sc.RemoteId, 126 } 127 _, err = th.App.SaveSharedChannelRemote(scr) 128 require.NoError(t, err) 129 130 t.Run("valid remote, user is member", func(t *testing.T) { 131 rcInfo, resp := th.Client.GetRemoteClusterInfo(rc.RemoteId) 132 CheckNoError(t, resp) 133 assert.Equal(t, rc.Name, rcInfo.Name) 134 }) 135 136 t.Run("invalid remote", func(t *testing.T) { 137 _, resp := th.Client.GetRemoteClusterInfo(model.NewId()) 138 CheckNotFoundStatus(t, resp) 139 }) 140 141 } 142 143 func TestCreateDirectChannelWithRemoteUser(t *testing.T) { 144 t.Run("creates a local DM channel that is shared", func(t *testing.T) { 145 th := Setup(t).InitBasic() 146 defer th.TearDown() 147 Client := th.Client 148 defer Client.Logout() 149 150 localUser := th.BasicUser 151 remoteUser := th.CreateUser() 152 remoteUser.RemoteId = model.NewString(model.NewId()) 153 remoteUser, err := th.App.UpdateUser(remoteUser, false) 154 require.Nil(t, err) 155 156 dm, resp := Client.CreateDirectChannel(localUser.Id, remoteUser.Id) 157 CheckNoError(t, resp) 158 159 channelName := model.GetDMNameFromIds(localUser.Id, remoteUser.Id) 160 require.Equal(t, channelName, dm.Name, "dm name didn't match") 161 assert.True(t, dm.IsShared()) 162 }) 163 164 t.Run("sends a shared channel invitation to the remote", func(t *testing.T) { 165 th := Setup(t).InitBasic() 166 defer th.TearDown() 167 Client := th.Client 168 defer Client.Logout() 169 170 mockService := app.NewMockSharedChannelService(nil, app.MockOptionSharedChannelServiceWithActive(true)) 171 th.App.Srv().SetSharedChannelSyncService(mockService) 172 173 localUser := th.BasicUser 174 remoteUser := th.CreateUser() 175 rc := &model.RemoteCluster{ 176 Name: "test", 177 Token: model.NewId(), 178 CreatorId: localUser.Id, 179 } 180 rc, err := th.App.AddRemoteCluster(rc) 181 require.Nil(t, err) 182 183 remoteUser.RemoteId = model.NewString(rc.RemoteId) 184 remoteUser, err = th.App.UpdateUser(remoteUser, false) 185 require.Nil(t, err) 186 187 dm, resp := Client.CreateDirectChannel(localUser.Id, remoteUser.Id) 188 CheckNoError(t, resp) 189 190 channelName := model.GetDMNameFromIds(localUser.Id, remoteUser.Id) 191 require.Equal(t, channelName, dm.Name, "dm name didn't match") 192 require.True(t, dm.IsShared()) 193 194 assert.Equal(t, 1, mockService.NumInvitations()) 195 }) 196 197 t.Run("does not send a shared channel invitation to the remote when creator is remote", func(t *testing.T) { 198 th := Setup(t).InitBasic() 199 defer th.TearDown() 200 Client := th.Client 201 defer Client.Logout() 202 203 mockService := app.NewMockSharedChannelService(nil, app.MockOptionSharedChannelServiceWithActive(true)) 204 th.App.Srv().SetSharedChannelSyncService(mockService) 205 206 localUser := th.BasicUser 207 remoteUser := th.CreateUser() 208 rc := &model.RemoteCluster{ 209 Name: "test", 210 Token: model.NewId(), 211 CreatorId: localUser.Id, 212 } 213 rc, err := th.App.AddRemoteCluster(rc) 214 require.Nil(t, err) 215 216 remoteUser.RemoteId = model.NewString(rc.RemoteId) 217 remoteUser, err = th.App.UpdateUser(remoteUser, false) 218 require.Nil(t, err) 219 220 dm, resp := Client.CreateDirectChannel(remoteUser.Id, localUser.Id) 221 CheckNoError(t, resp) 222 223 channelName := model.GetDMNameFromIds(localUser.Id, remoteUser.Id) 224 require.Equal(t, channelName, dm.Name, "dm name didn't match") 225 require.True(t, dm.IsShared()) 226 227 assert.Zero(t, mockService.NumInvitations()) 228 }) 229 }