2021-07-19 11:34:17 +02:00
|
|
|
package ws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/auth"
|
2022-04-11 19:14:58 +02:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
func TestTeamSubscription(t *testing.T) {
|
|
|
|
server := NewServer(&auth.Auth{}, "token", false, &mlog.Logger{}, nil)
|
|
|
|
session := &websocketSession{
|
|
|
|
conn: &websocket.Conn{},
|
|
|
|
mu: sync.Mutex{},
|
|
|
|
teams: []string{},
|
|
|
|
blocks: []string{},
|
|
|
|
}
|
|
|
|
teamID := "fake-team-id"
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
t.Run("Should correctly add a session", func(t *testing.T) {
|
2022-03-22 16:24:34 +02:00
|
|
|
server.addListener(session)
|
2021-07-19 11:34:17 +02:00
|
|
|
require.Len(t, server.listeners, 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Empty(t, server.listenersByTeam)
|
|
|
|
require.Empty(t, session.teams)
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
t.Run("Should correctly subscribe to a team", func(t *testing.T) {
|
|
|
|
require.False(t, session.isSubscribedToTeam(teamID))
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.subscribeListenerToTeam(session, teamID)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Len(t, server.listenersByTeam[teamID], 1)
|
|
|
|
require.Contains(t, server.listenersByTeam[teamID], session)
|
|
|
|
require.Len(t, session.teams, 1)
|
|
|
|
require.Contains(t, session.teams, teamID)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.True(t, session.isSubscribedToTeam(teamID))
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
t.Run("Subscribing again to a subscribed team would have no effect", func(t *testing.T) {
|
|
|
|
require.True(t, session.isSubscribedToTeam(teamID))
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.subscribeListenerToTeam(session, teamID)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Len(t, server.listenersByTeam[teamID], 1)
|
|
|
|
require.Contains(t, server.listenersByTeam[teamID], session)
|
|
|
|
require.Len(t, session.teams, 1)
|
|
|
|
require.Contains(t, session.teams, teamID)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.True(t, session.isSubscribedToTeam(teamID))
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
t.Run("Should correctly unsubscribe to a team", func(t *testing.T) {
|
|
|
|
require.True(t, session.isSubscribedToTeam(teamID))
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.unsubscribeListenerFromTeam(session, teamID)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Empty(t, server.listenersByTeam[teamID])
|
|
|
|
require.Empty(t, session.teams)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.False(t, session.isSubscribedToTeam(teamID))
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
t.Run("Unsubscribing again to an unsubscribed team would have no effect", func(t *testing.T) {
|
|
|
|
require.False(t, session.isSubscribedToTeam(teamID))
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.unsubscribeListenerFromTeam(session, teamID)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Empty(t, server.listenersByTeam[teamID])
|
|
|
|
require.Empty(t, session.teams)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.False(t, session.isSubscribedToTeam(teamID))
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Should correctly be removed from the server", func(t *testing.T) {
|
2022-03-22 16:24:34 +02:00
|
|
|
server.removeListener(session)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
require.Empty(t, server.listeners)
|
|
|
|
})
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
t.Run("If subscribed to teams and removed, should be removed from the teams subscription list", func(t *testing.T) {
|
|
|
|
teamID2 := "other-fake-team-id"
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.addListener(session)
|
|
|
|
server.subscribeListenerToTeam(session, teamID)
|
|
|
|
server.subscribeListenerToTeam(session, teamID2)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
require.Len(t, server.listeners, 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Contains(t, server.listenersByTeam[teamID], session)
|
|
|
|
require.Contains(t, server.listenersByTeam[teamID2], session)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.removeListener(session)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
require.Empty(t, server.listeners)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Empty(t, server.listenersByTeam[teamID])
|
|
|
|
require.Empty(t, server.listenersByTeam[teamID2])
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
2023-03-30 00:27:18 +02:00
|
|
|
|
|
|
|
t.Run("Subscribe users to team retrieve by user", func(t *testing.T) {
|
|
|
|
userID1 := "fake-user-id"
|
|
|
|
userSession1 := &websocketSession{
|
|
|
|
conn: &websocket.Conn{},
|
|
|
|
mu: sync.Mutex{},
|
|
|
|
userID: userID1,
|
|
|
|
teams: []string{},
|
|
|
|
blocks: []string{},
|
|
|
|
}
|
|
|
|
userID2 := "fake-user-id2"
|
|
|
|
userSession2 := &websocketSession{
|
|
|
|
conn: &websocket.Conn{},
|
|
|
|
mu: sync.Mutex{},
|
|
|
|
userID: userID2,
|
|
|
|
teams: []string{},
|
|
|
|
blocks: []string{},
|
|
|
|
}
|
|
|
|
teamID := "fake-team-id"
|
|
|
|
|
|
|
|
server.addListener(session)
|
|
|
|
server.subscribeListenerToTeam(session, teamID)
|
|
|
|
server.addListener(userSession1)
|
|
|
|
server.subscribeListenerToTeam(userSession1, teamID)
|
|
|
|
server.addListener(userSession2)
|
|
|
|
server.subscribeListenerToTeam(userSession2, teamID)
|
|
|
|
|
|
|
|
require.Len(t, server.listeners, 3)
|
|
|
|
require.Len(t, server.listenersByTeam[teamID], 3)
|
|
|
|
|
|
|
|
listener := server.getListenerForUser(teamID, userID1)
|
|
|
|
require.NotNil(t, listener)
|
|
|
|
require.Equal(t, listener.userID, userID1)
|
|
|
|
|
|
|
|
server.removeListener(session)
|
|
|
|
server.removeListener(userSession1)
|
|
|
|
server.removeListener(userSession2)
|
|
|
|
|
|
|
|
require.Empty(t, server.listeners)
|
|
|
|
require.Empty(t, server.listenersByTeam[teamID])
|
|
|
|
require.Empty(t, server.getListenerForUser(teamID, userID1))
|
|
|
|
})
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlocksSubscription(t *testing.T) {
|
2022-03-22 16:24:34 +02:00
|
|
|
server := NewServer(&auth.Auth{}, "token", false, &mlog.Logger{}, nil)
|
|
|
|
session := &websocketSession{
|
|
|
|
conn: &websocket.Conn{},
|
|
|
|
mu: sync.Mutex{},
|
|
|
|
teams: []string{},
|
|
|
|
blocks: []string{},
|
|
|
|
}
|
2021-07-19 11:34:17 +02:00
|
|
|
blockID1 := "block1"
|
|
|
|
blockID2 := "block2"
|
|
|
|
blockID3 := "block3"
|
|
|
|
blockIDs := []string{blockID1, blockID2, blockID3}
|
|
|
|
|
|
|
|
t.Run("Should correctly add a session", func(t *testing.T) {
|
2022-03-22 16:24:34 +02:00
|
|
|
server.addListener(session)
|
2021-07-19 11:34:17 +02:00
|
|
|
require.Len(t, server.listeners, 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Empty(t, server.listenersByTeam)
|
|
|
|
require.Empty(t, session.teams)
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Should correctly subscribe to a set of blocks", func(t *testing.T) {
|
2022-03-22 16:24:34 +02:00
|
|
|
require.False(t, session.isSubscribedToBlock(blockID1))
|
|
|
|
require.False(t, session.isSubscribedToBlock(blockID2))
|
|
|
|
require.False(t, session.isSubscribedToBlock(blockID3))
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.subscribeListenerToBlocks(session, blockIDs)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
require.Len(t, server.listenersByBlock[blockID1], 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Contains(t, server.listenersByBlock[blockID1], session)
|
2021-07-19 11:34:17 +02:00
|
|
|
require.Len(t, server.listenersByBlock[blockID2], 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Contains(t, server.listenersByBlock[blockID2], session)
|
2021-07-19 11:34:17 +02:00
|
|
|
require.Len(t, server.listenersByBlock[blockID3], 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Contains(t, server.listenersByBlock[blockID3], session)
|
|
|
|
require.Len(t, session.blocks, 3)
|
|
|
|
require.ElementsMatch(t, blockIDs, session.blocks)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.True(t, session.isSubscribedToBlock(blockID1))
|
|
|
|
require.True(t, session.isSubscribedToBlock(blockID2))
|
|
|
|
require.True(t, session.isSubscribedToBlock(blockID3))
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
t.Run("Subscribing again to a subscribed block would have no effect", func(t *testing.T) {
|
2022-03-22 16:24:34 +02:00
|
|
|
require.True(t, session.isSubscribedToBlock(blockID1))
|
|
|
|
require.True(t, session.isSubscribedToBlock(blockID2))
|
|
|
|
require.True(t, session.isSubscribedToBlock(blockID3))
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.subscribeListenerToBlocks(session, blockIDs)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
require.Len(t, server.listenersByBlock[blockID1], 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Contains(t, server.listenersByBlock[blockID1], session)
|
2021-07-19 11:34:17 +02:00
|
|
|
require.Len(t, server.listenersByBlock[blockID2], 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Contains(t, server.listenersByBlock[blockID2], session)
|
2021-07-19 11:34:17 +02:00
|
|
|
require.Len(t, server.listenersByBlock[blockID3], 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Contains(t, server.listenersByBlock[blockID3], session)
|
|
|
|
require.Len(t, session.blocks, 3)
|
|
|
|
require.ElementsMatch(t, blockIDs, session.blocks)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.True(t, session.isSubscribedToBlock(blockID1))
|
|
|
|
require.True(t, session.isSubscribedToBlock(blockID2))
|
|
|
|
require.True(t, session.isSubscribedToBlock(blockID3))
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Should correctly unsubscribe to a set of blocks", func(t *testing.T) {
|
2022-03-22 16:24:34 +02:00
|
|
|
require.True(t, session.isSubscribedToBlock(blockID1))
|
|
|
|
require.True(t, session.isSubscribedToBlock(blockID2))
|
|
|
|
require.True(t, session.isSubscribedToBlock(blockID3))
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.unsubscribeListenerFromBlocks(session, blockIDs)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
require.Empty(t, server.listenersByBlock[blockID1])
|
|
|
|
require.Empty(t, server.listenersByBlock[blockID2])
|
|
|
|
require.Empty(t, server.listenersByBlock[blockID3])
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Empty(t, session.blocks)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.False(t, session.isSubscribedToBlock(blockID1))
|
|
|
|
require.False(t, session.isSubscribedToBlock(blockID2))
|
|
|
|
require.False(t, session.isSubscribedToBlock(blockID3))
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Unsubscribing again to an unsubscribed block would have no effect", func(t *testing.T) {
|
2022-03-22 16:24:34 +02:00
|
|
|
require.False(t, session.isSubscribedToBlock(blockID1))
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.unsubscribeListenerFromBlocks(session, []string{blockID1})
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
require.Empty(t, server.listenersByBlock[blockID1])
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Empty(t, session.blocks)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
require.False(t, session.isSubscribedToBlock(blockID1))
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Should correctly be removed from the server", func(t *testing.T) {
|
2022-03-22 16:24:34 +02:00
|
|
|
server.removeListener(session)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
require.Empty(t, server.listeners)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("If subscribed to blocks and removed, should be removed from the blocks subscription list", func(t *testing.T) {
|
2022-03-22 16:24:34 +02:00
|
|
|
server.addListener(session)
|
|
|
|
server.subscribeListenerToBlocks(session, blockIDs)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
require.Len(t, server.listeners, 1)
|
|
|
|
require.Len(t, server.listenersByBlock[blockID1], 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Contains(t, server.listenersByBlock[blockID1], session)
|
2021-07-19 11:34:17 +02:00
|
|
|
require.Len(t, server.listenersByBlock[blockID2], 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Contains(t, server.listenersByBlock[blockID2], session)
|
2021-07-19 11:34:17 +02:00
|
|
|
require.Len(t, server.listenersByBlock[blockID3], 1)
|
2022-03-22 16:24:34 +02:00
|
|
|
require.Contains(t, server.listenersByBlock[blockID3], session)
|
|
|
|
require.Len(t, session.blocks, 3)
|
|
|
|
require.ElementsMatch(t, blockIDs, session.blocks)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
server.removeListener(session)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
require.Empty(t, server.listeners)
|
|
|
|
require.Empty(t, server.listenersByBlock[blockID1])
|
|
|
|
require.Empty(t, server.listenersByBlock[blockID2])
|
|
|
|
require.Empty(t, server.listenersByBlock[blockID3])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUserIDForTokenInSingleUserMode(t *testing.T) {
|
|
|
|
singleUserToken := "single-user-token"
|
2022-03-22 16:24:34 +02:00
|
|
|
server := NewServer(&auth.Auth{}, "token", false, &mlog.Logger{}, nil)
|
2021-07-19 11:34:17 +02:00
|
|
|
server.singleUserToken = singleUserToken
|
|
|
|
|
|
|
|
t.Run("Should return nothing if the token is empty", func(t *testing.T) {
|
|
|
|
require.Empty(t, server.getUserIDForToken(""))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Should return nothing if the token is invalid", func(t *testing.T) {
|
|
|
|
require.Empty(t, server.getUserIDForToken("invalid-token"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Should return the single user ID if the token is correct", func(t *testing.T) {
|
2022-04-11 19:14:58 +02:00
|
|
|
require.Equal(t, model.SingleUser, server.getUserIDForToken(singleUserToken))
|
2021-07-19 11:34:17 +02:00
|
|
|
})
|
|
|
|
}
|