2020-02-07 17:27:45 +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-12-07 23:28:45 +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-12-07 23:28:45 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2019-12-07 23:28:45 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-07 23:28:45 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
|
|
"code.vikunja.io/api/pkg/user"
|
2019-12-07 23:28:45 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestList_CreateOrUpdate(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
usr := &user.User{
|
2019-12-07 23:28:45 +01:00
|
|
|
ID: 1,
|
|
|
|
Username: "user1",
|
|
|
|
Email: "user1@example.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("create", func(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()
|
2019-12-07 23:28:45 +01:00
|
|
|
list := List{
|
|
|
|
Title: "test",
|
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
NamespaceID: 1,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := list.Create(s, usr)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2019-12-07 23:28:45 +01:00
|
|
|
assert.NoError(t, err)
|
2021-03-28 20:17:35 +02:00
|
|
|
db.AssertExists(t, "lists", map[string]interface{}{
|
2020-09-27 17:45:17 +02:00
|
|
|
"id": list.ID,
|
|
|
|
"title": list.Title,
|
|
|
|
"description": list.Description,
|
|
|
|
"namespace_id": list.NamespaceID,
|
|
|
|
}, false)
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
|
|
|
t.Run("nonexistant namespace", 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()
|
2019-12-07 23:28:45 +01:00
|
|
|
list := List{
|
|
|
|
Title: "test",
|
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
NamespaceID: 999999,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := list.Create(s, usr)
|
2019-12-07 23:28:45 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNamespaceDoesNotExist(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
|
|
|
t.Run("nonexistant owner", 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()
|
2020-01-26 18:08:06 +01:00
|
|
|
usr := &user.User{ID: 9482385}
|
2019-12-07 23:28:45 +01:00
|
|
|
list := List{
|
|
|
|
Title: "test",
|
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
NamespaceID: 1,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := list.Create(s, usr)
|
2019-12-07 23:28:45 +01:00
|
|
|
assert.Error(t, err)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.True(t, user.IsErrUserDoesNotExist(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
|
|
|
t.Run("existing identifier", 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()
|
2019-12-07 23:28:45 +01:00
|
|
|
list := List{
|
|
|
|
Title: "test",
|
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
Identifier: "test1",
|
|
|
|
NamespaceID: 1,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := list.Create(s, usr)
|
2019-12-07 23:28:45 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrListIdentifierIsNotUnique(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
2020-07-07 09:48:12 +02:00
|
|
|
t.Run("non ascii characters", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2020-07-07 09:48:12 +02:00
|
|
|
list := List{
|
|
|
|
Title: "приффки фсем",
|
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
NamespaceID: 1,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := list.Create(s, usr)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2020-07-07 09:48:12 +02:00
|
|
|
assert.NoError(t, err)
|
2021-03-28 20:17:35 +02:00
|
|
|
db.AssertExists(t, "lists", map[string]interface{}{
|
2020-09-27 17:45:17 +02:00
|
|
|
"id": list.ID,
|
|
|
|
"title": list.Title,
|
|
|
|
"description": list.Description,
|
|
|
|
"namespace_id": list.NamespaceID,
|
|
|
|
}, false)
|
2020-07-07 09:48:12 +02:00
|
|
|
})
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update", func(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()
|
2019-12-07 23:28:45 +01:00
|
|
|
list := List{
|
|
|
|
ID: 1,
|
|
|
|
Title: "test",
|
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
NamespaceID: 1,
|
|
|
|
}
|
|
|
|
list.Description = "Lorem Ipsum dolor sit amet."
|
2021-02-02 23:48:37 +01:00
|
|
|
err := list.Update(s, usr)
|
2020-12-23 16:32:28 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2019-12-07 23:28:45 +01:00
|
|
|
assert.NoError(t, err)
|
2021-03-28 20:17:35 +02:00
|
|
|
db.AssertExists(t, "lists", map[string]interface{}{
|
2020-09-27 17:45:17 +02:00
|
|
|
"id": list.ID,
|
|
|
|
"title": list.Title,
|
|
|
|
"description": list.Description,
|
|
|
|
"namespace_id": list.NamespaceID,
|
|
|
|
}, false)
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
|
|
|
t.Run("nonexistant", 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()
|
2019-12-07 23:28:45 +01:00
|
|
|
list := List{
|
2022-10-01 15:02:13 +02:00
|
|
|
ID: 99999999,
|
|
|
|
Title: "test",
|
|
|
|
NamespaceID: 1,
|
2019-12-07 23:28:45 +01:00
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := list.Update(s, usr)
|
2019-12-07 23:28:45 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrListDoesNotExist(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2019-12-07 23:28:45 +01:00
|
|
|
|
|
|
|
})
|
|
|
|
t.Run("existing identifier", 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()
|
2019-12-07 23:28:45 +01:00
|
|
|
list := List{
|
|
|
|
Title: "test",
|
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
Identifier: "test1",
|
|
|
|
NamespaceID: 1,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := list.Create(s, usr)
|
2019-12-07 23:28:45 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrListIdentifierIsNotUnique(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
2022-01-23 13:59:43 +01:00
|
|
|
t.Run("change namespace", func(t *testing.T) {
|
|
|
|
t.Run("own", func(t *testing.T) {
|
|
|
|
usr := &user.User{
|
|
|
|
ID: 6,
|
|
|
|
Username: "user6",
|
|
|
|
Email: "user6@example.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
list := List{
|
|
|
|
ID: 6,
|
|
|
|
Title: "Test6",
|
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
NamespaceID: 7, // from 6
|
|
|
|
}
|
|
|
|
can, err := list.CanUpdate(s, usr)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, can)
|
|
|
|
err = list.Update(s, usr)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.AssertExists(t, "lists", map[string]interface{}{
|
|
|
|
"id": list.ID,
|
|
|
|
"title": list.Title,
|
|
|
|
"description": list.Description,
|
|
|
|
"namespace_id": list.NamespaceID,
|
|
|
|
}, false)
|
|
|
|
})
|
|
|
|
// FIXME: The check for whether the namespace is archived is missing in namespace.CanWrite
|
|
|
|
// t.Run("archived own", func(t *testing.T) {
|
|
|
|
// db.LoadAndAssertFixtures(t)
|
|
|
|
// s := db.NewSession()
|
|
|
|
// list := List{
|
|
|
|
// ID: 1,
|
|
|
|
// Title: "Test1",
|
|
|
|
// Description: "Lorem Ipsum",
|
|
|
|
// NamespaceID: 16, // from 1
|
|
|
|
// }
|
|
|
|
// can, err := list.CanUpdate(s, usr)
|
|
|
|
// assert.NoError(t, err)
|
|
|
|
// assert.False(t, can) // namespace is archived and thus not writeable
|
|
|
|
// _ = s.Close()
|
|
|
|
// })
|
|
|
|
t.Run("others", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
list := List{
|
|
|
|
ID: 1,
|
|
|
|
Title: "Test1",
|
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
NamespaceID: 2, // from 1
|
|
|
|
}
|
|
|
|
can, _ := list.CanUpdate(s, usr)
|
|
|
|
assert.False(t, can) // namespace is not writeable by us
|
|
|
|
_ = s.Close()
|
|
|
|
})
|
2022-08-15 23:25:35 +02:00
|
|
|
t.Run("pseudo namespace", func(t *testing.T) {
|
|
|
|
usr := &user.User{
|
|
|
|
ID: 6,
|
|
|
|
Username: "user6",
|
|
|
|
Email: "user6@example.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
list := List{
|
|
|
|
ID: 6,
|
|
|
|
Title: "Test6",
|
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
NamespaceID: -1,
|
|
|
|
}
|
|
|
|
err := list.Update(s, usr)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrListCannotBelongToAPseudoNamespace(err))
|
|
|
|
})
|
2022-01-23 13:59:43 +01:00
|
|
|
})
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestList_Delete(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()
|
2019-12-07 23:28:45 +01:00
|
|
|
list := List{
|
|
|
|
ID: 1,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := list.Delete(s, &user.User{ID: 1})
|
2020-12-23 16:32:28 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2019-12-07 23:28:45 +01:00
|
|
|
assert.NoError(t, err)
|
2021-03-28 20:17:35 +02:00
|
|
|
db.AssertMissing(t, "lists", map[string]interface{}{
|
2020-09-27 17:45:17 +02:00
|
|
|
"id": 1,
|
|
|
|
})
|
2019-12-07 23:28:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestList_ReadAll(t *testing.T) {
|
|
|
|
t.Run("all in namespace", 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()
|
2019-12-07 23:28:45 +01:00
|
|
|
// Get all lists for our namespace
|
2020-12-23 16:32:28 +01:00
|
|
|
lists, err := GetListsByNamespaceID(s, 1, &user.User{})
|
2019-12-07 23:28:45 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(lists), 2)
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
|
|
|
t.Run("all lists for user", 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()
|
2020-01-26 18:08:06 +01:00
|
|
|
u := &user.User{ID: 1}
|
2019-12-07 23:28:45 +01:00
|
|
|
list := List{}
|
2020-12-23 16:32:28 +01:00
|
|
|
lists3, _, _, err := list.ReadAll(s, u, "", 1, 50)
|
2019-12-07 23:28:45 +01:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, reflect.TypeOf(lists3).Kind(), reflect.Slice)
|
2021-07-28 21:06:40 +02:00
|
|
|
ls := lists3.([]*List)
|
|
|
|
assert.Equal(t, 16, len(ls))
|
|
|
|
assert.Equal(t, int64(3), ls[0].ID) // List 3 has a position of 1 and should be sorted first
|
|
|
|
assert.Equal(t, int64(1), ls[1].ID)
|
|
|
|
assert.Equal(t, int64(4), ls[2].ID)
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
|
|
|
t.Run("lists for nonexistant user", 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()
|
2020-01-26 18:08:06 +01:00
|
|
|
usr := &user.User{ID: 999999}
|
2019-12-07 23:28:45 +01:00
|
|
|
list := List{}
|
2020-12-23 16:32:28 +01:00
|
|
|
_, _, _, err := list.ReadAll(s, usr, "", 1, 50)
|
2019-12-07 23:28:45 +01:00
|
|
|
assert.Error(t, err)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.True(t, user.IsErrUserDoesNotExist(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2019-12-07 23:28:45 +01:00
|
|
|
})
|
2021-08-01 23:40:25 +02:00
|
|
|
t.Run("search", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
u := &user.User{ID: 1}
|
|
|
|
list := List{}
|
|
|
|
lists3, _, _, err := list.ReadAll(s, u, "TEST10", 1, 50)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ls := lists3.([]*List)
|
|
|
|
assert.Equal(t, 1, len(ls))
|
|
|
|
assert.Equal(t, int64(10), ls[0].ID)
|
|
|
|
_ = s.Close()
|
|
|
|
})
|
2019-12-07 23:28:45 +01:00
|
|
|
}
|
2021-02-14 20:18:14 +01:00
|
|
|
|
|
|
|
func TestList_ReadOne(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
u := &user.User{ID: 1}
|
|
|
|
l := &List{ID: 1}
|
|
|
|
can, _, err := l.CanRead(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, can)
|
|
|
|
err = l.ReadOne(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "Test1", l.Title)
|
|
|
|
})
|
|
|
|
t.Run("with subscription", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
u := &user.User{ID: 6}
|
|
|
|
l := &List{ID: 12}
|
|
|
|
can, _, err := l.CanRead(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, can)
|
|
|
|
err = l.ReadOne(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, l.Subscription)
|
|
|
|
})
|
|
|
|
}
|