1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-27 13:48:52 +02:00
focalboard/server/services/store/storetests/boards.go
Chen-I Lim c3a4921ac3
GetBoardMetadata API (#2569)
* wip

* Added data migration for populating categories

* wip

* Added data migration for populating categories

* Store WIP

* migration WIP

* category CRUD APIs complete

* category block API WIP

* block category update API done

* Fetcehed data into store

* Started displayting sidebar data

* sidebar WIP

* Dashboard - basic changes

* Sidebar dashboard btn and board switcher UI only

* Sidebar dashboard btn and board switcher UI only

* create category dialog WIP

* Create category webapp side done

* Integrated move card to other category

* board to block

* Disabled dashboard route for now as we'll implement it in phase 2

* WIP

* Added logic to open last board/view on per team level

* Add workspace to teams and boards migrations (#1986)

* Add workspace to teams and boards migrations

* Update json annotations on board models

* boards search dialog WIP

* Seach dialog WIP

* Implemented opening boiard from search results

* Boards switcher styliung

* Handled update category WS event

* Template support

* personal server support and styling fixes

* test fix WIP

* Fixed a bug causing boards to not be moved correctly beteen categories

* Fixed webapp tests

* fix

* Store changes (#2011)

* Permissions phase 1 - Websocket updates (#2014)

* Store changes

* Websockets changes

* Permissions phase 1 - Permissions service (#2015)

* Store changes

* Websockets changes

* Permissions service

* Api and app updates (#2016)

* Store changes

* Websockets changes

* Permissions service

* New API and App changes

* Delete and Patch boards and blocks endpoints

* Used correct variable

* Webapp changes WIP

* Open correct team URL

* Fixed get block API

* Used React context for workspace users

* WIP

* On load navigation sorted out

* WIP

* Nav fix

* categories WS broadcast

* Used real search API

* Fixed unfurl ppreview

* set active team in sidebar

* IMplemented navigation on changing team in sidebar

* Misc fixes

* close rows inside transaction (#2045)

* update syntax for mysql (#2044)

* Upadted mutator for new patchBlock API

* Updated patchBlock API to use new URL

* Listeining to correct event in plugin mode

* Implemented WS messages for category operations:

* Fix duplicated build tags on Makefile

* Sidebar enhancements

* Add missing prefix to SQLite migration and fix flaky tests

* Sidebar boards menu enhancement

* Fix board page interactions (#2144)

* Fix patch board card properties error

* Fix board interactions

* Fix insert blocks interactions

* Fix app tests (#2104)

* Add json1 tag to vscode launch (#2157)

* Fix add, delete and update boards and add board patch generation (#2146)

* Fix update boards and add board patch generation

* Make add board and add template work, as well as deleting a board

* Update the state on board deletion

* Delete unused variable

* Fix bad parenthesis

* Fix board creation inside plugin, options were coming null due websocket message serialization

* update property type mutators to use boards API (#2168)

* Add permissions modal (#2196)

* Initial integration

* Permissions modal, websocket updates and API tests implemented

* Avoid updating/removing user if there is only one admin left

* Fix duplicates on board search

* Adds integration test

* Addressing PR review comments

Co-authored-by: Jesús Espino <jespinog@gmail.com>

* Merge

* I'm able to compile now

* Some fixes around tests execution

* Fixing migrations

* Fixing migrations order

* WIP

* Fixing some other compilation problems on tests

* Some typescript tests fixed

* Fixing javascript tests

* Fixing compilation

* Fixing some problems to create boards

* Load the templates on initial load

* Improvements over initial team templates import

* Adding new fields in the database

* Working on adding duplicate board api

* Removing RootID concept entirely

* Improving a bit the subscriptions

* Fixing store tests for notificationHints

* Fixing more tests

* fixing tests

* Fixing tests

* Fixing tests

* Fixing some small bugs related to templates

* Fixing registration link generation/regeneration

* Fixing cypress tests

* Adding store tests for duplicateBoard and duplicateBlock

* Addressing some TODO comments

* Making the export api simpler

* Add redirect component for old workspace urls

* Removing Dashboard code

* Delete only the built-in templates on update

* fixing tests

* Adding users autocompletion

* Updating snapshots

* Fixing bad merge

* fix panic when creating new card in notifysubscriptions (#2352)

* fix lint errors (#2353)

* fix lint errors

* fix panic when creating new card in notifysubscriptions (#2352)

* fix lint errors

* fix unit test

* Revert "fix unit test"

This reverts commit 0ad78aed65.

Co-authored-by: Doug Lauder <wiggin77@warpmail.net>

* fix sql syntax error for SearchUsersByTeam (#2357)

* Fix mentions delivery (#2358)

* fix sql syntax error for SearchUsersByTeam

* fix mentions delivery

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>

* update api for octoClient calls, pass correct variables to mutator (#2359)

* Fixing tests after merge

* Fix sidebar context menu UI issue (#2399)

* Fix notification diff for text blocks (#2386)

* fix notification diff for text blocks; fix various linter errors.

* fix URLs to cards

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>

* Permissions branch: Fix card links (#2391)

* fix notification diff for text blocks; fix various linter errors.

* fix URLs to cards

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>

* Fixing sqlite tests

* Fixing server tests

* Update migrations to create global templates. (#2397)

* fix duplicate templates

* revert migrate.go

* update UI for empty templates

* implement updating built-in templates as global (teamId = 0)

* handle error if board not found

* update unit test

* fix more tests

* Update blocks_test.go

Fix merge issue

* fix migration sql error (#2414)

* Fixing frontend tests

* Set target team ID when using a global template (#2419)

* Fix some server tests

* Fixing onboarding creation

* Permissions branch: Fix unit tests and CI errors (part 1) (#2425)

* Fixing some small memory leaks (#2400)

* Fixing some small memory leaks

* fixing tests

* passing the tags to all test targets

* Increasing the timeout of the tests

* Fix some type checkings

* Permissions branch: Fixes all the linter errors (#2429)

* fix linter errors

* Reestructuring the router and splitting in more subcomponents (#2403)

* Reestructuring the router and splitting in more subcomponents

* Removing console.log calls

* Removing unneeded selector

* Addressing PR comment

* Fix redirection to one team when you load directly the boards home path

* Using properly the lastTeamID to redirect the user if needed

* don't allow last admin change/deleted (#2416)

* don't allow last admin change/deleted

* update for i18-extract

* fixed en.json

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Harshil Sharma <harshilsharma63@gmail.com>

* Splitting BoardPage component into simpler/smaller components (#2435)

* Splitting BoardPage component into simpler/smaller components

* Removing unneeded import

* Replace go migrate with morph permissions (#2424)

* merge origin/replace-go-migrate-with-morph

* run go mod tidy on mattermost-plugin and increase test timeout

* fix merge issue temprorarily

* remove some debug changes

* fixing the linter

* Allow always team 0 (global) templates fetch (#2472)

* Fix problem with viewId 0 in the URL (#2473)

* Migrate from binddata to goembed (#2471)

* Adding join logic to the board switcher (#2434)

* Adding join logic to the board switcher

* Using already existing client function and removing the joinBoard one

* Adding support for autojoin based on url

* Fixing frontend tests

* fix webapp compile error, missing enableSharedBoards (#2501)

* Fixing duplication on postgres

* Adding back views to the sidebar (#2494)

* Fix #2507. Update Swagger comments (#2508)

* Fix the flash of the template selector on board/team switch (#2490)

* Fix the flash of the template selector on board/team switch

* More fixes specially around error handling

* Fixing the bot badge (#2487)

* simplifying a bit the team store sync between channels and focalboard (#2481)

* Fix menu tests (#2528)

* fix failing menu tests

* fix lint error

* Added keyboard shortcut for boards switcher (#2407)

* Added keyboard shortcut for boards switcher

* Fixed a type error

* Added some inline comments

* Fixed lint

* Fixed bug with scroll jumping when the card is opened: (#2477)

- avoid remounting of `ScrollingComponent` for each render of `Kanban` component
  - property `autoFocus` set to false for `CalculationOptions` because it triggers `blur` even for the button in Jest tests and closes the menu
  - snapshots for tests with `CalculationOptions` updated

* Adding the frontend support for permissions and applying it to a big part of the interface. (#2536)

* Initial work on permissions gates

* Applying permissions gates in more places

* Adding more checks to the interface

* Adding more permissions gates and keeping the store up to date

* fixing some tests

* Fixing some more tests

* Fixing another test

* Fixing all tests and adding some more

* Adding no-permission snapshot tests

* Addressing PR review comments

* Fixing invert behavior

* Permissions branch:  No sqlstore calls after app shutdown (#2530)

* fix webapp compile error, missing enableSharedBoards

* refactor app init wip

* - ensure all block change notifications are finished before shutting down app
- fix unit tests for mysql (insert_at only has 1 second resolution!)

* adjust logging

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>

* Fixed migrations to allow upgrading from previous version (#2535)

* Added mechanism to check if schema migration is needed

* WIP

* WIP

* WIP

* WIP

* Fixed migration

* Fixed for SQLite

* minor cleaniup

* Deleted old schema migration table after running migrations

* Removed a debug log

* Fixed a bug where the code always tried to delete a table which may or may not exist

* Show properly the user avatar in the ShareBoard component (#2542)

* Fixing the last CI problems from the permissions-branch (#2541)

* Fix history ordering

* Giving some times to avoid possible race conditions

* Empty

* Reverting accidental change in the config.json

* Optimizing table view (#2540)

* Optimizing table view

* Reducing the amount of rendering for tables

* Some other performance improvements

* Improve the activeView updates

* Some extra simplifications

* Another small improvement

* Fixing tests

* Fixing linter errors

* Reducing a bit the amount of dependency with big objects in the store

* Small simplification

* Removing Commenter role from the user role selector (#2561)

* Shareboard cleanup (#2550)

* Initial work on permissions gates

* Applying permissions gates in more places

* Adding more checks to the interface

* Adding more permissions gates and keeping the store up to date

* fixing some tests

* Fixing some more tests

* Fixing another test

* Fixing all tests and adding some more

* Adding no-permission snapshot tests

* Addressing PR review comments

* cleanup some shareboard settings

* remove unused property, fix for user items being displayed for non admin

* revert change, allow users to show

Co-authored-by: Jesús Espino <jespinog@gmail.com>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>

* GetBoardMetadata API

* Integration tests. WIP

* getBoardHistory

* Working integration test

* Fix ordering, add store tests

* Fix: Update board_history update_at on patch

* sqltests

* Fix unmarshall delete boards_history

* testGetBlockMetadata with delete and undelete

* Handle board not found

* Fixing comments and cards with the new optimizations in the store (#2560)

* Fixing property creation (#2563)

* Cleanup

* Fix user selection in table view (#2565)

* Fixing focus new row in table view (#2567)

* Permissions branch: Fix sqlite table lock (CI) (#2568)

* fix sqlite table lock

* remove test db on teardown

* revert .gitignore

* fix goimport on migration code

* fix typo

* more linter fixes

* clean up tmp db for sqlstore tests

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>

* Fixing snapshots

* Remove debug log

* Return metadata for deleted boards

* Migrating center panel to functional component (#2562)

* Migrating center panel to functional component

* Fixing some tests

* Fixing another test

* Fixing linter errors

* Fixing types errors

* Fixing linter error

* Fixing cypress tests

* Fixing the last cypress test

* Simpliying a bit the code

* Making property insertion more robust

* Updating checkbox test

* License check

* Cleanup and update Swagger docs

* Merge from main

* Fix bad merge

* Fix Linux-app build break

* do mod tidy

* Fix server lint

* Require credentials (not only read token)

* Add missing defer CloseRows

* do mod tidy

Co-authored-by: Harshil Sharma <harshilsharma63@gmail.com>
Co-authored-by: Miguel de la Cruz <miguel@mcrx.me>
Co-authored-by: Scott Bishel <scott.bishel@mattermost.com>
Co-authored-by: Jesús Espino <jespinog@gmail.com>
Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com>
Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>
Co-authored-by: kamre <eremchenko@gmail.com>
2022-03-29 14:47:49 -07:00

912 lines
26 KiB
Go

package storetests
import (
"database/sql"
"testing"
"time"
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/services/store"
"github.com/mattermost/focalboard/server/utils"
"github.com/stretchr/testify/require"
)
//nolint:dupl
func StoreTestBoardStore(t *testing.T, setup func(t *testing.T) (store.Store, func())) {
t.Run("GetBoard", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testGetBoard(t, store)
})
t.Run("GetBoardsForUserAndTeam", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testGetBoardsForUserAndTeam(t, store)
})
t.Run("InsertBoard", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testInsertBoard(t, store)
})
t.Run("PatchBoard", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testPatchBoard(t, store)
})
t.Run("DeleteBoard", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testDeleteBoard(t, store)
})
t.Run("InsertBoardWithAdmin", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testInsertBoardWithAdmin(t, store)
})
t.Run("SaveMember", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testSaveMember(t, store)
})
t.Run("GetMemberForBoard", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testGetMemberForBoard(t, store)
})
t.Run("GetMembersForBoard", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testGetMembersForBoard(t, store)
})
t.Run("DeleteMember", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testDeleteMember(t, store)
})
t.Run("SearchBoardsForUserAndTeam", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testSearchBoardsForUserAndTeam(t, store)
})
t.Run("GetBoardHistory", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testGetBoardHistory(t, store)
})
}
func testGetBoard(t *testing.T, store store.Store) {
userID := testUserID
t.Run("existing board", func(t *testing.T) {
board := &model.Board{
ID: "id-1",
TeamID: testTeamID,
Type: model.BoardTypeOpen,
}
_, err := store.InsertBoard(board, userID)
require.NoError(t, err)
rBoard, err := store.GetBoard(board.ID)
require.NoError(t, err)
require.Equal(t, board.ID, rBoard.ID)
require.Equal(t, board.TeamID, rBoard.TeamID)
require.Equal(t, userID, rBoard.CreatedBy)
require.Equal(t, userID, rBoard.ModifiedBy)
require.Equal(t, board.Type, rBoard.Type)
require.NotZero(t, rBoard.CreateAt)
require.NotZero(t, rBoard.UpdateAt)
})
t.Run("nonexisting board", func(t *testing.T) {
rBoard, err := store.GetBoard("nonexistent-id")
require.ErrorIs(t, err, sql.ErrNoRows)
require.Nil(t, rBoard)
})
}
func testGetBoardsForUserAndTeam(t *testing.T, store store.Store) {
userID := "user-id-1"
t.Run("should return only the boards of the team that the user is a member of", func(t *testing.T) {
teamID1 := "team-id-1"
teamID2 := "team-id-2"
// team 1 boards
board1 := &model.Board{
ID: "board-id-1",
TeamID: teamID1,
Type: model.BoardTypeOpen,
}
_, _, err := store.InsertBoardWithAdmin(board1, userID)
require.NoError(t, err)
board2 := &model.Board{
ID: "board-id-2",
TeamID: teamID1,
Type: model.BoardTypePrivate,
}
_, _, err = store.InsertBoardWithAdmin(board2, userID)
require.NoError(t, err)
board3 := &model.Board{
ID: "board-id-3",
TeamID: teamID1,
Type: model.BoardTypeOpen,
}
_, err = store.InsertBoard(board3, "other-user")
require.NoError(t, err)
board4 := &model.Board{
ID: "board-id-4",
TeamID: teamID1,
Type: model.BoardTypePrivate,
}
_, err = store.InsertBoard(board4, "other-user")
require.NoError(t, err)
// team 2 boards
board5 := &model.Board{
ID: "board-id-5",
TeamID: teamID2,
Type: model.BoardTypeOpen,
}
_, _, err = store.InsertBoardWithAdmin(board5, userID)
require.NoError(t, err)
board6 := &model.Board{
ID: "board-id-6",
TeamID: teamID1,
Type: model.BoardTypePrivate,
}
_, err = store.InsertBoard(board6, "other-user")
require.NoError(t, err)
t.Run("should only find the two boards that the user is a member of for team 1", func(t *testing.T) {
boards, err := store.GetBoardsForUserAndTeam(userID, teamID1)
require.NoError(t, err)
require.Len(t, boards, 2)
boardIDs := []string{}
for _, board := range boards {
boardIDs = append(boardIDs, board.ID)
}
require.ElementsMatch(t, []string{board1.ID, board2.ID}, boardIDs)
})
t.Run("should only find the board that the user is a member of for team 2", func(t *testing.T) {
boards, err := store.GetBoardsForUserAndTeam(userID, teamID2)
require.NoError(t, err)
require.Len(t, boards, 1)
require.Equal(t, board5.ID, boards[0].ID)
})
})
}
func testInsertBoard(t *testing.T, store store.Store) {
userID := testUserID
t.Run("valid public board", func(t *testing.T) {
board := &model.Board{
ID: "id-test-public",
TeamID: testTeamID,
Type: model.BoardTypeOpen,
}
newBoard, err := store.InsertBoard(board, userID)
require.NoError(t, err)
require.Equal(t, board.ID, newBoard.ID)
require.Equal(t, newBoard.Type, model.BoardTypeOpen)
require.NotZero(t, newBoard.CreateAt)
require.NotZero(t, newBoard.UpdateAt)
require.Zero(t, newBoard.DeleteAt)
require.Equal(t, userID, newBoard.CreatedBy)
require.Equal(t, newBoard.CreatedBy, newBoard.ModifiedBy)
})
t.Run("valid private board", func(t *testing.T) {
board := &model.Board{
ID: "id-test-private",
TeamID: testTeamID,
Type: model.BoardTypePrivate,
}
newBoard, err := store.InsertBoard(board, userID)
require.NoError(t, err)
require.Equal(t, board.ID, newBoard.ID)
require.Equal(t, newBoard.Type, model.BoardTypePrivate)
require.NotZero(t, newBoard.CreateAt)
require.NotZero(t, newBoard.UpdateAt)
require.Zero(t, newBoard.DeleteAt)
require.Equal(t, userID, newBoard.CreatedBy)
require.Equal(t, newBoard.CreatedBy, newBoard.ModifiedBy)
})
t.Run("invalid properties field board", func(t *testing.T) {
board := &model.Board{
ID: "id-test-props",
TeamID: testTeamID,
Properties: map[string]interface{}{"no-serializable-value": t.Run},
}
_, err := store.InsertBoard(board, userID)
require.Error(t, err)
rBoard, err := store.GetBoard(board.ID)
require.ErrorIs(t, err, sql.ErrNoRows)
require.Nil(t, rBoard)
})
t.Run("update board", func(t *testing.T) {
board := &model.Board{
ID: "id-test-public",
TeamID: testTeamID,
Title: "New title",
}
// wait to avoid hitting pk uniqueness constraint in history
time.Sleep(10 * time.Millisecond)
newBoard, err := store.InsertBoard(board, "user2")
require.NoError(t, err)
require.Equal(t, "New title", newBoard.Title)
require.Equal(t, "user2", newBoard.ModifiedBy)
})
t.Run("test update board type", func(t *testing.T) {
board := &model.Board{
ID: "id-test-type-board",
Title: "Public board",
Type: model.BoardTypeOpen,
}
newBoard, err := store.InsertBoard(board, userID)
require.NoError(t, err)
require.Equal(t, model.BoardTypeOpen, newBoard.Type)
boardUpdate := &model.Board{
ID: "id-test-type-board",
Type: model.BoardTypePrivate,
}
// wait to avoid hitting pk uniqueness constraint in history
time.Sleep(10 * time.Millisecond)
modifiedBoard, err := store.InsertBoard(boardUpdate, userID)
require.NoError(t, err)
require.Equal(t, model.BoardTypePrivate, modifiedBoard.Type)
})
}
func testPatchBoard(t *testing.T, store store.Store) {
userID := testUserID
t.Run("should return error if the board doesn't exist", func(t *testing.T) {
newTitle := "A new title"
patch := &model.BoardPatch{Title: &newTitle}
board, err := store.PatchBoard("nonexistent-board-id", patch, userID)
require.Error(t, err)
require.Nil(t, board)
})
t.Run("should correctly apply a simple patch", func(t *testing.T) {
boardID := utils.NewID(utils.IDTypeBoard)
userID2 := "user-id-2"
board := &model.Board{
ID: boardID,
TeamID: testTeamID,
Type: model.BoardTypeOpen,
Title: "A simple title",
Description: "A simple description",
}
newBoard, err := store.InsertBoard(board, userID)
require.NoError(t, err)
require.NotNil(t, newBoard)
require.Equal(t, userID, newBoard.CreatedBy)
// wait to avoid hitting pk uniqueness constraint in history
time.Sleep(10 * time.Millisecond)
newTitle := "A new title"
newDescription := "A new description"
patch := &model.BoardPatch{Title: &newTitle, Description: &newDescription}
patchedBoard, err := store.PatchBoard(boardID, patch, userID2)
require.NoError(t, err)
require.Equal(t, newTitle, patchedBoard.Title)
require.Equal(t, newDescription, patchedBoard.Description)
require.Equal(t, userID, patchedBoard.CreatedBy)
require.Equal(t, userID2, patchedBoard.ModifiedBy)
})
t.Run("should correctly update the board properties", func(t *testing.T) {
boardID := utils.NewID(utils.IDTypeBoard)
board := &model.Board{
ID: boardID,
TeamID: testTeamID,
Type: model.BoardTypeOpen,
Properties: map[string]interface{}{
"one": "1",
"two": "2",
},
}
newBoard, err := store.InsertBoard(board, userID)
require.NoError(t, err)
require.NotNil(t, newBoard)
require.Equal(t, "1", newBoard.Properties["one"].(string))
require.Equal(t, "2", newBoard.Properties["two"].(string))
// wait to avoid hitting pk uniqueness constraint in history
time.Sleep(10 * time.Millisecond)
patch := &model.BoardPatch{
UpdatedProperties: map[string]interface{}{"three": "3"},
DeletedProperties: []string{"one"},
}
patchedBoard, err := store.PatchBoard(boardID, patch, userID)
require.NoError(t, err)
require.NotContains(t, patchedBoard.Properties, "one")
require.Equal(t, "2", patchedBoard.Properties["two"].(string))
require.Equal(t, "3", patchedBoard.Properties["three"].(string))
})
t.Run("should correctly modify the board's type", func(t *testing.T) {
boardID := utils.NewID(utils.IDTypeBoard)
board := &model.Board{
ID: boardID,
TeamID: testTeamID,
Type: model.BoardTypeOpen,
}
newBoard, err := store.InsertBoard(board, userID)
require.NoError(t, err)
require.NotNil(t, newBoard)
require.Equal(t, newBoard.Type, model.BoardTypeOpen)
// wait to avoid hitting pk uniqueness constraint in history
time.Sleep(10 * time.Millisecond)
newType := model.BoardTypePrivate
patch := &model.BoardPatch{Type: &newType}
patchedBoard, err := store.PatchBoard(boardID, patch, userID)
require.NoError(t, err)
require.Equal(t, model.BoardTypePrivate, patchedBoard.Type)
})
t.Run("a patch that doesn't include any of the properties should not modify them", func(t *testing.T) {
boardID := utils.NewID(utils.IDTypeBoard)
properties := map[string]interface{}{"prop1": "val1"}
cardProperties := []map[string]interface{}{{"prop2": "val2"}}
columnCalculations := map[string]interface{}{"calc3": "val3"}
board := &model.Board{
ID: boardID,
TeamID: testTeamID,
Type: model.BoardTypeOpen,
Properties: properties,
CardProperties: cardProperties,
ColumnCalculations: columnCalculations,
}
newBoard, err := store.InsertBoard(board, userID)
require.NoError(t, err)
require.NotNil(t, newBoard)
require.Equal(t, newBoard.Type, model.BoardTypeOpen)
require.Equal(t, properties, newBoard.Properties)
require.Equal(t, cardProperties, newBoard.CardProperties)
require.Equal(t, columnCalculations, newBoard.ColumnCalculations)
// wait to avoid hitting pk uniqueness constraint in history
time.Sleep(10 * time.Millisecond)
newType := model.BoardTypePrivate
patch := &model.BoardPatch{Type: &newType}
patchedBoard, err := store.PatchBoard(boardID, patch, userID)
require.NoError(t, err)
require.Equal(t, model.BoardTypePrivate, patchedBoard.Type)
require.Equal(t, properties, patchedBoard.Properties)
require.Equal(t, cardProperties, patchedBoard.CardProperties)
require.Equal(t, columnCalculations, patchedBoard.ColumnCalculations)
})
t.Run("a patch that removes a card property and updates another should work correctly", func(t *testing.T) {
boardID := utils.NewID(utils.IDTypeBoard)
prop1 := map[string]interface{}{"id": "prop1", "value": "val1"}
prop2 := map[string]interface{}{"id": "prop2", "value": "val2"}
prop3 := map[string]interface{}{"id": "prop3", "value": "val3"}
cardProperties := []map[string]interface{}{prop1, prop2, prop3}
board := &model.Board{
ID: boardID,
TeamID: testTeamID,
Type: model.BoardTypeOpen,
CardProperties: cardProperties,
}
newBoard, err := store.InsertBoard(board, userID)
require.NoError(t, err)
require.NotNil(t, newBoard)
require.Equal(t, newBoard.Type, model.BoardTypeOpen)
require.Equal(t, cardProperties, newBoard.CardProperties)
// wait to avoid hitting pk uniqueness constraint in history
time.Sleep(10 * time.Millisecond)
newProp1 := map[string]interface{}{"id": "prop1", "value": "newval1"}
expectedCardProperties := []map[string]interface{}{newProp1, prop3}
patch := &model.BoardPatch{
UpdatedCardProperties: []map[string]interface{}{newProp1},
DeletedCardProperties: []string{"prop2"},
}
patchedBoard, err := store.PatchBoard(boardID, patch, userID)
require.NoError(t, err)
require.ElementsMatch(t, expectedCardProperties, patchedBoard.CardProperties)
})
}
func testDeleteBoard(t *testing.T, store store.Store) {
userID := testUserID
t.Run("should return an error if the board doesn't exist", func(t *testing.T) {
require.Error(t, store.DeleteBoard("nonexistent-board-id", userID))
})
t.Run("should correctly delete the board", func(t *testing.T) {
boardID := utils.NewID(utils.IDTypeBoard)
board := &model.Board{
ID: boardID,
TeamID: testTeamID,
Type: model.BoardTypeOpen,
}
newBoard, err := store.InsertBoard(board, userID)
require.NoError(t, err)
require.NotNil(t, newBoard)
rBoard, err := store.GetBoard(boardID)
require.NoError(t, err)
require.NotNil(t, rBoard)
// wait to avoid hitting pk uniqueness constraint in history
time.Sleep(10 * time.Millisecond)
require.NoError(t, store.DeleteBoard(boardID, userID))
r2Board, err := store.GetBoard(boardID)
require.ErrorIs(t, err, sql.ErrNoRows)
require.Nil(t, r2Board)
})
}
func testInsertBoardWithAdmin(t *testing.T, store store.Store) {
userID := testUserID
t.Run("should correctly create a board and the admin membership with the creator", func(t *testing.T) {
boardID := utils.NewID(utils.IDTypeBoard)
board := &model.Board{
ID: boardID,
TeamID: testTeamID,
Type: model.BoardTypeOpen,
}
newBoard, newMember, err := store.InsertBoardWithAdmin(board, userID)
require.NoError(t, err)
require.NotNil(t, newBoard)
require.Equal(t, userID, newBoard.CreatedBy)
require.Equal(t, userID, newBoard.ModifiedBy)
require.NotNil(t, newMember)
require.Equal(t, userID, newMember.UserID)
require.Equal(t, boardID, newMember.BoardID)
require.True(t, newMember.SchemeAdmin)
require.True(t, newMember.SchemeEditor)
})
}
func testSaveMember(t *testing.T, store store.Store) {
userID := testUserID
boardID := testBoardID
t.Run("should correctly create a member", func(t *testing.T) {
bm := &model.BoardMember{
UserID: userID,
BoardID: boardID,
SchemeAdmin: true,
}
memberHistory, err := store.GetBoardMemberHistory(boardID, userID, 0)
require.NoError(t, err)
initialMemberHistory := len(memberHistory)
nbm, err := store.SaveMember(bm)
require.NoError(t, err)
require.Equal(t, userID, nbm.UserID)
require.Equal(t, boardID, nbm.BoardID)
require.True(t, nbm.SchemeAdmin)
memberHistory, err = store.GetBoardMemberHistory(boardID, userID, 0)
require.NoError(t, err)
require.Len(t, memberHistory, initialMemberHistory+1)
})
t.Run("should correctly update a member", func(t *testing.T) {
bm := &model.BoardMember{
UserID: userID,
BoardID: boardID,
SchemeEditor: true,
SchemeViewer: true,
}
memberHistory, err := store.GetBoardMemberHistory(boardID, userID, 0)
require.NoError(t, err)
initialMemberHistory := len(memberHistory)
nbm, err := store.SaveMember(bm)
require.NoError(t, err)
require.Equal(t, userID, nbm.UserID)
require.Equal(t, boardID, nbm.BoardID)
require.False(t, nbm.SchemeAdmin)
require.True(t, nbm.SchemeEditor)
require.True(t, nbm.SchemeViewer)
memberHistory, err = store.GetBoardMemberHistory(boardID, userID, 0)
require.NoError(t, err)
require.Len(t, memberHistory, initialMemberHistory)
})
}
func testGetMemberForBoard(t *testing.T, store store.Store) {
userID := testUserID
boardID := testBoardID
t.Run("should return a no rows error for nonexisting membership", func(t *testing.T) {
bm, err := store.GetMemberForBoard(boardID, userID)
require.ErrorIs(t, err, sql.ErrNoRows)
require.Nil(t, bm)
})
t.Run("should return the membership if exists", func(t *testing.T) {
bm := &model.BoardMember{
UserID: userID,
BoardID: boardID,
SchemeAdmin: true,
}
nbm, err := store.SaveMember(bm)
require.NoError(t, err)
require.NotNil(t, nbm)
rbm, err := store.GetMemberForBoard(boardID, userID)
require.NoError(t, err)
require.NotNil(t, rbm)
require.Equal(t, userID, rbm.UserID)
require.Equal(t, boardID, rbm.BoardID)
require.True(t, rbm.SchemeAdmin)
})
}
func testGetMembersForBoard(t *testing.T, store store.Store) {
t.Run("should return empty if there are no members on a board", func(t *testing.T) {
members, err := store.GetMembersForBoard(testBoardID)
require.NoError(t, err)
require.Empty(t, members)
})
t.Run("should return the members of the board", func(t *testing.T) {
boardID1 := "board-id-1"
boardID2 := "board-id-2"
userID1 := "user-id-11"
userID2 := "user-id-12"
userID3 := "user-id-13"
bm1 := &model.BoardMember{BoardID: boardID1, UserID: userID1, SchemeAdmin: true}
_, err1 := store.SaveMember(bm1)
require.NoError(t, err1)
bm2 := &model.BoardMember{BoardID: boardID1, UserID: userID2, SchemeEditor: true}
_, err2 := store.SaveMember(bm2)
require.NoError(t, err2)
bm3 := &model.BoardMember{BoardID: boardID2, UserID: userID3, SchemeAdmin: true}
_, err3 := store.SaveMember(bm3)
require.NoError(t, err3)
getMemberIDs := func(members []*model.BoardMember) []string {
ids := make([]string, len(members))
for i, member := range members {
ids[i] = member.UserID
}
return ids
}
board1Members, err := store.GetMembersForBoard(boardID1)
require.NoError(t, err)
require.Len(t, board1Members, 2)
require.ElementsMatch(t, []string{userID1, userID2}, getMemberIDs(board1Members))
board2Members, err := store.GetMembersForBoard(boardID2)
require.NoError(t, err)
require.Len(t, board2Members, 1)
require.ElementsMatch(t, []string{userID3}, getMemberIDs(board2Members))
})
}
func testDeleteMember(t *testing.T, store store.Store) {
userID := testUserID
boardID := testBoardID
t.Run("should return nil if deleting a nonexistent member", func(t *testing.T) {
memberHistory, err := store.GetBoardMemberHistory(boardID, userID, 0)
require.NoError(t, err)
initialMemberHistory := len(memberHistory)
require.NoError(t, store.DeleteMember(boardID, userID))
memberHistory, err = store.GetBoardMemberHistory(boardID, userID, 0)
require.NoError(t, err)
require.Len(t, memberHistory, initialMemberHistory)
})
t.Run("should correctly delete a member", func(t *testing.T) {
bm := &model.BoardMember{
UserID: userID,
BoardID: boardID,
SchemeAdmin: true,
}
nbm, err := store.SaveMember(bm)
require.NoError(t, err)
require.NotNil(t, nbm)
memberHistory, err := store.GetBoardMemberHistory(boardID, userID, 0)
require.NoError(t, err)
initialMemberHistory := len(memberHistory)
require.NoError(t, store.DeleteMember(boardID, userID))
rbm, err := store.GetMemberForBoard(boardID, userID)
require.ErrorIs(t, err, sql.ErrNoRows)
require.Nil(t, rbm)
memberHistory, err = store.GetBoardMemberHistory(boardID, userID, 0)
require.NoError(t, err)
require.Len(t, memberHistory, initialMemberHistory+1)
})
}
func testSearchBoardsForUserAndTeam(t *testing.T, store store.Store) {
teamID1 := "team-id-1"
teamID2 := "team-id-2"
userID := "user-id-1"
t.Run("should return empty if user is not a member of any board and there are no public boards on the team", func(t *testing.T) {
boards, err := store.SearchBoardsForUserAndTeam("", userID, teamID1)
require.NoError(t, err)
require.Empty(t, boards)
})
board1 := &model.Board{
ID: "board-id-1",
TeamID: teamID1,
Type: model.BoardTypeOpen,
Title: "Public Board with admin",
}
_, _, err := store.InsertBoardWithAdmin(board1, userID)
require.NoError(t, err)
board2 := &model.Board{
ID: "board-id-2",
TeamID: teamID1,
Type: model.BoardTypeOpen,
Title: "Public Board",
}
_, err = store.InsertBoard(board2, userID)
require.NoError(t, err)
board3 := &model.Board{
ID: "board-id-3",
TeamID: teamID1,
Type: model.BoardTypePrivate,
Title: "Private Board with admin",
}
_, _, err = store.InsertBoardWithAdmin(board3, userID)
require.NoError(t, err)
board4 := &model.Board{
ID: "board-id-4",
TeamID: teamID1,
Type: model.BoardTypePrivate,
Title: "Private Board",
}
_, err = store.InsertBoard(board4, userID)
require.NoError(t, err)
board5 := &model.Board{
ID: "board-id-5",
TeamID: teamID2,
Type: model.BoardTypeOpen,
Title: "Public Board with admin in team 2",
}
_, _, err = store.InsertBoardWithAdmin(board5, userID)
require.NoError(t, err)
testCases := []struct {
Name string
TeamID string
UserID string
Term string
ExpectedBoardIDs []string
}{
{
Name: "should find all private boards that the user is a member of and public boards with an empty term",
TeamID: teamID1,
UserID: userID,
Term: "",
ExpectedBoardIDs: []string{board1.ID, board2.ID, board3.ID},
},
{
Name: "should find all with term board",
TeamID: teamID1,
UserID: userID,
Term: "board",
ExpectedBoardIDs: []string{board1.ID, board2.ID, board3.ID},
},
{
Name: "should find only public as per the term, wether user is a member or not",
TeamID: teamID1,
UserID: userID,
Term: "public",
ExpectedBoardIDs: []string{board1.ID, board2.ID},
},
{
Name: "should find only private as per the term, wether user is a member or not",
TeamID: teamID1,
UserID: userID,
Term: "priv",
ExpectedBoardIDs: []string{board3.ID},
},
{
Name: "should find the only board in team 2",
TeamID: teamID2,
UserID: userID,
Term: "",
ExpectedBoardIDs: []string{board5.ID},
},
{
Name: "should find no board in team 2 with a non matching term",
TeamID: teamID2,
UserID: userID,
Term: "non-matching-term",
ExpectedBoardIDs: []string{},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
boards, err := store.SearchBoardsForUserAndTeam(tc.Term, tc.UserID, tc.TeamID)
require.NoError(t, err)
boardIDs := []string{}
for _, board := range boards {
boardIDs = append(boardIDs, board.ID)
}
require.ElementsMatch(t, tc.ExpectedBoardIDs, boardIDs)
})
}
}
func testGetBoardHistory(t *testing.T, store store.Store) {
userID := testUserID
t.Run("testGetBoardHistory: create board", func(t *testing.T) {
originalTitle := "Board: original title"
boardID := utils.NewID(utils.IDTypeBoard)
board := &model.Board{
ID: boardID,
Title: originalTitle,
TeamID: testTeamID,
Type: model.BoardTypeOpen,
}
rBoard1, err := store.InsertBoard(board, userID)
require.NoError(t, err)
opts := model.QueryBlockHistoryOptions{
Limit: 0,
Descending: false,
}
boards, err := store.GetBoardHistory(board.ID, opts)
require.NoError(t, err)
require.Len(t, boards, 1)
// wait to avoid hitting pk uniqueness constraint in history
time.Sleep(10 * time.Millisecond)
userID2 := "user-id-2"
newTitle := "Board: A new title"
newDescription := "A new description"
patch := &model.BoardPatch{Title: &newTitle, Description: &newDescription}
patchedBoard, err := store.PatchBoard(boardID, patch, userID2)
require.NoError(t, err)
// Updated history
boards, err = store.GetBoardHistory(board.ID, opts)
require.NoError(t, err)
require.Len(t, boards, 2)
require.Equal(t, boards[0].Title, originalTitle)
require.Equal(t, boards[1].Title, newTitle)
require.Equal(t, boards[1].Description, newDescription)
// Check history against latest board
rBoard2, err := store.GetBoard(board.ID)
require.NoError(t, err)
require.Equal(t, rBoard2.Title, newTitle)
require.Equal(t, rBoard2.Title, boards[1].Title)
require.NotZero(t, rBoard2.UpdateAt)
require.Equal(t, rBoard1.UpdateAt, boards[0].UpdateAt)
require.Equal(t, rBoard2.UpdateAt, patchedBoard.UpdateAt)
require.Equal(t, rBoard2.UpdateAt, boards[1].UpdateAt)
require.Equal(t, rBoard1, boards[0])
require.Equal(t, rBoard2, boards[1])
// wait to avoid hitting pk uniqueness constraint in history
time.Sleep(10 * time.Millisecond)
newTitle2 := "Board: A new title 2"
patch2 := &model.BoardPatch{Title: &newTitle2}
patchBoard2, err := store.PatchBoard(boardID, patch2, userID2)
require.NoError(t, err)
// Updated history
opts = model.QueryBlockHistoryOptions{
Limit: 1,
Descending: true,
}
boards, err = store.GetBoardHistory(board.ID, opts)
require.NoError(t, err)
require.Len(t, boards, 1)
require.Equal(t, boards[0].Title, newTitle2)
require.Equal(t, boards[0], patchBoard2)
// Delete board
time.Sleep(10 * time.Millisecond)
err = store.DeleteBoard(boardID, userID)
require.NoError(t, err)
// Updated history after delete
opts = model.QueryBlockHistoryOptions{
Limit: 0,
Descending: true,
}
boards, err = store.GetBoardHistory(board.ID, opts)
require.NoError(t, err)
require.Len(t, boards, 4)
require.NotZero(t, boards[0].UpdateAt)
require.Greater(t, boards[0].UpdateAt, patchBoard2.UpdateAt)
require.NotZero(t, boards[0].DeleteAt)
require.Greater(t, boards[0].DeleteAt, patchBoard2.UpdateAt)
})
t.Run("testGetBoardHistory: nonexisting board", func(t *testing.T) {
opts := model.QueryBlockHistoryOptions{
Limit: 0,
Descending: false,
}
boards, err := store.GetBoardHistory("nonexistent-id", opts)
require.NoError(t, err)
require.Len(t, boards, 0)
})
}