Skip to content

Commit

Permalink
Updated template loading validation (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshilsharma63 authored Feb 5, 2025
1 parent 2fb4d08 commit 40259ad
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 8 deletions.
8 changes: 4 additions & 4 deletions server/app/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,23 +212,23 @@ func (a *App) ImportBoardJSONL(r io.Reader, opt model.ImportArchiveOptions) (*mo
if err2 := json.Unmarshal(archiveLine.Data, &block); err2 != nil {
return nil, fmt.Errorf("invalid board block in archive line %d: %w", lineNum, err2)
}
if err := block.IsValid(); err != nil {
return nil, err
}
block.ModifiedBy = userID
block.UpdateAt = now
board, err := a.blockToBoard(block, opt)
if err != nil {
return nil, fmt.Errorf("cannot convert archive line %d to block: %w", lineNum, err)
}
if err := board.IsValidForImport(); err != nil {
return nil, err
}
boardsAndBlocks.Boards = append(boardsAndBlocks.Boards, board)
boardID = board.ID
case "block":
var block *model.Block
if err2 := json.Unmarshal(archiveLine.Data, &block); err2 != nil {
return nil, fmt.Errorf("invalid block in archive line %d: %w", lineNum, err2)
}
if err := block.IsValid(); err != nil {
if err := block.IsValidForImport(); err != nil {
return nil, err
}
block.ModifiedBy = userID
Expand Down
2 changes: 1 addition & 1 deletion server/app/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestApp_ImportArchive(t *testing.T) {
t.Run("import asana archive", func(t *testing.T) {
r := bytes.NewReader([]byte(asana))
opts := model.ImportArchiveOptions{
TeamID: "test-team",
TeamID: "y5tuzz9yb3y99gmobyc4hg5wnr",
ModifiedBy: "user",
}

Expand Down
3 changes: 0 additions & 3 deletions server/manifest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions server/model/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ func (b *Block) IsValid() error {
return err
}

return b.baseValidations()
}

func (b *Block) IsValidForImport() error {
if err := IsValidId(b.BoardID); err != nil {
if !errors.Is(err, errEmptyId) {
return err
}
}

return b.baseValidations()
}

func (b *Block) baseValidations() error {
if utf8.RuneCountInString(b.Title) > BlockTitleMaxRunes {
return ErrBlockTitleSizeLimitExceeded
}
Expand Down
235 changes: 235 additions & 0 deletions server/model/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package model
import (
"testing"

mmModel "github.com/mattermost/mattermost/server/public/model"

"github.com/stretchr/testify/assert"

"github.com/mattermost/mattermost/server/public/shared/mlog"
Expand Down Expand Up @@ -456,3 +458,236 @@ func TestValidateFileId(t *testing.T) {
require.EqualError(t, err, "Invalid Block ID")
})
}

func TestBlockIsValid(t *testing.T) {
t.Run("Should return nil for valid block", func(t *testing.T) {
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: string(utils.IDTypeNone) + mmModel.NewId(),
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: "Valid Block",
Fields: map[string]interface{}{},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValid()
require.NoError(t, err)
})

t.Run("Should return error for block with empty BoardID", func(t *testing.T) {
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: "",
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: "Invalid Block",
Fields: map[string]interface{}{},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValid()
require.Error(t, err)
require.EqualError(t, err, "Block ID cannot be empty")
})

t.Run("Should return error for block with title exceeding max runes", func(t *testing.T) {
longTitle := make([]rune, BlockTitleMaxRunes+1)
for i := range longTitle {
longTitle[i] = 'a'
}
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: string(utils.IDTypeNone) + mmModel.NewId(),
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: string(longTitle),
Fields: map[string]interface{}{},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValid()
require.Error(t, err)
require.EqualError(t, err, "block title size limit exceeded")
})

t.Run("Should return error for block with fields exceeding max runes", func(t *testing.T) {
longField := make([]rune, BlockFieldsMaxRunes+1)
for i := range longField {
longField[i] = 'a'
}
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: string(utils.IDTypeNone) + mmModel.NewId(),
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: "Valid Block",
Fields: map[string]interface{}{"field": string(longField)},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValid()
require.Error(t, err)
require.EqualError(t, err, "block fields size limit exceeded")
})

t.Run("Should return error for block with invalid file ID in fields", func(t *testing.T) {
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: string(utils.IDTypeNone) + mmModel.NewId(),
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: "Valid Block",
Fields: map[string]interface{}{BlockFieldFileId: "invalid-file-id"},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValid()
require.Error(t, err)
require.EqualError(t, err, "Invalid Block ID")
})

t.Run("Should return error for block with invalid attachment ID in fields", func(t *testing.T) {
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: string(utils.IDTypeNone) + mmModel.NewId(),
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: "Valid Block",
Fields: map[string]interface{}{BlockFieldAttachmentId: "invalid-attachment-id"},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValid()
require.Error(t, err)
require.EqualError(t, err, "Invalid Block ID")
})
}

func TestBlock_IsValidForImport(t *testing.T) {
t.Run("Should return nil for valid block", func(t *testing.T) {
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: string(utils.IDTypeNone) + mmModel.NewId(),
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: "Valid Block",
Fields: map[string]interface{}{},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValidForImport()
require.NoError(t, err)
})

t.Run("Should not return error for block with empty BoardID", func(t *testing.T) {
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: "",
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: "Invalid Block",
Fields: map[string]interface{}{},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValidForImport()
require.NoError(t, err)
})

t.Run("Should return error for block with title exceeding max runes", func(t *testing.T) {
longTitle := make([]rune, BlockTitleMaxRunes+1)
for i := range longTitle {
longTitle[i] = 'a'
}
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: string(utils.IDTypeNone) + mmModel.NewId(),
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: string(longTitle),
Fields: map[string]interface{}{},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValidForImport()
require.Error(t, err)
require.EqualError(t, err, "block title size limit exceeded")
})

t.Run("Should return error for block with fields exceeding max runes", func(t *testing.T) {
longField := make([]rune, BlockFieldsMaxRunes+1)
for i := range longField {
longField[i] = 'a'
}
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: string(utils.IDTypeNone) + mmModel.NewId(),
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: "Valid Block",
Fields: map[string]interface{}{"field": string(longField)},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValidForImport()
require.Error(t, err)
require.EqualError(t, err, "block fields size limit exceeded")
})

t.Run("Should return error for block with invalid file ID in fields", func(t *testing.T) {
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: string(utils.IDTypeNone) + mmModel.NewId(),
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: "Valid Block",
Fields: map[string]interface{}{BlockFieldFileId: "invalid-file-id"},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValidForImport()
require.Error(t, err)
require.EqualError(t, err, "Invalid Block ID")
})

t.Run("Should return error for block with invalid attachment ID in fields", func(t *testing.T) {
block := &Block{
ID: string(utils.IDTypeNone) + mmModel.NewId(),
BoardID: string(utils.IDTypeNone) + mmModel.NewId(),
CreatedBy: string(utils.IDTypeNone) + mmModel.NewId(),
ModifiedBy: string(utils.IDTypeNone) + mmModel.NewId(),
Schema: 1,
Type: TypeCard,
Title: "Valid Block",
Fields: map[string]interface{}{BlockFieldAttachmentId: "invalid-attachment-id"},
CreateAt: 1234567890,
UpdateAt: 1234567890,
}
err := block.IsValidForImport()
require.Error(t, err)
require.EqualError(t, err, "Invalid Block ID")
})
}
12 changes: 12 additions & 0 deletions server/model/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,18 @@ func (b *Board) IsValid() error {
return InvalidBoardErr{"invalid-team-id"}
}

return b.baseValidation()
}

func (b *Board) IsValidForImport() error {
if !mmModel.IsValidId(b.TeamID) && b.TeamID != GlobalTeamID {
return InvalidBoardErr{"invalid-team-id"}
}

return b.baseValidation()
}

func (b *Board) baseValidation() error {
if !IsBoardTypeValid(b.Type) {
return InvalidBoardErr{"invalid-board-type"}
}
Expand Down
Loading

0 comments on commit 40259ad

Please sign in to comment.