2020-12-29 02:04:20 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2019-09-25 20:44:41 +02:00
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-09-25 20:44:41 +02:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
2019-09-25 20:44:41 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2019-09-25 20:44:41 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-12-29 02:04:20 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-09-25 20:44:41 +02:00
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
|
|
"code.vikunja.io/api/pkg/user"
|
2019-09-25 20:44:41 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTaskRelation_Create(t *testing.T) {
|
|
|
|
t.Run("Normal", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 1,
|
|
|
|
OtherTaskID: 2,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := rel.Create(s, &user.User{ID: 1})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.NoError(t, err)
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertExists(t, "task_relations", map[string]interface{}{
|
|
|
|
"task_id": 1,
|
|
|
|
"other_task_id": 2,
|
|
|
|
"relation_kind": RelationKindSubtask,
|
|
|
|
"created_by_id": 1,
|
|
|
|
}, false)
|
2019-09-25 20:44:41 +02:00
|
|
|
})
|
|
|
|
t.Run("Two Tasks In Different Lists", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 1,
|
|
|
|
OtherTaskID: 13,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := rel.Create(s, &user.User{ID: 1})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.NoError(t, err)
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertExists(t, "task_relations", map[string]interface{}{
|
|
|
|
"task_id": 1,
|
|
|
|
"other_task_id": 13,
|
|
|
|
"relation_kind": RelationKindSubtask,
|
|
|
|
"created_by_id": 1,
|
|
|
|
}, false)
|
2019-09-25 20:44:41 +02:00
|
|
|
})
|
|
|
|
t.Run("Already Existing", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 1,
|
|
|
|
OtherTaskID: 29,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := rel.Create(s, &user.User{ID: 1})
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrRelationAlreadyExists(err))
|
|
|
|
})
|
|
|
|
t.Run("Same Task", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 1,
|
|
|
|
OtherTaskID: 1,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := rel.Create(s, &user.User{ID: 1})
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrRelationTasksCannotBeTheSame(err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRelation_Delete(t *testing.T) {
|
2021-02-02 23:48:37 +01:00
|
|
|
u := &user.User{ID: 1}
|
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
t.Run("Normal", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 1,
|
|
|
|
OtherTaskID: 29,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := rel.Delete(s, u)
|
2020-12-23 16:32:28 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.NoError(t, err)
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertMissing(t, "task_relations", map[string]interface{}{
|
|
|
|
"task_id": 1,
|
|
|
|
"other_task_id": 29,
|
|
|
|
"relation_kind": RelationKindSubtask,
|
|
|
|
})
|
2019-09-25 20:44:41 +02:00
|
|
|
})
|
|
|
|
t.Run("Not existing", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 9999,
|
|
|
|
OtherTaskID: 3,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := rel.Delete(s, u)
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrRelationDoesNotExist(err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRelation_CanCreate(t *testing.T) {
|
|
|
|
t.Run("Normal", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 1,
|
|
|
|
OtherTaskID: 2,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
can, err := rel.CanCreate(s, &user.User{ID: 1})
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, can)
|
|
|
|
})
|
|
|
|
t.Run("Two tasks on different lists", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 1,
|
|
|
|
OtherTaskID: 13,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
can, err := rel.CanCreate(s, &user.User{ID: 1})
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, can)
|
|
|
|
})
|
|
|
|
t.Run("No update rights on base task", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 14,
|
|
|
|
OtherTaskID: 1,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
can, err := rel.CanCreate(s, &user.User{ID: 1})
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, can)
|
|
|
|
})
|
|
|
|
t.Run("No update rights on base task, but read rights", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 15,
|
|
|
|
OtherTaskID: 1,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
can, err := rel.CanCreate(s, &user.User{ID: 1})
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, can)
|
|
|
|
})
|
|
|
|
t.Run("No read rights on other task", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 1,
|
|
|
|
OtherTaskID: 14,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
can, err := rel.CanCreate(s, &user.User{ID: 1})
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, can)
|
|
|
|
})
|
|
|
|
t.Run("Nonexisting base task", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 999999,
|
|
|
|
OtherTaskID: 1,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
can, err := rel.CanCreate(s, &user.User{ID: 1})
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskDoesNotExist(err))
|
|
|
|
assert.False(t, can)
|
|
|
|
})
|
|
|
|
t.Run("Nonexisting other task", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2019-09-25 20:44:41 +02:00
|
|
|
rel := TaskRelation{
|
|
|
|
TaskID: 1,
|
|
|
|
OtherTaskID: 999999,
|
|
|
|
RelationKind: RelationKindSubtask,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
can, err := rel.CanCreate(s, &user.User{ID: 1})
|
2019-09-25 20:44:41 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskDoesNotExist(err))
|
|
|
|
assert.False(t, can)
|
|
|
|
})
|
|
|
|
}
|