1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-21 13:38:56 +02:00
focalboard/server/app/category_test.go
Miguel de la Cruz 9fac8f476e
Revert dnd (#4294)
* Revert "Fixed bug where boards would move to category of a different team (#4284)"

This reverts commit e075f408d3.

* Revert "Merge branch 'main' into only-explicit-boards-on-default-category"

This reverts commit 7db7e56296, reversing
changes made to 3feda10b6d.

* Revert "DND support for category and boards in LHS (#3964)"

This reverts commit 9918a0b3f8.
2022-12-05 15:03:34 -05:00

296 lines
8.1 KiB
Go

package app
import (
"testing"
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/utils"
"github.com/stretchr/testify/assert"
)
func TestCreateCategory(t *testing.T) {
th, tearDown := SetupTestHelper(t)
defer tearDown()
t.Run("base case", func(t *testing.T) {
th.Store.EXPECT().CreateCategory(utils.Anything).Return(nil)
th.Store.EXPECT().GetCategory(utils.Anything).Return(&model.Category{
ID: "category_id_1",
}, nil)
category := &model.Category{
Name: "Category",
UserID: "user_id",
TeamID: "team_id",
Type: "custom",
}
createdCategory, err := th.App.CreateCategory(category)
assert.NotNil(t, createdCategory)
assert.NoError(t, err)
})
t.Run("creating invalid category", func(t *testing.T) {
category := &model.Category{
Name: "", // empty name shouldn't be allowed
UserID: "user_id",
TeamID: "team_id",
Type: "custom",
}
createdCategory, err := th.App.CreateCategory(category)
assert.Nil(t, createdCategory)
assert.Error(t, err)
category.Name = "Name"
category.UserID = "" // empty creator user id shouldn't be allowed
createdCategory, err = th.App.CreateCategory(category)
assert.Nil(t, createdCategory)
assert.Error(t, err)
category.UserID = "user_id"
category.TeamID = "" // empty TeamID shouldn't be allowed
createdCategory, err = th.App.CreateCategory(category)
assert.Nil(t, createdCategory)
assert.Error(t, err)
category.Type = "invalid" // unknown type shouldn't be allowed
createdCategory, err = th.App.CreateCategory(category)
assert.Nil(t, createdCategory)
assert.Error(t, err)
})
}
func TestUpdateCategory(t *testing.T) {
th, tearDown := SetupTestHelper(t)
defer tearDown()
t.Run("base case", func(t *testing.T) {
th.Store.EXPECT().GetCategory(utils.Anything).Return(&model.Category{
ID: "category_id_1",
Name: "Category",
TeamID: "team_id_1",
UserID: "user_id_1",
Type: "custom",
}, nil)
th.Store.EXPECT().UpdateCategory(utils.Anything).Return(nil)
th.Store.EXPECT().GetCategory("category_id_1").Return(&model.Category{
ID: "category_id_1",
Name: "Category",
}, nil)
category := &model.Category{
ID: "category_id_1",
Name: "Category",
UserID: "user_id_1",
TeamID: "team_id_1",
Type: "custom",
}
updatedCategory, err := th.App.UpdateCategory(category)
assert.NotNil(t, updatedCategory)
assert.NoError(t, err)
})
t.Run("updating invalid category", func(t *testing.T) {
category := &model.Category{
ID: "category_id_1",
Name: "Name",
UserID: "user_id",
TeamID: "team_id",
Type: "custom",
}
category.ID = ""
createdCategory, err := th.App.UpdateCategory(category)
assert.Nil(t, createdCategory)
assert.Error(t, err)
category.ID = "category_id_1"
category.Name = ""
createdCategory, err = th.App.UpdateCategory(category)
assert.Nil(t, createdCategory)
assert.Error(t, err)
category.Name = "Name"
category.UserID = "" // empty creator user id shouldn't be allowed
createdCategory, err = th.App.UpdateCategory(category)
assert.Nil(t, createdCategory)
assert.Error(t, err)
category.UserID = "user_id"
category.TeamID = "" // empty TeamID shouldn't be allowed
createdCategory, err = th.App.UpdateCategory(category)
assert.Nil(t, createdCategory)
assert.Error(t, err)
category.Type = "invalid" // unknown type shouldn't be allowed
createdCategory, err = th.App.UpdateCategory(category)
assert.Nil(t, createdCategory)
assert.Error(t, err)
})
t.Run("trying to update someone else's category", func(t *testing.T) {
th.Store.EXPECT().GetCategory(utils.Anything).Return(&model.Category{
ID: "category_id_1",
Name: "Category",
TeamID: "team_id_1",
UserID: "user_id_1",
Type: "custom",
}, nil)
category := &model.Category{
ID: "category_id_1",
Name: "Category",
UserID: "user_id_2",
TeamID: "team_id_1",
Type: "custom",
}
updatedCategory, err := th.App.UpdateCategory(category)
assert.Nil(t, updatedCategory)
assert.Error(t, err)
})
t.Run("trying to update some other team's category", func(t *testing.T) {
th.Store.EXPECT().GetCategory(utils.Anything).Return(&model.Category{
ID: "category_id_1",
Name: "Category",
TeamID: "team_id_1",
UserID: "user_id_1",
Type: "custom",
}, nil)
category := &model.Category{
ID: "category_id_1",
Name: "Category",
UserID: "user_id_1",
TeamID: "team_id_2",
Type: "custom",
}
updatedCategory, err := th.App.UpdateCategory(category)
assert.Nil(t, updatedCategory)
assert.Error(t, err)
})
t.Run("should not be allowed to rename system category", func(t *testing.T) {
th.Store.EXPECT().GetCategory(utils.Anything).Return(&model.Category{
ID: "category_id_1",
Name: "Category",
TeamID: "team_id_1",
UserID: "user_id_1",
Type: "system",
}, nil).Times(1)
th.Store.EXPECT().UpdateCategory(utils.Anything).Return(nil)
th.Store.EXPECT().GetCategory(utils.Anything).Return(&model.Category{
ID: "category_id_1",
Name: "Category",
TeamID: "team_id_1",
UserID: "user_id_1",
Type: "system",
Collapsed: true,
}, nil).Times(1)
category := &model.Category{
ID: "category_id_1",
Name: "Updated Name",
UserID: "user_id_1",
TeamID: "team_id_1",
Type: "system",
}
updatedCategory, err := th.App.UpdateCategory(category)
assert.NotNil(t, updatedCategory)
assert.NoError(t, err)
assert.Equal(t, "Category", updatedCategory.Name)
})
t.Run("should be allowed to collapse and expand any category type", func(t *testing.T) {
th.Store.EXPECT().GetCategory(utils.Anything).Return(&model.Category{
ID: "category_id_1",
Name: "Category",
TeamID: "team_id_1",
UserID: "user_id_1",
Type: "system",
Collapsed: false,
}, nil).Times(1)
th.Store.EXPECT().UpdateCategory(utils.Anything).Return(nil)
th.Store.EXPECT().GetCategory(utils.Anything).Return(&model.Category{
ID: "category_id_1",
Name: "Category",
TeamID: "team_id_1",
UserID: "user_id_1",
Type: "system",
Collapsed: true,
}, nil).Times(1)
category := &model.Category{
ID: "category_id_1",
Name: "Updated Name",
UserID: "user_id_1",
TeamID: "team_id_1",
Type: "system",
Collapsed: true,
}
updatedCategory, err := th.App.UpdateCategory(category)
assert.NotNil(t, updatedCategory)
assert.NoError(t, err)
assert.Equal(t, "Category", updatedCategory.Name, "The name should have not been updated")
assert.True(t, updatedCategory.Collapsed)
})
}
func TestDeleteCategory(t *testing.T) {
th, tearDown := SetupTestHelper(t)
defer tearDown()
t.Run("base case", func(t *testing.T) {
th.Store.EXPECT().GetCategory("category_id_1").Return(&model.Category{
ID: "category_id_1",
DeleteAt: 0,
UserID: "user_id_1",
TeamID: "team_id_1",
Type: "custom",
}, nil)
th.Store.EXPECT().DeleteCategory("category_id_1", "user_id_1", "team_id_1").Return(nil)
th.Store.EXPECT().GetCategory("category_id_1").Return(&model.Category{
DeleteAt: 10000,
}, nil)
deletedCategory, err := th.App.DeleteCategory("category_id_1", "user_id_1", "team_id_1")
assert.NotNil(t, deletedCategory)
assert.NoError(t, err)
})
t.Run("trying to delete already deleted category", func(t *testing.T) {
th.Store.EXPECT().GetCategory("category_id_1").Return(&model.Category{
ID: "category_id_1",
DeleteAt: 1000,
UserID: "user_id_1",
TeamID: "team_id_1",
Type: "custom",
}, nil)
deletedCategory, err := th.App.DeleteCategory("category_id_1", "user_id_1", "team_id_1")
assert.NotNil(t, deletedCategory)
assert.NoError(t, err)
})
t.Run("trying to delete system category", func(t *testing.T) {
th.Store.EXPECT().GetCategory("category_id_1").Return(&model.Category{
ID: "category_id_1",
DeleteAt: 0,
UserID: "user_id_1",
TeamID: "team_id_1",
Type: "system",
}, nil)
deletedCategory, err := th.App.DeleteCategory("category_id_1", "user_id_1", "team_id_1")
assert.Nil(t, deletedCategory)
assert.Error(t, err)
})
}