Skip to content

Commit

Permalink
fix: check if message.parent_id exists (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziine authored Aug 1, 2023
1 parent 8467205 commit 2dda3c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/cmd/chat/imports/validator/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/cmd/chat/imports/validator/testdata/invalid-messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/chat/imports/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`),
Expand All @@ -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{
Expand Down

0 comments on commit 2dda3c4

Please sign in to comment.