Add tests for double notifications
This commit is contained in:
parent
b49d223f68
commit
b1ad9ffb7f
|
@ -17,8 +17,12 @@
|
||||||
package events
|
package events
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ThreeDotsLabs/watermill"
|
||||||
|
"github.com/ThreeDotsLabs/watermill/message"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"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.")
|
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"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/events"
|
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/config"
|
"code.vikunja.io/api/pkg/config"
|
||||||
|
"code.vikunja.io/api/pkg/events"
|
||||||
"code.vikunja.io/api/pkg/files"
|
"code.vikunja.io/api/pkg/files"
|
||||||
"code.vikunja.io/api/pkg/user"
|
"code.vikunja.io/api/pkg/user"
|
||||||
)
|
)
|
||||||
|
|
|
@ -17,9 +17,12 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"code.vikunja.io/api/pkg/notifications"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/user"
|
"code.vikunja.io/api/pkg/user"
|
||||||
"testing"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFindMentionedUsersInText(t *testing.T) {
|
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 (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"code.vikunja.io/api/pkg/events"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/user"
|
"code.vikunja.io/api/pkg/user"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -41,6 +43,7 @@ func TestTaskComment_Create(t *testing.T) {
|
||||||
assert.Equal(t, int64(1), tc.Author.ID)
|
assert.Equal(t, int64(1), tc.Author.ID)
|
||||||
err = s.Commit()
|
err = s.Commit()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
events.AssertDispatched(t, &TaskCommentCreatedEvent{})
|
||||||
|
|
||||||
db.AssertExists(t, "task_comments", map[string]interface{}{
|
db.AssertExists(t, "task_comments", map[string]interface{}{
|
||||||
"id": tc.ID,
|
"id": tc.ID,
|
||||||
|
@ -62,6 +65,32 @@ func TestTaskComment_Create(t *testing.T) {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.True(t, IsErrTaskDoesNotExist(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) {
|
func TestTaskComment_Delete(t *testing.T) {
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
"code.vikunja.io/api/pkg/mail"
|
"code.vikunja.io/api/pkg/mail"
|
||||||
|
"code.vikunja.io/api/pkg/notifications"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetupTests takes care of seting up the db, fixtures etc.
|
// SetupTests takes care of seting up the db, fixtures etc.
|
||||||
|
@ -32,7 +33,11 @@ func SetupTests() {
|
||||||
log.Fatal(err)
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,10 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"code.vikunja.io/api/pkg/config"
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
"code.vikunja.io/api/pkg/mail"
|
"code.vikunja.io/api/pkg/mail"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetupTests initializes all db tests
|
// SetupTests initializes all db tests
|
||||||
|
|
Loading…
Reference in New Issue