Files
server/plugin/messagehandler_test.go
TowyTowy 074b822b4a fix(plugin): reject messenger messages without an internal application
Add a final safety net in redirectToChannel.SendMessage that refuses a
message when ApplicationID == 0, so it can never be stored as an orphaned
message (application_id = 0, not shown, not deletable). Requested in review.

Cover the previously untested error branches around internal-application
back-fill (create/update failures) so patch coverage no longer regresses.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-15 22:02:09 +02:00

32 lines
1002 B
Go

package plugin
import (
"testing"
"github.com/gotify/server/v2/plugin/compat"
"github.com/stretchr/testify/assert"
)
func TestRedirectToChannel_SendMessage_rejectsMissingApplication(t *testing.T) {
messages := make(chan MessageWithUserID, 1)
handler := redirectToChannel{ApplicationID: 0, UserID: 1, Messages: messages}
err := handler.SendMessage(compat.Message{Message: "orphan"})
assert.Error(t, err)
assert.Empty(t, messages, "no message should be queued when the internal application is missing")
}
func TestRedirectToChannel_SendMessage_forwardsWithApplication(t *testing.T) {
messages := make(chan MessageWithUserID, 1)
handler := redirectToChannel{ApplicationID: 7, UserID: 3, Messages: messages}
assert.NoError(t, handler.SendMessage(compat.Message{Message: "hi", Title: "t"}))
got := <-messages
assert.Equal(t, uint(7), got.Message.ApplicationID)
assert.Equal(t, uint(3), got.UserID)
assert.Equal(t, "hi", got.Message.Message)
assert.Equal(t, "t", got.Message.Title)
}