From d50846ed42e7061be7b099c3e322674f789b752b Mon Sep 17 00:00:00 2001 From: Shivashis Padhi Date: Mon, 8 Aug 2022 19:04:03 +0530 Subject: [PATCH] MM-43781: boards insights - cherry pick PR suggestions (#3596) * Uncomment team insights test * Add checks to make sure insights endpoints work only in plugin mode * adding constant for my insights Co-authored-by: Benjamin Cooke --- mattermost-plugin/webapp/src/index.tsx | 3 ++- server/api/api.go | 11 ++++++++ .../store/storetests/board_insights.go | 26 +++++++++---------- webapp/src/constants.ts | 2 ++ 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/mattermost-plugin/webapp/src/index.tsx b/mattermost-plugin/webapp/src/index.tsx index c92995ebc..661185b4a 100644 --- a/mattermost-plugin/webapp/src/index.tsx +++ b/mattermost-plugin/webapp/src/index.tsx @@ -37,6 +37,7 @@ import '../../../webapp/src/styles/focalboard-variables.scss' import '../../../webapp/src/styles/main.scss' import '../../../webapp/src/styles/labels.scss' import octoClient from '../../../webapp/src/octoClient' +import {Constants} from '../../../webapp/src/constants' import BoardsUnfurl from './components/boardsUnfurl/boardsUnfurl' import RHSChannelBoards from './components/rhsChannelBoards' @@ -323,7 +324,7 @@ export default class Plugin { // Insights handler if (this.registry?.registerInsightsHandler) { this.registry?.registerInsightsHandler(async (timeRange: string, page: number, perPage: number, teamId: string, insightType: string) => { - if (insightType === 'MY') { + if (insightType === Constants.myInsights) { const data = await octoClient.getMyTopBoards(timeRange, page, perPage, teamId) return data diff --git a/server/api/api.go b/server/api/api.go index b1a13e825..5b717c6ba 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -2226,6 +2226,11 @@ func (a *API) handleTeamBoardsInsights(w http.ResponseWriter, r *http.Request) { // schema: // "$ref": "#/definitions/ErrorResponse" + if !a.MattermostAuth { + a.errorResponse(w, r.URL.Path, http.StatusNotImplemented, "not permitted in standalone mode", nil) + return + } + vars := mux.Vars(r) teamID := vars["teamID"] userID := getUserID(r) @@ -2333,6 +2338,12 @@ func (a *API) handleUserBoardsInsights(w http.ResponseWriter, r *http.Request) { // description: internal error // schema: // "$ref": "#/definitions/ErrorResponse" + + if !a.MattermostAuth { + a.errorResponse(w, r.URL.Path, http.StatusNotImplemented, "not permitted in standalone mode", nil) + return + } + userID := getUserID(r) query := r.URL.Query() teamID := query.Get("team_id") diff --git a/server/services/store/storetests/board_insights.go b/server/services/store/storetests/board_insights.go index df1b6b114..4bc6f0f95 100644 --- a/server/services/store/storetests/board_insights.go +++ b/server/services/store/storetests/board_insights.go @@ -1,6 +1,7 @@ package storetests import ( + "strconv" "testing" "github.com/mattermost/focalboard/server/model" @@ -69,19 +70,18 @@ func getBoardsInsightsTest(t *testing.T, store store.Store) { boardsUser1, _ := store.GetBoardsForUserAndTeam(testUserID, testTeamID) boardsUser2, _ := store.GetBoardsForUserAndTeam(testInsightsUserID1, testTeamID) - // t.Run("team insights", func(t *testing.T) { - // boardIDs := []string{boardsUser1[0].ID, boardsUser1[1].ID, boardsUser1[2].ID} - // topTeamBoards, err := store.GetTeamBoardsInsights(testTeamID, testUserID, - // 0, 0, 10, boardIDs) - // require.NoError(t, err) - // require.Len(t, topTeamBoards.Items, 3) - // // validate board insight content - // require.Equal(t, topTeamBoards.Items[0].ActivityCount, strconv.Itoa(8)) - // require.Equal(t, topTeamBoards.Items[0].Icon, "💬") - // require.Equal(t, topTeamBoards.Items[1].ActivityCount, strconv.Itoa(5)) - // require.Equal(t, topTeamBoards.Items[2].ActivityCount, strconv.Itoa(4)) - - // }) + t.Run("team insights", func(t *testing.T) { + boardIDs := []string{boardsUser1[0].ID, boardsUser1[1].ID, boardsUser1[2].ID} + topTeamBoards, err := store.GetTeamBoardsInsights(testTeamID, testUserID, + 0, 0, 10, boardIDs) + require.NoError(t, err) + require.Len(t, topTeamBoards.Items, 3) + // validate board insight content + require.Equal(t, topTeamBoards.Items[0].ActivityCount, strconv.Itoa(8)) + require.Equal(t, topTeamBoards.Items[0].Icon, "💬") + require.Equal(t, topTeamBoards.Items[1].ActivityCount, strconv.Itoa(5)) + require.Equal(t, topTeamBoards.Items[2].ActivityCount, strconv.Itoa(4)) + }) t.Run("user insights", func(t *testing.T) { boardIDs := []string{boardsUser1[0].ID, boardsUser1[1].ID, boardsUser1[2].ID} diff --git a/webapp/src/constants.ts b/webapp/src/constants.ts index 191ab58c4..c699e4606 100644 --- a/webapp/src/constants.ts +++ b/webapp/src/constants.ts @@ -194,6 +194,8 @@ class Constants { } static readonly globalTeamId = '0' + + static readonly myInsights = 'MY' } export {Constants, Permission}