From 9a55280d7acf3013f06114727b64278fbe6e9d2d Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Mon, 5 Jun 2023 10:00:06 +0200 Subject: [PATCH] [MM-52955] Fix panic for not found posts (#23561) --- server/channels/store/sqlstore/reaction_store.go | 5 +++++ server/channels/store/storetest/reaction_store.go | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/server/channels/store/sqlstore/reaction_store.go b/server/channels/store/sqlstore/reaction_store.go index bd7136c7ce..826c61f4b7 100644 --- a/server/channels/store/sqlstore/reaction_store.go +++ b/server/channels/store/sqlstore/reaction_store.go @@ -41,6 +41,11 @@ func (s *SqlReactionStore) Save(reaction *model.Reaction) (re *model.Reaction, e if err != nil { return nil, errors.Wrap(err, "failed while getting channelId from Posts") } + + if len(channelIds) == 0 { + return nil, store.NewErrNotFound("Post", reaction.PostId) + } + reaction.ChannelId = channelIds[0] } err = s.saveReactionAndUpdatePost(transaction, reaction) diff --git a/server/channels/store/storetest/reaction_store.go b/server/channels/store/storetest/reaction_store.go index 9c5241901b..c7456365ef 100644 --- a/server/channels/store/storetest/reaction_store.go +++ b/server/channels/store/storetest/reaction_store.go @@ -118,6 +118,16 @@ func testReactionSave(t *testing.T, ss store.Store) { _, nErr = ss.Reaction().Save(reaction5) require.Error(t, nErr, "should've failed for invalid reaction") + t.Run("channel not found", func(t *testing.T) { + // invalid reaction + reaction5 := &model.Reaction{ + UserId: reaction1.UserId, + PostId: model.NewId(), // Unknown PostId + EmojiName: model.NewId(), + } + _, nErr = ss.Reaction().Save(reaction5) + require.Error(t, nErr, "should've failed because postId doesn't belong to a stored post") + }) } func testReactionDelete(t *testing.T, ss store.Store) {