Spaces:
Sleeping
Sleeping
File size: 16,308 Bytes
13555f3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | package ws
import (
"sync"
"testing"
"github.com/mattermost/focalboard/server/model"
mmModel "github.com/mattermost/mattermost/server/public/model"
"github.com/stretchr/testify/require"
)
func TestPluginAdapterTeamSubscription(t *testing.T) {
th := SetupTestHelper(t)
webConnID := mmModel.NewId()
userID := mmModel.NewId()
teamID := mmModel.NewId()
var pac *PluginAdapterClient
t.Run("Should correctly add a connection", func(t *testing.T) {
require.Empty(t, th.pa.listeners)
require.Empty(t, th.pa.listenersByTeam)
th.pa.OnWebSocketConnect(webConnID, userID)
require.Len(t, th.pa.listeners, 1)
var ok bool
pac, ok = th.pa.listeners[webConnID]
require.True(t, ok)
require.NotNil(t, pac)
require.Equal(t, userID, pac.userID)
require.Empty(t, th.pa.listenersByTeam)
})
t.Run("Should correctly subscribe to a team", func(t *testing.T) {
require.False(t, pac.isSubscribedToTeam(teamID))
th.SubscribeWebConnToTeam(pac.webConnID, pac.userID, teamID)
require.Len(t, th.pa.listenersByTeam[teamID], 1)
require.Contains(t, th.pa.listenersByTeam[teamID], pac)
require.Len(t, pac.teams, 1)
require.Contains(t, pac.teams, teamID)
require.True(t, pac.isSubscribedToTeam(teamID))
})
t.Run("Subscribing again to a subscribed team would have no effect", func(t *testing.T) {
require.True(t, pac.isSubscribedToTeam(teamID))
th.SubscribeWebConnToTeam(pac.webConnID, pac.userID, teamID)
require.Len(t, th.pa.listenersByTeam[teamID], 1)
require.Contains(t, th.pa.listenersByTeam[teamID], pac)
require.Len(t, pac.teams, 1)
require.Contains(t, pac.teams, teamID)
require.True(t, pac.isSubscribedToTeam(teamID))
})
t.Run("Should correctly unsubscribe to a team", func(t *testing.T) {
require.True(t, pac.isSubscribedToTeam(teamID))
th.UnsubscribeWebConnFromTeam(pac.webConnID, pac.userID, teamID)
require.Empty(t, th.pa.listenersByTeam[teamID])
require.Empty(t, pac.teams)
require.False(t, pac.isSubscribedToTeam(teamID))
})
t.Run("Unsubscribing again to an unsubscribed team would have no effect", func(t *testing.T) {
require.False(t, pac.isSubscribedToTeam(teamID))
th.UnsubscribeWebConnFromTeam(pac.webConnID, pac.userID, teamID)
require.Empty(t, th.pa.listenersByTeam[teamID])
require.Empty(t, pac.teams)
require.False(t, pac.isSubscribedToTeam(teamID))
})
t.Run("Should correctly be marked as inactive if disconnected", func(t *testing.T) {
require.Len(t, th.pa.listeners, 1)
require.True(t, th.pa.listeners[webConnID].isActive())
th.pa.OnWebSocketDisconnect(webConnID, userID)
require.Len(t, th.pa.listeners, 1)
require.False(t, th.pa.listeners[webConnID].isActive())
})
t.Run("Should be marked back as active if reconnect", func(t *testing.T) {
require.Len(t, th.pa.listeners, 1)
require.False(t, th.pa.listeners[webConnID].isActive())
th.pa.OnWebSocketConnect(webConnID, userID)
require.Len(t, th.pa.listeners, 1)
require.True(t, th.pa.listeners[webConnID].isActive())
})
}
func TestPluginAdapterClientReconnect(t *testing.T) {
th := SetupTestHelper(t)
webConnID := mmModel.NewId()
userID := mmModel.NewId()
teamID := mmModel.NewId()
var pac *PluginAdapterClient
t.Run("A user should be able to reconnect within the accepted threshold and keep their subscriptions", func(t *testing.T) {
// create the connection
require.Len(t, th.pa.listeners, 0)
require.Len(t, th.pa.listenersByUserID[userID], 0)
th.pa.OnWebSocketConnect(webConnID, userID)
require.Len(t, th.pa.listeners, 1)
require.Len(t, th.pa.listenersByUserID[userID], 1)
var ok bool
pac, ok = th.pa.listeners[webConnID]
require.True(t, ok)
require.NotNil(t, pac)
th.SubscribeWebConnToTeam(pac.webConnID, pac.userID, teamID)
require.True(t, pac.isSubscribedToTeam(teamID))
// disconnect
th.pa.OnWebSocketDisconnect(webConnID, userID)
require.False(t, pac.isActive())
require.Len(t, th.pa.listeners, 1)
require.Len(t, th.pa.listenersByUserID[userID], 1)
// reconnect right away. The connection should still be subscribed
th.pa.OnWebSocketConnect(webConnID, userID)
require.Len(t, th.pa.listeners, 1)
require.Len(t, th.pa.listenersByUserID[userID], 1)
require.True(t, pac.isActive())
require.True(t, pac.isSubscribedToTeam(teamID))
})
t.Run("Should remove old inactive connection when user connects with a different ID", func(t *testing.T) {
// we set the stale threshold to zero so inactive connections always get deleted
oldStaleThreshold := th.pa.staleThreshold
th.pa.staleThreshold = 0
defer func() { th.pa.staleThreshold = oldStaleThreshold }()
th.pa.OnWebSocketDisconnect(webConnID, userID)
require.Len(t, th.pa.listeners, 1)
require.Len(t, th.pa.listenersByUserID[userID], 1)
require.Equal(t, webConnID, th.pa.listenersByUserID[userID][0].webConnID)
newWebConnID := mmModel.NewId()
th.pa.OnWebSocketConnect(newWebConnID, userID)
require.Len(t, th.pa.listeners, 1)
require.Len(t, th.pa.listenersByUserID[userID], 1)
require.Contains(t, th.pa.listeners, newWebConnID)
require.NotContains(t, th.pa.listeners, webConnID)
require.Equal(t, newWebConnID, th.pa.listenersByUserID[userID][0].webConnID)
// if the same ID connects again, it should have no subscriptions
th.pa.OnWebSocketConnect(webConnID, userID)
require.Len(t, th.pa.listeners, 2)
require.Len(t, th.pa.listenersByUserID[userID], 2)
reconnectedPAC, ok := th.pa.listeners[webConnID]
require.True(t, ok)
require.False(t, reconnectedPAC.isSubscribedToTeam(teamID))
})
t.Run("Should not remove active connections when user connects with a different ID", func(t *testing.T) {
// we set the stale threshold to zero so inactive connections always get deleted
oldStaleThreshold := th.pa.staleThreshold
th.pa.staleThreshold = 0
defer func() { th.pa.staleThreshold = oldStaleThreshold }()
// currently we have two listeners for userID, both active
require.Len(t, th.pa.listeners, 2)
// a new user connects
th.pa.OnWebSocketConnect(mmModel.NewId(), userID)
// and we should have three connections, all of them active
require.Len(t, th.pa.listeners, 3)
for _, listener := range th.pa.listeners {
require.True(t, listener.isActive())
}
})
}
func TestGetUserIDsForTeam(t *testing.T) {
th := SetupTestHelper(t)
// we have two teams
teamID1 := mmModel.NewId()
teamID2 := mmModel.NewId()
// user 1 has two connections
userID1 := mmModel.NewId()
webConnID1 := mmModel.NewId()
webConnID2 := mmModel.NewId()
// user 2 has one connection
userID2 := mmModel.NewId()
webConnID3 := mmModel.NewId()
wg := new(sync.WaitGroup)
wg.Add(3)
go func(wg *sync.WaitGroup) {
th.pa.OnWebSocketConnect(webConnID1, userID1)
th.SubscribeWebConnToTeam(webConnID1, userID1, teamID1)
wg.Done()
}(wg)
go func(wg *sync.WaitGroup) {
th.pa.OnWebSocketConnect(webConnID2, userID1)
th.SubscribeWebConnToTeam(webConnID2, userID1, teamID2)
wg.Done()
}(wg)
go func(wg *sync.WaitGroup) {
th.pa.OnWebSocketConnect(webConnID3, userID2)
th.SubscribeWebConnToTeam(webConnID3, userID2, teamID2)
wg.Done()
}(wg)
wg.Wait()
t.Run("should find that only user1 is connected to team 1", func(t *testing.T) {
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID1).
Return(true).
Times(1)
userIDs := th.pa.getUserIDsForTeam(teamID1)
require.ElementsMatch(t, []string{userID1}, userIDs)
})
t.Run("should find that both users are connected to team 2", func(t *testing.T) {
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID2).
Return(true).
Times(1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID2, teamID2).
Return(true).
Times(1)
userIDs := th.pa.getUserIDsForTeam(teamID2)
require.ElementsMatch(t, []string{userID1, userID2}, userIDs)
})
t.Run("should ignore user1 if webConn 2 inactive when getting team 2 user ids", func(t *testing.T) {
th.pa.OnWebSocketDisconnect(webConnID2, userID1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID2, teamID2).
Return(true).
Times(1)
userIDs := th.pa.getUserIDsForTeam(teamID2)
require.ElementsMatch(t, []string{userID2}, userIDs)
})
t.Run("should still find user 1 in team 1 after the webConn 2 disconnection", func(t *testing.T) {
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID1).
Return(true).
Times(1)
userIDs := th.pa.getUserIDsForTeam(teamID1)
require.ElementsMatch(t, []string{userID1}, userIDs)
})
t.Run("should find again both users if the webConn 2 comes back", func(t *testing.T) {
th.pa.OnWebSocketConnect(webConnID2, userID1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID2).
Return(true).
Times(1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID2, teamID2).
Return(true).
Times(1)
userIDs := th.pa.getUserIDsForTeam(teamID2)
require.ElementsMatch(t, []string{userID1, userID2}, userIDs)
})
t.Run("should only find user 1 if user 2 has an active connection but is not a team member anymore", func(t *testing.T) {
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID2).
Return(true).
Times(1)
// userID2 does not have team access
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID2, teamID2).
Return(false).
Times(1)
userIDs := th.pa.getUserIDsForTeam(teamID2)
require.ElementsMatch(t, []string{userID1}, userIDs)
})
}
func TestGetUserIDsForTeamAndBoard(t *testing.T) {
th := SetupTestHelper(t)
// we have two teams
teamID1 := mmModel.NewId()
boardID1 := mmModel.NewId()
teamID2 := mmModel.NewId()
boardID2 := mmModel.NewId()
// user 1 has two connections
userID1 := mmModel.NewId()
webConnID1 := mmModel.NewId()
webConnID2 := mmModel.NewId()
// user 2 has one connection
userID2 := mmModel.NewId()
webConnID3 := mmModel.NewId()
wg := new(sync.WaitGroup)
wg.Add(3)
go func(wg *sync.WaitGroup) {
th.pa.OnWebSocketConnect(webConnID1, userID1)
th.SubscribeWebConnToTeam(webConnID1, userID1, teamID1)
wg.Done()
}(wg)
go func(wg *sync.WaitGroup) {
th.pa.OnWebSocketConnect(webConnID2, userID1)
th.SubscribeWebConnToTeam(webConnID2, userID1, teamID2)
wg.Done()
}(wg)
go func(wg *sync.WaitGroup) {
th.pa.OnWebSocketConnect(webConnID3, userID2)
th.SubscribeWebConnToTeam(webConnID3, userID2, teamID2)
wg.Done()
}(wg)
wg.Wait()
t.Run("should find that only user1 is connected to team 1 and board 1", func(t *testing.T) {
mockedMembers := []*model.BoardMember{{UserID: userID1}}
th.store.EXPECT().
GetMembersForBoard(boardID1).
Return(mockedMembers, nil).
Times(1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID1).
Return(true).
Times(1)
userIDs := th.pa.getUserIDsForTeamAndBoard(teamID1, boardID1)
require.ElementsMatch(t, []string{userID1}, userIDs)
})
t.Run("should find that both users are connected to team 2 and board 2", func(t *testing.T) {
mockedMembers := []*model.BoardMember{{UserID: userID1}, {UserID: userID2}}
th.store.EXPECT().
GetMembersForBoard(boardID2).
Return(mockedMembers, nil).
Times(1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID2).
Return(true).
Times(1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID2, teamID2).
Return(true).
Times(1)
userIDs := th.pa.getUserIDsForTeamAndBoard(teamID2, boardID2)
require.ElementsMatch(t, []string{userID1, userID2}, userIDs)
})
t.Run("should find that only one user is connected to team 2 and board 2 if there is only one membership with both connected", func(t *testing.T) {
mockedMembers := []*model.BoardMember{{UserID: userID1}}
th.store.EXPECT().
GetMembersForBoard(boardID2).
Return(mockedMembers, nil).
Times(1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID2).
Return(true).
Times(1)
userIDs := th.pa.getUserIDsForTeamAndBoard(teamID2, boardID2)
require.ElementsMatch(t, []string{userID1}, userIDs)
})
t.Run("should find only one if the other is inactive", func(t *testing.T) {
th.pa.OnWebSocketDisconnect(webConnID3, userID2)
defer th.pa.OnWebSocketConnect(webConnID3, userID2)
mockedMembers := []*model.BoardMember{{UserID: userID1}, {UserID: userID2}}
th.store.EXPECT().
GetMembersForBoard(boardID2).
Return(mockedMembers, nil).
Times(1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID2).
Return(true).
Times(1)
userIDs := th.pa.getUserIDsForTeamAndBoard(teamID2, boardID2)
require.ElementsMatch(t, []string{userID1}, userIDs)
})
t.Run("should include a user that is not present if it's ensured", func(t *testing.T) {
userID3 := mmModel.NewId()
mockedMembers := []*model.BoardMember{{UserID: userID1}, {UserID: userID2}}
th.store.EXPECT().
GetMembersForBoard(boardID2).
Return(mockedMembers, nil).
Times(1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID2).
Return(true).
Times(1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID2, teamID2).
Return(true).
Times(1)
userIDs := th.pa.getUserIDsForTeamAndBoard(teamID2, boardID2, userID3)
require.ElementsMatch(t, []string{userID1, userID2, userID3}, userIDs)
})
t.Run("should not include a user that, although present, has no team access anymore", func(t *testing.T) {
mockedMembers := []*model.BoardMember{{UserID: userID1}, {UserID: userID2}}
th.store.EXPECT().
GetMembersForBoard(boardID2).
Return(mockedMembers, nil).
Times(1)
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID1, teamID2).
Return(true).
Times(1)
// userID2 has no team access
th.auth.EXPECT().
DoesUserHaveTeamAccess(userID2, teamID2).
Return(false).
Times(1)
userIDs := th.pa.getUserIDsForTeamAndBoard(teamID2, boardID2)
require.ElementsMatch(t, []string{userID1}, userIDs)
})
}
func TestParallelSubscriptionsOnMultipleConnections(t *testing.T) {
th := SetupTestHelper(t)
teamID1 := mmModel.NewId()
teamID2 := mmModel.NewId()
teamID3 := mmModel.NewId()
teamID4 := mmModel.NewId()
userID := mmModel.NewId()
webConnID1 := mmModel.NewId()
webConnID2 := mmModel.NewId()
th.pa.OnWebSocketConnect(webConnID1, userID)
pac1, ok := th.pa.GetListenerByWebConnID(webConnID1)
require.True(t, ok)
th.pa.OnWebSocketConnect(webConnID2, userID)
pac2, ok := th.pa.GetListenerByWebConnID(webConnID2)
require.True(t, ok)
wg := new(sync.WaitGroup)
wg.Add(4)
go func(wg *sync.WaitGroup) {
th.SubscribeWebConnToTeam(webConnID1, userID, teamID1)
require.True(t, pac1.isSubscribedToTeam(teamID1))
th.SubscribeWebConnToTeam(webConnID2, userID, teamID1)
require.True(t, pac2.isSubscribedToTeam(teamID1))
th.UnsubscribeWebConnFromTeam(webConnID1, userID, teamID1)
require.False(t, pac1.isSubscribedToTeam(teamID1))
th.UnsubscribeWebConnFromTeam(webConnID2, userID, teamID1)
require.False(t, pac2.isSubscribedToTeam(teamID1))
wg.Done()
}(wg)
go func(wg *sync.WaitGroup) {
th.SubscribeWebConnToTeam(webConnID1, userID, teamID2)
require.True(t, pac1.isSubscribedToTeam(teamID2))
th.SubscribeWebConnToTeam(webConnID2, userID, teamID2)
require.True(t, pac2.isSubscribedToTeam(teamID2))
th.UnsubscribeWebConnFromTeam(webConnID1, userID, teamID2)
require.False(t, pac1.isSubscribedToTeam(teamID2))
th.UnsubscribeWebConnFromTeam(webConnID2, userID, teamID2)
require.False(t, pac2.isSubscribedToTeam(teamID2))
wg.Done()
}(wg)
go func(wg *sync.WaitGroup) {
th.SubscribeWebConnToTeam(webConnID1, userID, teamID3)
require.True(t, pac1.isSubscribedToTeam(teamID3))
th.SubscribeWebConnToTeam(webConnID2, userID, teamID3)
require.True(t, pac2.isSubscribedToTeam(teamID3))
th.UnsubscribeWebConnFromTeam(webConnID1, userID, teamID3)
require.False(t, pac1.isSubscribedToTeam(teamID3))
th.UnsubscribeWebConnFromTeam(webConnID2, userID, teamID3)
require.False(t, pac2.isSubscribedToTeam(teamID3))
wg.Done()
}(wg)
go func(wg *sync.WaitGroup) {
th.SubscribeWebConnToTeam(webConnID1, userID, teamID4)
require.True(t, pac1.isSubscribedToTeam(teamID4))
th.SubscribeWebConnToTeam(webConnID2, userID, teamID4)
require.True(t, pac2.isSubscribedToTeam(teamID4))
th.UnsubscribeWebConnFromTeam(webConnID1, userID, teamID4)
require.False(t, pac1.isSubscribedToTeam(teamID4))
th.UnsubscribeWebConnFromTeam(webConnID2, userID, teamID4)
require.False(t, pac2.isSubscribedToTeam(teamID4))
wg.Done()
}(wg)
wg.Wait()
}
|