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.
|
2020-02-19 22:57: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
|
2020-02-19 22:57:56 +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.
|
2020-02-19 22:57:56 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-02-19 22:57:56 +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
|
|
|
"testing"
|
|
|
|
|
2021-07-29 17:42:49 +02:00
|
|
|
"code.vikunja.io/api/pkg/events"
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
|
|
"code.vikunja.io/api/pkg/user"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTaskComment_Create(t *testing.T) {
|
|
|
|
u := &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()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
tc := &TaskComment{
|
|
|
|
Comment: "test",
|
|
|
|
TaskID: 1,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := tc.Create(s, u)
|
2020-02-19 22:57:56 +01:00
|
|
|
assert.NoError(t, err)
|
2020-02-25 21:18:42 +01:00
|
|
|
assert.Equal(t, "test", tc.Comment)
|
|
|
|
assert.Equal(t, int64(1), tc.Author.ID)
|
2020-12-23 16:32:28 +01:00
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
2021-07-29 17:42:49 +02:00
|
|
|
events.AssertDispatched(t, &TaskCommentCreatedEvent{})
|
2020-12-23 16:32:28 +01:00
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertExists(t, "task_comments", map[string]interface{}{
|
|
|
|
"id": tc.ID,
|
|
|
|
"author_id": u.ID,
|
|
|
|
"comment": "test",
|
|
|
|
"task_id": 1,
|
|
|
|
}, false)
|
2020-02-19 22:57:56 +01:00
|
|
|
})
|
|
|
|
t.Run("nonexisting task", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
tc := &TaskComment{
|
|
|
|
Comment: "test",
|
|
|
|
TaskID: 99999,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := tc.Create(s, u)
|
2020-02-19 22:57:56 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskDoesNotExist(err))
|
|
|
|
})
|
2021-07-29 17:42:49 +02:00
|
|
|
t.Run("should send notifications for comment mentions", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
task, err := GetTaskByIDSimple(s, 32)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tc := &TaskComment{
|
|
|
|
Comment: "Lorem Ipsum @user2",
|
|
|
|
TaskID: 32, // user2 has access to the list that task belongs to
|
|
|
|
}
|
|
|
|
err = tc.Create(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ev := &TaskCommentCreatedEvent{
|
|
|
|
Task: &task,
|
|
|
|
Doer: u,
|
|
|
|
Comment: tc,
|
|
|
|
}
|
|
|
|
|
|
|
|
events.TestListener(t, ev, &SendTaskCommentNotification{})
|
|
|
|
db.AssertExists(t, "notifications", map[string]interface{}{
|
|
|
|
"subject_id": tc.ID,
|
|
|
|
"notifiable_id": 2,
|
|
|
|
"name": (&TaskCommentNotification{}).Name(),
|
|
|
|
}, false)
|
|
|
|
})
|
2020-02-19 22:57:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskComment_Delete(t *testing.T) {
|
2021-02-02 23:48:37 +01:00
|
|
|
u := &user.User{ID: 1}
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
tc := &TaskComment{ID: 1}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := tc.Delete(s, u)
|
2020-12-23 16:32:28 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2020-02-19 22:57:56 +01:00
|
|
|
assert.NoError(t, err)
|
2020-12-23 16:32:28 +01:00
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertMissing(t, "task_comments", map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
})
|
2020-02-19 22:57:56 +01:00
|
|
|
})
|
|
|
|
t.Run("nonexisting comment", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
tc := &TaskComment{ID: 9999}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := tc.Delete(s, u)
|
2020-02-19 22:57:56 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskCommentDoesNotExist(err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskComment_Update(t *testing.T) {
|
2021-02-02 23:48:37 +01:00
|
|
|
u := &user.User{ID: 1}
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
tc := &TaskComment{
|
|
|
|
ID: 1,
|
|
|
|
Comment: "testing",
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := tc.Update(s, u)
|
2020-12-23 16:32:28 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2020-02-19 22:57:56 +01:00
|
|
|
assert.NoError(t, err)
|
2020-12-23 16:32:28 +01:00
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertExists(t, "task_comments", map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
"comment": "testing",
|
|
|
|
}, false)
|
2020-02-19 22:57:56 +01:00
|
|
|
})
|
|
|
|
t.Run("nonexisting comment", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
tc := &TaskComment{
|
|
|
|
ID: 9999,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := tc.Update(s, u)
|
2020-02-19 22:57:56 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskCommentDoesNotExist(err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskComment_ReadOne(t *testing.T) {
|
2021-02-02 23:48:37 +01:00
|
|
|
u := &user.User{ID: 1}
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
tc := &TaskComment{ID: 1}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := tc.ReadOne(s, u)
|
2020-02-19 22:57:56 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "Lorem Ipsum Dolor Sit Amet", tc.Comment)
|
|
|
|
assert.NotEmpty(t, tc.Author.ID)
|
|
|
|
})
|
|
|
|
t.Run("nonexisting", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
tc := &TaskComment{ID: 9999}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := tc.ReadOne(s, u)
|
2020-02-19 22:57:56 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskCommentDoesNotExist(err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskComment_ReadAll(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
tc := &TaskComment{TaskID: 1}
|
|
|
|
u := &user.User{ID: 1}
|
2020-12-23 16:32:28 +01:00
|
|
|
result, resultCount, total, err := tc.ReadAll(s, u, "", 0, -1)
|
2020-02-19 22:57:56 +01:00
|
|
|
resultComment := result.([]*TaskComment)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, resultCount)
|
|
|
|
assert.Equal(t, int64(1), total)
|
|
|
|
assert.Equal(t, int64(1), resultComment[0].ID)
|
|
|
|
assert.Equal(t, "Lorem Ipsum Dolor Sit Amet", resultComment[0].Comment)
|
|
|
|
assert.NotEmpty(t, resultComment[0].Author.ID)
|
|
|
|
})
|
|
|
|
t.Run("no access to task", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-02-19 22:57:56 +01:00
|
|
|
tc := &TaskComment{TaskID: 14}
|
|
|
|
u := &user.User{ID: 1}
|
2020-12-23 16:32:28 +01:00
|
|
|
_, _, _, err := tc.ReadAll(s, u, "", 0, -1)
|
2020-02-19 22:57:56 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrGenericForbidden(err))
|
|
|
|
})
|
2021-04-07 14:44:39 +02:00
|
|
|
t.Run("comment from link share", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
tc := &TaskComment{TaskID: 35}
|
|
|
|
u := &user.User{ID: 1}
|
|
|
|
result, _, _, err := tc.ReadAll(s, u, "", 0, -1)
|
|
|
|
comments := result.([]*TaskComment)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, comments, 2)
|
2021-07-20 11:00:20 +02:00
|
|
|
var foundComment bool
|
|
|
|
for _, comment := range comments {
|
|
|
|
if comment.AuthorID == -2 {
|
|
|
|
foundComment = true
|
|
|
|
}
|
|
|
|
assert.NotNil(t, comment.Author)
|
|
|
|
}
|
|
|
|
assert.True(t, foundComment)
|
2021-04-07 14:44:39 +02:00
|
|
|
})
|
2020-02-19 22:57:56 +01:00
|
|
|
}
|