2021-09-29 18:19:34 +02:00
|
|
|
package ws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
authMocks "github.com/mattermost/focalboard/server/auth/mocks"
|
|
|
|
wsMocks "github.com/mattermost/focalboard/server/ws/mocks"
|
|
|
|
|
|
|
|
mmModel "github.com/mattermost/mattermost-server/v6/model"
|
2022-02-08 00:04:24 +01:00
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2021-09-29 18:19:34 +02:00
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestHelper struct {
|
|
|
|
api *wsMocks.MockAPI
|
|
|
|
auth *authMocks.MockAuthInterface
|
|
|
|
ctrl *gomock.Controller
|
|
|
|
pa *PluginAdapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetupTestHelper(t *testing.T) *TestHelper {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
mockAPI := wsMocks.NewMockAPI(ctrl)
|
|
|
|
mockAuth := authMocks.NewMockAuthInterface(ctrl)
|
|
|
|
|
|
|
|
mockAPI.EXPECT().LogDebug(gomock.Any(), gomock.Any()).AnyTimes()
|
|
|
|
mockAPI.EXPECT().LogInfo(gomock.Any(), gomock.Any()).AnyTimes()
|
|
|
|
mockAPI.EXPECT().LogError(gomock.Any(), gomock.Any()).AnyTimes()
|
|
|
|
mockAPI.EXPECT().LogWarn(gomock.Any(), gomock.Any()).AnyTimes()
|
|
|
|
|
|
|
|
return &TestHelper{
|
|
|
|
api: mockAPI,
|
|
|
|
auth: mockAuth,
|
|
|
|
ctrl: ctrl,
|
2022-02-08 00:04:24 +01:00
|
|
|
pa: NewPluginAdapter(mockAPI, mockAuth, mlog.CreateConsoleTestLogger(true, mlog.LvlDebug)),
|
2021-09-29 18:19:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *TestHelper) ReceiveWebSocketMessage(webConnID, userID, action string, data map[string]interface{}) {
|
|
|
|
req := &mmModel.WebSocketRequest{Action: websocketMessagePrefix + action, Data: data}
|
|
|
|
|
|
|
|
th.pa.WebSocketMessageHasBeenPosted(webConnID, userID, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *TestHelper) SubscribeWebConnToWorkspace(webConnID, userID, workspaceID string) {
|
|
|
|
th.auth.EXPECT().
|
|
|
|
DoesUserHaveWorkspaceAccess(userID, workspaceID).
|
|
|
|
Return(true)
|
|
|
|
|
|
|
|
msgData := map[string]interface{}{"workspaceId": workspaceID}
|
|
|
|
th.ReceiveWebSocketMessage(webConnID, userID, websocketActionSubscribeWorkspace, msgData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *TestHelper) UnsubscribeWebConnFromWorkspace(webConnID, userID, workspaceID string) {
|
|
|
|
msgData := map[string]interface{}{"workspaceId": workspaceID}
|
|
|
|
th.ReceiveWebSocketMessage(webConnID, userID, websocketActionUnsubscribeWorkspace, msgData)
|
|
|
|
}
|