From 86c37e59517914d9912685271f74453f3fe49fce Mon Sep 17 00:00:00 2001 From: yaziine Date: Tue, 1 Aug 2023 17:53:35 +0200 Subject: [PATCH] fix: check if message.parent_id exists --- pkg/cmd/chat/imports/validator/items.go | 3 +++ .../imports/validator/testdata/invalid-messages.json | 11 +++++++++++ pkg/cmd/chat/imports/validator/validator_test.go | 3 ++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/chat/imports/validator/items.go b/pkg/cmd/chat/imports/validator/items.go index 2f03bd7f..577dcac8 100644 --- a/pkg/cmd/chat/imports/validator/items.go +++ b/pkg/cmd/chat/imports/validator/items.go @@ -519,6 +519,9 @@ func (m *messageItem) validateReferences(idx *index) error { if idx.isReply(m.ParentID) { return errors.New("only one level thread is supported") } + if !idx.messageExist(m.ParentID) { + return fmt.Errorf("message parent_id %q doesn't exist", m.ParentID) + } } channelID, isDistinct := getChannelID(m.ChannelID, m.ChannelMemberIDs) if !idx.channelExist(m.ChannelType, channelID) { diff --git a/pkg/cmd/chat/imports/validator/testdata/invalid-messages.json b/pkg/cmd/chat/imports/validator/testdata/invalid-messages.json index 21349937..2ed95fae 100644 --- a/pkg/cmd/chat/imports/validator/testdata/invalid-messages.json +++ b/pkg/cmd/chat/imports/validator/testdata/invalid-messages.json @@ -92,6 +92,17 @@ "deleted_at": "2022-02-14T12:34:30+00:00" } }, + { + "type": "message", + "item": { + "id": "thread", + "channel_type": "messaging", + "channel_id": "channelA", + "user": "userA", + "text": "reply!", + "parent_id": "parentID" + } + }, { "type": "channel", "item": { diff --git a/pkg/cmd/chat/imports/validator/validator_test.go b/pkg/cmd/chat/imports/validator/validator_test.go index 5e969ae7..6136b72d 100644 --- a/pkg/cmd/chat/imports/validator/validator_test.go +++ b/pkg/cmd/chat/imports/validator/validator_test.go @@ -81,7 +81,7 @@ func TestValidator_Validate(t *testing.T) { }, }}, {name: "Invalid messages", filename: "invalid-messages.json", want: &Results{ - Stats: map[string]int{"channels": 2, "devices": 0, "members": 2, "messages": 0, "reactions": 0, "users": 1}, + Stats: map[string]int{"channels": 2, "devices": 0, "members": 2, "messages": 1, "reactions": 0, "users": 1}, Errors: []error{ errors.New(`validation error: message.id max length exceeded (255)`), errors.New(`validation error: message.channel_type required`), @@ -93,6 +93,7 @@ func TestValidator_Validate(t *testing.T) { errors.New(`reference error: channel ":channelA" doesn't exist`), errors.New(`reference error: distinct channel with type "messaging" and members:[] doesn't exist`), errors.New(`reference error: user "" doesn't exist (message_id messageA)`), + errors.New(`reference error: message parent_id "parentID" doesn't exist`), }, }}, {name: "Invalid devices", filename: "invalid-devices.json", want: &Results{