2018-11-26 21:17:33 +01:00
|
|
|
// Vikunja is a todo-list application to facilitate your life.
|
|
|
|
// Copyright 2018 Vikunja and contributors. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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-07-26 10:29:19 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-07-26 10:50:03 +02:00
|
|
|
"testing"
|
2018-07-26 10:29:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestList_Create(t *testing.T) {
|
|
|
|
// Create test database
|
2019-04-21 20:18:17 +02:00
|
|
|
//assert.NoError(t, LoadFixtures())
|
2018-07-26 10:29:19 +02:00
|
|
|
|
|
|
|
// Get our doer
|
2018-08-30 19:14:02 +02:00
|
|
|
doer, err := GetUserByID(1)
|
2018-07-26 10:29:19 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Dummy list for testing
|
|
|
|
dummylist := List{
|
2018-07-26 10:50:03 +02:00
|
|
|
Title: "test",
|
2018-07-26 10:29:19 +02:00
|
|
|
Description: "Lorem Ipsum",
|
2018-07-26 10:49:17 +02:00
|
|
|
NamespaceID: 1,
|
2018-07-26 10:29:19 +02:00
|
|
|
}
|
|
|
|
|
2018-07-26 10:49:17 +02:00
|
|
|
// Check if the user can create
|
2019-03-24 13:35:50 +01:00
|
|
|
allowed, _ := dummylist.CanCreate(&doer)
|
|
|
|
assert.True(t, allowed)
|
2018-07-26 10:49:17 +02:00
|
|
|
|
2018-07-26 10:29:19 +02:00
|
|
|
// Create it
|
|
|
|
err = dummylist.Create(&doer)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Get the list
|
2018-07-26 10:50:03 +02:00
|
|
|
newdummy := List{ID: dummylist.ID}
|
2019-04-01 21:48:48 +02:00
|
|
|
canRead, err := newdummy.CanRead(&doer)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, canRead)
|
2018-07-26 10:29:19 +02:00
|
|
|
err = newdummy.ReadOne()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, dummylist.Title, newdummy.Title)
|
|
|
|
assert.Equal(t, dummylist.Description, newdummy.Description)
|
|
|
|
assert.Equal(t, dummylist.OwnerID, doer.ID)
|
|
|
|
|
2018-07-26 10:49:17 +02:00
|
|
|
// Check if the user can see it
|
2019-03-24 13:35:50 +01:00
|
|
|
allowed, _ = dummylist.CanRead(&doer)
|
|
|
|
assert.True(t, allowed)
|
2018-07-26 10:49:17 +02:00
|
|
|
|
2018-07-27 15:11:47 +02:00
|
|
|
// Try updating a list
|
2019-03-24 13:35:50 +01:00
|
|
|
allowed, _ = dummylist.CanUpdate(&doer)
|
|
|
|
assert.True(t, allowed)
|
2018-07-27 15:11:47 +02:00
|
|
|
dummylist.Description = "Lorem Ipsum dolor sit amet."
|
|
|
|
err = dummylist.Update()
|
|
|
|
assert.NoError(t, err)
|
2018-07-26 10:49:17 +02:00
|
|
|
|
|
|
|
// Delete it
|
2019-03-24 13:35:50 +01:00
|
|
|
allowed, _ = dummylist.CanDelete(&doer)
|
|
|
|
assert.True(t, allowed)
|
2018-07-26 10:49:17 +02:00
|
|
|
|
|
|
|
err = dummylist.Delete()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-07-27 15:11:47 +02:00
|
|
|
// Try updating a nonexistant list
|
|
|
|
err = dummylist.Update()
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrListDoesNotExist(err))
|
|
|
|
|
2018-07-26 10:49:17 +02:00
|
|
|
// Check creation with a nonexistant namespace
|
|
|
|
list3 := List{
|
2018-07-26 10:50:03 +02:00
|
|
|
Title: "test",
|
2018-07-26 10:49:17 +02:00
|
|
|
Description: "Lorem Ipsum",
|
|
|
|
NamespaceID: 876694,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = list3.Create(&doer)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNamespaceDoesNotExist(err))
|
2018-07-27 15:11:47 +02:00
|
|
|
|
|
|
|
// Try creating with a nonexistant owner
|
|
|
|
nUser := &User{ID: 9482385}
|
|
|
|
err = dummylist.Create(nUser)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUserDoesNotExist(err))
|
2018-07-26 10:29:19 +02:00
|
|
|
}
|