package app import ( "testing" "github.com/golang/mock/gomock" "github.com/mattermost/focalboard/server/model" "github.com/stretchr/testify/require" ) type blockError struct { msg string } func (be blockError) Error() string { return be.msg } func TestInsertBlock(t *testing.T) { th, tearDown := SetupTestHelper(t) defer tearDown() t.Run("success scenerio", func(t *testing.T) { boardID := testBoardID block := model.Block{BoardID: boardID} board := &model.Board{ID: boardID} th.Store.EXPECT().GetBoard(boardID).Return(board, nil) th.Store.EXPECT().InsertBlock(&block, "user-id-1").Return(nil) th.Store.EXPECT().GetMembersForBoard(boardID).Return([]*model.BoardMember{}, nil) err := th.App.InsertBlock(block, "user-id-1") require.NoError(t, err) }) t.Run("error scenerio", func(t *testing.T) { boardID := testBoardID block := model.Block{BoardID: boardID} board := &model.Board{ID: boardID} th.Store.EXPECT().GetBoard(boardID).Return(board, nil) th.Store.EXPECT().InsertBlock(&block, "user-id-1").Return(blockError{"error"}) err := th.App.InsertBlock(block, "user-id-1") require.Error(t, err, "error") }) } func TestPatchBlocks(t *testing.T) { th, tearDown := SetupTestHelper(t) defer tearDown() t.Run("patchBlocks success scenerio", func(t *testing.T) { blockPatches := model.BlockPatchBatch{} th.Store.EXPECT().PatchBlocks(gomock.Eq(&blockPatches), gomock.Eq("user-id-1")).Return(nil) err := th.App.PatchBlocks("team-id", &blockPatches, "user-id-1") require.NoError(t, err) }) t.Run("patchBlocks error scenerio", func(t *testing.T) { blockPatches := model.BlockPatchBatch{} th.Store.EXPECT().PatchBlocks(gomock.Eq(&blockPatches), gomock.Eq("user-id-1")).Return(blockError{"error"}) err := th.App.PatchBlocks("team-id", &blockPatches, "user-id-1") require.Error(t, err, "error") }) } func TestDeleteBlock(t *testing.T) { th, tearDown := SetupTestHelper(t) defer tearDown() t.Run("success scenerio", func(t *testing.T) { boardID := testBoardID board := &model.Board{ID: boardID} block := model.Block{ ID: "block-id", BoardID: board.ID, } th.Store.EXPECT().GetBlock(gomock.Eq("block-id")).Return(&block, nil) th.Store.EXPECT().DeleteBlock(gomock.Eq("block-id"), gomock.Eq("user-id-1")).Return(nil) th.Store.EXPECT().GetBoard(gomock.Eq(testBoardID)).Return(board, nil) th.Store.EXPECT().GetMembersForBoard(boardID).Return([]*model.BoardMember{}, nil) err := th.App.DeleteBlock("block-id", "user-id-1") require.NoError(t, err) }) t.Run("error scenerio", func(t *testing.T) { boardID := testBoardID board := &model.Board{ID: boardID} block := model.Block{ ID: "block-id", BoardID: board.ID, } th.Store.EXPECT().GetBlock(gomock.Eq("block-id")).Return(&block, nil) th.Store.EXPECT().DeleteBlock(gomock.Eq("block-id"), gomock.Eq("user-id-1")).Return(blockError{"error"}) th.Store.EXPECT().GetBoard(gomock.Eq(testBoardID)).Return(board, nil) err := th.App.DeleteBlock("block-id", "user-id-1") require.Error(t, err, "error") }) } func TestUndeleteBlock(t *testing.T) { th, tearDown := SetupTestHelper(t) defer tearDown() t.Run("success scenerio", func(t *testing.T) { boardID := testBoardID board := &model.Board{ID: boardID} block := model.Block{ ID: "block-id", BoardID: board.ID, } th.Store.EXPECT().GetBlockHistory( gomock.Eq("block-id"), gomock.Eq(model.QueryBlockHistoryOptions{Limit: 1, Descending: true}), ).Return([]model.Block{block}, nil) th.Store.EXPECT().UndeleteBlock(gomock.Eq("block-id"), gomock.Eq("user-id-1")).Return(nil) th.Store.EXPECT().GetBlock(gomock.Eq("block-id")).Return(&block, nil) th.Store.EXPECT().GetBoard(boardID).Return(board, nil) th.Store.EXPECT().GetMembersForBoard(boardID).Return([]*model.BoardMember{}, nil) err := th.App.UndeleteBlock("block-id", "user-id-1") require.NoError(t, err) }) t.Run("error scenerio", func(t *testing.T) { block := model.Block{ ID: "block-id", } th.Store.EXPECT().GetBlockHistory( gomock.Eq("block-id"), gomock.Eq(model.QueryBlockHistoryOptions{Limit: 1, Descending: true}), ).Return([]model.Block{block}, nil) th.Store.EXPECT().UndeleteBlock(gomock.Eq("block-id"), gomock.Eq("user-id-1")).Return(blockError{"error"}) th.Store.EXPECT().GetBlock(gomock.Eq("block-id")).Return(&block, nil) err := th.App.UndeleteBlock("block-id", "user-id-1") require.Error(t, err, "error") }) }