MM-53924 - Implement NotificationWillBePushed plugin hook (#24263)
* add NotificationWillBePushed hook * mocks * use a struct for hook parameters; simplify number of parameters sent across RPC * missing wg.Wait * change to a bool return value
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
56a0becbca
Коммит
f13a531bca
@@ -6,11 +6,14 @@ package app
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -1331,3 +1334,118 @@ func TestHookOnCloudLimitsUpdated(t *testing.T) {
|
||||
|
||||
require.True(t, hookCalled)
|
||||
}
|
||||
|
||||
//go:embed test_templates/hook_notification_will_be_pushed.tmpl
|
||||
var hookNotificationWillBePushedTmpl string
|
||||
|
||||
func TestHookNotificationWillBePushed(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping TestHookNotificationWillBePushed test in short mode")
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
testCode string
|
||||
expectedNotifications int
|
||||
}{
|
||||
{
|
||||
name: "successfully pushed",
|
||||
testCode: `return false`,
|
||||
expectedNotifications: 6,
|
||||
},
|
||||
{
|
||||
name: "push notification rejected",
|
||||
testCode: `return true`,
|
||||
expectedNotifications: 0,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
templatedPlugin := fmt.Sprintf(hookNotificationWillBePushedTmpl, tt.testCode)
|
||||
tearDown, _, _ := SetAppEnvironmentWithPlugins(t, []string{templatedPlugin}, th.App, th.NewPluginAPI)
|
||||
defer tearDown()
|
||||
|
||||
// Create 3 users, each having 2 sessions.
|
||||
type userSession struct {
|
||||
user *model.User
|
||||
session *model.Session
|
||||
}
|
||||
var userSessions []userSession
|
||||
for i := 0; i < 3; i++ {
|
||||
u := th.CreateUser()
|
||||
sess, err := th.App.CreateSession(&model.Session{
|
||||
UserId: u.Id,
|
||||
DeviceId: "deviceID" + u.Id,
|
||||
ExpiresAt: model.GetMillis() + 100000,
|
||||
})
|
||||
require.Nil(t, err)
|
||||
// We don't need to track the 2nd session.
|
||||
_, err = th.App.CreateSession(&model.Session{
|
||||
UserId: u.Id,
|
||||
DeviceId: "deviceID" + u.Id,
|
||||
ExpiresAt: model.GetMillis() + 100000,
|
||||
})
|
||||
require.Nil(t, err)
|
||||
_, err = th.App.AddTeamMember(th.Context, th.BasicTeam.Id, u.Id)
|
||||
require.Nil(t, err)
|
||||
th.AddUserToChannel(u, th.BasicChannel)
|
||||
userSessions = append(userSessions, userSession{
|
||||
user: u,
|
||||
session: sess,
|
||||
})
|
||||
}
|
||||
|
||||
handler := &testPushNotificationHandler{
|
||||
t: t,
|
||||
behavior: "simple",
|
||||
}
|
||||
pushServer := httptest.NewServer(
|
||||
http.HandlerFunc(handler.handleReq),
|
||||
)
|
||||
defer pushServer.Close()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.EmailSettings.PushNotificationContents = model.GenericNotification
|
||||
*cfg.EmailSettings.PushNotificationServer = pushServer.URL
|
||||
})
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for _, data := range userSessions {
|
||||
wg.Add(1)
|
||||
go func(user model.User) {
|
||||
defer wg.Done()
|
||||
notification := &PostNotification{
|
||||
Post: th.CreatePost(th.BasicChannel),
|
||||
Channel: th.BasicChannel,
|
||||
ProfileMap: map[string]*model.User{
|
||||
user.Id: &user,
|
||||
},
|
||||
Sender: &user,
|
||||
}
|
||||
th.App.sendPushNotification(notification, &user, true, false, model.CommentsNotifyAny)
|
||||
}(*data.user)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Hack to let the worker goroutines complete.
|
||||
time.Sleep(1 * time.Second)
|
||||
// Server side verification.
|
||||
assert.Equal(t, tt.expectedNotifications, handler.numReqs())
|
||||
var numMessages int
|
||||
for _, n := range handler.notifications() {
|
||||
switch n.Type {
|
||||
case model.PushTypeMessage:
|
||||
numMessages++
|
||||
assert.Equal(t, th.BasicChannel.Id, n.ChannelId)
|
||||
assert.Contains(t, n.Message, "mentioned you")
|
||||
default:
|
||||
assert.Fail(t, "should not receive any other push notification types")
|
||||
}
|
||||
}
|
||||
assert.Equal(t, tt.expectedNotifications, numMessages)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user