github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+incompatible/app/channel_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "testing" 8 9 "github.com/mattermost/mattermost-server/model" 10 "github.com/mattermost/mattermost-server/store" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestPermanentDeleteChannel(t *testing.T) { 15 th := Setup().InitBasic() 16 defer th.TearDown() 17 18 th.App.UpdateConfig(func(cfg *model.Config) { 19 cfg.ServiceSettings.EnableIncomingWebhooks = true 20 cfg.ServiceSettings.EnableOutgoingWebhooks = true 21 }) 22 23 channel, err := th.App.CreateChannel(&model.Channel{DisplayName: "deletion-test", Name: "deletion-test", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id}, false) 24 if err != nil { 25 t.Fatal(err.Error()) 26 } 27 defer func() { 28 th.App.PermanentDeleteChannel(channel) 29 }() 30 31 incoming, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, channel, &model.IncomingWebhook{ChannelId: channel.Id}) 32 if err != nil { 33 t.Fatal(err.Error()) 34 } 35 defer th.App.DeleteIncomingWebhook(incoming.Id) 36 37 if incoming, err = th.App.GetIncomingWebhook(incoming.Id); incoming == nil || err != nil { 38 t.Fatal("unable to get new incoming webhook") 39 } 40 41 outgoing, err := th.App.CreateOutgoingWebhook(&model.OutgoingWebhook{ 42 ChannelId: channel.Id, 43 TeamId: channel.TeamId, 44 CreatorId: th.BasicUser.Id, 45 CallbackURLs: []string{"http://foo"}, 46 }) 47 if err != nil { 48 t.Fatal(err.Error()) 49 } 50 defer th.App.DeleteOutgoingWebhook(outgoing.Id) 51 52 if outgoing, err = th.App.GetOutgoingWebhook(outgoing.Id); outgoing == nil || err != nil { 53 t.Fatal("unable to get new outgoing webhook") 54 } 55 56 if err := th.App.PermanentDeleteChannel(channel); err != nil { 57 t.Fatal(err.Error()) 58 } 59 60 if incoming, err = th.App.GetIncomingWebhook(incoming.Id); incoming != nil || err == nil { 61 t.Error("incoming webhook wasn't deleted") 62 } 63 64 if outgoing, err = th.App.GetOutgoingWebhook(outgoing.Id); outgoing != nil || err == nil { 65 t.Error("outgoing webhook wasn't deleted") 66 } 67 } 68 69 func TestMoveChannel(t *testing.T) { 70 th := Setup().InitBasic() 71 defer th.TearDown() 72 73 sourceTeam := th.CreateTeam() 74 targetTeam := th.CreateTeam() 75 channel1 := th.CreateChannel(sourceTeam) 76 defer func() { 77 th.App.PermanentDeleteChannel(channel1) 78 th.App.PermanentDeleteTeam(sourceTeam) 79 th.App.PermanentDeleteTeam(targetTeam) 80 }() 81 82 if _, err := th.App.AddUserToTeam(sourceTeam.Id, th.BasicUser.Id, ""); err != nil { 83 t.Fatal(err) 84 } 85 if _, err := th.App.AddUserToTeam(sourceTeam.Id, th.BasicUser2.Id, ""); err != nil { 86 t.Fatal(err) 87 } 88 89 if _, err := th.App.AddUserToTeam(targetTeam.Id, th.BasicUser.Id, ""); err != nil { 90 t.Fatal(err) 91 } 92 93 if _, err := th.App.AddUserToChannel(th.BasicUser, channel1); err != nil { 94 t.Fatal(err) 95 } 96 if _, err := th.App.AddUserToChannel(th.BasicUser2, channel1); err != nil { 97 t.Fatal(err) 98 } 99 100 if err := th.App.MoveChannel(targetTeam, channel1, th.BasicUser); err == nil { 101 t.Fatal("Should have failed due to mismatched members.") 102 } 103 104 if _, err := th.App.AddUserToTeam(targetTeam.Id, th.BasicUser2.Id, ""); err != nil { 105 t.Fatal(err) 106 } 107 108 if err := th.App.MoveChannel(targetTeam, channel1, th.BasicUser); err != nil { 109 t.Fatal(err) 110 } 111 } 112 113 func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordTownSquare(t *testing.T) { 114 th := Setup().InitBasic() 115 defer th.TearDown() 116 117 // figure out the initial number of users in town square 118 townSquareChannelId := store.Must(th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "town-square", true)).(*model.Channel).Id 119 initialNumTownSquareUsers := len(store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, townSquareChannelId)).([]*model.ChannelMemberHistoryResult)) 120 121 // create a new user that joins the default channels 122 user := th.CreateUser() 123 th.App.JoinDefaultChannels(th.BasicTeam.Id, user, model.CHANNEL_USER_ROLE_ID, "") 124 125 // there should be a ChannelMemberHistory record for the user 126 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, townSquareChannelId)).([]*model.ChannelMemberHistoryResult) 127 assert.Len(t, histories, initialNumTownSquareUsers+1) 128 129 found := false 130 for _, history := range histories { 131 if user.Id == history.UserId && townSquareChannelId == history.ChannelId { 132 found = true 133 break 134 } 135 } 136 assert.True(t, found) 137 } 138 139 func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordOffTopic(t *testing.T) { 140 th := Setup().InitBasic() 141 defer th.TearDown() 142 143 // figure out the initial number of users in off-topic 144 offTopicChannelId := store.Must(th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "off-topic", true)).(*model.Channel).Id 145 initialNumTownSquareUsers := len(store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, offTopicChannelId)).([]*model.ChannelMemberHistoryResult)) 146 147 // create a new user that joins the default channels 148 user := th.CreateUser() 149 th.App.JoinDefaultChannels(th.BasicTeam.Id, user, model.CHANNEL_USER_ROLE_ID, "") 150 151 // there should be a ChannelMemberHistory record for the user 152 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, offTopicChannelId)).([]*model.ChannelMemberHistoryResult) 153 assert.Len(t, histories, initialNumTownSquareUsers+1) 154 155 found := false 156 for _, history := range histories { 157 if user.Id == history.UserId && offTopicChannelId == history.ChannelId { 158 found = true 159 break 160 } 161 } 162 assert.True(t, found) 163 } 164 165 func TestCreateChannelPublicCreatesChannelMemberHistoryRecord(t *testing.T) { 166 th := Setup().InitBasic() 167 defer th.TearDown() 168 169 // creates a public channel and adds basic user to it 170 publicChannel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN) 171 172 // there should be a ChannelMemberHistory record for the user 173 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistoryResult) 174 assert.Len(t, histories, 1) 175 assert.Equal(t, th.BasicUser.Id, histories[0].UserId) 176 assert.Equal(t, publicChannel.Id, histories[0].ChannelId) 177 } 178 179 func TestCreateChannelPrivateCreatesChannelMemberHistoryRecord(t *testing.T) { 180 th := Setup().InitBasic() 181 defer th.TearDown() 182 183 // creates a private channel and adds basic user to it 184 privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE) 185 186 // there should be a ChannelMemberHistory record for the user 187 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, privateChannel.Id)).([]*model.ChannelMemberHistoryResult) 188 assert.Len(t, histories, 1) 189 assert.Equal(t, th.BasicUser.Id, histories[0].UserId) 190 assert.Equal(t, privateChannel.Id, histories[0].ChannelId) 191 } 192 193 func TestUpdateChannelPrivacy(t *testing.T) { 194 th := Setup().InitBasic() 195 defer th.TearDown() 196 197 privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE) 198 privateChannel.Type = model.CHANNEL_OPEN 199 200 if publicChannel, err := th.App.UpdateChannelPrivacy(privateChannel, th.BasicUser); err != nil { 201 t.Fatal("Failed to update channel privacy. Error: " + err.Error()) 202 } else { 203 assert.Equal(t, publicChannel.Id, privateChannel.Id) 204 assert.Equal(t, publicChannel.Type, model.CHANNEL_OPEN) 205 } 206 } 207 208 func TestCreateGroupChannelCreatesChannelMemberHistoryRecord(t *testing.T) { 209 th := Setup().InitBasic() 210 defer th.TearDown() 211 212 user1 := th.CreateUser() 213 user2 := th.CreateUser() 214 215 groupUserIds := make([]string, 0) 216 groupUserIds = append(groupUserIds, user1.Id) 217 groupUserIds = append(groupUserIds, user2.Id) 218 groupUserIds = append(groupUserIds, th.BasicUser.Id) 219 220 if channel, err := th.App.CreateGroupChannel(groupUserIds, th.BasicUser.Id); err != nil { 221 t.Fatal("Failed to create group channel. Error: " + err.Message) 222 } else { 223 // there should be a ChannelMemberHistory record for each user 224 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult) 225 assert.Len(t, histories, 3) 226 227 channelMemberHistoryUserIds := make([]string, 0) 228 for _, history := range histories { 229 assert.Equal(t, channel.Id, history.ChannelId) 230 channelMemberHistoryUserIds = append(channelMemberHistoryUserIds, history.UserId) 231 } 232 assert.Equal(t, groupUserIds, channelMemberHistoryUserIds) 233 } 234 } 235 236 func TestCreateDirectChannelCreatesChannelMemberHistoryRecord(t *testing.T) { 237 th := Setup().InitBasic() 238 defer th.TearDown() 239 240 user1 := th.CreateUser() 241 user2 := th.CreateUser() 242 243 if channel, err := th.App.CreateDirectChannel(user1.Id, user2.Id); err != nil { 244 t.Fatal("Failed to create direct channel. Error: " + err.Message) 245 } else { 246 // there should be a ChannelMemberHistory record for both users 247 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult) 248 assert.Len(t, histories, 2) 249 250 historyId0 := histories[0].UserId 251 historyId1 := histories[1].UserId 252 switch historyId0 { 253 case user1.Id: 254 assert.Equal(t, user2.Id, historyId1) 255 case user2.Id: 256 assert.Equal(t, user1.Id, historyId1) 257 default: 258 t.Fatal("Unexpected user id " + historyId0 + " in ChannelMemberHistory table") 259 } 260 } 261 } 262 263 func TestGetDirectChannelCreatesChannelMemberHistoryRecord(t *testing.T) { 264 th := Setup().InitBasic() 265 defer th.TearDown() 266 267 user1 := th.CreateUser() 268 user2 := th.CreateUser() 269 270 // this function call implicitly creates a direct channel between the two users if one doesn't already exist 271 if channel, err := th.App.GetDirectChannel(user1.Id, user2.Id); err != nil { 272 t.Fatal("Failed to create direct channel. Error: " + err.Message) 273 } else { 274 // there should be a ChannelMemberHistory record for both users 275 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult) 276 assert.Len(t, histories, 2) 277 278 historyId0 := histories[0].UserId 279 historyId1 := histories[1].UserId 280 switch historyId0 { 281 case user1.Id: 282 assert.Equal(t, user2.Id, historyId1) 283 case user2.Id: 284 assert.Equal(t, user1.Id, historyId1) 285 default: 286 t.Fatal("Unexpected user id " + historyId0 + " in ChannelMemberHistory table") 287 } 288 } 289 } 290 291 func TestAddUserToChannelCreatesChannelMemberHistoryRecord(t *testing.T) { 292 th := Setup().InitBasic() 293 defer th.TearDown() 294 295 // create a user and add it to a channel 296 user := th.CreateUser() 297 if _, err := th.App.AddTeamMember(th.BasicTeam.Id, user.Id); err != nil { 298 t.Fatal("Failed to add user to team. Error: " + err.Message) 299 } 300 301 groupUserIds := make([]string, 0) 302 groupUserIds = append(groupUserIds, th.BasicUser.Id) 303 groupUserIds = append(groupUserIds, user.Id) 304 305 channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN) 306 if _, err := th.App.AddUserToChannel(user, channel); err != nil { 307 t.Fatal("Failed to add user to channel. Error: " + err.Message) 308 } 309 310 // there should be a ChannelMemberHistory record for the user 311 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult) 312 assert.Len(t, histories, 2) 313 channelMemberHistoryUserIds := make([]string, 0) 314 for _, history := range histories { 315 assert.Equal(t, channel.Id, history.ChannelId) 316 channelMemberHistoryUserIds = append(channelMemberHistoryUserIds, history.UserId) 317 } 318 assert.Equal(t, groupUserIds, channelMemberHistoryUserIds) 319 } 320 321 func TestRemoveUserFromChannelUpdatesChannelMemberHistoryRecord(t *testing.T) { 322 th := Setup().InitBasic() 323 defer th.TearDown() 324 325 // a user creates a channel 326 publicChannel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN) 327 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistoryResult) 328 assert.Len(t, histories, 1) 329 assert.Equal(t, th.BasicUser.Id, histories[0].UserId) 330 assert.Equal(t, publicChannel.Id, histories[0].ChannelId) 331 assert.Nil(t, histories[0].LeaveTime) 332 333 // the user leaves that channel 334 if err := th.App.LeaveChannel(publicChannel.Id, th.BasicUser.Id); err != nil { 335 t.Fatal("Failed to remove user from channel. Error: " + err.Message) 336 } 337 histories = store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistoryResult) 338 assert.Len(t, histories, 1) 339 assert.Equal(t, th.BasicUser.Id, histories[0].UserId) 340 assert.Equal(t, publicChannel.Id, histories[0].ChannelId) 341 assert.NotNil(t, histories[0].LeaveTime) 342 }