fix crash in DeleteBlock (#2040)

* fix crash in deleteblock

* fix unit test
This commit is contained in:
Doug Lauder 2022-01-05 04:02:06 -05:00 committed by GitHub
parent 6e747d5731
commit 8cf73382b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 4 deletions

View file

@ -1594,8 +1594,8 @@ func (a *API) handleCreateSubscription(w http.ResponseWriter, r *http.Request) {
}
// check for valid block
_, err = a.app.GetBlockWithID(*container, sub.BlockID)
if err != nil {
block, err := a.app.GetBlockWithID(*container, sub.BlockID)
if err != nil || block == nil {
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "invalid blockID", err)
return
}

View file

@ -151,6 +151,11 @@ func (a *App) DeleteBlock(c store.Container, blockID string, modifiedBy string)
return err
}
if block == nil {
// deleting non-existing block not considered an error
return nil
}
err = a.store.DeleteBlock(c, blockID, modifiedBy)
if err != nil {
return err

View file

@ -20,10 +20,37 @@ func createTestSubscriptions(client *client.Client, num int, workspaceID string)
return nil, "", fmt.Errorf("cannot get current user: %w", resp.Error)
}
board := model.Block{
ID: utils.NewID(utils.IDTypeBoard),
RootID: workspaceID,
CreateAt: 1,
UpdateAt: 1,
Type: model.TypeBoard,
}
boards, resp := client.InsertBlocks([]model.Block{board})
if resp.Error != nil {
return nil, "", fmt.Errorf("cannot insert test board block: %w", resp.Error)
}
board = boards[0]
for n := 0; n < num; n++ {
newBlock := model.Block{
ID: utils.NewID(utils.IDTypeCard),
RootID: board.ID,
CreateAt: 1,
UpdateAt: 1,
Type: model.TypeCard,
}
newBlocks, resp := client.InsertBlocks([]model.Block{newBlock})
if resp.Error != nil {
return nil, "", fmt.Errorf("cannot insert test card block: %w", resp.Error)
}
newBlock = newBlocks[0]
sub := &model.Subscription{
BlockType: model.TypeCard,
BlockID: utils.NewID(utils.IDTypeCard),
BlockType: newBlock.Type,
BlockID: newBlock.ID,
WorkspaceID: workspaceID,
SubscriberType: model.SubTypeUser,
SubscriberID: user.ID,