main

mattermost/focalboard

Last updated at: 29/12/2023 09:47

helpers_test.go

TLDR

The helpers_test.go file provides a test helper struct and methods for testing a WebSocket API in the ws package.

Methods

SetupTestHelper

This method sets up a test helper struct for testing the WebSocket API. It creates mock objects for the API, authentication interface, and store, and initializes a PluginAdapter with the mocks and a test logger. It returns the test helper struct.

ReceiveWebSocketMessage

This method simulates receiving a WebSocket message on a web connection. It takes the web connection ID, user ID, action, and data as parameters and creates a WebSocketRequest with the action and data. It then calls the WebSocketMessageHasBeenPosted method of the PluginAdapter with the web connection ID, user ID, and the created request.

SubscribeWebConnToTeam

This method simulates subscribing a web connection to a team. It takes the web connection ID, user ID, and team ID as parameters. It expects the DoesUserHaveTeamAccess method of the authentication interface to be called with the user ID and team ID and returns true. It then creates a message data map with the team ID and calls the ReceiveWebSocketMessage method with the web connection ID, user ID, websocketActionSubscribeTeam, and the message data.

UnsubscribeWebConnFromTeam

This method simulates unsubscribing a web connection from a team. It takes the web connection ID, user ID, and team ID as parameters. It creates a message data map with the team ID and calls the ReceiveWebSocketMessage method with the web connection ID, user ID, websocketActionUnsubscribeTeam, and the message data.

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"
	"github.com/mattermost/mattermost-server/v6/shared/mlog"

	"github.com/golang/mock/gomock"
)

type TestHelper struct {
	api   *wsMocks.MockAPI
	auth  *authMocks.MockAuthInterface
	store *wsMocks.MockStore
	ctrl  *gomock.Controller
	pa    *PluginAdapter
}

func SetupTestHelper(t *testing.T) *TestHelper {
	ctrl := gomock.NewController(t)
	mockAPI := wsMocks.NewMockAPI(ctrl)
	mockAuth := authMocks.NewMockAuthInterface(ctrl)
	mockStore := wsMocks.NewMockStore(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,
		store: mockStore,
		ctrl:  ctrl,
		pa:    NewPluginAdapter(mockAPI, mockAuth, mockStore, mlog.CreateConsoleTestLogger(true, mlog.LvlDebug)),
	}
}

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) SubscribeWebConnToTeam(webConnID, userID, teamID string) {
	th.auth.EXPECT().
		DoesUserHaveTeamAccess(userID, teamID).
		Return(true)

	msgData := map[string]interface{}{"teamId": teamID}
	th.ReceiveWebSocketMessage(webConnID, userID, websocketActionSubscribeTeam, msgData)
}

func (th *TestHelper) UnsubscribeWebConnFromTeam(webConnID, userID, teamID string) {
	msgData := map[string]interface{}{"teamId": teamID}
	th.ReceiveWebSocketMessage(webConnID, userID, websocketActionUnsubscribeTeam, msgData)
}