2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2020-01-09 18:33:22 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-07-27 14:47:52 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
2020-02-08 13:48:49 +01:00
|
|
|
"code.vikunja.io/api/pkg/timeutil"
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/user"
|
2018-07-27 14:47:52 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
2020-06-13 23:32:31 +02:00
|
|
|
"time"
|
2018-07-27 14:47:52 +02:00
|
|
|
)
|
|
|
|
|
2019-08-14 22:19:04 +02:00
|
|
|
func TestTask_Create(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
usr := &user.User{
|
2019-12-08 16:10:34 +01:00
|
|
|
ID: 1,
|
|
|
|
Username: "user1",
|
|
|
|
Email: "user1@example.com",
|
2018-07-27 14:47:52 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
// We only test creating a task here, the rights are all well tested in the integration tests.
|
2018-07-27 14:47:52 +02:00
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "Lorem",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
err := task.Create(usr)
|
2019-12-08 16:10:34 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// Assert getting a uid
|
|
|
|
assert.NotEmpty(t, task.UID)
|
|
|
|
// Assert getting a new index
|
|
|
|
assert.NotEmpty(t, task.Index)
|
|
|
|
assert.Equal(t, int64(18), task.Index)
|
2020-04-25 22:32:02 +02:00
|
|
|
// Assert moving it into the default bucket
|
|
|
|
assert.Equal(t, int64(1), task.BucketID)
|
2018-07-27 14:47:52 +02:00
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
})
|
2020-05-16 12:17:44 +02:00
|
|
|
t.Run("empty title", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
err := task.Create(usr)
|
2019-12-08 16:10:34 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskCannotBeEmpty(err))
|
|
|
|
})
|
|
|
|
t.Run("nonexistant list", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "Test",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 9999999,
|
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
err := task.Create(usr)
|
2019-12-08 16:10:34 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrListDoesNotExist(err))
|
|
|
|
})
|
|
|
|
t.Run("noneixtant user", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
nUser := &user.User{ID: 99999999}
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "Test",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
}
|
|
|
|
err := task.Create(nUser)
|
|
|
|
assert.Error(t, err)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.True(t, user.IsErrUserDoesNotExist(err))
|
2019-12-08 16:10:34 +01:00
|
|
|
})
|
|
|
|
}
|
2018-07-27 14:47:52 +02:00
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
func TestTask_Update(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
|
|
|
ID: 1,
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "test10000",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
}
|
|
|
|
err := task.Update()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("nonexistant task", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
|
|
|
ID: 9999999,
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "test10000",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
}
|
|
|
|
err := task.Update()
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskDoesNotExist(err))
|
|
|
|
})
|
|
|
|
}
|
2018-07-27 14:47:52 +02:00
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
func TestTask_Delete(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
|
|
|
ID: 1,
|
|
|
|
}
|
|
|
|
err := task.Delete()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2018-07-27 14:47:52 +02:00
|
|
|
}
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
func TestUpdateDone(t *testing.T) {
|
|
|
|
t.Run("marking a task as done", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2019-08-14 22:19:04 +02:00
|
|
|
oldTask := &Task{Done: false}
|
|
|
|
newTask := &Task{Done: true}
|
2019-05-22 19:48:48 +02:00
|
|
|
updateDone(oldTask, newTask)
|
2020-02-26 22:09:45 +01:00
|
|
|
assert.NotEqual(t, timeutil.TimeStamp(0), newTask.DoneAt)
|
2019-05-22 19:48:48 +02:00
|
|
|
})
|
|
|
|
t.Run("unmarking a task as done", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2019-08-14 22:19:04 +02:00
|
|
|
oldTask := &Task{Done: true}
|
|
|
|
newTask := &Task{Done: false}
|
2019-05-22 19:48:48 +02:00
|
|
|
updateDone(oldTask, newTask)
|
2020-02-26 22:09:45 +01:00
|
|
|
assert.Equal(t, timeutil.TimeStamp(0), newTask.DoneAt)
|
|
|
|
})
|
|
|
|
t.Run("repeating interval", func(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
DueDate: timeutil.TimeStamp(1550000000),
|
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
2020-06-13 23:32:31 +02:00
|
|
|
|
|
|
|
var expected int64 = 1550008600
|
|
|
|
for expected < time.Now().Unix() {
|
|
|
|
expected += oldTask.RepeatAfter
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, timeutil.TimeStamp(expected), newTask.DueDate)
|
2020-02-26 22:09:45 +01:00
|
|
|
})
|
|
|
|
t.Run("don't update if due date is zero", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
DueDate: timeutil.TimeStamp(0),
|
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
DueDate: timeutil.TimeStamp(1543626724),
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
|
|
|
assert.Equal(t, timeutil.TimeStamp(1543626724), newTask.DueDate)
|
|
|
|
})
|
|
|
|
t.Run("update reminders", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
Reminders: []timeutil.TimeStamp{
|
|
|
|
1550000000,
|
|
|
|
1555000000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
2020-06-13 23:32:31 +02:00
|
|
|
|
|
|
|
var expected1 int64 = 1550008600
|
|
|
|
var expected2 int64 = 1555008600
|
|
|
|
for expected1 < time.Now().Unix() {
|
|
|
|
expected1 += oldTask.RepeatAfter
|
|
|
|
}
|
|
|
|
for expected2 < time.Now().Unix() {
|
|
|
|
expected2 += oldTask.RepeatAfter
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:09:45 +01:00
|
|
|
assert.Len(t, newTask.Reminders, 2)
|
2020-06-13 23:32:31 +02:00
|
|
|
assert.Equal(t, timeutil.TimeStamp(expected1), newTask.Reminders[0])
|
|
|
|
assert.Equal(t, timeutil.TimeStamp(expected2), newTask.Reminders[1])
|
2020-02-26 22:09:45 +01:00
|
|
|
})
|
|
|
|
t.Run("update start date", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
StartDate: timeutil.TimeStamp(1550000000),
|
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
2020-06-13 23:32:31 +02:00
|
|
|
|
|
|
|
var expected int64 = 1550008600
|
|
|
|
for expected < time.Now().Unix() {
|
|
|
|
expected += oldTask.RepeatAfter
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, timeutil.TimeStamp(expected), newTask.StartDate)
|
2020-02-26 22:09:45 +01:00
|
|
|
})
|
|
|
|
t.Run("update end date", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
EndDate: timeutil.TimeStamp(1550000000),
|
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
2020-06-13 23:32:31 +02:00
|
|
|
|
|
|
|
var expected int64 = 1550008600
|
|
|
|
for expected < time.Now().Unix() {
|
|
|
|
expected += oldTask.RepeatAfter
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, timeutil.TimeStamp(expected), newTask.EndDate)
|
|
|
|
})
|
|
|
|
t.Run("ensure due date is repeated even if the original one is in the future", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
DueDate: timeutil.FromTime(time.Now().Add(time.Hour)),
|
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
|
|
|
expected := int64(oldTask.DueDate) + oldTask.RepeatAfter
|
|
|
|
assert.Equal(t, timeutil.TimeStamp(expected), newTask.DueDate)
|
2020-02-26 22:09:45 +01:00
|
|
|
})
|
2019-05-22 19:48:48 +02:00
|
|
|
})
|
|
|
|
}
|
2019-11-02 21:33:18 +01:00
|
|
|
|
|
|
|
func TestTask_ReadOne(t *testing.T) {
|
|
|
|
t.Run("default", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2019-11-02 21:33:18 +01:00
|
|
|
task := &Task{ID: 1}
|
|
|
|
err := task.ReadOne()
|
|
|
|
assert.NoError(t, err)
|
2020-05-16 12:17:44 +02:00
|
|
|
assert.Equal(t, "task #1", task.Title)
|
2019-11-02 21:33:18 +01:00
|
|
|
})
|
|
|
|
t.Run("nonexisting", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2019-11-02 21:33:18 +01:00
|
|
|
task := &Task{ID: 99999}
|
|
|
|
err := task.ReadOne()
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskDoesNotExist(err))
|
|
|
|
})
|
|
|
|
}
|