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.
|
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
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-11-26 21:17:33 +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-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-07-27 15:52:50 +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"
|
2018-07-27 15:52:50 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNamespace_Create(t *testing.T) {
|
|
|
|
|
|
|
|
// Dummy namespace
|
|
|
|
dummynamespace := Namespace{
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "Test",
|
2018-07-27 15:52:50 +02:00
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
}
|
|
|
|
|
2020-08-15 22:16:36 +02:00
|
|
|
user1 := &user.User{ID: 1}
|
|
|
|
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
err := dummynamespace.Create(s, user1)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.NoError(t, err)
|
2020-12-23 16:32:28 +01:00
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertExists(t, "namespaces", map[string]interface{}{
|
|
|
|
"title": "Test",
|
|
|
|
"description": "Lorem Ipsum",
|
|
|
|
}, false)
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
t.Run("no title", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2020-08-15 22:16:36 +02:00
|
|
|
n2 := Namespace{}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := n2.Create(s, user1)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNamespaceNameCannotBeEmpty(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
t.Run("nonexistant user", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2020-08-15 22:16:36 +02:00
|
|
|
nUser := &user.User{ID: 9482385}
|
|
|
|
dnsp2 := dummynamespace
|
2020-12-23 16:32:28 +01:00
|
|
|
err := dnsp2.Create(s, nUser)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, user.IsErrUserDoesNotExist(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
}
|
2018-07-27 15:52:50 +02:00
|
|
|
|
2020-08-15 22:16:36 +02:00
|
|
|
func TestNamespace_ReadOne(t *testing.T) {
|
2021-02-02 23:48:37 +01:00
|
|
|
u := &user.User{ID: 1}
|
|
|
|
|
2020-08-15 22:16:36 +02:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
n := &Namespace{ID: 1}
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2021-02-02 23:48:37 +01:00
|
|
|
err := n.ReadOne(s, u)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, n.Title, "testnamespace")
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
t.Run("nonexistant", func(t *testing.T) {
|
|
|
|
n := &Namespace{ID: 99999}
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2021-02-02 23:48:37 +01:00
|
|
|
err := n.ReadOne(s, u)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNamespaceDoesNotExist(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
}
|
2018-07-27 15:52:50 +02:00
|
|
|
|
2020-08-15 22:16:36 +02:00
|
|
|
func TestNamespace_Update(t *testing.T) {
|
2021-02-02 23:48:37 +01:00
|
|
|
u := &user.User{ID: 1}
|
|
|
|
|
2020-08-15 22:16:36 +02:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2020-08-15 22:16:36 +02:00
|
|
|
n := &Namespace{
|
|
|
|
ID: 1,
|
|
|
|
Title: "Lorem Ipsum",
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := n.Update(s, u)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.NoError(t, err)
|
2020-12-23 16:32:28 +01:00
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertExists(t, "namespaces", map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
"title": "Lorem Ipsum",
|
|
|
|
}, false)
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
t.Run("nonexisting", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2020-08-15 22:16:36 +02:00
|
|
|
n := &Namespace{
|
|
|
|
ID: 99999,
|
|
|
|
Title: "Lorem Ipsum",
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := n.Update(s, u)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNamespaceDoesNotExist(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
t.Run("nonexisting owner", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2020-08-15 22:16:36 +02:00
|
|
|
n := &Namespace{
|
|
|
|
ID: 1,
|
|
|
|
Title: "Lorem Ipsum",
|
|
|
|
Owner: &user.User{ID: 99999},
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := n.Update(s, u)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, user.IsErrUserDoesNotExist(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
t.Run("no title", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2020-08-15 22:16:36 +02:00
|
|
|
n := &Namespace{
|
|
|
|
ID: 1,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := n.Update(s, u)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNamespaceNameCannotBeEmpty(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
}
|
2018-07-27 15:52:50 +02:00
|
|
|
|
2020-08-15 22:16:36 +02:00
|
|
|
func TestNamespace_Delete(t *testing.T) {
|
2021-02-02 23:48:37 +01:00
|
|
|
u := &user.User{ID: 1}
|
|
|
|
|
2020-08-15 22:16:36 +02:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2020-08-15 22:16:36 +02:00
|
|
|
n := &Namespace{
|
|
|
|
ID: 1,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := n.Delete(s, u)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.NoError(t, err)
|
2020-12-23 16:32:28 +01:00
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertMissing(t, "namespaces", map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
})
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
t.Run("nonexisting", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
2020-08-15 22:16:36 +02:00
|
|
|
n := &Namespace{
|
|
|
|
ID: 9999,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := n.Delete(s, u)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNamespaceDoesNotExist(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Close()
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
|
|
|
}
|
2018-07-27 15:52:50 +02:00
|
|
|
|
2020-08-15 22:16:36 +02:00
|
|
|
func TestNamespace_ReadAll(t *testing.T) {
|
|
|
|
user1 := &user.User{ID: 1}
|
2020-12-22 00:13:15 +01:00
|
|
|
user7 := &user.User{ID: 7}
|
2020-09-06 16:20:16 +02:00
|
|
|
user11 := &user.User{ID: 11}
|
|
|
|
user12 := &user.User{ID: 12}
|
2020-08-15 22:16:36 +02:00
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-08-15 22:16:36 +02:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
n := &Namespace{}
|
2020-12-23 16:32:28 +01:00
|
|
|
nn, _, _, err := n.ReadAll(s, user1, "", 1, -1)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.NoError(t, err)
|
2020-09-26 23:02:17 +02:00
|
|
|
namespaces := nn.([]*NamespaceWithLists)
|
2020-08-15 22:16:36 +02:00
|
|
|
assert.NotNil(t, namespaces)
|
2020-12-22 00:13:15 +01:00
|
|
|
assert.Len(t, namespaces, 11) // Total of 11 including shared, favorites and saved filters
|
2020-09-26 23:02:17 +02:00
|
|
|
assert.Equal(t, int64(-3), namespaces[0].ID) // The first one should be the one with shared filters
|
|
|
|
assert.Equal(t, int64(-2), namespaces[1].ID) // The second one should be the one with favorites
|
|
|
|
assert.Equal(t, int64(-1), namespaces[2].ID) // The third one should be the one with the shared namespaces
|
2020-08-15 22:16:36 +02:00
|
|
|
// Ensure every list and namespace are not archived
|
|
|
|
for _, namespace := range namespaces {
|
|
|
|
assert.False(t, namespace.IsArchived)
|
|
|
|
for _, list := range namespace.Lists {
|
|
|
|
assert.False(t, list.IsArchived)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-12-22 00:13:15 +01:00
|
|
|
t.Run("namespaces only", func(t *testing.T) {
|
|
|
|
n := &Namespace{
|
|
|
|
NamespacesOnly: true,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
nn, _, _, err := n.ReadAll(s, user1, "", 1, -1)
|
2020-12-22 00:13:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
namespaces := nn.([]*NamespaceWithLists)
|
|
|
|
assert.NotNil(t, namespaces)
|
|
|
|
assert.Len(t, namespaces, 8) // Total of 8 - excluding shared, favorites and saved filters (normally 11)
|
|
|
|
// Ensure every namespace does not contain lists
|
|
|
|
for _, namespace := range namespaces {
|
|
|
|
assert.Nil(t, namespace.Lists)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("ids only", func(t *testing.T) {
|
|
|
|
n := &Namespace{
|
|
|
|
NamespacesOnly: true,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
nn, _, _, err := n.ReadAll(s, user7, "13,14", 1, -1)
|
2020-12-22 00:13:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
namespaces := nn.([]*NamespaceWithLists)
|
|
|
|
assert.NotNil(t, namespaces)
|
|
|
|
assert.Len(t, namespaces, 2)
|
|
|
|
assert.Equal(t, int64(13), namespaces[0].ID)
|
|
|
|
assert.Equal(t, int64(14), namespaces[1].ID)
|
|
|
|
})
|
|
|
|
t.Run("ids only but ids with other people's namespace", func(t *testing.T) {
|
|
|
|
n := &Namespace{
|
|
|
|
NamespacesOnly: true,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
nn, _, _, err := n.ReadAll(s, user1, "1,w", 1, -1)
|
2020-12-22 00:13:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
namespaces := nn.([]*NamespaceWithLists)
|
|
|
|
assert.NotNil(t, namespaces)
|
|
|
|
assert.Len(t, namespaces, 1)
|
|
|
|
assert.Equal(t, int64(1), namespaces[0].ID)
|
|
|
|
})
|
2020-08-15 22:16:36 +02:00
|
|
|
t.Run("archived", func(t *testing.T) {
|
|
|
|
n := &Namespace{
|
|
|
|
IsArchived: true,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
nn, _, _, err := n.ReadAll(s, user1, "", 1, -1)
|
2020-08-15 22:16:36 +02:00
|
|
|
namespaces := nn.([]*NamespaceWithLists)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, namespaces)
|
2020-09-26 23:02:17 +02:00
|
|
|
assert.Len(t, namespaces, 12) // Total of 12 including shared & favorites, one is archived
|
|
|
|
assert.Equal(t, int64(-3), namespaces[0].ID) // The first one should be the one with shared filters
|
|
|
|
assert.Equal(t, int64(-2), namespaces[1].ID) // The second one should be the one with favorites
|
|
|
|
assert.Equal(t, int64(-1), namespaces[2].ID) // The third one should be the one with the shared namespaces
|
2020-08-15 22:16:36 +02:00
|
|
|
})
|
2020-09-06 16:20:16 +02:00
|
|
|
t.Run("no favorites", func(t *testing.T) {
|
|
|
|
n := &Namespace{}
|
2020-12-23 16:32:28 +01:00
|
|
|
nn, _, _, err := n.ReadAll(s, user11, "", 1, -1)
|
2020-09-06 16:20:16 +02:00
|
|
|
namespaces := nn.([]*NamespaceWithLists)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// Assert the first namespace is not the favorites namespace
|
|
|
|
assert.NotEqual(t, FavoritesPseudoNamespace.ID, namespaces[0].ID)
|
|
|
|
})
|
|
|
|
t.Run("no favorite tasks but namespace", func(t *testing.T) {
|
|
|
|
n := &Namespace{}
|
2020-12-23 16:32:28 +01:00
|
|
|
nn, _, _, err := n.ReadAll(s, user12, "", 1, -1)
|
2020-09-06 16:20:16 +02:00
|
|
|
namespaces := nn.([]*NamespaceWithLists)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// Assert the first namespace is the favorites namespace and contains lists
|
|
|
|
assert.Equal(t, FavoritesPseudoNamespace.ID, namespaces[0].ID)
|
|
|
|
assert.NotEqual(t, 0, namespaces[0].Lists)
|
|
|
|
})
|
2020-09-26 23:02:17 +02:00
|
|
|
t.Run("no saved filters", func(t *testing.T) {
|
|
|
|
n := &Namespace{}
|
2020-12-23 16:32:28 +01:00
|
|
|
nn, _, _, err := n.ReadAll(s, user11, "", 1, -1)
|
2020-09-26 23:02:17 +02:00
|
|
|
namespaces := nn.([]*NamespaceWithLists)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// Assert the first namespace is not the favorites namespace
|
|
|
|
assert.NotEqual(t, SavedFiltersPseudoNamespace.ID, namespaces[0].ID)
|
|
|
|
})
|
2018-07-27 15:52:50 +02:00
|
|
|
}
|