From 9f82537f9e54de725b3ddc7ad25b5a0478aa2fc6 Mon Sep 17 00:00:00 2001 From: Chen-I Lim <46905241+chenilim@users.noreply.github.com> Date: Tue, 29 Mar 2022 15:55:39 -0700 Subject: [PATCH] Add TestGetTemplates integration test (#2635) * Add TestGetTemplates integration test * Use t.Logf instead of fmt Co-authored-by: Mattermod --- server/client/client.go | 33 ++++++++++++++++-- server/integrationtests/board_test.go | 50 +++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/server/client/client.go b/server/client/client.go index a497c5b19..789d8f7e3 100644 --- a/server/client/client.go +++ b/server/client/client.go @@ -188,6 +188,10 @@ func (c *Client) GetBlocksRoute(boardID string) string { return fmt.Sprintf("%s/blocks", c.GetBoardRoute(boardID)) } +func (c *Client) GetAllBlocksRoute(boardID string) string { + return fmt.Sprintf("%s/blocks?all=true", c.GetBoardRoute(boardID)) +} + func (c *Client) GetBoardsAndBlocksRoute() string { return "/boards-and-blocks" } @@ -212,6 +216,16 @@ func (c *Client) GetBlocksForBoard(boardID string) ([]model.Block, *Response) { return model.BlocksFromJSON(r.Body), BuildResponse(r) } +func (c *Client) GetAllBlocksForBoard(boardID string) ([]model.Block, *Response) { + r, err := c.DoAPIGet(c.GetAllBlocksRoute(boardID), "") + if err != nil { + return nil, BuildErrorResponse(r, err) + } + defer closeBody(r) + + return model.BlocksFromJSON(r.Body), BuildResponse(r) +} + func (c *Client) PatchBlock(boardID, blockID string, blockPatch *model.BlockPatch) (bool, *Response) { r, err := c.DoAPIPatch(c.GetBlockRoute(boardID, blockID), toJSON(blockPatch)) if err != nil { @@ -222,18 +236,21 @@ func (c *Client) PatchBlock(boardID, blockID string, blockPatch *model.BlockPatc return true, BuildResponse(r) } -func (c *Client) DuplicateBoard(boardID string, asTemplate bool, teamID string) (bool, *Response) { +func (c *Client) DuplicateBoard(boardID string, asTemplate bool, teamID string) (*model.BoardsAndBlocks, *Response) { queryParams := "?asTemplate=false&" if asTemplate { queryParams = "?asTemplate=true" } + if len(teamID) > 0 { + queryParams = queryParams + "&toTeam=" + teamID + } r, err := c.DoAPIPost(c.GetBoardRoute(boardID)+"/duplicate"+queryParams, "") if err != nil { - return false, BuildErrorResponse(r, err) + return nil, BuildErrorResponse(r, err) } defer closeBody(r) - return true, BuildResponse(r) + return model.BoardsAndBlocksFromJSON(r.Body), BuildResponse(r) } func (c *Client) DuplicateBlock(boardID, blockID string, asTemplate bool) (bool, *Response) { @@ -646,3 +663,13 @@ func (c *Client) GetSubscriptions(subscriberID string) ([]*model.Subscription, * return subs, BuildResponse(r) } + +func (c *Client) GetTemplatesForTeam(teamID string) ([]*model.Board, *Response) { + r, err := c.DoAPIGet(c.GetTeamRoute(teamID)+"/templates", "") + if err != nil { + return nil, BuildErrorResponse(r, err) + } + defer closeBody(r) + + return model.BoardsFromJSON(r.Body), BuildResponse(r) +} diff --git a/server/integrationtests/board_test.go b/server/integrationtests/board_test.go index e68d14d4b..dd593b686 100644 --- a/server/integrationtests/board_test.go +++ b/server/integrationtests/board_test.go @@ -1450,6 +1450,56 @@ func TestDeleteMember(t *testing.T) { }) } +func TestGetTemplates(t *testing.T) { + t.Run("should be able to retrieve built-in templates", func(t *testing.T) { + th := SetupTestHelper(t).InitBasic() + defer th.TearDown() + + teamID := "my-team-id" + rBoards, resp := th.Client.GetTemplatesForTeam("0") + th.CheckOK(resp) + require.NotNil(t, rBoards) + require.GreaterOrEqual(t, len(rBoards), 6) + + t.Log("\n\n") + for _, board := range rBoards { + t.Logf("Test get template: %s - %s\n", board.Title, board.ID) + rBoard, resp := th.Client.GetBoard(board.ID, "") + th.CheckOK(resp) + require.NotNil(t, rBoard) + require.Equal(t, board, rBoard) + + rBlocks, resp := th.Client.GetAllBlocksForBoard(board.ID) + th.CheckOK(resp) + require.NotNil(t, rBlocks) + require.Greater(t, len(rBlocks), 0) + t.Logf("Got %d block(s)\n", len(rBlocks)) + + rBoardsAndBlock, resp := th.Client.DuplicateBoard(board.ID, false, teamID) + th.CheckOK(resp) + require.NotNil(t, rBoardsAndBlock) + require.Greater(t, len(rBoardsAndBlock.Boards), 0) + require.Greater(t, len(rBoardsAndBlock.Blocks), 0) + + rBoard2 := rBoardsAndBlock.Boards[0] + require.Contains(t, board.Title, rBoard2.Title) + require.False(t, rBoard2.IsTemplate) + + t.Logf("Duplicate template: %s - %s, %d block(s)\n", rBoard2.Title, rBoard2.ID, len(rBoardsAndBlock.Blocks)) + rBoard3, resp := th.Client.GetBoard(rBoard2.ID, "") + th.CheckOK(resp) + require.NotNil(t, rBoard3) + require.Equal(t, rBoard2, rBoard3) + + rBlocks2, resp := th.Client.GetAllBlocksForBoard(rBoard2.ID) + th.CheckOK(resp) + require.NotNil(t, rBlocks2) + require.Equal(t, len(rBoardsAndBlock.Blocks), len(rBlocks2)) + } + t.Log("\n\n") + }) +} + func TestJoinBoard(t *testing.T) { t.Run("create and join public board", func(t *testing.T) { th := SetupTestHelper(t).InitBasic()