Avoid panic when push messages are empty or nil (#13751)

Этот коммит содержится в:
Mario de Frutos Dieguez
2020-01-29 10:08:27 +01:00
коммит произвёл GitHub
родитель 3049378506
Коммит 682a1d5d15
7 изменённых файлов: 137 добавлений и 13 удалений

Просмотреть файл

@@ -78,13 +78,35 @@ func (a *App) sendPushNotificationToAllSessions(msg *model.PushNotification, use
return err
}
if msg == nil {
return model.NewAppError(
"pushNotification",
"api.push_notifications.message.parse.app_error",
nil,
"",
http.StatusBadRequest,
)
}
notification, parseError := model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
if parseError != nil {
return model.NewAppError(
"pushNotification",
"api.push_notifications.message.parse.app_error",
nil,
parseError.Error(),
http.StatusInternalServerError,
)
}
for _, session := range sessions {
// Don't send notifications to this session if it's expired or we want to skip it
if session.IsExpired() || (skipSessionId != "" && skipSessionId == session.Id) {
continue
}
tmpMessage := model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
// We made a copy to avoid decoding and parsing all the time
tmpMessage := notification
tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
tmpMessage.AckId = model.NewId()

Просмотреть файл

@@ -955,3 +955,22 @@ func TestBuildPushNotificationMessageMentions(t *testing.T) {
})
}
}
func TestSendPushNotifications(t *testing.T) {
th := Setup(t).InitBasic()
th.App.CreateSession(&model.Session{
UserId: th.BasicUser.Id,
DeviceId: "test",
ExpiresAt: model.GetMillis() + 100000,
})
defer th.TearDown()
t.Run("should return error if data is not valid or nil", func(t *testing.T) {
err := th.App.sendPushNotificationToAllSessions(nil, th.BasicUser.Id, "")
assert.NotNil(t, err)
assert.Equal(t, "pushNotification: An error occurred building the push notification message, ", err.Error())
// Errors derived of using an empty object are handled internally through the notifications log
err = th.App.sendPushNotificationToAllSessions(&model.PushNotification{}, th.BasicUser.Id, "")
assert.Nil(t, err)
})
}