Add tests for double notifications
This commit is contained in:
parent
b49d223f68
commit
b1ad9ffb7f
@ -17,8 +17,12 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -47,3 +51,13 @@ func AssertDispatched(t *testing.T, event Event) {
|
||||
|
||||
assert.True(t, found, "Failed to assert "+event.Name()+" has been dispatched.")
|
||||
}
|
||||
|
||||
// TestListener takes an event and a listener and calls the listener's Handle method.
|
||||
func TestListener(t *testing.T, event Event, listener Listener) {
|
||||
content, err := json.Marshal(event)
|
||||
assert.NoError(t, err)
|
||||
|
||||
msg := message.NewMessage(watermill.NewUUID(), content)
|
||||
err = listener.Handle(msg)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
@ -22,9 +22,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.vikunja.io/api/pkg/events"
|
||||
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/api/pkg/events"
|
||||
"code.vikunja.io/api/pkg/files"
|
||||
"code.vikunja.io/api/pkg/user"
|
||||
)
|
||||
|
@ -17,9 +17,12 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/notifications"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"code.vikunja.io/api/pkg/db"
|
||||
"code.vikunja.io/api/pkg/user"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFindMentionedUsersInText(t *testing.T) {
|
||||
@ -86,3 +89,91 @@ func TestFindMentionedUsersInText(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendingMentionNotification(t *testing.T) {
|
||||
u := &user.User{ID: 1}
|
||||
|
||||
t.Run("should send notifications to all users having access", 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 @user1 @user2 @user3 @user4 @user5 @user6",
|
||||
TaskID: 32, // user2 has access to the list that task belongs to
|
||||
}
|
||||
err = tc.Create(s, u)
|
||||
assert.NoError(t, err)
|
||||
n := &TaskCommentNotification{
|
||||
Doer: u,
|
||||
Task: &task,
|
||||
Comment: tc,
|
||||
}
|
||||
|
||||
_, err = notifyMentionedUsers(s, &task, tc.Comment, n)
|
||||
assert.NoError(t, err)
|
||||
|
||||
db.AssertExists(t, "notifications", map[string]interface{}{
|
||||
"subject_id": tc.ID,
|
||||
"notifiable_id": 1,
|
||||
"name": n.Name(),
|
||||
}, false)
|
||||
db.AssertExists(t, "notifications", map[string]interface{}{
|
||||
"subject_id": tc.ID,
|
||||
"notifiable_id": 2,
|
||||
"name": n.Name(),
|
||||
}, false)
|
||||
db.AssertExists(t, "notifications", map[string]interface{}{
|
||||
"subject_id": tc.ID,
|
||||
"notifiable_id": 3,
|
||||
"name": n.Name(),
|
||||
}, false)
|
||||
db.AssertMissing(t, "notifications", map[string]interface{}{
|
||||
"subject_id": tc.ID,
|
||||
"notifiable_id": 4,
|
||||
"name": n.Name(),
|
||||
})
|
||||
db.AssertMissing(t, "notifications", map[string]interface{}{
|
||||
"subject_id": tc.ID,
|
||||
"notifiable_id": 5,
|
||||
"name": n.Name(),
|
||||
})
|
||||
db.AssertMissing(t, "notifications", map[string]interface{}{
|
||||
"subject_id": tc.ID,
|
||||
"notifiable_id": 6,
|
||||
"name": n.Name(),
|
||||
})
|
||||
})
|
||||
t.Run("should not send notifications multiple times", 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)
|
||||
n := &TaskCommentNotification{
|
||||
Doer: u,
|
||||
Task: &task,
|
||||
Comment: tc,
|
||||
}
|
||||
|
||||
_, err = notifyMentionedUsers(s, &task, tc.Comment, n)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = notifyMentionedUsers(s, &task, "Lorem Ipsum @user2 @user3", n)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// The second time mentioning the user in the same task should not create another notification
|
||||
dbNotifications, err := notifications.GetNotificationsForNameAndUser(s, 2, n.Name(), tc.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, dbNotifications, 1)
|
||||
})
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ package models
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.vikunja.io/api/pkg/events"
|
||||
|
||||
"code.vikunja.io/api/pkg/db"
|
||||
"code.vikunja.io/api/pkg/user"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -41,6 +43,7 @@ func TestTaskComment_Create(t *testing.T) {
|
||||
assert.Equal(t, int64(1), tc.Author.ID)
|
||||
err = s.Commit()
|
||||
assert.NoError(t, err)
|
||||
events.AssertDispatched(t, &TaskCommentCreatedEvent{})
|
||||
|
||||
db.AssertExists(t, "task_comments", map[string]interface{}{
|
||||
"id": tc.ID,
|
||||
@ -62,6 +65,32 @@ func TestTaskComment_Create(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrTaskDoesNotExist(err))
|
||||
})
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
func TestTaskComment_Delete(t *testing.T) {
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
"code.vikunja.io/api/pkg/db"
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
"code.vikunja.io/api/pkg/mail"
|
||||
"code.vikunja.io/api/pkg/notifications"
|
||||
)
|
||||
|
||||
// SetupTests takes care of seting up the db, fixtures etc.
|
||||
@ -32,7 +33,11 @@ func SetupTests() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = x.Sync2(GetTables()...)
|
||||
tables := []interface{}{}
|
||||
tables = append(tables, GetTables()...)
|
||||
tables = append(tables, notifications.GetTables()...)
|
||||
|
||||
err = x.Sync2(tables...)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -20,11 +20,10 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/api/pkg/db"
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
"code.vikunja.io/api/pkg/mail"
|
||||
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
)
|
||||
|
||||
// SetupTests initializes all db tests
|
||||
|
Loading…
x
Reference in New Issue
Block a user