Fixed bug where boards would move to category of a different team (#4284)

This commit is contained in:
Harshil Sharma 2022-12-02 23:07:49 +05:30 committed by GitHub
parent 60cafbe6eb
commit e075f408d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 5 deletions

View file

@ -434,6 +434,7 @@ func TestBoardCategory(t *testing.T) {
Name: "Boards", Name: "Boards",
}, nil) }, nil)
th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{}, nil) th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{}, nil)
th.Store.EXPECT().GetBoardsForUserAndTeam("user_id", "team_id", false).Return([]*model.Board{}, nil)
th.Store.EXPECT().AddUpdateCategoryBoard("user_id", map[string]string{ th.Store.EXPECT().AddUpdateCategoryBoard("user_id", map[string]string{
"board_id_1": "default_category_id", "board_id_1": "default_category_id",
"board_id_2": "default_category_id", "board_id_2": "default_category_id",

View file

@ -11,6 +11,7 @@ const defaultCategoryBoards = "Boards"
var errCategoryBoardsLengthMismatch = errors.New("cannot update category boards order, passed list of categories boards different size than in database") var errCategoryBoardsLengthMismatch = errors.New("cannot update category boards order, passed list of categories boards different size than in database")
var errBoardNotFoundInCategory = errors.New("specified board ID not found in specified category ID") var errBoardNotFoundInCategory = errors.New("specified board ID not found in specified category ID")
var errBoardMembershipNotFound = errors.New("board membership not found for user's board")
func (a *App) GetUserCategoryBoards(userID, teamID string) ([]model.CategoryBoards, error) { func (a *App) GetUserCategoryBoards(userID, teamID string) ([]model.CategoryBoards, error) {
categoryBoards, err := a.store.GetUserCategoryBoards(userID, teamID) categoryBoards, err := a.store.GetUserCategoryBoards(userID, teamID)
@ -66,23 +67,40 @@ func (a *App) createBoardsCategory(userID, teamID string, existingCategoryBoards
// once the category is created, we need to move all boards which do not // once the category is created, we need to move all boards which do not
// belong to any category, into this category. // belong to any category, into this category.
boardMembers, err := a.GetMembersForUser(userID) boardMembers, err := a.GetMembersForUser(userID)
if err != nil { if err != nil {
return nil, fmt.Errorf("createBoardsCategory error fetching user's board memberships: %w", err) return nil, fmt.Errorf("createBoardsCategory error fetching user's board memberships: %w", err)
} }
boardMemberByBoardID := map[string]*model.BoardMember{}
for _, boardMember := range boardMembers {
boardMemberByBoardID[boardMember.BoardID] = boardMember
}
createdCategoryBoards := &model.CategoryBoards{ createdCategoryBoards := &model.CategoryBoards{
Category: *createdCategory, Category: *createdCategory,
BoardIDs: []string{}, BoardIDs: []string{},
} }
for _, bm := range boardMembers { // get user's current team's baords
userTeamBoards, err := a.GetBoardsForUserAndTeam(userID, teamID, false)
if err != nil {
return nil, fmt.Errorf("createBoardsCategory error fetching user's team's boards: %w", err)
}
for _, board := range userTeamBoards {
boardMembership, ok := boardMemberByBoardID[board.ID]
if !ok {
return nil, fmt.Errorf("createBoardsCategory: %w", errBoardMembershipNotFound)
}
// boards with implicit access (aka synthetic membership), // boards with implicit access (aka synthetic membership),
// should show up in LHS only when openign them explicitelly. // should show up in LHS only when openign them explicitelly.
// So we don't process any synthetic membership boards // So we don't process any synthetic membership boards
// and only add boards with explicit access to, to the the LHS, // and only add boards with explicit access to, to the the LHS,
// for example, if a user explicitelly added another user to a board. // for example, if a user explicitelly added another user to a board.
if bm.Synthetic { if boardMembership.Synthetic {
continue continue
} }
@ -90,7 +108,7 @@ func (a *App) createBoardsCategory(userID, teamID string, existingCategoryBoards
for _, categoryBoard := range existingCategoryBoards { for _, categoryBoard := range existingCategoryBoards {
for _, boardID := range categoryBoard.BoardIDs { for _, boardID := range categoryBoard.BoardIDs {
if boardID == bm.BoardID { if boardID == board.ID {
belongsToCategory = true belongsToCategory = true
break break
} }
@ -104,11 +122,11 @@ func (a *App) createBoardsCategory(userID, teamID string, existingCategoryBoards
} }
if !belongsToCategory { if !belongsToCategory {
if err := a.AddUpdateUserCategoryBoard(teamID, userID, map[string]string{bm.BoardID: createdCategory.ID}); err != nil { if err := a.AddUpdateUserCategoryBoard(teamID, userID, map[string]string{board.ID: createdCategory.ID}); err != nil {
return nil, fmt.Errorf("createBoardsCategory failed to add category-less board to the default category, defaultCategoryID: %s, error: %w", createdCategory.ID, err) return nil, fmt.Errorf("createBoardsCategory failed to add category-less board to the default category, defaultCategoryID: %s, error: %w", createdCategory.ID, err)
} }
createdCategoryBoards.BoardIDs = append(createdCategoryBoards.BoardIDs, bm.BoardID) createdCategoryBoards.BoardIDs = append(createdCategoryBoards.BoardIDs, board.ID)
} }
} }

View file

@ -21,6 +21,20 @@ func TestGetUserCategoryBoards(t *testing.T) {
Name: "Boards", Name: "Boards",
}, nil) }, nil)
board1 := &model.Board{
ID: "board_id_1",
}
board2 := &model.Board{
ID: "board_id_2",
}
board3 := &model.Board{
ID: "board_id_3",
}
th.Store.EXPECT().GetBoardsForUserAndTeam("user_id", "team_id", false).Return([]*model.Board{board1, board2, board3}, nil)
th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{ th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{
{ {
BoardID: "board_id_1", BoardID: "board_id_1",
@ -58,6 +72,7 @@ func TestGetUserCategoryBoards(t *testing.T) {
}, nil) }, nil)
th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{}, nil) th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{}, nil)
th.Store.EXPECT().GetBoardsForUserAndTeam("user_id", "team_id", false).Return([]*model.Board{}, nil)
categoryBoards, err := th.App.GetUserCategoryBoards("user_id", "team_id") categoryBoards, err := th.App.GetUserCategoryBoards("user_id", "team_id")
assert.NoError(t, err) assert.NoError(t, err)
@ -93,6 +108,7 @@ func TestCreateBoardsCategory(t *testing.T) {
Type: "system", Type: "system",
Name: "Boards", Name: "Boards",
}, nil) }, nil)
th.Store.EXPECT().GetBoardsForUserAndTeam("user_id", "team_id", false).Return([]*model.Board{}, nil)
th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{}, nil) th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{}, nil)
existingCategoryBoards := []model.CategoryBoards{} existingCategoryBoards := []model.CategoryBoards{}
@ -110,6 +126,7 @@ func TestCreateBoardsCategory(t *testing.T) {
Type: "system", Type: "system",
Name: "Boards", Name: "Boards",
}, nil) }, nil)
th.Store.EXPECT().GetBoardsForUserAndTeam("user_id", "team_id", false).Return([]*model.Board{}, nil)
th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{ th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{
{ {
BoardID: "board_id_1", BoardID: "board_id_1",
@ -143,6 +160,17 @@ func TestCreateBoardsCategory(t *testing.T) {
Type: "system", Type: "system",
Name: "Boards", Name: "Boards",
}, nil) }, nil)
board1 := &model.Board{
ID: "board_id_1",
}
board2 := &model.Board{
ID: "board_id_2",
}
board3 := &model.Board{
ID: "board_id_3",
}
th.Store.EXPECT().GetBoardsForUserAndTeam("user_id", "team_id", false).Return([]*model.Board{board1, board2, board3}, nil)
th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{ th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{
{ {
BoardID: "board_id_1", BoardID: "board_id_1",
@ -179,6 +207,11 @@ func TestCreateBoardsCategory(t *testing.T) {
Type: "system", Type: "system",
Name: "Boards", Name: "Boards",
}, nil) }, nil)
board1 := &model.Board{
ID: "board_id_1",
}
th.Store.EXPECT().GetBoardsForUserAndTeam("user_id", "team_id", false).Return([]*model.Board{board1}, nil)
th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{ th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{
{ {
BoardID: "board_id_1", BoardID: "board_id_1",

View file

@ -375,6 +375,7 @@ func TestMoveBoardsToDefaultCategory(t *testing.T) {
Type: "system", Type: "system",
}, nil) }, nil)
th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{}, nil) th.Store.EXPECT().GetMembersForUser("user_id").Return([]*model.BoardMember{}, nil)
th.Store.EXPECT().GetBoardsForUserAndTeam("user_id", "team_id", false).Return([]*model.Board{}, nil)
th.Store.EXPECT().AddUpdateCategoryBoard("user_id", utils.Anything).Return(nil) th.Store.EXPECT().AddUpdateCategoryBoard("user_id", utils.Anything).Return(nil)
err := th.App.moveBoardsToDefaultCategory("user_id", "team_id", "category_id_2") err := th.App.moveBoardsToDefaultCategory("user_id", "team_id", "category_id_2")

View file

@ -55,6 +55,7 @@ func TestApp_ImportArchive(t *testing.T) {
ID: "boards_category_id", ID: "boards_category_id",
Name: "Boards", Name: "Boards",
}, nil) }, nil)
th.Store.EXPECT().GetBoardsForUserAndTeam("user", "test-team", false).Return([]*model.Board{}, nil)
th.Store.EXPECT().GetMembersForUser("user").Return([]*model.BoardMember{}, nil) th.Store.EXPECT().GetMembersForUser("user").Return([]*model.BoardMember{}, nil)
th.Store.EXPECT().AddUpdateCategoryBoard("user", utils.Anything).Return(nil) th.Store.EXPECT().AddUpdateCategoryBoard("user", utils.Anything).Return(nil)

View file

@ -77,6 +77,7 @@ func TestPrepareOnboardingTour(t *testing.T) {
ID: "boards_category", ID: "boards_category",
Name: "Boards", Name: "Boards",
}, nil) }, nil)
th.Store.EXPECT().GetBoardsForUserAndTeam("user_id_1", teamID, false).Return([]*model.Board{}, nil)
th.Store.EXPECT().AddUpdateCategoryBoard("user_id_1", map[string]string{"board_id_2": "boards_category_id"}).Return(nil) th.Store.EXPECT().AddUpdateCategoryBoard("user_id_1", map[string]string{"board_id_2": "boards_category_id"}).Return(nil)
teamID, boardID, err := th.App.PrepareOnboardingTour(userID, teamID) teamID, boardID, err := th.App.PrepareOnboardingTour(userID, teamID)