From 866d23529e146f080139bea8ad96e9808ff16add Mon Sep 17 00:00:00 2001 From: wiggin77 Date: Sun, 1 Jan 2023 14:16:46 -0500 Subject: [PATCH] integration tests for GetBlocksComplianceHistory --- server/api/compliance.go | 7 + server/integrationtests/compliance_test.go | 167 ++++++++++++++++++- server/services/store/sqlstore/compliance.go | 4 +- 3 files changed, 171 insertions(+), 7 deletions(-) diff --git a/server/api/compliance.go b/server/api/compliance.go index 218a9e2ac..3402e3440 100644 --- a/server/api/compliance.go +++ b/server/api/compliance.go @@ -235,6 +235,13 @@ func (a *API) handleGetBlocksComplianceHistory(w http.ResponseWriter, r *http.Re return } + // check for valid team + _, err = a.app.GetBoard(boardID) + if err != nil { + a.errorResponse(w, r, model.NewErrBadRequest("invalid board id: "+boardID)) + return + } + if strPage == "" { strPage = complianceDefaultPage } diff --git a/server/integrationtests/compliance_test.go b/server/integrationtests/compliance_test.go index dc4a449f7..c22929fcf 100644 --- a/server/integrationtests/compliance_test.go +++ b/server/integrationtests/compliance_test.go @@ -153,23 +153,52 @@ func TestGetBoardsComplianceHistory(t *testing.T) { _ = th.CreateBoards(testTeamID, model.BoardTypeOpen, 2) - bcr, resp := clients.TeamMember.GetBoardsComplianceHistory(utils.GetMillis()-OneDay, true, testTeamID, 0, 0) + bchr, resp := clients.TeamMember.GetBoardsComplianceHistory(utils.GetMillis()-OneDay, true, testTeamID, 0, 0) th.CheckUnauthorized(resp) - require.Nil(t, bcr) + require.Nil(t, bchr) }) - t.Run("good call", func(t *testing.T) { + t.Run("good call, no deleted", func(t *testing.T) { th, clients := setupTestHelperForCompliance(t, true) defer th.TearDown() const count = 10 - _ = th.CreateBoards(testTeamID, model.BoardTypeOpen, count) + boards := th.CreateBoards(testTeamID, model.BoardTypeOpen, count) + + deleted, resp := th.Client.DeleteBoard(boards[0].ID) + th.CheckOK(resp) + require.True(t, deleted) + + deleted, resp = th.Client.DeleteBoard(boards[1].ID) + th.CheckOK(resp) + require.True(t, deleted) + + bchr, resp := clients.Admin.GetBoardsComplianceHistory(utils.GetMillis()-OneDay, false, testTeamID, 0, 0) + th.CheckOK(resp) + require.False(t, bchr.HasNext) + require.Len(t, bchr.Results, count) // both deleted boards have one non-deleted record each + }) + + t.Run("good call, include deleted", func(t *testing.T) { + th, clients := setupTestHelperForCompliance(t, true) + defer th.TearDown() + + const count = 10 + boards := th.CreateBoards(testTeamID, model.BoardTypeOpen, count) + + deleted, resp := th.Client.DeleteBoard(boards[0].ID) + th.CheckOK(resp) + require.True(t, deleted) + + deleted, resp = th.Client.DeleteBoard(boards[1].ID) + th.CheckOK(resp) + require.True(t, deleted) bchr, resp := clients.Admin.GetBoardsComplianceHistory(utils.GetMillis()-OneDay, true, testTeamID, 0, 0) th.CheckOK(resp) require.False(t, bchr.HasNext) - require.Len(t, bchr.Results, count) + require.Len(t, bchr.Results, count+2) // both deleted boards have 2 history records each }) t.Run("pagination", func(t *testing.T) { @@ -208,3 +237,131 @@ func TestGetBoardsComplianceHistory(t *testing.T) { }) } + +func TestGetBlocksComplianceHistory(t *testing.T) { + t.Run("missing Features.Compliance license should fail", func(t *testing.T) { + th, clients := setupTestHelperForCompliance(t, false) + defer th.TearDown() + + board, _ := th.CreateBoardAndCards(testTeamID, model.BoardTypeOpen, 2) + + bchr, resp := clients.Admin.GetBlocksComplianceHistory(utils.GetMillis()-OneDay, true, testTeamID, board.ID, 0, 0) + + th.CheckNotImplemented(resp) + require.Nil(t, bchr) + }) + + t.Run("a non authenticated user should be rejected", func(t *testing.T) { + th, clients := setupTestHelperForCompliance(t, true) + defer th.TearDown() + + board, _ := th.CreateBoardAndCards(testTeamID, model.BoardTypeOpen, 2) + + bchr, resp := clients.Anon.GetBlocksComplianceHistory(utils.GetMillis()-OneDay, true, testTeamID, board.ID, 0, 0) + + th.CheckUnauthorized(resp) + require.Nil(t, bchr) + }) + + t.Run("a user without manage_system permission should be rejected", func(t *testing.T) { + th, clients := setupTestHelperForCompliance(t, true) + defer th.TearDown() + + board, _ := th.CreateBoardAndCards(testTeamID, model.BoardTypeOpen, 2) + + bchr, resp := clients.TeamMember.GetBlocksComplianceHistory(utils.GetMillis()-OneDay, true, testTeamID, board.ID, 0, 0) + + th.CheckUnauthorized(resp) + require.Nil(t, bchr) + }) + + t.Run("good call, no deleted", func(t *testing.T) { + th, clients := setupTestHelperForCompliance(t, true) + defer th.TearDown() + + const count = 10 + board, cards := th.CreateBoardAndCards(testTeamID, model.BoardTypeOpen, count) + + deleted, resp := th.Client.DeleteBlock(board.ID, cards[0].ID, true) + th.CheckOK(resp) + require.True(t, deleted) + + deleted, resp = th.Client.DeleteBlock(board.ID, cards[1].ID, true) + th.CheckOK(resp) + require.True(t, deleted) + + bchr, resp := clients.Admin.GetBlocksComplianceHistory(utils.GetMillis()-OneDay, false, testTeamID, board.ID, 0, 0) + th.CheckOK(resp) + require.False(t, bchr.HasNext) + require.Len(t, bchr.Results, count) // both deleted cards have one non-deleted record each + }) + + t.Run("good call, include deleted", func(t *testing.T) { + th, clients := setupTestHelperForCompliance(t, true) + defer th.TearDown() + + const count = 10 + board, cards := th.CreateBoardAndCards(testTeamID, model.BoardTypeOpen, count) + + deleted, resp := th.Client.DeleteBlock(board.ID, cards[0].ID, true) + th.CheckOK(resp) + require.True(t, deleted) + + deleted, resp = th.Client.DeleteBlock(board.ID, cards[1].ID, true) + th.CheckOK(resp) + require.True(t, deleted) + + bchr, resp := clients.Admin.GetBlocksComplianceHistory(utils.GetMillis()-OneDay, true, testTeamID, board.ID, 0, 0) + th.CheckOK(resp) + require.False(t, bchr.HasNext) + require.Len(t, bchr.Results, count+2) // both deleted boards have 2 history records each + }) + + t.Run("pagination", func(t *testing.T) { + th, clients := setupTestHelperForCompliance(t, true) + defer th.TearDown() + + const count = 20 + const perPage = 3 + board, _ := th.CreateBoardAndCards(testTeamID, model.BoardTypeOpen, count) + + blockHistory := make([]model.BlockHistory, 0, count) + page := 0 + for { + bchr, resp := clients.Admin.GetBlocksComplianceHistory(utils.GetMillis()-OneDay, true, testTeamID, board.ID, page, perPage) + page++ + th.CheckOK(resp) + blockHistory = append(blockHistory, bchr.Results...) + if !bchr.HasNext { + break + } + } + require.Len(t, blockHistory, count) + require.Equal(t, int(math.Floor((count/perPage)+1)), page) + }) + + t.Run("invalid teamID", func(t *testing.T) { + th, clients := setupTestHelperForCompliance(t, true) + defer th.TearDown() + + board, _ := th.CreateBoardAndCards(testTeamID, model.BoardTypeOpen, 2) + + bchr, resp := clients.Admin.GetBlocksComplianceHistory(utils.GetMillis()-OneDay, true, utils.NewID(utils.IDTypeTeam), board.ID, 0, 0) + + th.CheckBadRequest(resp) + require.Nil(t, bchr) + }) + + t.Run("invalid boardID", func(t *testing.T) { + th, clients := setupTestHelperForCompliance(t, true) + defer th.TearDown() + + _, _ = th.CreateBoardAndCards(testTeamID, model.BoardTypeOpen, 2) + + bchr, resp := clients.Admin.GetBlocksComplianceHistory(utils.GetMillis()-OneDay, true, testTeamID, utils.NewID(utils.IDTypeBoard), 0, 0) + + th.CheckBadRequest(resp) + require.Nil(t, bchr) + }) + +} diff --git a/server/services/store/sqlstore/compliance.go b/server/services/store/sqlstore/compliance.go index 3b4cb9111..2ea8990d4 100644 --- a/server/services/store/sqlstore/compliance.go +++ b/server/services/store/sqlstore/compliance.go @@ -135,7 +135,7 @@ func (s *SQLStore) getBlocksComplianceHistory(db sq.BaseRunner, opts model.Query From(s.tablePrefix+"blocks_history as bh"). Join(s.tablePrefix+"boards as brd on brd.id=bh.board_id"). Where(sq.Gt{"bh.update_at": opts.ModifiedSince}). - GroupBy("bh.id", "bh.team_id", "bh.board_id", "bh.type", "bh.delete_at", "bh.created_by", "bh.modified_by"). + GroupBy("bh.id", "brd.team_id", "bh.board_id", "bh.type", "bh.delete_at", "bh.created_by", "bh.modified_by"). OrderBy("lastUpdateAt desc") if !opts.IncludeDeleted { @@ -143,7 +143,7 @@ func (s *SQLStore) getBlocksComplianceHistory(db sq.BaseRunner, opts model.Query } if opts.TeamID != "" { - query = query.Where(sq.Eq{"bh.team_id": opts.TeamID}) + query = query.Where(sq.Eq{"brd.team_id": opts.TeamID}) } if opts.BoardID != "" {